]> Creatis software - gdcm.git/blob - Testing/TestWriteSimple.cxx
* Rename the NO_SEQ, NO_SHADOW, NO_SHADOWSEQ to
[gdcm.git] / Testing / TestWriteSimple.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestWriteSimple.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/08/30 14:40:30 $
7   Version:   $Revision: 1.35 $
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 
253                            //              GDCM_LD_ALL, 
254                            //              GDCM_LD_NOSEQ, 
255                            //              GDCM_LD_NOSHADOW,
256                            //              GDCM_LD_NOSEQ|GDCM_LD_NOSHADOW, 
257                            //              GDCM_LD_NOSHADOWSEQ
258    reread->Load();
259
260    if( !reread->GetFile()->IsReadable() )
261    {
262       std::cerr << "Failed" << std::endl
263                 << "Could not read written image : " << fileName << std::endl;
264       delete fileToBuild;
265       delete fileH;
266       delete reread;
267       delete[] imageData;
268       return 1;
269    }
270
271 // Step 6 : Compare to the written image
272    std::cout << "6...";
273    size_t dataSizeWritten = reread->GetImageDataSize();
274    uint8_t *imageDataWritten = reread->GetImageData();
275
276    // Test the image write mode
277    if (reread->GetFile()->GetFileType() != fileH->GetWriteType())
278    {
279       std::cout << "Failed" << std::endl
280          << "        File type differ: "
281          << fileH->GetWriteType() << " # " 
282          << reread->GetFile()->GetFileType() << std::endl;
283       delete fileToBuild;
284       delete fileH;
285       delete reread;
286       delete[] imageData;
287
288       return 1;
289    }
290
291    // Test the image size
292    if (fileToBuild->GetXSize() != reread->GetFile()->GetXSize() ||
293        fileToBuild->GetYSize() != reread->GetFile()->GetYSize() ||
294        fileToBuild->GetZSize() != reread->GetFile()->GetZSize())
295    {
296       std::cout << "Failed" << std::endl
297          << "        X Size differs: "
298          << "X: " << fileToBuild->GetXSize() << " # " 
299                   << reread->GetFile()->GetXSize() << " | "
300          << "Y: " << fileToBuild->GetYSize() << " # " 
301                   << reread->GetFile()->GetYSize() << " | "
302          << "Z: " << fileToBuild->GetZSize() << " # " 
303                   << reread->GetFile()->GetZSize() << std::endl;
304       delete fileToBuild;
305       delete fileH;
306       delete reread;
307       delete[] imageData;
308
309       return 1;
310    }
311
312    // Test the data size
313    if (size != dataSizeWritten)
314    {
315       std::cout << "Failed" << std::endl
316          << "        Pixel areas lengths differ: "
317          << size << " # " << dataSizeWritten << std::endl;
318       delete fileToBuild;
319       delete fileH;
320       delete reread;
321       delete[] imageData;
322
323       return 1;
324    }
325
326    // Test the data's content
327    if ( memcmp(imageData, imageDataWritten, size) !=0 )
328    {
329       std::cout << "Failed" << std::endl
330                 << "        Pixel differ (as expanded in memory)." << std::endl;
331       delete fileToBuild;
332       delete fileH;
333       delete reread;
334       delete[] imageData;
335
336       return 1;
337    }
338
339    std::cout << "OK" << std::endl;
340
341    delete fileToBuild;
342    delete fileH;
343    delete reread;
344    delete[] imageData;
345
346    return 0;
347 }
348
349 int TestWriteSimple(int argc, char *argv[])
350 {
351    if (argc < 1) 
352    {
353       std::cerr << "usage: \n" 
354                 << argv[0] << " (without parameters) " << std::endl 
355                 << std::endl;
356       return 1;
357    }
358
359    gdcm::Debug::DebugOn();
360        
361    int ret=0;
362    int i=0;
363    while( Images[i].sizeX>0 && Images[i].sizeY>0 )
364    {
365       std::cout << "Test n :" << i; 
366       ret += WriteSimple(Images[i] );
367       i++;
368    }
369
370    return ret;
371 }