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