1 /*=========================================================================
4 Module: $RCSfile: TestWriteSimple.cxx,v $
6 Date: $Date: 2005/09/06 11:16:04 $
7 Version: $Revision: 1.37 $
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'},
52 {512, 256, 1, 1, 8, 8, 0, 'a'},
53 {512, 256, 1, 1, 8, 8, 0, 'e'},
54 {512, 256, 1, 1, 8, 8, 0, 'i'},
56 {256, 512, 1, 1, 8, 8, 0, 'a'},
57 {256, 512, 1, 1, 8, 8, 0, 'e'},
58 {256, 512, 1, 1, 8, 8, 0, 'i'},
60 {256, 512, 1, 1, 16, 16, 0, 'a'},
61 {256, 512, 1, 1, 16, 16, 0, 'e'},
62 {256, 512, 1, 1, 16, 16, 0, 'i'},
63 {256, 512, 1, 1, 16, 16, 0, 'a'},
64 {256, 512, 1, 1, 16, 16, 0, 'e'},
65 {256, 512, 1, 1, 16, 16, 0, 'i'},
67 {512, 256, 10, 1, 8, 8, 0, 'a'},
68 {512, 256, 10, 1, 8, 8, 0, 'e'},
69 {512, 256, 10, 1, 8, 8, 0, 'i'},
70 {512, 256, 10, 3, 8, 8, 0, 'a'},
71 {512, 256, 10, 3, 8, 8, 0, 'e'},
72 {512, 256, 10, 3, 8, 8, 0, 'i'},
74 {256, 256, 1, 1, 8, 8, 1, 'a'},
75 {256, 256, 1, 1, 8, 8, 1, 'e'},
76 {256, 256, 1, 1, 8, 8, 1, 'i'},
78 {512, 256, 1, 1, 8, 8, 1, 'a'},
79 {512, 256, 1, 1, 8, 8, 1, 'e'},
80 {512, 256, 1, 1, 8, 8, 1, 'i'},
82 {256, 512, 1, 1, 8, 8, 1, 'a'},
83 {256, 512, 1, 1, 8, 8, 1, 'e'},
84 {256, 512, 1, 1, 8, 8, 1, 'i'},
86 {256, 512, 1, 1, 16, 16, 1, 'a'},
87 {256, 512, 1, 1, 16, 16, 1, 'e'},
88 {256, 512, 1, 1, 16, 16, 1, 'i'},
89 {256, 512, 1, 1, 16, 16, 1, 'a'},
90 {256, 512, 1, 1, 16, 16, 1, 'e'},
91 {256, 512, 1, 1, 16, 16, 1, 'i'},
93 {512, 256, 10, 1, 8, 8, 1, 'a'},
94 {512, 256, 10, 1, 8, 8, 1, 'e'},
95 {512, 256, 10, 1, 8, 8, 1, 'i'},
96 {512, 256, 10, 3, 8, 8, 1, 'a'},
97 {512, 256, 10, 3, 8, 8, 1, 'e'},
98 {512, 256, 10, 3, 8, 8, 1, 'i'},
99 {0, 0, 1, 1, 8, 8, 0, 'i'} // to find the end
102 int WriteSimple(Image &img)
104 std::ostringstream fileName;
106 fileName << "TestWriteSimple";
108 // Step 1 : Create the header of the image
110 std::cout << " 1...";
111 gdcm::File *fileToBuild = new gdcm::File();
112 std::ostringstream str;
114 // Set the image size
117 fileToBuild->InsertValEntry(str.str(),0x0028,0x0011); // Columns
120 fileToBuild->InsertValEntry(str.str(),0x0028,0x0010); // Rows
126 fileToBuild->InsertValEntry(str.str(),0x0028,0x0008); // Number of Frames
129 fileName << "-" << img.sizeX << "-" << img.sizeY << "-" << img.sizeZ;
131 // Set the pixel type
133 str << img.componentSize;
134 fileToBuild->InsertValEntry(str.str(),0x0028,0x0100); // Bits Allocated
137 str << img.componentUse;
138 fileToBuild->InsertValEntry(str.str(),0x0028,0x0101); // Bits Stored
141 str << ( img.componentSize - 1 );
142 fileToBuild->InsertValEntry(str.str(),0x0028,0x0102); // High Bit
144 // Set the pixel representation
147 fileToBuild->InsertValEntry(str.str(),0x0028,0x0103); // Pixel Representation
149 fileName << "-" << img.componentSize;
155 switch (img.writeMode)
158 fileName << ".ACR"; break;
160 fileName << ".EXPL"; break;
162 fileName << ".IMPL"; break;
165 std::cout << "[" << fileName.str() << "]...";
167 // Set the samples per pixel
169 str << img.components;
170 fileToBuild->InsertValEntry(str.str(),0x0028,0x0002); // Samples per Pixel
172 // Step 2 : Create the output image
174 if( img.componentSize%8 > 0 )
176 img.componentSize += 8-img.componentSize%8;
178 size_t size = img.sizeX * img.sizeY * img.sizeZ
179 * img.components * img.componentSize / 8;
180 unsigned char *imageData = new unsigned char[size];
182 // FIXME : find a better heuristic to create the image
183 unsigned char *tmp = imageData;
184 for(int k=0;k<img.sizeZ;k++)
186 for(int j=0;j<img.sizeY;j++)
188 for(int i=0;i<img.sizeX;i++)
190 for(int c=0;c<img.components;c++)
192 *tmp = (unsigned char)(j%256);
193 if( img.componentSize>8 )
195 *(tmp+1) = (unsigned char)(j/256);
197 tmp += img.componentSize/8;
203 // Step 3 : Create the file of the image
205 gdcm::FileHelper *fileH = new gdcm::FileHelper(fileToBuild);
206 fileH->SetImageData(imageData,size);
208 // Step 4 : Set the writting mode and write the image
211 fileH->SetWriteModeToRaw();
212 switch (img.writeMode)
214 case 'a' : // Write an ACR file
215 fileH->SetWriteTypeToAcr();
218 case 'e' : // Write a DICOM Explicit VR file
219 fileH->SetWriteTypeToDcmExplVR();
222 case 'i' : // Write a DICOM Implicit VR file
223 fileH->SetWriteTypeToDcmImplVR();
227 std::cout << "Failed for [" << fileName.str() << "]\n"
228 << " Write mode '"<<img.writeMode<<"' is undefined\n";
236 if( !fileH->Write(fileName.str()) )
238 std::cout << "Failed for [" << fileName.str() << "]\n"
239 << " File in unwrittable\n";
247 // Step 5 : Read the written image
250 //gdcm::FileHelper *reread = new gdcm::FileHelper( fileName.str() );
252 gdcm::FileHelper *reread = new gdcm::FileHelper( );
253 reread->SetFileName( fileName.str() );
254 reread->SetLoadMode(0); // Load everything
255 // Other possible values are
258 // gdcm::LD_NOSHADOW,
259 // gdcm::LD_NOSEQ|gdcm::LD_NOSHADOW,
260 // gdcm::LD_NOSHADOWSEQ
263 if( !reread->GetFile()->IsReadable() )
265 std::cerr << "Failed" << std::endl
266 << "Could not read written image : " << fileName.str() << std::endl;
274 // Step 6 : Compare to the written image
276 size_t dataSizeWritten = reread->GetImageDataSize();
277 uint8_t *imageDataWritten = reread->GetImageData();
279 // Test the image write mode
280 if (reread->GetFile()->GetFileType() != fileH->GetWriteType())
282 std::cout << "Failed" << std::endl
283 << " File type differ: "
284 << fileH->GetWriteType() << " # "
285 << reread->GetFile()->GetFileType() << std::endl;
294 // Test the image size
295 if (fileToBuild->GetXSize() != reread->GetFile()->GetXSize() ||
296 fileToBuild->GetYSize() != reread->GetFile()->GetYSize() ||
297 fileToBuild->GetZSize() != reread->GetFile()->GetZSize())
299 std::cout << "Failed for [" << fileName.str() << "]" << std::endl
300 << " X Size differs: "
301 << "X: " << fileToBuild->GetXSize() << " # "
302 << reread->GetFile()->GetXSize() << " | "
303 << "Y: " << fileToBuild->GetYSize() << " # "
304 << reread->GetFile()->GetYSize() << " | "
305 << "Z: " << fileToBuild->GetZSize() << " # "
306 << reread->GetFile()->GetZSize() << std::endl;
315 // Test the data size
316 if (size != dataSizeWritten)
318 std::cout << "Failed" << std::endl
319 << " Pixel areas lengths differ: "
320 << size << " # " << dataSizeWritten << std::endl;
329 // Test the data's content
330 if ( memcmp(imageData, imageDataWritten, size) !=0 )
332 std::cout << "Failed" << std::endl
333 << " Pixel differ (as expanded in memory)." << std::endl;
342 std::cout << "OK" << std::endl;
352 int TestWriteSimple(int argc, char *argv[])
356 std::cerr << "usage: \n"
357 << argv[0] << " (without parameters) " << std::endl
362 gdcm::Debug::DebugOn();
366 while( Images[i].sizeX>0 && Images[i].sizeY>0 )
368 std::cout << "Test n :" << i;
369 ret += WriteSimple(Images[i] );