1 /*=========================================================================
4 Module: $RCSfile: TestWriteSimple.cxx,v $
6 Date: $Date: 2004/12/14 13:05:33 $
7 Version: $Revision: 1.9 $
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
25 #include "gdcmHeader.h"
32 int sizeX; // Size X of the image
33 int sizeY; // Size Y of the image
34 int sizeZ; // Size Z of the image
35 int components; // Number of components for a pixel
36 int componentSize; // Component size (in bits : 8, 16)
37 int componentUse ; // Component size (in bits)
38 int sign; // Sign of components
39 char writeMode; // Write mode
41 // - 'e' : Explicit VR
42 // - 'i' : Implicit VR
46 {256, 256, 1, 1, 8, 8, 0, 'a'},
47 {256, 256, 1, 1, 8, 8, 0, 'e'},
48 {256, 256, 1, 1, 8, 8, 0, 'i'},
50 {512, 256, 1, 1, 8, 8, 0, 'a'},
51 {512, 256, 1, 1, 8, 8, 0, 'e'},
52 {512, 256, 1, 1, 8, 8, 0, 'i'},
54 {256, 512, 1, 1, 8, 8, 0, 'a'},
55 {256, 512, 1, 1, 8, 8, 0, 'e'},
56 {256, 512, 1, 1, 8, 8, 0, 'i'},
58 {256, 512, 1, 1, 16, 16, 0, 'a'},
59 {256, 512, 1, 1, 16, 16, 0, 'e'},
60 {256, 512, 1, 1, 16, 16, 0, 'i'},
61 {256, 512, 1, 1, 16, 16, 0, 'a'},
62 {256, 512, 1, 1, 16, 16, 0, 'e'},
63 {256, 512, 1, 1, 16, 16, 0, 'i'},
64 {0, 0, 1, 1, 8, 8, 0, 'i'} // to find the end
67 int WriteSimple(Image &img)
69 std::string fileName = "TestWriteSimple.dcm";
71 // Step 1 : Create the header of the image
73 gdcm::Header *header = new gdcm::Header();
74 std::ostringstream str;
79 header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0011); // Columns
83 header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0010); // Rows
89 header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0008); // Number of Frames
94 str << img.componentSize;
95 header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0100); // Bits Allocated
98 str << img.componentUse;
99 header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0101); // Bits Stored
102 str << img.componentSize - 1;
103 header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0102); // High Bit
105 // Set the pixel representation
108 header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0103); // Pixel Representation
110 // Set the samples per pixel
112 str << img.components;
113 header->ReplaceOrCreateByNumber(str.str(),0x0028,0x0002); // Samples per Pixel
115 if( !header->IsReadable() )
117 std::cout << "Failed\n"
118 << " Prepared image isn't readable\n";
124 // Step 2 : Create the output image
126 if( img.componentSize%8 > 0 )
128 img.componentSize += 8-img.componentSize%8;
130 size_t size = img.sizeX * img.sizeY * img.sizeZ
131 * img.components * img.componentSize / 8;
132 unsigned char *imageData = new unsigned char[size];
134 // FIXME : find a best heuristic to create the image
135 unsigned char *tmp = imageData;
136 for(int k=0;k<img.sizeZ;k++)
138 for(int j=0;j<img.sizeY;j++)
140 for(int i=0;i<img.sizeX;i++)
142 for(int c=0;c<img.components;c++)
145 if( img.componentSize>8 )
149 tmp += img.componentSize * img.components/8;
155 // Step 3 : Create the file of the image
157 gdcm::File *file = new gdcm::File(header);
158 file->SetImageData(imageData,size);
160 // Step 4 : Set the writting mode and write the image
163 file->SetWriteModeToRaw();
164 switch (img.writeMode)
166 case 'a' : // Write an ACR file
167 file->SetWriteTypeToAcr();
170 case 'e' : // Write a DICOM Explicit VR file
171 file->SetWriteTypeToDcmExplVR();
174 case 'i' : // Write a DICOM Implicit VR file
175 file->SetWriteTypeToDcmImplVR();
179 std::cout << "Failed\n"
180 << " Write mode '"<<img.writeMode<<"' is undefined\n";
188 if( !file->Write(fileName) )
190 std::cout << "Failed\n"
191 << "File in unwrittable\n";
199 // Step 5 : Read the written image
201 gdcm::File* reread = new gdcm::File( fileName );
202 if( !reread->GetHeader()->IsReadable() )
204 std::cerr << "Failed" << std::endl
205 << "Test::TestReadWriteReadCompare: Could not reread image "
206 << "written:" << fileName << std::endl;
213 // Step 6 : Compare to the written image
215 size_t dataSizeWritten = reread->GetImageDataSize();
216 uint8_t* imageDataWritten = reread->GetImageData();
218 // Test the image size
219 if (header->GetXSize() != reread->GetHeader()->GetXSize() ||
220 header->GetYSize() != reread->GetHeader()->GetYSize() ||
221 header->GetZSize() != reread->GetHeader()->GetZSize())
223 std::cout << "Failed" << std::endl
224 << " X Size differs: "
225 << "X: " << header->GetXSize() << " # "
226 << reread->GetHeader()->GetXSize() << " | "
227 << "Y: " << header->GetYSize() << " # "
228 << reread->GetHeader()->GetYSize() << " | "
229 << "Z: " << header->GetZSize() << " # "
230 << reread->GetHeader()->GetZSize() << std::endl;
239 // Test the data size
240 if (size != dataSizeWritten)
242 std::cout << "Failed" << std::endl
243 << " Pixel areas lengths differ: "
244 << size << " # " << dataSizeWritten << std::endl;
253 // Test the data's content
254 if (int res = memcmp(imageData, imageDataWritten, size) !=0)
257 std::cout << "Failed" << std::endl
258 << " Pixel differ (as expanded in memory)." << std::endl;
267 std::cout << "OK" << std::endl;
277 int TestWriteSimple(int argc, char* argv[])
281 std::cerr << "usage: \n"
282 << argv[0] << " (without parameters) " << std::endl
289 while( Images[i].sizeX>0 && Images[i].sizeY>0 )
291 ret += WriteSimple(Images[i]);