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