]> Creatis software - gdcm.git/blob - Testing/TestWriteSimple.cxx
* Minor coding-style clean up
[gdcm.git] / Testing / TestWriteSimple.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestWriteSimple.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/18 08:35:46 $
7   Version:   $Revision: 1.39 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18
19 /**
20  * Write a dicom file from nothing
21  * The written image is 256x256, 8 bits, unsigned char
22  * The image content is a horizontal grayscale from 
23  * 
24  */
25 #include "gdcmFile.h"
26 #include "gdcmFileHelper.h"
27 #include "gdcmDebug.h"
28
29 #include <iostream>
30 #include <sstream>
31
32 typedef struct
33 {
34    int sizeX;         // Size X of the image
35    int sizeY;         // Size Y of the image
36    int sizeZ;         // Size Z of the image
37    int components;    // Number of components for a pixel
38    int componentSize; // Component size (in bits : 8, 16)
39    int componentUse ; // Component size (in bits)
40    int sign;          // Sign of components
41    char writeMode;    // Write mode
42                       //  - 'a' : ACR
43                       //  - 'e' : Explicit VR
44                       //  - 'i' : Implicit VR
45 } Image;
46
47 Image Images [] = {
48    {256, 256, 1, 1, 8,  8,  0, 'a'},
49    {256, 256, 1, 1, 8,  8,  0, 'e'},
50    {256, 256, 1, 1, 8,  8,  0, 'i'},
51
52    {512, 256, 1, 1, 8,  8,  0, 'a'},
53    {512, 256, 1, 1, 8,  8,  0, 'e'},
54    {512, 256, 1, 1, 8,  8,  0, 'i'},
55
56    {256, 512, 1, 1, 8,  8,  0, 'a'},
57    {256, 512, 1, 1, 8,  8,  0, 'e'},
58    {256, 512, 1, 1, 8,  8,  0, 'i'},
59
60    {256, 512, 1, 1, 16, 16, 0, 'a'},
61    {256, 512, 1, 1, 16, 16, 0, 'e'},
62    {256, 512, 1, 1, 16, 16, 0, 'i'},
63    {256, 512, 1, 1, 16, 16, 0, 'a'},
64    {256, 512, 1, 1, 16, 16, 0, 'e'},
65    {256, 512, 1, 1, 16, 16, 0, 'i'},
66
67    {512, 256, 10, 1, 8, 8,  0, 'a'},
68    {512, 256, 10, 1, 8, 8,  0, 'e'},
69    {512, 256, 10, 1, 8, 8,  0, 'i'},
70    {512, 256, 10, 3, 8, 8,  0, 'a'},
71    {512, 256, 10, 3, 8, 8,  0, 'e'},
72    {512, 256, 10, 3, 8, 8,  0, 'i'},
73
74    {256, 256, 1, 1, 8,  8,  1, 'a'},
75    {256, 256, 1, 1, 8,  8,  1, 'e'},
76    {256, 256, 1, 1, 8,  8,  1, 'i'},
77
78    {512, 256, 1, 1, 8,  8,  1, 'a'},
79    {512, 256, 1, 1, 8,  8,  1, 'e'},
80    {512, 256, 1, 1, 8,  8,  1, 'i'},
81
82    {256, 512, 1, 1, 8,  8,  1, 'a'},
83    {256, 512, 1, 1, 8,  8,  1, 'e'},
84    {256, 512, 1, 1, 8,  8,  1, 'i'},
85
86    {256, 512, 1, 1, 16, 16, 1, 'a'},
87    {256, 512, 1, 1, 16, 16, 1, 'e'},
88    {256, 512, 1, 1, 16, 16, 1, 'i'},
89    {256, 512, 1, 1, 16, 16, 1, 'a'},
90    {256, 512, 1, 1, 16, 16, 1, 'e'},
91    {256, 512, 1, 1, 16, 16, 1, 'i'},
92
93    {512, 256, 10, 1, 8, 8,  1, 'a'},
94    {512, 256, 10, 1, 8, 8,  1, 'e'},
95    {512, 256, 10, 1, 8, 8,  1, 'i'},
96    {512, 256, 10, 3, 8, 8,  1, 'a'},
97    {512, 256, 10, 3, 8, 8,  1, 'e'},
98    {512, 256, 10, 3, 8, 8,  1, 'i'},
99    {0,   0,   1,  1, 8, 8,  0, 'i'} // to find the end
100 };
101
102 const unsigned int MAX_NUMBER_OF_DIFFERENCE = 10;
103
104 int WriteSimple(Image &img)
105 {
106    std::ostringstream fileName;
107    fileName.str("");
108    fileName << "TestWriteSimple";
109
110 // Step 1 : Create the header of the image
111
112    std::cout << "        1...";
113    gdcm::File *fileToBuild = new gdcm::File();
114    std::ostringstream str;
115
116    // Set the image size
117    str.str("");
118    str << img.sizeX;
119    fileToBuild->InsertEntryString(str.str(),0x0028,0x0011); // Columns
120    str.str("");
121    str << img.sizeY;
122    fileToBuild->InsertEntryString(str.str(),0x0028,0x0010); // Rows
123
124    if(img.sizeZ>1)
125    {
126       str.str("");
127       str << img.sizeZ;
128       fileToBuild->InsertEntryString(str.str(),0x0028,0x0008); // Number of Frames
129    }
130
131    fileName << "-" << img.sizeX << "-" << img.sizeY << "-" << img.sizeZ;
132
133    // Set the pixel type
134    str.str("");
135    str << img.componentSize;
136    fileToBuild->InsertEntryString(str.str(),0x0028,0x0100); // Bits Allocated
137
138    str.str("");
139    str << img.componentUse;
140    fileToBuild->InsertEntryString(str.str(),0x0028,0x0101); // Bits Stored
141
142    str.str("");
143    str << ( img.componentSize - 1 );
144    fileToBuild->InsertEntryString(str.str(),0x0028,0x0102); // High Bit
145
146    // Set the pixel representation
147    str.str("");
148    str << img.sign;
149    fileToBuild->InsertEntryString(str.str(),0x0028,0x0103); // Pixel Representation
150
151    fileName << "-" << img.componentSize;
152    if(img.sign == 0)
153       fileName << "U";
154    else
155       fileName << "S";
156    
157    switch (img.writeMode)
158    {
159       case 'a' :
160          fileName << ".ACR";  break; 
161       case 'e' :
162          fileName << ".EXPL"; break; 
163       case 'i' :
164          fileName << ".IMPL"; break;
165    } 
166
167    std::cout << "[" << fileName.str() << "]...";
168
169    // Set the samples per pixel
170    str.str("");
171    str << img.components;
172    fileToBuild->InsertEntryString(str.str(),0x0028,0x0002); // Samples per Pixel
173
174 // Step 2 : Create the output image
175    std::cout << "2...";
176    if( img.componentSize%8 > 0 )
177    {
178       img.componentSize += 8-img.componentSize%8;
179    }
180    size_t size = img.sizeX * img.sizeY * img.sizeZ 
181                * img.components * img.componentSize / 8;
182    unsigned char *imageData = new unsigned char[size];
183
184    // FIXME : find a better heuristic to create the image
185    unsigned char *tmp = imageData;
186    for(int k=0;k<img.sizeZ;k++)
187    {
188       for(int j=0;j<img.sizeY;j++)
189       {
190          for(int i=0;i<img.sizeX;i++)
191          {
192             for(int c=0;c<img.components;c++)
193             {
194                *tmp = (unsigned char)(j%256);
195                if( img.componentSize>8 )
196                {
197                   *(tmp+1) = (unsigned char)(j/256);
198                }
199                tmp += img.componentSize/8;
200             }
201          }
202       }
203    }
204
205 // Step 3 : Create the file of the image
206    std::cout << "3...";
207    gdcm::FileHelper *fileH = new gdcm::FileHelper(fileToBuild);
208    fileH->SetImageData(imageData,size);
209
210 // Step 4 : Set the writting mode and write the image
211    std::cout << "4...";
212
213    fileH->SetWriteModeToRaw();
214    switch (img.writeMode)
215    {
216       case 'a' : // Write an ACR file
217          fileH->SetWriteTypeToAcr();
218          break;
219
220       case 'e' : // Write a DICOM Explicit VR file
221          fileH->SetWriteTypeToDcmExplVR();
222          break;
223
224       case 'i' : // Write a DICOM Implicit VR file
225          fileH->SetWriteTypeToDcmImplVR();
226          break;
227
228       default :
229          std::cout << "Failed for [" << fileName.str() << "]\n"
230                    << "        Write mode '"<<img.writeMode<<"' is undefined\n";
231
232          delete fileH;
233          delete fileToBuild;
234          delete[] imageData;
235          return 1;
236    }
237
238    if( !fileH->Write(fileName.str()) )
239    {
240       std::cout << "Failed for [" << fileName.str() << "]\n"
241                 << "           File is unwrittable\n";
242
243       delete fileH;
244       delete fileToBuild;
245       delete[] imageData;
246       return 1;
247    }
248
249 // Step 5 : Read the written image
250    std::cout << "5...";
251    // old form.
252    //gdcm::FileHelper *reread = new gdcm::FileHelper( fileName.str() );
253    // Better use :
254    gdcm::FileHelper *reread = new gdcm::FileHelper( );
255    reread->SetFileName( fileName.str() );
256    reread->SetLoadMode(0); // Load everything
257                            // Other possible values are 
258                            //              gdcm::LD_ALL, 
259                            //              gdcm::LD_NOSEQ, 
260                            //              gdcm::LD_NOSHADOW,
261                            //              gdcm::LD_NOSEQ|gdcm::LD_NOSHADOW, 
262                            //              gdcm::LD_NOSHADOWSEQ
263    reread->Load();
264
265    if( !reread->GetFile()->IsReadable() )
266    {
267       std::cerr << "Failed" << std::endl
268                 << "Could not read written image : " << fileName.str() << std::endl;
269       delete fileToBuild;
270       delete fileH;
271       delete reread;
272       delete[] imageData;
273       return 1;
274    }
275
276 // Step 6 : Compare to the written image
277    std::cout << "6...";
278    size_t dataSizeWritten = reread->GetImageDataSize();
279    uint8_t *imageDataWritten = reread->GetImageData();
280
281    // Test the image write mode
282    if (reread->GetFile()->GetFileType() != fileH->GetWriteType())
283    {
284       std::cout << "Failed" << std::endl
285          << "        File type differ: "
286          << fileH->GetWriteType() << " # " 
287          << reread->GetFile()->GetFileType() << std::endl;
288       delete fileToBuild;
289       delete fileH;
290       delete reread;
291       delete[] imageData;
292
293       return 1;
294    }
295
296    // Test the image size
297    if (fileToBuild->GetXSize() != reread->GetFile()->GetXSize() ||
298        fileToBuild->GetYSize() != reread->GetFile()->GetYSize() ||
299        fileToBuild->GetZSize() != reread->GetFile()->GetZSize())
300    {
301       std::cout << "Failed for [" << fileName.str() << "]" << std::endl
302          << "        X Size differs: "
303          << "X: " << fileToBuild->GetXSize() << " # " 
304                   << reread->GetFile()->GetXSize() << " | "
305          << "Y: " << fileToBuild->GetYSize() << " # " 
306                   << reread->GetFile()->GetYSize() << " | "
307          << "Z: " << fileToBuild->GetZSize() << " # " 
308                   << reread->GetFile()->GetZSize() << std::endl;
309       delete fileToBuild;
310       delete fileH;
311       delete reread;
312       delete[] imageData;
313
314       return 1;
315    }
316
317    // Test the data size
318    if (size != dataSizeWritten)
319    {
320       std::cout << "Failed" << std::endl
321          << "        Pixel areas lengths differ: "
322          << size << " # " << dataSizeWritten << std::endl;
323       delete fileToBuild;
324       delete fileH;
325       delete reread;
326       delete[] imageData;
327
328       return 1;
329    }
330
331    // Test the data's content
332    if ( memcmp(imageData, imageDataWritten, size) !=0 )
333    {
334       std::cout << "Failed" << std::endl
335                 << "        Pixel differ (as expanded in memory)." << std::endl;
336       std::cout << "        list of the first " << MAX_NUMBER_OF_DIFFERENCE
337                   << " pixels differing (pos : test - ref) :" 
338                   << std::endl;
339       int i;
340       unsigned int j;
341       for(i=0, j=0;i<dataSizeWritten && j<MAX_NUMBER_OF_DIFFERENCE;i++)
342       {
343          if(imageDataWritten[i]!=imageData[i])
344             {
345             std::cout << std::hex << "(" << i << " : " 
346                         << std::hex << (int)(imageDataWritten[i]) << " - "
347                         << std::hex << (int)(imageData[i]) << ") "
348                         << std::dec;
349             ++j;
350             }
351       }
352       std::cout << std::endl;
353       delete fileToBuild;
354       delete fileH;
355       delete reread;
356       delete[] imageData;
357
358       return 1;
359    }
360
361    std::cout << "OK" << std::endl;
362
363    delete fileToBuild;
364    delete fileH;
365    delete reread;
366    delete[] imageData;
367
368    return 0;
369 }
370
371 int TestWriteSimple(int argc, char *argv[])
372 {
373    if (argc < 1) 
374    {
375       std::cerr << "usage: \n" 
376                 << argv[0] << " (without parameters) " << std::endl 
377                 << std::endl;
378       return 1;
379    }
380
381    //gdcm::Debug::DebugOn();
382        
383    int ret=0;
384    int i=0;
385    while( Images[i].sizeX>0 && Images[i].sizeY>0 )
386    {
387       std::cout << "Test n :" << i; 
388       ret += WriteSimple(Images[i] );
389       i++;
390    }
391
392    return ret;
393 }