1 /*=========================================================================
4 Module: $RCSfile: TestCopyRescaleDicom.cxx,v $
6 Date: $Date: 2005/07/08 13:39:57 $
7 Version: $Revision: 1.20 $
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 =========================================================================*/
19 #include "gdcmFileHelper.h"
20 #include "gdcmValEntry.h"
21 #include "gdcmBinEntry.h"
24 #include <sys/times.h>
25 #include <iomanip> // for std::ios::left, ...
28 #include "gdcmDataImages.h"
31 #include <unistd.h> //for access, unlink
33 #include <io.h> //for _access on Win32
36 bool FileExists(const char *filename)
39 # define access _access
44 if ( access(filename, R_OK) != 0 )
54 bool RemoveFile(const char *source)
57 #define _unlink unlink
59 return unlink(source) != 0 ? false : true;
62 int CopyRescaleDicom(std::string const &filename,
63 std::string const &output )
65 std::cout << " Testing: " << filename << std::endl;
66 if( FileExists( output.c_str() ) )
68 // std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
69 if( !RemoveFile( output.c_str() ) )
71 std::cout << "Ouch, the file exist, but I cannot remove it" << std::endl;
76 //////////////// Step 1:
78 gdcm::File originalF = gdcm::File( );
79 originalF.SetFileName( filename );
82 gdcm::File copyF = gdcm::File( );
84 //First of all copy the file, field by field
86 //////////////// Step 2:
88 // Copy of the file content
89 gdcm::DocEntry *d = originalF.GetFirstEntry();
92 if ( gdcm::BinEntry *b = dynamic_cast<gdcm::BinEntry*>(d) )
94 copyF.InsertBinEntry( b->GetBinArea(),b->GetLength(),
95 b->GetGroup(),b->GetElement(),
98 else if ( gdcm::ValEntry *v = dynamic_cast<gdcm::ValEntry*>(d) )
100 copyF.InsertValEntry( v->GetValue(),
101 v->GetGroup(),v->GetElement(),
106 // We skip pb of SQ recursive exploration
109 d = originalF.GetNextEntry();
112 gdcm::FileHelper original = gdcm::FileHelper( &originalF );
113 gdcm::FileHelper copy = gdcm::FileHelper( ©F );
115 size_t dataSize = original.GetImageDataSize();
118 uint8_t *rescaleImage;
120 const std::string &bitsStored = originalF.GetEntryValue(0x0028,0x0101);
121 if( bitsStored == "16" )
123 std::cout << "Rescale...";
124 copyF.InsertValEntry( "8", 0x0028, 0x0100); // Bits Allocated
125 copyF.InsertValEntry( "8", 0x0028, 0x0101); // Bits Stored
126 copyF.InsertValEntry( "7", 0x0028, 0x0102); // High Bit
127 copyF.InsertValEntry( "0", 0x0028, 0x0103); // Pixel Representation
129 // We assume the value were from 0 to uint16_t max
130 rescaleSize = dataSize / 2;
131 rescaleImage = new uint8_t[dataSize];
133 uint16_t *imageData16 = (uint16_t*)original.GetImageData();
134 uint16_t *tmpImage = imageData16;
135 uint8_t *tmpRescale = rescaleImage;
136 for(unsigned int i=0; i<rescaleSize; i++)
138 *tmpRescale = (uint8_t)( (*tmpImage)>>8 );
145 std::cout << "Same...";
146 rescaleSize = dataSize;
147 rescaleImage = new uint8_t[dataSize];
148 memcpy(rescaleImage,original.GetImageData(),dataSize);
151 copy.SetImageData(rescaleImage, rescaleSize);
153 //////////////// Step 3:
155 copy.SetWriteModeToRGB();
156 if( !copy.WriteDcmExplVR(output) )
158 std::cout << " Failed" << std::endl
159 << " " << output << " not written" << std::endl;
161 delete[] rescaleImage;
166 //////////////// Step 4:
168 gdcm::FileHelper copy2 = gdcm::FileHelper( output );
170 //Is the file written still gdcm parsable ?
171 if ( !copy2.GetFile()->IsReadable() )
173 std::cout << " Failed" << std::endl
174 << " " << output << " not readable" << std::endl;
176 delete[] rescaleImage;
181 //////////////// Step 5:
183 size_t dataSizeWritten = copy2.GetImageDataSize();
184 uint8_t *imageDataWritten = copy2.GetImageData();
186 if (originalF.GetXSize() != copy2.GetFile()->GetXSize() ||
187 originalF.GetYSize() != copy2.GetFile()->GetYSize() ||
188 originalF.GetZSize() != copy2.GetFile()->GetZSize())
190 std::cout << "Failed" << std::endl
191 << " X Size differs: "
192 << "X: " << originalF.GetXSize() << " # "
193 << copy2.GetFile()->GetXSize() << " | "
194 << "Y: " << originalF.GetYSize() << " # "
195 << copy2.GetFile()->GetYSize() << " | "
196 << "Z: " << originalF.GetZSize() << " # "
197 << copy2.GetFile()->GetZSize() << std::endl;
198 delete[] rescaleImage;
203 if (rescaleSize != dataSizeWritten)
205 std::cout << " Failed" << std::endl
206 << " Pixel areas lengths differ: "
207 << dataSize << " # " << dataSizeWritten << std::endl;
209 delete[] rescaleImage;
214 if (int res = memcmp(rescaleImage, imageDataWritten, rescaleSize) !=0)
217 std::cout << " Failed" << std::endl
218 << " Pixel differ (as expanded in memory)." << std::endl;
220 delete[] rescaleImage;
224 std::cout << "OK." << std::endl ;
226 delete[] rescaleImage;
231 // Here we load a gdcmFile and then try to create from scratch a copy of it,
232 // copying field by field the dicom image
234 int TestCopyRescaleDicom(int argc, char *argv[])
238 // The test is specified a specific filename, use it instead of looping
240 const std::string input = argv[1];
241 const std::string reference = argv[2];
242 return CopyRescaleDicom( input, reference );
244 else if ( argc > 3 || argc == 2 )
246 std::cout << " Usage: " << argv[0]
247 << " (no arguments needed)." << std::endl;
248 std::cout << "or Usage: " << argv[0]
249 << " filename.dcm reference.dcm" << std::endl;
254 std::cout << " Description (Test::TestCopyDicom): "
256 std::cout << " For all images in gdcmData (and not blacklisted in "
257 "Test/CMakeLists.txt)"
259 std::cout << " apply the following to each filename.xxx: "
261 std::cout << " step 1: parse the image (as gdcmFile) and call"
262 << " IsReadable(). After that, call GetImageData() and "
263 << "GetImageDataSize() "
265 std::cout << " step 2: create a copy of the readed file and the new"
266 << " pixel data are set to the copy"
268 std::cout << " step 3: write the copy of the image"
270 std::cout << " step 4: read the copy and call IsReadable()"
272 std::cout << " step 5: compare (in memory with memcmp) that the two "
273 << "images " << std::endl
274 << " match (as expanded by gdcm)." << std::endl;
275 std::cout << std::endl;
278 clock_t r1,r2, r3,r4;
279 struct tms tms1,tms2, tms3,tms4;
283 int retVal = 0; //by default this is an error
284 while( gdcmDataImages[i] != 0 )
286 std::string filename = GDCM_DATA_ROOT;
287 filename += "/"; //doh!
288 filename += gdcmDataImages[i];
290 std::string output = "output.dcm";
293 if( CopyRescaleDicom( filename, output ) != 0 )
300 << std::setw(150-strlen(gdcmDataImages[i]))
301 << gdcmDataImages[i] << " user time: "
302 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
304 << (long) ((tms2.tms_stime) - (tms1.tms_stime))
305 << "\t elapsed time: " << r2 - r1
313 << std::setw(150-strlen("Gross Total")) << " --> "
314 << "Gross Total" << " user time: "
315 << (long) ((tms4.tms_utime) - (tms3.tms_utime))
317 << (long) ((tms4.tms_stime) - (tms3.tms_stime))
318 << "\t elapsed time: " << (long) (r4 - r3)