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