1 /*=========================================================================
4 Module: $RCSfile: TestWriteSimple.cxx,v $
6 Date: $Date: 2007/05/23 14:18:06 $
7 Version: $Revision: 1.51 $
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_NAME_SPACE::FileHelper *fileH = GDCM_NAME_SPACE::FileHelper::New();
123 // Get the (empty) image header.
124 GDCM_NAME_SPACE::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 fileToBuild->InsertEntryString("0",0x0008,0x0000,"UL"); // Should be removed
170 switch (img.writeMode)
183 std::cout << "[" << fileName.str() << "]...";
184 // Set the samples per pixel
186 str << img.components;
187 fileToBuild->InsertEntryString(str.str(),0x0028,0x0002,"US"); // Samples per Pixel
189 // Step 2 : Create the output image
191 if( img.componentSize%8 > 0 )
193 img.componentSize += 8-img.componentSize%8;
195 size_t size = img.sizeX * img.sizeY * img.sizeZ
196 * img.components * img.componentSize / 8;
197 unsigned char *imageData = new unsigned char[size];
199 // FIXME : find a better heuristic to create the image
200 unsigned char *tmp = imageData;
201 for(int k=0;k<img.sizeZ;k++)
203 for(int j=0;j<img.sizeY;j++)
205 for(int i=0;i<img.sizeX;i++)
207 for(int c=0;c<img.components;c++)
209 *tmp = (unsigned char)(j%256);
210 if( img.componentSize>8 )
212 *(tmp+1) = (unsigned char)(j/256);
214 tmp += img.componentSize/8;
220 // Step 3 : Set the image Pixel Data
222 fileH->SetImageData(imageData,size);
224 // Step 4 : Set the writting mode and write the image
226 fileH->SetWriteModeToRaw();
227 std::cout << "4'...";
228 switch (img.writeMode)
230 case 'a' : // Write an ACR file
231 fileH->SetWriteTypeToAcr();
234 case 'e' : // Write a DICOM Explicit VR file
235 fileH->SetWriteTypeToDcmExplVR();
238 case 'i' : // Write a DICOM Implicit VR file
239 fileH->SetWriteTypeToDcmImplVR();
243 std::cout << "Failed for [" << fileName.str() << "]\n"
244 << " Write mode '"<<img.writeMode<<"' is undefined\n";
251 std::cout << std::endl;
252 fileToBuild->Print();
254 if( !fileH->Write(fileName.str()) )
256 std::cout << "Failed for [" << fileName.str() << "]\n"
257 << " File is unwrittable" << std::endl;
265 // Step 5 : Read the written image
266 std::cout << "5..." << std::endl;
268 //GDCM_NAME_SPACE::FileHelper *reread = new GDCM_NAME_SPACE::FileHelper( fileName.str() );
270 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New( );
271 f->SetLoadMode(GDCM_NAME_SPACE::LD_ALL);
272 f->SetFileName( fileName.str() );
274 //reread->SetFileName( fileName.str() );
275 //reread->SetLoadMode(GDCM_NAME_SPACE::LD_ALL); // Load everything
276 // Possible values are
277 // GDCM_NAME_SPACE::LD_ALL,
278 // GDCM_NAME_SPACE::LD_NOSEQ,
279 // GDCM_NAME_SPACE::LD_NOSHADOW,
280 // GDCM_NAME_SPACE::LD_NOSEQ|GDCM_NAME_SPACE::LD_NOSHADOW,
281 // GDCM_NAME_SPACE::LD_NOSHADOWSEQ
285 GDCM_NAME_SPACE::FileHelper *reread = GDCM_NAME_SPACE::FileHelper::New( f );
289 if( !reread->GetFile()->IsReadable() )
291 std::cerr << "Failed" << std::endl
292 << "Could not read written image : " << fileName.str() << std::endl;
293 fileToBuild->Delete();
300 // Step 6 : Compare to the written image
301 std::cout << "6..." << std::endl;
302 size_t dataSizeWritten = reread->GetImageDataSize();
303 uint8_t *imageDataWritten = reread->GetImageData();
305 // Test the image write mode
306 if (reread->GetFile()->GetFileType() != fileH->GetWriteType())
308 std::cout << "Failed" << std::endl
309 << " File type differ: "
310 << fileH->GetWriteType() << " # "
311 << reread->GetFile()->GetFileType() << std::endl;
312 fileToBuild->Delete();
320 // Test the image size
321 if (fileToBuild->GetXSize() != reread->GetFile()->GetXSize() ||
322 fileToBuild->GetYSize() != reread->GetFile()->GetYSize() ||
323 fileToBuild->GetZSize() != reread->GetFile()->GetZSize())
325 std::cout << "Failed for [" << fileName.str() << "]" << std::endl
326 << " X Size differs: "
327 << "X: " << fileToBuild->GetXSize() << " # "
328 << reread->GetFile()->GetXSize() << " | "
329 << "Y: " << fileToBuild->GetYSize() << " # "
330 << reread->GetFile()->GetYSize() << " | "
331 << "Z: " << fileToBuild->GetZSize() << " # "
332 << reread->GetFile()->GetZSize() << std::endl;
333 fileToBuild->Delete();
341 // Test the data size
342 if (size != dataSizeWritten)
344 std::cout << "Failed" << std::endl
345 << " Pixel areas lengths differ: "
346 << size << " # " << dataSizeWritten << std::endl;
347 fileToBuild->Delete();
355 // Test the data content
356 if ( memcmp(imageData, imageDataWritten, size) !=0 )
358 std::cout << "Failed" << std::endl
359 << " Pixel differ (as expanded in memory)." << std::endl;
360 std::cout << " list of the first " << MAX_NUMBER_OF_DIFFERENCE
361 << " pixels differing (pos : test - ref) :"
365 for(i=0, j=0;i<dataSizeWritten && j<MAX_NUMBER_OF_DIFFERENCE;i++)
367 if(imageDataWritten[i]!=imageData[i])
369 std::cout << std::hex << "(" << i << " : "
370 << std::hex << (int)(imageDataWritten[i]) << " - "
371 << std::hex << (int)(imageData[i]) << ") "
376 std::cout << std::endl;
377 fileToBuild->Delete();
391 int TestWriteSimple(int argc, char *argv[])
395 std::cerr << "usage: \n"
396 << argv[0] << " (without parameters) " << std::endl
401 // GDCM_NAME_SPACE::Debug::DebugOn();
405 while( Images[i].sizeX>0 && Images[i].sizeY>0 )
407 std::cout << "Test n :" << i <<std::endl;;
408 ret += WriteSimple(Images[i] );