]> Creatis software - gdcm.git/blob - Testing/TestWriteSimple.cxx
* Fix memory leaks
[gdcm.git] / Testing / TestWriteSimple.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestWriteSimple.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/26 08:28:56 $
7   Version:   $Revision: 1.44 $
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    {512, 256, 1, 1, 8,  8,  0, 'a'},
52    {512, 256, 1, 1, 8,  8,  0, 'e'},
53    {512, 256, 1, 1, 8,  8,  0, 'i'},
54
55    {256, 512, 1, 1, 8,  8,  0, 'a'},
56    {256, 512, 1, 1, 8,  8,  0, 'e'},
57    {256, 512, 1, 1, 8,  8,  0, 'i'},
58
59    {256, 512, 1, 1, 16, 16, 0, 'a'},
60    {256, 512, 1, 1, 16, 16, 0, 'e'},
61    {256, 512, 1, 1, 16, 16, 0, 'i'},
62    {256, 512, 1, 1, 16, 16, 0, 'a'},
63    {256, 512, 1, 1, 16, 16, 0, 'e'},
64    {256, 512, 1, 1, 16, 16, 0, 'i'},
65
66    {512, 256, 10, 1, 8, 8,  0, 'a'},
67    {512, 256, 10, 1, 8, 8,  0, 'e'},
68    {512, 256, 10, 1, 8, 8,  0, 'i'},
69    {512, 256, 10, 3, 8, 8,  0, 'a'},
70    {512, 256, 10, 3, 8, 8,  0, 'e'},
71    {512, 256, 10, 3, 8, 8,  0, 'i'},
72
73    {256, 256, 1, 1, 8,  8,  1, 'a'},
74    {256, 256, 1, 1, 8,  8,  1, 'e'},
75    {256, 256, 1, 1, 8,  8,  1, 'i'},
76
77    {512, 256, 1, 1, 8,  8,  1, 'a'},
78    {512, 256, 1, 1, 8,  8,  1, 'e'},
79    {512, 256, 1, 1, 8,  8,  1, 'i'},
80
81    {256, 512, 1, 1, 8,  8,  1, 'a'},
82    {256, 512, 1, 1, 8,  8,  1, 'e'},
83    {256, 512, 1, 1, 8,  8,  1, 'i'},
84
85    {256, 512, 1, 1, 16, 16, 1, 'a'},
86    {256, 512, 1, 1, 16, 16, 1, 'e'},
87    {256, 512, 1, 1, 16, 16, 1, 'i'},
88    {256, 512, 1, 1, 16, 16, 1, 'a'},
89    {256, 512, 1, 1, 16, 16, 1, 'e'},
90    {256, 512, 1, 1, 16, 16, 1, 'i'},
91
92    {512, 256, 10, 1, 8, 8,  1, 'a'},
93    {512, 256, 10, 1, 8, 8,  1, 'e'},
94    {512, 256, 10, 1, 8, 8,  1, 'i'},
95    {512, 256, 10, 3, 8, 8,  1, 'a'},
96    {512, 256, 10, 3, 8, 8,  1, 'e'},
97    {512, 256, 10, 3, 8, 8,  1, 'i'},
98    {0,   0,   1,  1, 8, 8,  0, 'i'} // to find the end
99 };
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 an empty FileHelper
111
112    std::cout << "        1...";
113    gdcm::FileHelper *fileH = gdcm::FileHelper::New();
114  
115  //  Get the (empty) image header.  
116    gdcm::File *fileToBuild = fileH->GetFile();
117    std::ostringstream str;
118
119    // Set the image size
120    str.str("");
121    str << img.sizeX;
122    fileToBuild->InsertEntryString(str.str(),0x0028,0x0011); // Columns
123    str.str("");
124    str << img.sizeY;
125    fileToBuild->InsertEntryString(str.str(),0x0028,0x0010); // Rows
126
127    if(img.sizeZ>1)
128    {
129       str.str("");
130       str << img.sizeZ;
131       fileToBuild->InsertEntryString(str.str(),0x0028,0x0008); // Number of Frames
132    }
133
134    fileName << "-" << img.sizeX << "-" << img.sizeY << "-" << img.sizeZ;
135
136    // Set the pixel type
137    str.str("");
138    str << img.componentSize;
139    fileToBuild->InsertEntryString(str.str(),0x0028,0x0100); // Bits Allocated
140
141    str.str("");
142    str << img.componentUse;
143    fileToBuild->InsertEntryString(str.str(),0x0028,0x0101); // Bits Stored
144
145    str.str("");
146    str << ( img.componentSize - 1 );
147    fileToBuild->InsertEntryString(str.str(),0x0028,0x0102); // High Bit
148
149    // Set the pixel representation
150    str.str("");
151    str << img.sign;
152    fileToBuild->InsertEntryString(str.str(),0x0028,0x0103); // Pixel Representation
153
154    fileName << "-" << img.componentSize;
155    if(img.sign == 0)
156       fileName << "U";
157    else
158       fileName << "S";
159    
160    switch (img.writeMode)
161    {
162       case 'a' :
163          fileName << ".ACR";  break; 
164       case 'e' :
165          fileName << ".EXPL"; break; 
166       case 'i' :
167          fileName << ".IMPL"; break;
168    } 
169
170    std::cout << "[" << fileName.str() << "]...";
171    // Set the samples per pixel
172    str.str("");
173    str << img.components;
174    fileToBuild->InsertEntryString(str.str(),0x0028,0x0002); // Samples per Pixel
175
176 // Step 2 : Create the output image
177    std::cout << "2...";
178    if( img.componentSize%8 > 0 )
179    {
180       img.componentSize += 8-img.componentSize%8;
181    }
182    size_t size = img.sizeX * img.sizeY * img.sizeZ 
183                * img.components * img.componentSize / 8;
184    unsigned char *imageData = new unsigned char[size];
185
186    // FIXME : find a better heuristic to create the image
187    unsigned char *tmp = imageData;
188    for(int k=0;k<img.sizeZ;k++)
189    {
190       for(int j=0;j<img.sizeY;j++)
191       {
192          for(int i=0;i<img.sizeX;i++)
193          {
194             for(int c=0;c<img.components;c++)
195             {
196                *tmp = (unsigned char)(j%256);
197                if( img.componentSize>8 )
198                {
199                   *(tmp+1) = (unsigned char)(j/256);
200                }
201                tmp += img.componentSize/8;
202             }
203          }
204       }
205    }
206
207 // Step 3 : Set the image Pixel Data
208    std::cout << "3...";
209    fileH->SetImageData(imageData,size);
210
211 // Step 4 : Set the writting mode and write the image
212    std::cout << "4...";
213
214    fileH->SetWriteModeToRaw();
215    switch (img.writeMode)
216    {
217       case 'a' : // Write an ACR file
218          fileH->SetWriteTypeToAcr();
219          break;
220
221       case 'e' : // Write a DICOM Explicit VR file
222          fileH->SetWriteTypeToDcmExplVR();
223          break;
224
225       case 'i' : // Write a DICOM Implicit VR file
226          fileH->SetWriteTypeToDcmImplVR();
227          break;
228
229       default :
230          std::cout << "Failed for [" << fileName.str() << "]\n"
231                    << "        Write mode '"<<img.writeMode<<"' is undefined\n";
232
233          fileH->Delete();
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       fileH->Delete();
244
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 = gdcm::FileHelper::New( );
255    reread->SetFileName( fileName.str() );
256    reread->SetLoadMode(gdcm::LD_ALL); // Load everything
257                            // 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       fileToBuild->Delete();
270       fileH->Delete();
271       reread->Delete();
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       fileToBuild->Delete();
289       fileH->Delete();
290       reread->Delete();
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       fileToBuild->Delete();
310       fileH->Delete();
311       reread->Delete();
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       fileToBuild->Delete();
324       fileH->Delete();
325       reread->Delete();
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       unsigned 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       fileToBuild->Delete();
354       fileH->Delete();
355       reread->Delete();
356       delete[] imageData;
357
358       return 1;
359    }
360
361    std::cout << "OK" << std::endl;
362
363    fileH->Delete();
364    reread->Delete();
365    delete[] imageData;
366
367    return 0;
368 }
369
370 int TestWriteSimple(int argc, char *argv[])
371 {
372    if (argc < 1) 
373    {
374       std::cerr << "usage: \n" 
375                 << argv[0] << " (without parameters) " << std::endl 
376                 << std::endl;
377       return 1;
378    }
379
380    gdcm::Debug::DebugOn();
381        
382    int ret=0;
383    int i=0;
384    while( Images[i].sizeX>0 && Images[i].sizeY>0 )
385    {
386       std::cout << "Test n :" << i; 
387       ret += WriteSimple(Images[i] );
388       i++;
389    }
390
391    return ret;
392 }