1 /*=========================================================================
4 Module: $RCSfile: TestWriteSimple.cxx,v $
6 Date: $Date: 2005/10/28 13:16:48 $
7 Version: $Revision: 1.46 $
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 {128, 128, 1, 1, 8, 8, 0, 'e'},
50 {256, 128, 1, 1, 8, 8, 0, 'e'},
51 {128, 128, 1, 1, 16, 16, 0, 'e'},
52 {128, 256, 1, 1, 16, 16, 0, 'e'},
54 {128, 128, 1, 1, 8, 8, 0, 'i'},
55 {256, 128, 1, 1, 8, 8, 0, 'i'},
56 {256, 128, 1, 1, 8, 8, 0, 'a'},
57 {128, 128, 1, 1, 8, 8, 0, 'a'},
59 {128, 256, 1, 1, 8, 8, 0, 'e'},
60 {128, 256, 1, 1, 8, 8, 0, 'i'},
61 {128, 256, 1, 1, 8, 8, 0, 'a'},
63 {128, 256, 1, 1, 16, 16, 0, 'i'},
64 {128, 256, 1, 1, 16, 16, 0, 'i'},
65 {128, 256, 1, 1, 16, 16, 0, 'a'},
66 {128, 256, 1, 1, 16, 16, 0, 'a'},
68 {256, 128, 10, 1, 8, 8, 0, 'e'},
69 {256, 128, 10, 3, 8, 8, 0, 'e'},
70 {256, 128, 10, 3, 8, 8, 0, 'i'},
71 {256, 128, 10, 1, 8, 8, 0, 'i'},
72 {256, 128, 10, 1, 8, 8, 0, 'a'},
73 {256, 128, 10, 3, 8, 8, 0, 'a'},
75 {128, 128, 1, 1, 8, 8, 1, 'e'},
76 {128, 128, 1, 1, 8, 8, 1, 'i'},
77 {128, 128, 1, 1, 8, 8, 1, 'a'},
79 {256, 128, 1, 1, 8, 8, 1, 'e'},
80 {256, 128, 1, 1, 8, 8, 1, 'i'},
81 {256, 128, 1, 1, 8, 8, 1, 'a'},
83 {128, 256, 1, 1, 8, 8, 1, 'a'},
84 {128, 256, 1, 1, 8, 8, 1, 'e'},
85 {128, 256, 1, 1, 8, 8, 1, 'i'},
87 {128, 256, 1, 1, 16, 16, 1, 'e'},
88 {128, 256, 1, 1, 16, 16, 1, 'e'},
89 {128, 256, 1, 1, 16, 16, 1, 'i'},
90 {128, 256, 1, 1, 16, 16, 1, 'i'},
91 {128, 256, 1, 1, 16, 16, 1, 'a'},
92 {128, 256, 1, 1, 16, 16, 1, 'a'},
94 {256, 128, 10, 1, 8, 8, 1, 'e'},
95 {256, 128, 10, 1, 8, 8, 1, 'i'},
96 {256, 128, 10, 1, 8, 8, 1, 'a'},
98 {256, 128, 10, 3, 8, 8, 1, 'e'},
99 {256, 128, 10, 3, 8, 8, 1, 'i'},
100 {256, 128, 10, 3, 8, 8, 1, 'a'},
101 {0, 0, 1, 1, 8, 8, 0, 'i'} // to find the end
105 const unsigned int MAX_NUMBER_OF_DIFFERENCE = 10;
107 int WriteSimple(Image &img)
109 std::ostringstream fileName;
111 fileName << "TestWriteSimple";
113 // Step 1 : Create an empty FileHelper
115 std::cout << " 1...";
116 gdcm::FileHelper *fileH = gdcm::FileHelper::New();
118 // Get the (empty) image header.
119 gdcm::File *fileToBuild = fileH->GetFile();
120 std::ostringstream str;
122 // Set the image size
125 fileToBuild->InsertEntryString(str.str(),0x0028,0x0011); // Columns
128 fileToBuild->InsertEntryString(str.str(),0x0028,0x0010); // Rows
134 fileToBuild->InsertEntryString(str.str(),0x0028,0x0008); // Number of Frames
137 fileName << "-" << img.sizeX << "-" << img.sizeY << "-" << img.sizeZ;
139 // Set the pixel type
141 str << img.componentSize;
142 fileToBuild->InsertEntryString(str.str(),0x0028,0x0100); // Bits Allocated
145 str << img.componentUse;
146 fileToBuild->InsertEntryString(str.str(),0x0028,0x0101); // Bits Stored
149 str << ( img.componentSize - 1 );
150 fileToBuild->InsertEntryString(str.str(),0x0028,0x0102); // High Bit
152 // Set the pixel representation
155 fileToBuild->InsertEntryString(str.str(),0x0028,0x0103); // Pixel Representation
157 fileName << "-" << img.componentSize;
163 switch (img.writeMode)
166 fileName << ".ACR"; break;
168 fileName << ".EXPL"; break;
170 fileName << ".IMPL"; break;
173 std::cout << "[" << fileName.str() << "]...";
174 // Set the samples per pixel
176 str << img.components;
177 fileToBuild->InsertEntryString(str.str(),0x0028,0x0002); // Samples per Pixel
179 // Step 2 : Create the output image
181 if( img.componentSize%8 > 0 )
183 img.componentSize += 8-img.componentSize%8;
185 size_t size = img.sizeX * img.sizeY * img.sizeZ
186 * img.components * img.componentSize / 8;
187 unsigned char *imageData = new unsigned char[size];
189 // FIXME : find a better heuristic to create the image
190 unsigned char *tmp = imageData;
191 for(int k=0;k<img.sizeZ;k++)
193 for(int j=0;j<img.sizeY;j++)
195 for(int i=0;i<img.sizeX;i++)
197 for(int c=0;c<img.components;c++)
199 *tmp = (unsigned char)(j%256);
200 if( img.componentSize>8 )
202 *(tmp+1) = (unsigned char)(j/256);
204 tmp += img.componentSize/8;
210 // Step 3 : Set the image Pixel Data
212 fileH->SetImageData(imageData,size);
214 // Step 4 : Set the writting mode and write the image
217 fileH->SetWriteModeToRaw();
218 switch (img.writeMode)
220 case 'a' : // Write an ACR file
221 fileH->SetWriteTypeToAcr();
224 case 'e' : // Write a DICOM Explicit VR file
225 fileH->SetWriteTypeToDcmExplVR();
228 case 'i' : // Write a DICOM Implicit VR file
229 fileH->SetWriteTypeToDcmImplVR();
233 std::cout << "Failed for [" << fileName.str() << "]\n"
234 << " Write mode '"<<img.writeMode<<"' is undefined\n";
241 if( !fileH->Write(fileName.str()) )
243 std::cout << "Failed for [" << fileName.str() << "]\n"
244 << " File is unwrittable\n";
252 // Step 5 : Read the written image
255 //gdcm::FileHelper *reread = new gdcm::FileHelper( fileName.str() );
257 gdcm::FileHelper *reread = gdcm::FileHelper::New( );
258 reread->SetFileName( fileName.str() );
259 reread->SetLoadMode(gdcm::LD_ALL); // Load everything
260 // Possible values are
263 // gdcm::LD_NOSHADOW,
264 // gdcm::LD_NOSEQ|gdcm::LD_NOSHADOW,
265 // gdcm::LD_NOSHADOWSEQ
268 if( !reread->GetFile()->IsReadable() )
270 std::cerr << "Failed" << std::endl
271 << "Could not read written image : " << fileName.str() << std::endl;
272 fileToBuild->Delete();
279 // Step 6 : Compare to the written image
281 size_t dataSizeWritten = reread->GetImageDataSize();
282 uint8_t *imageDataWritten = reread->GetImageData();
284 // Test the image write mode
285 if (reread->GetFile()->GetFileType() != fileH->GetWriteType())
287 std::cout << "Failed" << std::endl
288 << " File type differ: "
289 << fileH->GetWriteType() << " # "
290 << reread->GetFile()->GetFileType() << std::endl;
291 fileToBuild->Delete();
299 // Test the image size
300 if (fileToBuild->GetXSize() != reread->GetFile()->GetXSize() ||
301 fileToBuild->GetYSize() != reread->GetFile()->GetYSize() ||
302 fileToBuild->GetZSize() != reread->GetFile()->GetZSize())
304 std::cout << "Failed for [" << fileName.str() << "]" << std::endl
305 << " X Size differs: "
306 << "X: " << fileToBuild->GetXSize() << " # "
307 << reread->GetFile()->GetXSize() << " | "
308 << "Y: " << fileToBuild->GetYSize() << " # "
309 << reread->GetFile()->GetYSize() << " | "
310 << "Z: " << fileToBuild->GetZSize() << " # "
311 << reread->GetFile()->GetZSize() << std::endl;
312 fileToBuild->Delete();
320 // Test the data size
321 if (size != dataSizeWritten)
323 std::cout << "Failed" << std::endl
324 << " Pixel areas lengths differ: "
325 << size << " # " << dataSizeWritten << std::endl;
326 fileToBuild->Delete();
334 // Test the data's content
335 if ( memcmp(imageData, imageDataWritten, size) !=0 )
337 std::cout << "Failed" << std::endl
338 << " Pixel differ (as expanded in memory)." << std::endl;
339 std::cout << " list of the first " << MAX_NUMBER_OF_DIFFERENCE
340 << " pixels differing (pos : test - ref) :"
344 for(i=0, j=0;i<dataSizeWritten && j<MAX_NUMBER_OF_DIFFERENCE;i++)
346 if(imageDataWritten[i]!=imageData[i])
348 std::cout << std::hex << "(" << i << " : "
349 << std::hex << (int)(imageDataWritten[i]) << " - "
350 << std::hex << (int)(imageData[i]) << ") "
355 std::cout << std::endl;
356 fileToBuild->Delete();
364 std::cout << "OK" << std::endl;
373 int TestWriteSimple(int argc, char *argv[])
377 std::cerr << "usage: \n"
378 << argv[0] << " (without parameters) " << std::endl
383 gdcm::Debug::DebugOn();
387 while( Images[i].sizeX>0 && Images[i].sizeY>0 )
389 std::cout << "Test n :" << i;
390 ret += WriteSimple(Images[i] );