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