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