1 /*=========================================================================
4 Module: $RCSfile: TestWriteSimple.cxx,v $
6 Date: $Date: 2005/10/25 16:43:46 $
7 Version: $Revision: 1.43 $
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'},
51 {512, 256, 1, 1, 8, 8, 0, 'a'},
52 {512, 256, 1, 1, 8, 8, 0, 'e'},
53 {512, 256, 1, 1, 8, 8, 0, 'i'},
55 {256, 512, 1, 1, 8, 8, 0, 'a'},
56 {256, 512, 1, 1, 8, 8, 0, 'e'},
57 {256, 512, 1, 1, 8, 8, 0, 'i'},
59 {256, 512, 1, 1, 16, 16, 0, 'a'},
60 {256, 512, 1, 1, 16, 16, 0, 'e'},
61 {256, 512, 1, 1, 16, 16, 0, 'i'},
62 {256, 512, 1, 1, 16, 16, 0, 'a'},
63 {256, 512, 1, 1, 16, 16, 0, 'e'},
64 {256, 512, 1, 1, 16, 16, 0, 'i'},
66 {512, 256, 10, 1, 8, 8, 0, 'a'},
67 {512, 256, 10, 1, 8, 8, 0, 'e'},
68 {512, 256, 10, 1, 8, 8, 0, 'i'},
69 {512, 256, 10, 3, 8, 8, 0, 'a'},
70 {512, 256, 10, 3, 8, 8, 0, 'e'},
71 {512, 256, 10, 3, 8, 8, 0, 'i'},
73 {256, 256, 1, 1, 8, 8, 1, 'a'},
74 {256, 256, 1, 1, 8, 8, 1, 'e'},
75 {256, 256, 1, 1, 8, 8, 1, 'i'},
77 {512, 256, 1, 1, 8, 8, 1, 'a'},
78 {512, 256, 1, 1, 8, 8, 1, 'e'},
79 {512, 256, 1, 1, 8, 8, 1, 'i'},
81 {256, 512, 1, 1, 8, 8, 1, 'a'},
82 {256, 512, 1, 1, 8, 8, 1, 'e'},
83 {256, 512, 1, 1, 8, 8, 1, 'i'},
85 {256, 512, 1, 1, 16, 16, 1, 'a'},
86 {256, 512, 1, 1, 16, 16, 1, 'e'},
87 {256, 512, 1, 1, 16, 16, 1, 'i'},
88 {256, 512, 1, 1, 16, 16, 1, 'a'},
89 {256, 512, 1, 1, 16, 16, 1, 'e'},
90 {256, 512, 1, 1, 16, 16, 1, 'i'},
92 {512, 256, 10, 1, 8, 8, 1, 'a'},
93 {512, 256, 10, 1, 8, 8, 1, 'e'},
94 {512, 256, 10, 1, 8, 8, 1, 'i'},
95 {512, 256, 10, 3, 8, 8, 1, 'a'},
96 {512, 256, 10, 3, 8, 8, 1, 'e'},
97 {512, 256, 10, 3, 8, 8, 1, 'i'},
98 {0, 0, 1, 1, 8, 8, 0, 'i'} // to find the end
102 const unsigned int MAX_NUMBER_OF_DIFFERENCE = 10;
104 int WriteSimple(Image &img)
106 std::ostringstream fileName;
108 fileName << "TestWriteSimple";
110 // Step 1 : Create an empty FileHelper
112 std::cout << " 1...";
113 gdcm::FileHelper *fileH = gdcm::FileHelper::New();
115 // Get the (empty) image header.
116 gdcm::File *fileToBuild = fileH->GetFile()
118 std::ostringstream str;
120 // Set the image size
123 fileToBuild->InsertEntryString(str.str(),0x0028,0x0011); // Columns
126 fileToBuild->InsertEntryString(str.str(),0x0028,0x0010); // Rows
132 fileToBuild->InsertEntryString(str.str(),0x0028,0x0008); // Number of Frames
135 fileName << "-" << img.sizeX << "-" << img.sizeY << "-" << img.sizeZ;
137 // Set the pixel type
139 str << img.componentSize;
140 fileToBuild->InsertEntryString(str.str(),0x0028,0x0100); // Bits Allocated
143 str << img.componentUse;
144 fileToBuild->InsertEntryString(str.str(),0x0028,0x0101); // Bits Stored
147 str << ( img.componentSize - 1 );
148 fileToBuild->InsertEntryString(str.str(),0x0028,0x0102); // High Bit
150 // Set the pixel representation
153 fileToBuild->InsertEntryString(str.str(),0x0028,0x0103); // Pixel Representation
155 fileName << "-" << img.componentSize;
161 switch (img.writeMode)
164 fileName << ".ACR"; break;
166 fileName << ".EXPL"; break;
168 fileName << ".IMPL"; break;
171 std::cout << "[" << fileName.str() << "]...";
172 // Set the samples per pixel
174 str << img.components;
175 fileToBuild->InsertEntryString(str.str(),0x0028,0x0002); // Samples per Pixel
177 // Step 2 : Create the output image
179 if( img.componentSize%8 > 0 )
181 img.componentSize += 8-img.componentSize%8;
183 size_t size = img.sizeX * img.sizeY * img.sizeZ
184 * img.components * img.componentSize / 8;
185 unsigned char *imageData = new unsigned char[size];
187 // FIXME : find a better heuristic to create the image
188 unsigned char *tmp = imageData;
189 for(int k=0;k<img.sizeZ;k++)
191 for(int j=0;j<img.sizeY;j++)
193 for(int i=0;i<img.sizeX;i++)
195 for(int c=0;c<img.components;c++)
197 *tmp = (unsigned char)(j%256);
198 if( img.componentSize>8 )
200 *(tmp+1) = (unsigned char)(j/256);
202 tmp += img.componentSize/8;
208 // Step 3 : Set the image Pixel Data
210 fileH->SetImageData(imageData,size);
212 // Step 4 : Set the writting mode and write the image
215 fileH->SetWriteModeToRaw();
216 switch (img.writeMode)
218 case 'a' : // Write an ACR file
219 fileH->SetWriteTypeToAcr();
222 case 'e' : // Write a DICOM Explicit VR file
223 fileH->SetWriteTypeToDcmExplVR();
226 case 'i' : // Write a DICOM Implicit VR file
227 fileH->SetWriteTypeToDcmImplVR();
231 std::cout << "Failed for [" << fileName.str() << "]\n"
232 << " Write mode '"<<img.writeMode<<"' is undefined\n";
239 if( !fileH->Write(fileName.str()) )
241 std::cout << "Failed for [" << fileName.str() << "]\n"
242 << " File is unwrittable\n";
250 // Step 5 : Read the written image
253 //gdcm::FileHelper *reread = new gdcm::FileHelper( fileName.str() );
255 gdcm::FileHelper *reread = gdcm::FileHelper::New( );
256 reread->SetFileName( fileName.str() );
257 reread->SetLoadMode(gdcm::LD_ALL); // Load everything
258 // Possible values are
261 // gdcm::LD_NOSHADOW,
262 // gdcm::LD_NOSEQ|gdcm::LD_NOSHADOW,
263 // gdcm::LD_NOSHADOWSEQ
266 if( !reread->GetFile()->IsReadable() )
268 std::cerr << "Failed" << std::endl
269 << "Could not read written image : " << fileName.str() << std::endl;
270 fileToBuild->Delete();
277 // Step 6 : Compare to the written image
279 size_t dataSizeWritten = reread->GetImageDataSize();
280 uint8_t *imageDataWritten = reread->GetImageData();
282 // Test the image write mode
283 if (reread->GetFile()->GetFileType() != fileH->GetWriteType())
285 std::cout << "Failed" << std::endl
286 << " File type differ: "
287 << fileH->GetWriteType() << " # "
288 << reread->GetFile()->GetFileType() << std::endl;
289 fileToBuild->Delete();
297 // Test the image size
298 if (fileToBuild->GetXSize() != reread->GetFile()->GetXSize() ||
299 fileToBuild->GetYSize() != reread->GetFile()->GetYSize() ||
300 fileToBuild->GetZSize() != reread->GetFile()->GetZSize())
302 std::cout << "Failed for [" << fileName.str() << "]" << std::endl
303 << " X Size differs: "
304 << "X: " << fileToBuild->GetXSize() << " # "
305 << reread->GetFile()->GetXSize() << " | "
306 << "Y: " << fileToBuild->GetYSize() << " # "
307 << reread->GetFile()->GetYSize() << " | "
308 << "Z: " << fileToBuild->GetZSize() << " # "
309 << reread->GetFile()->GetZSize() << std::endl;
310 fileToBuild->Delete();
318 // Test the data size
319 if (size != dataSizeWritten)
321 std::cout << "Failed" << std::endl
322 << " Pixel areas lengths differ: "
323 << size << " # " << dataSizeWritten << std::endl;
324 fileToBuild->Delete();
332 // Test the data's content
333 if ( memcmp(imageData, imageDataWritten, size) !=0 )
335 std::cout << "Failed" << std::endl
336 << " Pixel differ (as expanded in memory)." << std::endl;
337 std::cout << " list of the first " << MAX_NUMBER_OF_DIFFERENCE
338 << " pixels differing (pos : test - ref) :"
342 for(i=0, j=0;i<dataSizeWritten && j<MAX_NUMBER_OF_DIFFERENCE;i++)
344 if(imageDataWritten[i]!=imageData[i])
346 std::cout << std::hex << "(" << i << " : "
347 << std::hex << (int)(imageDataWritten[i]) << " - "
348 << std::hex << (int)(imageData[i]) << ") "
353 std::cout << std::endl;
354 fileToBuild->Delete();
362 std::cout << "OK" << std::endl;
364 fileToBuild->Delete();
372 int TestWriteSimple(int argc, char *argv[])
376 std::cerr << "usage: \n"
377 << argv[0] << " (without parameters) " << std::endl
382 gdcm::Debug::DebugOn();
386 while( Images[i].sizeX>0 && Images[i].sizeY>0 )
388 std::cout << "Test n :" << i;
389 ret += WriteSimple(Images[i] );