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