1 /*=========================================================================
4 Module: $RCSfile: TestWriteSimple.cxx,v $
6 Date: $Date: 2005/06/03 10:15:19 $
7 Version: $Revision: 1.30 $
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.
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.
17 =========================================================================*/
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
26 #include "gdcmFileHelper.h"
27 #include "gdcmDebug.h"
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
43 // - 'e' : Explicit VR
44 // - 'i' : Implicit VR
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'},
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'},
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'},
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'},
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'},
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'},
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'},
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'},
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'},
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
102 int WriteSimple(Image &img)
104 std::ostringstream fileName;
106 fileName << "TestWriteSimple";
108 // Step 1 : Create the header of the image
109 std::cout << " 1...";
110 gdcm::File *fileToBuild = new gdcm::File();
111 std::ostringstream str;
113 // Set the image size
116 fileToBuild->InsertValEntry(str.str(),0x0028,0x0011); // Columns
119 fileToBuild->InsertValEntry(str.str(),0x0028,0x0010); // Rows
125 fileToBuild->InsertValEntry(str.str(),0x0028,0x0008); // Number of Frames
128 fileName << "-" << img.sizeX << "-" << img.sizeY << "-" << img.sizeZ;
130 // Set the pixel type
132 str << img.componentSize;
133 fileToBuild->InsertValEntry(str.str(),0x0028,0x0100); // Bits Allocated
136 str << img.componentUse;
137 fileToBuild->InsertValEntry(str.str(),0x0028,0x0101); // Bits Stored
140 str << ( img.componentSize - 1 );
141 fileToBuild->InsertValEntry(str.str(),0x0028,0x0102); // High Bit
143 // Set the pixel representation
146 fileToBuild->InsertValEntry(str.str(),0x0028,0x0103); // Pixel Representation
148 fileName << "-" << img.componentSize;
154 switch (img.writeMode)
157 fileName << ".ACR"; break;
159 fileName << ".EXPL"; break;
161 fileName << ".IMPL"; break;
164 // Set the samples per pixel
166 str << img.components;
167 fileToBuild->InsertValEntry(str.str(),0x0028,0x0002); // Samples per Pixel
171 // Step 2 : Create the output image
173 if( img.componentSize%8 > 0 )
175 img.componentSize += 8-img.componentSize%8;
177 size_t size = img.sizeX * img.sizeY * img.sizeZ
178 * img.components * img.componentSize / 8;
179 unsigned char *imageData = new unsigned char[size];
181 // FIXME : find a better heuristic to create the image
182 unsigned char *tmp = imageData;
183 for(int k=0;k<img.sizeZ;k++)
185 for(int j=0;j<img.sizeY;j++)
187 for(int i=0;i<img.sizeX;i++)
189 for(int c=0;c<img.components;c++)
191 *tmp = (unsigned char)(j%256);
192 if( img.componentSize>8 )
194 *(tmp+1) = (unsigned char)(j/256);
196 tmp += img.componentSize/8;
202 // Step 3 : Create the file of the image
204 gdcm::FileHelper *fileH = new gdcm::FileHelper(fileToBuild);
205 fileH->SetImageData(imageData,size);
207 // Step 4 : Set the writting mode and write the image
210 fileH->SetWriteModeToRaw();
211 switch (img.writeMode)
213 case 'a' : // Write an ACR file
214 fileH->SetWriteTypeToAcr();
217 case 'e' : // Write a DICOM Explicit VR file
218 fileH->SetWriteTypeToDcmExplVR();
221 case 'i' : // Write a DICOM Implicit VR file
222 fileH->SetWriteTypeToDcmImplVR();
226 std::cout << "Failed\n"
227 << " Write mode '"<<img.writeMode<<"' is undefined\n";
235 if( !fileH->Write(fileName.str()) )
237 std::cout << "Failed\n"
238 << " File in unwrittable\n";
246 // Step 5 : Read the written image
248 gdcm::FileHelper *reread = new gdcm::FileHelper( fileName.str() );
249 if( !reread->GetFile()->IsReadable() )
251 std::cerr << "Failed" << std::endl
252 << "Could not read written image : " << fileName << std::endl;
260 // Step 6 : Compare to the written image
262 size_t dataSizeWritten = reread->GetImageDataSize();
263 uint8_t *imageDataWritten = reread->GetImageData();
265 // Test the image write mode
266 if (reread->GetFile()->GetFileType() != fileH->GetWriteType())
268 std::cout << "Failed" << std::endl
269 << " File type differ: "
270 << fileH->GetWriteType() << " # "
271 << reread->GetFile()->GetFileType() << std::endl;
280 // Test the image size
281 if (fileToBuild->GetXSize() != reread->GetFile()->GetXSize() ||
282 fileToBuild->GetYSize() != reread->GetFile()->GetYSize() ||
283 fileToBuild->GetZSize() != reread->GetFile()->GetZSize())
285 std::cout << "Failed" << std::endl
286 << " X Size differs: "
287 << "X: " << fileToBuild->GetXSize() << " # "
288 << reread->GetFile()->GetXSize() << " | "
289 << "Y: " << fileToBuild->GetYSize() << " # "
290 << reread->GetFile()->GetYSize() << " | "
291 << "Z: " << fileToBuild->GetZSize() << " # "
292 << reread->GetFile()->GetZSize() << std::endl;
301 // Test the data size
302 if (size != dataSizeWritten)
304 std::cout << "Failed" << std::endl
305 << " Pixel areas lengths differ: "
306 << size << " # " << dataSizeWritten << std::endl;
315 // Test the data's content
316 if ( memcmp(imageData, imageDataWritten, size) !=0 )
318 std::cout << "Failed" << std::endl
319 << " Pixel differ (as expanded in memory)." << std::endl;
328 std::cout << "OK" << std::endl;
338 int TestWriteSimple(int argc, char *argv[])
342 std::cerr << "usage: \n"
343 << argv[0] << " (without parameters) " << std::endl
348 gdcm::Debug::DebugOn();
352 while( Images[i].sizeX>0 && Images[i].sizeY>0 )
354 std::cout << "Test n :" << i;
355 ret += WriteSimple(Images[i] );