1 /*=========================================================================
4 Module: $RCSfile: TestWriteSimple.cxx,v $
6 Date: $Date: 2006/04/11 16:05:03 $
7 Version: $Revision: 1.49 $
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"
28 #include "gdcmGlobal.h"
29 #include "gdcmDictSet.h"
36 int sizeX; // Size X of the image
37 int sizeY; // Size Y of the image
38 int sizeZ; // Size Z of the image
39 int components; // Number of components for a pixel
40 int componentSize; // Component size (in bits : 8, 16)
41 int componentUse ; // Component size (in bits)
42 int sign; // Sign of components
43 char writeMode; // Write mode
45 // - 'e' : Explicit VR
46 // - 'i' : Implicit VR
50 {128, 128, 1, 1, 8, 8, 0, 'e'},
51 {256, 128, 1, 1, 8, 8, 0, 'a'},
52 {128, 128, 1, 1, 8, 8, 0, 'i'},
54 {128, 128, 1, 1, 8, 8, 0, 'a'},
55 {256, 128, 1, 1, 8, 8, 0, 'i'},
58 {256, 128, 1, 1, 8, 8, 0, 'e'},
59 {128, 128, 1, 1, 16, 16, 0, 'e'},
60 {128, 256, 1, 1, 16, 16, 0, 'e'},
62 {128, 256, 1, 1, 8, 8, 0, 'e'},
63 {128, 256, 1, 1, 8, 8, 0, 'i'},
64 {128, 256, 1, 1, 8, 8, 0, 'a'},
66 {128, 256, 1, 1, 16, 16, 0, 'i'},
67 {128, 256, 1, 1, 16, 16, 0, 'i'},
68 {128, 256, 1, 1, 16, 16, 0, 'a'},
69 {128, 256, 1, 1, 16, 16, 0, 'a'},
71 {256, 128, 10, 1, 8, 8, 0, 'e'},
72 {256, 128, 10, 3, 8, 8, 0, 'e'},
73 {256, 128, 10, 3, 8, 8, 0, 'i'},
74 {256, 128, 10, 1, 8, 8, 0, 'i'},
75 {256, 128, 10, 1, 8, 8, 0, 'a'},
76 {256, 128, 10, 3, 8, 8, 0, 'a'},
78 {128, 128, 1, 1, 8, 8, 1, 'e'},
79 {128, 128, 1, 1, 8, 8, 1, 'i'},
80 {128, 128, 1, 1, 8, 8, 1, 'a'},
82 {256, 128, 1, 1, 8, 8, 1, 'e'},
83 {256, 128, 1, 1, 8, 8, 1, 'i'},
84 {256, 128, 1, 1, 8, 8, 1, 'a'},
86 {128, 256, 1, 1, 8, 8, 1, 'a'},
87 {128, 256, 1, 1, 8, 8, 1, 'e'},
88 {128, 256, 1, 1, 8, 8, 1, 'i'},
90 {128, 256, 1, 1, 16, 16, 1, 'e'},
91 {128, 256, 1, 1, 16, 16, 1, 'e'},
92 {128, 256, 1, 1, 16, 16, 1, 'i'},
93 {128, 256, 1, 1, 16, 16, 1, 'i'},
94 {128, 256, 1, 1, 16, 16, 1, 'a'},
95 {128, 256, 1, 1, 16, 16, 1, 'a'},
97 {256, 128, 10, 1, 8, 8, 1, 'e'},
98 {256, 128, 10, 1, 8, 8, 1, 'i'},
99 {256, 128, 10, 1, 8, 8, 1, 'a'},
101 {256, 128, 10, 3, 8, 8, 1, 'e'},
102 {256, 128, 10, 3, 8, 8, 1, 'i'},
103 {256, 128, 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)
113 std::cout << "======================= WriteSimple =========(begin of processing current image)" << std::endl;
114 std::ostringstream fileName;
116 fileName << "TestWriteSimple";
118 // Step 1 : Create an empty FileHelper
120 std::cout << " 1...";
121 gdcm::FileHelper *fileH = gdcm::FileHelper::New();
123 // Get the (empty) image header.
124 gdcm::File *fileToBuild = fileH->GetFile();
125 std::ostringstream str;
127 // Set the image size
130 fileToBuild->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
133 fileToBuild->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
139 fileToBuild->InsertEntryString(str.str(),0x0028,0x0008, "IS"); // Number of Frames
142 fileName << "-" << img.sizeX << "-" << img.sizeY << "-" << img.sizeZ;
144 // Set the pixel type
146 str << img.componentSize;
147 fileToBuild->InsertEntryString(str.str(),0x0028,0x0100,"US"); // Bits Allocated
150 str << img.componentUse;
151 fileToBuild->InsertEntryString(str.str(),0x0028,0x0101,"US"); // Bits Stored
154 str << ( img.componentSize - 1 );
155 fileToBuild->InsertEntryString(str.str(),0x0028,0x0102,"US"); // High Bit
157 // Set the pixel representation
160 fileToBuild->InsertEntryString(str.str(),0x0028,0x0103, "US"); // Pixel Representation
162 fileName << "-" << img.componentSize;
168 switch (img.writeMode)
171 fileName << ".ACR"; break;
173 fileName << ".EXPL"; break;
175 fileName << ".IMPL"; break;
178 std::cout << "[" << fileName.str() << "]...";
179 // Set the samples per pixel
181 str << img.components;
182 fileToBuild->InsertEntryString(str.str(),0x0028,0x0002, "US"); // Samples per Pixel
184 // Step 2 : Create the output image
186 if( img.componentSize%8 > 0 )
188 img.componentSize += 8-img.componentSize%8;
190 size_t size = img.sizeX * img.sizeY * img.sizeZ
191 * img.components * img.componentSize / 8;
192 unsigned char *imageData = new unsigned char[size];
194 // FIXME : find a better heuristic to create the image
195 unsigned char *tmp = imageData;
196 for(int k=0;k<img.sizeZ;k++)
198 for(int j=0;j<img.sizeY;j++)
200 for(int i=0;i<img.sizeX;i++)
202 for(int c=0;c<img.components;c++)
204 *tmp = (unsigned char)(j%256);
205 if( img.componentSize>8 )
207 *(tmp+1) = (unsigned char)(j/256);
209 tmp += img.componentSize/8;
215 // Step 3 : Set the image Pixel Data
217 fileH->SetImageData(imageData,size);
219 // Step 4 : Set the writting mode and write the image
221 fileH->SetWriteModeToRaw();
222 std::cout << "4'...";
223 switch (img.writeMode)
225 case 'a' : // Write an ACR file
226 fileH->SetWriteTypeToAcr();
229 case 'e' : // Write a DICOM Explicit VR file
230 fileH->SetWriteTypeToDcmExplVR();
233 case 'i' : // Write a DICOM Implicit VR file
234 fileH->SetWriteTypeToDcmImplVR();
238 std::cout << "Failed for [" << fileName.str() << "]\n"
239 << " Write mode '"<<img.writeMode<<"' is undefined\n";
246 std::cout << std::endl;
247 fileToBuild->Print();
249 if( !fileH->Write(fileName.str()) )
251 std::cout << "Failed for [" << fileName.str() << "]\n"
252 << " File is unwrittable" << std::endl;
260 // Step 5 : Read the written image
261 std::cout << "5..." << std::endl;
263 //gdcm::FileHelper *reread = new gdcm::FileHelper( fileName.str() );
265 gdcm::File *f = gdcm::File::New( );
266 f->SetLoadMode(gdcm::LD_ALL);
267 f->SetFileName( fileName.str() );
269 //reread->SetFileName( fileName.str() );
270 //reread->SetLoadMode(gdcm::LD_ALL); // Load everything
271 // Possible values are
274 // gdcm::LD_NOSHADOW,
275 // gdcm::LD_NOSEQ|gdcm::LD_NOSHADOW,
276 // gdcm::LD_NOSHADOWSEQ
280 gdcm::FileHelper *reread = gdcm::FileHelper::New( f );
284 if( !reread->GetFile()->IsReadable() )
286 std::cerr << "Failed" << std::endl
287 << "Could not read written image : " << fileName.str() << std::endl;
288 fileToBuild->Delete();
295 // Step 6 : Compare to the written image
296 std::cout << "6..." << std::endl;
297 size_t dataSizeWritten = reread->GetImageDataSize();
298 uint8_t *imageDataWritten = reread->GetImageData();
300 // Test the image write mode
301 if (reread->GetFile()->GetFileType() != fileH->GetWriteType())
303 std::cout << "Failed" << std::endl
304 << " File type differ: "
305 << fileH->GetWriteType() << " # "
306 << reread->GetFile()->GetFileType() << std::endl;
307 fileToBuild->Delete();
315 // Test the image size
316 if (fileToBuild->GetXSize() != reread->GetFile()->GetXSize() ||
317 fileToBuild->GetYSize() != reread->GetFile()->GetYSize() ||
318 fileToBuild->GetZSize() != reread->GetFile()->GetZSize())
320 std::cout << "Failed for [" << fileName.str() << "]" << std::endl
321 << " X Size differs: "
322 << "X: " << fileToBuild->GetXSize() << " # "
323 << reread->GetFile()->GetXSize() << " | "
324 << "Y: " << fileToBuild->GetYSize() << " # "
325 << reread->GetFile()->GetYSize() << " | "
326 << "Z: " << fileToBuild->GetZSize() << " # "
327 << reread->GetFile()->GetZSize() << std::endl;
328 fileToBuild->Delete();
336 // Test the data size
337 if (size != dataSizeWritten)
339 std::cout << "Failed" << std::endl
340 << " Pixel areas lengths differ: "
341 << size << " # " << dataSizeWritten << std::endl;
342 fileToBuild->Delete();
350 // Test the data content
351 if ( memcmp(imageData, imageDataWritten, size) !=0 )
353 std::cout << "Failed" << std::endl
354 << " Pixel differ (as expanded in memory)." << std::endl;
355 std::cout << " list of the first " << MAX_NUMBER_OF_DIFFERENCE
356 << " pixels differing (pos : test - ref) :"
360 for(i=0, j=0;i<dataSizeWritten && j<MAX_NUMBER_OF_DIFFERENCE;i++)
362 if(imageDataWritten[i]!=imageData[i])
364 std::cout << std::hex << "(" << i << " : "
365 << std::hex << (int)(imageDataWritten[i]) << " - "
366 << std::hex << (int)(imageData[i]) << ") "
371 std::cout << std::endl;
372 fileToBuild->Delete();
386 int TestWriteSimple(int argc, char *argv[])
390 std::cerr << "usage: \n"
391 << argv[0] << " (without parameters) " << std::endl
396 // gdcm::Debug::DebugOn();
400 while( Images[i].sizeX>0 && Images[i].sizeY>0 )
402 std::cout << "Test n :" << i <<std::endl;;
403 ret += WriteSimple(Images[i] );