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