1 /*=========================================================================
4 Module: $RCSfile: TestWriteSimple.cxx,v $
6 Date: $Date: 2005/10/27 09:05:44 $
7 Version: $Revision: 1.45 $
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
49 {256, 256, 1, 1, 8, 8, 0, 'e'},
50 {512, 256, 1, 1, 8, 8, 0, 'e'},
51 {256, 256, 1, 1, 8, 8, 0, 'i'},
52 {512, 256, 1, 1, 8, 8, 0, 'i'},
53 {512, 256, 1, 1, 8, 8, 0, 'a'},
54 {256, 256, 1, 1, 8, 8, 0, 'a'},
56 {256, 512, 1, 1, 8, 8, 0, 'e'},
57 {256, 512, 1, 1, 8, 8, 0, 'i'},
58 {256, 512, 1, 1, 8, 8, 0, 'a'},
61 {256, 512, 1, 1, 16, 16, 0, 'e'},
62 {256, 512, 1, 1, 16, 16, 0, 'e'},
63 {256, 512, 1, 1, 16, 16, 0, 'i'},
64 {256, 512, 1, 1, 16, 16, 0, 'i'},
65 {256, 512, 1, 1, 16, 16, 0, 'a'},
66 {256, 512, 1, 1, 16, 16, 0, 'a'},
69 {512, 256, 10, 1, 8, 8, 0, 'e'},
70 {512, 256, 10, 3, 8, 8, 0, 'e'},
71 {512, 256, 10, 3, 8, 8, 0, 'i'},
72 {512, 256, 10, 1, 8, 8, 0, 'i'},
73 {512, 256, 10, 1, 8, 8, 0, 'a'},
74 {512, 256, 10, 3, 8, 8, 0, 'a'},
76 {256, 256, 1, 1, 8, 8, 1, 'e'},
77 {256, 256, 1, 1, 8, 8, 1, 'i'},
78 {256, 256, 1, 1, 8, 8, 1, 'a'},
80 {512, 256, 1, 1, 8, 8, 1, 'e'},
81 {512, 256, 1, 1, 8, 8, 1, 'i'},
82 {512, 256, 1, 1, 8, 8, 1, 'a'},
84 {256, 512, 1, 1, 8, 8, 1, 'a'},
85 {256, 512, 1, 1, 8, 8, 1, 'e'},
86 {256, 512, 1, 1, 8, 8, 1, 'i'},
89 {256, 512, 1, 1, 16, 16, 1, 'e'},
90 {256, 512, 1, 1, 16, 16, 1, 'e'},
91 {256, 512, 1, 1, 16, 16, 1, 'i'},
92 {256, 512, 1, 1, 16, 16, 1, 'i'},
93 {256, 512, 1, 1, 16, 16, 1, 'a'},
94 {256, 512, 1, 1, 16, 16, 1, 'a'},
97 {512, 256, 10, 1, 8, 8, 1, 'e'},
98 {512, 256, 10, 1, 8, 8, 1, 'i'},
99 {512, 256, 10, 1, 8, 8, 1, 'a'},
101 {512, 256, 10, 3, 8, 8, 1, 'e'},
102 {512, 256, 10, 3, 8, 8, 1, 'i'},
103 {512, 256, 10, 3, 8, 8, 1, 'a'},
104 {0, 0, 1, 1, 8, 8, 0, 'i'} // to find the end
108 const unsigned int MAX_NUMBER_OF_DIFFERENCE = 10;
110 int WriteSimple(Image &img)
112 std::ostringstream fileName;
114 fileName << "TestWriteSimple";
116 // Step 1 : Create an empty FileHelper
118 std::cout << " 1...";
119 gdcm::FileHelper *fileH = gdcm::FileHelper::New();
121 // Get the (empty) image header.
122 gdcm::File *fileToBuild = fileH->GetFile();
123 std::ostringstream str;
125 // Set the image size
128 fileToBuild->InsertEntryString(str.str(),0x0028,0x0011); // Columns
131 fileToBuild->InsertEntryString(str.str(),0x0028,0x0010); // Rows
137 fileToBuild->InsertEntryString(str.str(),0x0028,0x0008); // Number of Frames
140 fileName << "-" << img.sizeX << "-" << img.sizeY << "-" << img.sizeZ;
142 // Set the pixel type
144 str << img.componentSize;
145 fileToBuild->InsertEntryString(str.str(),0x0028,0x0100); // Bits Allocated
148 str << img.componentUse;
149 fileToBuild->InsertEntryString(str.str(),0x0028,0x0101); // Bits Stored
152 str << ( img.componentSize - 1 );
153 fileToBuild->InsertEntryString(str.str(),0x0028,0x0102); // High Bit
155 // Set the pixel representation
158 fileToBuild->InsertEntryString(str.str(),0x0028,0x0103); // Pixel Representation
160 fileName << "-" << img.componentSize;
166 switch (img.writeMode)
169 fileName << ".ACR"; break;
171 fileName << ".EXPL"; break;
173 fileName << ".IMPL"; break;
176 std::cout << "[" << fileName.str() << "]...";
177 // Set the samples per pixel
179 str << img.components;
180 fileToBuild->InsertEntryString(str.str(),0x0028,0x0002); // Samples per Pixel
182 // Step 2 : Create the output image
184 if( img.componentSize%8 > 0 )
186 img.componentSize += 8-img.componentSize%8;
188 size_t size = img.sizeX * img.sizeY * img.sizeZ
189 * img.components * img.componentSize / 8;
190 unsigned char *imageData = new unsigned char[size];
192 // FIXME : find a better heuristic to create the image
193 unsigned char *tmp = imageData;
194 for(int k=0;k<img.sizeZ;k++)
196 for(int j=0;j<img.sizeY;j++)
198 for(int i=0;i<img.sizeX;i++)
200 for(int c=0;c<img.components;c++)
202 *tmp = (unsigned char)(j%256);
203 if( img.componentSize>8 )
205 *(tmp+1) = (unsigned char)(j/256);
207 tmp += img.componentSize/8;
213 // Step 3 : Set the image Pixel Data
215 fileH->SetImageData(imageData,size);
217 // Step 4 : Set the writting mode and write the image
220 fileH->SetWriteModeToRaw();
221 switch (img.writeMode)
223 case 'a' : // Write an ACR file
224 fileH->SetWriteTypeToAcr();
227 case 'e' : // Write a DICOM Explicit VR file
228 fileH->SetWriteTypeToDcmExplVR();
231 case 'i' : // Write a DICOM Implicit VR file
232 fileH->SetWriteTypeToDcmImplVR();
236 std::cout << "Failed for [" << fileName.str() << "]\n"
237 << " Write mode '"<<img.writeMode<<"' is undefined\n";
244 if( !fileH->Write(fileName.str()) )
246 std::cout << "Failed for [" << fileName.str() << "]\n"
247 << " File is unwrittable\n";
255 // Step 5 : Read the written image
258 //gdcm::FileHelper *reread = new gdcm::FileHelper( fileName.str() );
260 gdcm::FileHelper *reread = gdcm::FileHelper::New( );
261 reread->SetFileName( fileName.str() );
262 reread->SetLoadMode(gdcm::LD_ALL); // Load everything
263 // Possible values are
266 // gdcm::LD_NOSHADOW,
267 // gdcm::LD_NOSEQ|gdcm::LD_NOSHADOW,
268 // gdcm::LD_NOSHADOWSEQ
271 if( !reread->GetFile()->IsReadable() )
273 std::cerr << "Failed" << std::endl
274 << "Could not read written image : " << fileName.str() << std::endl;
275 fileToBuild->Delete();
282 // Step 6 : Compare to the written image
284 size_t dataSizeWritten = reread->GetImageDataSize();
285 uint8_t *imageDataWritten = reread->GetImageData();
287 // Test the image write mode
288 if (reread->GetFile()->GetFileType() != fileH->GetWriteType())
290 std::cout << "Failed" << std::endl
291 << " File type differ: "
292 << fileH->GetWriteType() << " # "
293 << reread->GetFile()->GetFileType() << std::endl;
294 fileToBuild->Delete();
302 // Test the image size
303 if (fileToBuild->GetXSize() != reread->GetFile()->GetXSize() ||
304 fileToBuild->GetYSize() != reread->GetFile()->GetYSize() ||
305 fileToBuild->GetZSize() != reread->GetFile()->GetZSize())
307 std::cout << "Failed for [" << fileName.str() << "]" << std::endl
308 << " X Size differs: "
309 << "X: " << fileToBuild->GetXSize() << " # "
310 << reread->GetFile()->GetXSize() << " | "
311 << "Y: " << fileToBuild->GetYSize() << " # "
312 << reread->GetFile()->GetYSize() << " | "
313 << "Z: " << fileToBuild->GetZSize() << " # "
314 << reread->GetFile()->GetZSize() << std::endl;
315 fileToBuild->Delete();
323 // Test the data size
324 if (size != dataSizeWritten)
326 std::cout << "Failed" << std::endl
327 << " Pixel areas lengths differ: "
328 << size << " # " << dataSizeWritten << std::endl;
329 fileToBuild->Delete();
337 // Test the data's content
338 if ( memcmp(imageData, imageDataWritten, size) !=0 )
340 std::cout << "Failed" << std::endl
341 << " Pixel differ (as expanded in memory)." << std::endl;
342 std::cout << " list of the first " << MAX_NUMBER_OF_DIFFERENCE
343 << " pixels differing (pos : test - ref) :"
347 for(i=0, j=0;i<dataSizeWritten && j<MAX_NUMBER_OF_DIFFERENCE;i++)
349 if(imageDataWritten[i]!=imageData[i])
351 std::cout << std::hex << "(" << i << " : "
352 << std::hex << (int)(imageDataWritten[i]) << " - "
353 << std::hex << (int)(imageData[i]) << ") "
358 std::cout << std::endl;
359 fileToBuild->Delete();
367 std::cout << "OK" << std::endl;
376 int TestWriteSimple(int argc, char *argv[])
380 std::cerr << "usage: \n"
381 << argv[0] << " (without parameters) " << std::endl
386 gdcm::Debug::DebugOn();
390 while( Images[i].sizeX>0 && Images[i].sizeY>0 )
392 std::cout << "Test n :" << i;
393 ret += WriteSimple(Images[i] );