1 /*=========================================================================
4 Module: $RCSfile: TestCopyRescaleDicom.cxx,v $
6 Date: $Date: 2005/05/20 15:50:27 $
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 =========================================================================*/
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( filename );
79 gdcm::File copyF = gdcm::File( );
81 //First of all copy the file, field by field
83 //////////////// Step 2:
85 // Copy of the file content
86 gdcm::DocEntry *d = originalF.GetFirstEntry();
89 if ( gdcm::BinEntry *b = dynamic_cast<gdcm::BinEntry*>(d) )
91 copyF.InsertBinEntry( b->GetBinArea(),b->GetLength(),
92 b->GetGroup(),b->GetElement(),
95 else if ( gdcm::ValEntry *v = dynamic_cast<gdcm::ValEntry*>(d) )
97 copyF.InsertValEntry( v->GetValue(),
98 v->GetGroup(),v->GetElement(),
103 // We skip pb of SQ recursive exploration
106 d = originalF.GetNextEntry();
109 gdcm::FileHelper original = gdcm::FileHelper( &originalF );
110 gdcm::FileHelper copy = gdcm::FileHelper( ©F );
112 size_t dataSize = original.GetImageDataSize();
115 uint8_t *rescaleImage;
117 const std::string &bitsStored = originalF.GetEntryValue(0x0028,0x0101);
118 if( bitsStored == "16" )
120 std::cout << "Rescale...";
121 copyF.InsertValEntry( "8", 0x0028, 0x0100); // Bits Allocated
122 copyF.InsertValEntry( "8", 0x0028, 0x0101); // Bits Stored
123 copyF.InsertValEntry( "7", 0x0028, 0x0102); // High Bit
124 copyF.InsertValEntry( "0", 0x0028, 0x0103); // Pixel Representation
126 // We assume the value were from 0 to uint16_t max
127 rescaleSize = dataSize / 2;
128 rescaleImage = new uint8_t[dataSize];
130 uint16_t *imageData16 = (uint16_t*)original.GetImageData();
131 uint16_t *tmpImage = imageData16;
132 uint8_t *tmpRescale = rescaleImage;
133 for(unsigned int i=0; i<rescaleSize; i++)
135 *tmpRescale = (uint8_t)( (*tmpImage)>>8 );
142 std::cout << "Same...";
143 rescaleSize = dataSize;
144 rescaleImage = new uint8_t[dataSize];
145 memcpy(rescaleImage,original.GetImageData(),dataSize);
148 copy.SetImageData(rescaleImage, rescaleSize);
150 //////////////// Step 3:
152 copy.SetWriteModeToRGB();
153 if( !copy.WriteDcmExplVR(output) )
155 std::cout << " Failed" << std::endl
156 << " " << output << " not written" << std::endl;
158 delete[] rescaleImage;
163 //////////////// Step 4:
165 gdcm::FileHelper copy2 = gdcm::FileHelper( output );
167 //Is the file written still gdcm parsable ?
168 if ( !copy2.GetFile()->IsReadable() )
170 std::cout << " Failed" << std::endl
171 << " " << output << " not readable" << std::endl;
173 delete[] rescaleImage;
178 //////////////// Step 5:
180 size_t dataSizeWritten = copy2.GetImageDataSize();
181 uint8_t *imageDataWritten = copy2.GetImageData();
183 if (originalF.GetXSize() != copy2.GetFile()->GetXSize() ||
184 originalF.GetYSize() != copy2.GetFile()->GetYSize() ||
185 originalF.GetZSize() != copy2.GetFile()->GetZSize())
187 std::cout << "Failed" << std::endl
188 << " X Size differs: "
189 << "X: " << originalF.GetXSize() << " # "
190 << copy2.GetFile()->GetXSize() << " | "
191 << "Y: " << originalF.GetYSize() << " # "
192 << copy2.GetFile()->GetYSize() << " | "
193 << "Z: " << originalF.GetZSize() << " # "
194 << copy2.GetFile()->GetZSize() << std::endl;
195 delete[] rescaleImage;
200 if (rescaleSize != dataSizeWritten)
202 std::cout << " Failed" << std::endl
203 << " Pixel areas lengths differ: "
204 << dataSize << " # " << dataSizeWritten << std::endl;
206 delete[] rescaleImage;
211 if (int res = memcmp(rescaleImage, imageDataWritten, rescaleSize) !=0)
214 std::cout << " Failed" << std::endl
215 << " Pixel differ (as expanded in memory)." << std::endl;
217 delete[] rescaleImage;
221 std::cout << "OK." << std::endl ;
223 delete[] rescaleImage;
228 // Here we load a gdcmFile and then try to create from scratch a copy of it,
229 // copying field by field the dicom image
231 int TestCopyRescaleDicom(int argc, char *argv[])
235 // The test is specified a specific filename, use it instead of looping
237 const std::string input = argv[1];
238 const std::string reference = argv[2];
239 return CopyRescaleDicom( input, reference );
241 else if ( argc > 3 || argc == 2 )
243 std::cout << " Usage: " << argv[0]
244 << " (no arguments needed)." << std::endl;
245 std::cout << "or Usage: " << argv[0]
246 << " filename.dcm reference.dcm" << std::endl;
251 std::cout << " Description (Test::TestCopyDicom): "
253 std::cout << " For all images in gdcmData (and not blacklisted in "
254 "Test/CMakeLists.txt)"
256 std::cout << " apply the following to each filename.xxx: "
258 std::cout << " step 1: parse the image (as gdcmFile) and call"
259 << " IsReadable(). After that, call GetImageData() and "
260 << "GetImageDataSize() "
262 std::cout << " step 2: create a copy of the readed file and the new"
263 << " pixel data are set to the copy"
265 std::cout << " step 3: write the copy of the image"
267 std::cout << " step 4: read the copy and call IsReadable()"
269 std::cout << " step 5: compare (in memory with memcmp) that the two "
270 << "images " << std::endl
271 << " match (as expanded by gdcm)." << std::endl;
272 std::cout << std::endl;
275 clock_t r1,r2, r3,r4;
276 struct tms tms1,tms2, tms3,tms4;
280 int retVal = 0; //by default this is an error
281 while( gdcmDataImages[i] != 0 )
283 std::string filename = GDCM_DATA_ROOT;
284 filename += "/"; //doh!
285 filename += gdcmDataImages[i];
287 std::string output = "output.dcm";
290 if( CopyRescaleDicom( filename, output ) != 0 )
297 << std::setw(150-strlen(gdcmDataImages[i]))
298 << gdcmDataImages[i] << " user time: "
299 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
301 << (long) ((tms2.tms_stime) - (tms1.tms_stime))
302 << "\t elapsed time: " << r2 - r1
310 << std::setw(150-strlen("Gross Total")) << " --> "
311 << "Gross Total" << " user time: "
312 << (long) ((tms4.tms_utime) - (tms3.tms_utime))
314 << (long) ((tms4.tms_stime) - (tms3.tms_stime))
315 << "\t elapsed time: " << (long) (r4 - r3)