1 /*=========================================================================
4 Module: $RCSfile: TestWriteSimple.cxx,v $
6 Date: $Date: 2005/02/05 03:12:28 $
7 Version: $Revision: 1.19 $
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'},
66 {0, 0, 1, 1, 8, 8, 0, 'i'} // to find the end
69 int WriteSimple(Image &img)
71 std::string fileName = "TestWriteSimple.dcm";
73 // Step 1 : Create the header of the image
75 gdcm::File *fileToBuild = new gdcm::File();
76 std::ostringstream str;
81 fileToBuild->InsertValEntry(str.str(),0x0028,0x0011); // Columns
85 fileToBuild->InsertValEntry(str.str(),0x0028,0x0010); // Rows
91 fileToBuild->InsertValEntry(str.str(),0x0028,0x0008); // Number of Frames
96 str << img.componentSize;
97 fileToBuild->InsertValEntry(str.str(),0x0028,0x0100); // Bits Allocated
99 /******************************************/
100 /******************************************/
101 // Super duper kludge !!
102 if( img.componentSize == 16 )
104 // I guess by design user should know that...
105 fileToBuild->InsertBinEntry(0,0, 0x7fe0, 0x0010, "OW");
107 /******************************************/
108 /******************************************/
112 str << img.componentUse;
113 fileToBuild->InsertValEntry(str.str(),0x0028,0x0101); // Bits Stored
116 str << img.componentSize - 1;
117 fileToBuild->InsertValEntry(str.str(),0x0028,0x0102); // High Bit
119 // Set the pixel representation
122 fileToBuild->InsertValEntry(str.str(),0x0028,0x0103); // Pixel Representation
124 // Set the samples per pixel
126 str << img.components;
127 fileToBuild->InsertValEntry(str.str(),0x0028,0x0002); // Samples per Pixel
129 if( !fileToBuild->IsReadable() )
131 std::cout << "Failed\n"
132 << " Prepared image isn't readable\n";
138 // Step 2 : Create the output image
140 if( img.componentSize%8 > 0 )
142 img.componentSize += 8-img.componentSize%8;
144 size_t size = img.sizeX * img.sizeY * img.sizeZ
145 * img.components * img.componentSize / 8;
146 unsigned char *imageData = new unsigned char[size];
148 // FIXME : find a best heuristic to create the image
149 unsigned char *tmp = imageData;
150 for(int k=0;k<img.sizeZ;k++)
152 for(int j=0;j<img.sizeY;j++)
154 for(int i=0;i<img.sizeX;i++)
156 for(int c=0;c<img.components;c++)
159 if( img.componentSize>8 )
163 tmp += img.componentSize * img.components/8;
169 // Step 3 : Create the file of the image
171 gdcm::FileHelper *file = new gdcm::FileHelper(fileToBuild);
172 file->SetImageData(imageData,size);
174 // Step 4 : Set the writting mode and write the image
177 file->SetWriteModeToRaw();
178 switch (img.writeMode)
180 case 'a' : // Write an ACR file
181 file->SetWriteTypeToAcr();
184 case 'e' : // Write a DICOM Explicit VR file
185 file->SetWriteTypeToDcmExplVR();
188 case 'i' : // Write a DICOM Implicit VR file
189 file->SetWriteTypeToDcmImplVR();
193 std::cout << "Failed\n"
194 << " Write mode '"<<img.writeMode<<"' is undefined\n";
202 if( !file->Write(fileName) )
204 std::cout << "Failed\n"
205 << "File in unwrittable\n";
213 // Step 5 : Read the written image
215 gdcm::FileHelper *reread = new gdcm::FileHelper( fileName );
216 if( !reread->GetFile()->IsReadable() )
218 std::cerr << "Failed" << std::endl
219 << "Test::TestReadWriteReadCompare: Could not reread image "
220 << "written:" << fileName << std::endl;
227 // Step 6 : Compare to the written image
229 size_t dataSizeWritten = reread->GetImageDataSize();
230 uint8_t *imageDataWritten = reread->GetImageData();
232 // Test the image size
233 if (fileToBuild->GetXSize() != reread->GetFile()->GetXSize() ||
234 fileToBuild->GetYSize() != reread->GetFile()->GetYSize() ||
235 fileToBuild->GetZSize() != reread->GetFile()->GetZSize())
237 std::cout << "Failed" << std::endl
238 << " X Size differs: "
239 << "X: " << fileToBuild->GetXSize() << " # "
240 << reread->GetFile()->GetXSize() << " | "
241 << "Y: " << fileToBuild->GetYSize() << " # "
242 << reread->GetFile()->GetYSize() << " | "
243 << "Z: " << fileToBuild->GetZSize() << " # "
244 << reread->GetFile()->GetZSize() << std::endl;
253 // Test the data size
254 if (size != dataSizeWritten)
256 std::cout << "Failed" << std::endl
257 << " Pixel areas lengths differ: "
258 << size << " # " << dataSizeWritten << std::endl;
267 // Test the data's content
268 if (int res = memcmp(imageData, imageDataWritten, size) !=0)
271 std::cout << "Failed" << std::endl
272 << " Pixel differ (as expanded in memory)." << std::endl;
281 std::cout << "OK" << std::endl;
291 int TestWriteSimple(int argc, char *argv[])
295 std::cerr << "usage: \n"
296 << argv[0] << " (without parameters) " << std::endl
303 while( Images[i].sizeX>0 && Images[i].sizeY>0 )
305 ret += WriteSimple(Images[i]);