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