1 /*=========================================================================
4 Module: $RCSfile: TestWriteSimple.cxx,v $
6 Date: $Date: 2005/01/25 15:44:23 $
7 Version: $Revision: 1.16 $
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 *header = new gdcm::File();
76 std::ostringstream str;
81 header->InsertValEntry(str.str(),0x0028,0x0011); // Columns
85 header->InsertValEntry(str.str(),0x0028,0x0010); // Rows
91 header->InsertValEntry(str.str(),0x0028,0x0008); // Number of Frames
96 str << img.componentSize;
97 header->InsertValEntry(str.str(),0x0028,0x0100); // Bits Allocated
100 str << img.componentUse;
101 header->InsertValEntry(str.str(),0x0028,0x0101); // Bits Stored
104 str << img.componentSize - 1;
105 header->InsertValEntry(str.str(),0x0028,0x0102); // High Bit
107 // Set the pixel representation
110 header->InsertValEntry(str.str(),0x0028,0x0103); // Pixel Representation
112 // Set the samples per pixel
114 str << img.components;
115 header->InsertValEntry(str.str(),0x0028,0x0002); // Samples per Pixel
117 if( !header->IsReadable() )
119 std::cout << "Failed\n"
120 << " Prepared image isn't readable\n";
126 // Step 2 : Create the output image
128 if( img.componentSize%8 > 0 )
130 img.componentSize += 8-img.componentSize%8;
132 size_t size = img.sizeX * img.sizeY * img.sizeZ
133 * img.components * img.componentSize / 8;
134 unsigned char *imageData = new unsigned char[size];
136 // FIXME : find a best heuristic to create the image
137 unsigned char *tmp = imageData;
138 for(int k=0;k<img.sizeZ;k++)
140 for(int j=0;j<img.sizeY;j++)
142 for(int i=0;i<img.sizeX;i++)
144 for(int c=0;c<img.components;c++)
147 if( img.componentSize>8 )
151 tmp += img.componentSize * img.components/8;
157 // Step 3 : Create the file of the image
159 gdcm::FileHelper *file = new gdcm::FileHelper(header);
160 file->SetImageData(imageData,size);
162 // Step 4 : Set the writting mode and write the image
165 file->SetWriteModeToRaw();
166 switch (img.writeMode)
168 case 'a' : // Write an ACR file
169 file->SetWriteTypeToAcr();
172 case 'e' : // Write a DICOM Explicit VR file
173 file->SetWriteTypeToDcmExplVR();
176 case 'i' : // Write a DICOM Implicit VR file
177 file->SetWriteTypeToDcmImplVR();
181 std::cout << "Failed\n"
182 << " Write mode '"<<img.writeMode<<"' is undefined\n";
190 if( !file->Write(fileName) )
192 std::cout << "Failed\n"
193 << "File in unwrittable\n";
201 // Step 5 : Read the written image
203 gdcm::FileHelper* reread = new gdcm::FileHelper( fileName );
204 if( !reread->GetFile()->IsReadable() )
206 std::cerr << "Failed" << std::endl
207 << "Test::TestReadWriteReadCompare: Could not reread image "
208 << "written:" << fileName << std::endl;
215 // Step 6 : Compare to the written image
217 size_t dataSizeWritten = reread->GetImageDataSize();
218 uint8_t* imageDataWritten = reread->GetImageData();
220 // Test the image size
221 if (header->GetXSize() != reread->GetFile()->GetXSize() ||
222 header->GetYSize() != reread->GetFile()->GetYSize() ||
223 header->GetZSize() != reread->GetFile()->GetZSize())
225 std::cout << "Failed" << std::endl
226 << " X Size differs: "
227 << "X: " << header->GetXSize() << " # "
228 << reread->GetFile()->GetXSize() << " | "
229 << "Y: " << header->GetYSize() << " # "
230 << reread->GetFile()->GetYSize() << " | "
231 << "Z: " << header->GetZSize() << " # "
232 << reread->GetFile()->GetZSize() << std::endl;
241 // Test the data size
242 if (size != dataSizeWritten)
244 std::cout << "Failed" << std::endl
245 << " Pixel areas lengths differ: "
246 << size << " # " << dataSizeWritten << std::endl;
255 // Test the data's content
256 if (int res = memcmp(imageData, imageDataWritten, size) !=0)
259 std::cout << "Failed" << std::endl
260 << " Pixel differ (as expanded in memory)." << std::endl;
269 std::cout << "OK" << std::endl;
279 int TestWriteSimple(int argc, char* argv[])
283 std::cerr << "usage: \n"
284 << argv[0] << " (without parameters) " << std::endl
291 while( Images[i].sizeX>0 && Images[i].sizeY>0 )
293 ret += WriteSimple(Images[i]);