1 /*=========================================================================
4 Module: $RCSfile: TestWriteSimple.cxx,v $
6 Date: $Date: 2005/02/07 19:17:27 $
7 Version: $Revision: 1.22 $
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'},
73 {0, 0, 1, 1, 8, 8, 0, 'i'} // to find the end
76 int WriteSimple(Image &img)
78 std::string fileName = "TestWriteSimple.dcm";
80 // Step 1 : Create the header of the image
82 gdcm::File *fileToBuild = new gdcm::File();
83 std::ostringstream str;
88 fileToBuild->InsertValEntry(str.str(),0x0028,0x0011); // Columns
92 fileToBuild->InsertValEntry(str.str(),0x0028,0x0010); // Rows
98 fileToBuild->InsertValEntry(str.str(),0x0028,0x0008); // Number of Frames
101 // Set the pixel type
103 str << img.componentSize;
104 fileToBuild->InsertValEntry(str.str(),0x0028,0x0100); // Bits Allocated
106 /******************************************/
107 /******************************************/
108 // Super duper kludge !!
109 if( img.componentSize == 16 )
111 // I guess by design user should know that...
112 fileToBuild->InsertBinEntry(0,0, 0x7fe0, 0x0010, "OW");
114 /******************************************/
115 /******************************************/
119 str << img.componentUse;
120 fileToBuild->InsertValEntry(str.str(),0x0028,0x0101); // Bits Stored
123 str << img.componentSize - 1;
124 fileToBuild->InsertValEntry(str.str(),0x0028,0x0102); // High Bit
126 // Set the pixel representation
129 fileToBuild->InsertValEntry(str.str(),0x0028,0x0103); // Pixel Representation
131 // Set the samples per pixel
133 str << img.components;
134 fileToBuild->InsertValEntry(str.str(),0x0028,0x0002); // Samples per Pixel
136 if( !fileToBuild->IsReadable() )
138 std::cout << "Failed\n"
139 << " Prepared image isn't readable\n";
145 // Step 2 : Create the output image
147 if( img.componentSize%8 > 0 )
149 img.componentSize += 8-img.componentSize%8;
151 size_t size = img.sizeX * img.sizeY * img.sizeZ
152 * img.components * img.componentSize / 8;
153 unsigned char *imageData = new unsigned char[size];
155 // FIXME : find a better heuristic to create the image
156 unsigned char *tmp = imageData;
157 for(int k=0;k<img.sizeZ;k++)
159 for(int j=0;j<img.sizeY;j++)
161 for(int i=0;i<img.sizeX;i++)
163 for(int c=0;c<img.components;c++)
166 if( img.componentSize>8 )
170 tmp += img.components/8;
176 // Step 3 : Create the file of the image
178 gdcm::FileHelper *file = new gdcm::FileHelper(fileToBuild);
179 file->SetImageData(imageData,size);
181 // Step 4 : Set the writting mode and write the image
184 file->SetWriteModeToRaw();
185 switch (img.writeMode)
187 case 'a' : // Write an ACR file
188 file->SetWriteTypeToAcr();
191 case 'e' : // Write a DICOM Explicit VR file
192 file->SetWriteTypeToDcmExplVR();
195 case 'i' : // Write a DICOM Implicit VR file
196 file->SetWriteTypeToDcmImplVR();
200 std::cout << "Failed\n"
201 << " Write mode '"<<img.writeMode<<"' is undefined\n";
209 if( !file->Write(fileName) )
211 std::cout << "Failed\n"
212 << "File in unwrittable\n";
220 // Step 5 : Read the written image
222 gdcm::FileHelper *reread = new gdcm::FileHelper( fileName );
223 if( !reread->GetFile()->IsReadable() )
225 std::cerr << "Failed" << std::endl
226 << "Could not reread image written: " << fileName << std::endl;
234 // Step 6 : Compare to the written image
236 size_t dataSizeWritten = reread->GetImageDataSize();
237 uint8_t *imageDataWritten = reread->GetImageData();
239 // Test the image size
240 if (fileToBuild->GetXSize() != reread->GetFile()->GetXSize() ||
241 fileToBuild->GetYSize() != reread->GetFile()->GetYSize() ||
242 fileToBuild->GetZSize() != reread->GetFile()->GetZSize())
244 std::cout << "Failed" << std::endl
245 << " X Size differs: "
246 << "X: " << fileToBuild->GetXSize() << " # "
247 << reread->GetFile()->GetXSize() << " | "
248 << "Y: " << fileToBuild->GetYSize() << " # "
249 << reread->GetFile()->GetYSize() << " | "
250 << "Z: " << fileToBuild->GetZSize() << " # "
251 << reread->GetFile()->GetZSize() << std::endl;
260 // Test the data size
261 if (size != dataSizeWritten)
263 std::cout << "Failed" << std::endl
264 << " Pixel areas lengths differ: "
265 << size << " # " << dataSizeWritten << std::endl;
274 // Test the data's content
275 if (int res = memcmp(imageData, imageDataWritten, size) !=0)
278 std::cout << "Failed" << std::endl
279 << " Pixel differ (as expanded in memory)." << std::endl;
288 std::cout << "OK" << std::endl;
298 int TestWriteSimple(int argc, char *argv[])
302 std::cerr << "usage: \n"
303 << argv[0] << " (without parameters) " << std::endl
310 while( Images[i].sizeX>0 && Images[i].sizeY>0 )
312 ret += WriteSimple(Images[i]);