1 /*=========================================================================
4 Module: $RCSfile: TestCopyRescaleDicom.cxx,v $
6 Date: $Date: 2005/02/09 22:50:47 $
7 Version: $Revision: 1.17 $
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 "gdcmDataImages.h"
27 #include <unistd.h> //for access, unlink
29 #include <io.h> //for _access on Win32
32 bool FileExists(const char *filename)
35 # define access _access
40 if ( access(filename, R_OK) != 0 )
50 bool RemoveFile(const char *source)
53 #define _unlink unlink
55 return unlink(source) != 0 ? false : true;
58 int CopyRescaleDicom(std::string const &filename,
59 std::string const &output )
61 std::cout << " Testing: " << filename << std::endl;
62 if( FileExists( output.c_str() ) )
64 // std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
65 if( !RemoveFile( output.c_str() ) )
67 std::cout << "Ouch, the file exist, but I cannot remove it" << std::endl;
72 //////////////// Step 1:
74 gdcm::File originalF = gdcm::File( filename );
75 gdcm::File copyF = gdcm::File( );
77 //First of all copy the file, field by field
79 //////////////// Step 2:
81 // Copy of the file content
82 gdcm::DocEntry *d = originalF.GetFirstEntry();
85 if ( gdcm::BinEntry *b = dynamic_cast<gdcm::BinEntry*>(d) )
87 copyF.InsertBinEntry( b->GetBinArea(),b->GetLength(),
88 b->GetGroup(),b->GetElement(),
91 else if ( gdcm::ValEntry *v = dynamic_cast<gdcm::ValEntry*>(d) )
93 copyF.InsertValEntry( v->GetValue(),
94 v->GetGroup(),v->GetElement(),
99 // We skip pb of SQ recursive exploration
102 d = originalF.GetNextEntry();
105 gdcm::FileHelper original = gdcm::FileHelper( &originalF );
106 gdcm::FileHelper copy = gdcm::FileHelper( ©F );
108 size_t dataSize = original.GetImageDataSize();
111 uint8_t *rescaleImage;
113 const std::string &bitsStored = originalF.GetEntryValue(0x0028,0x0101);
114 if( bitsStored == "16" )
116 std::cout << "Rescale...";
117 copyF.InsertValEntry( "8", 0x0028, 0x0100); // Bits Allocated
118 copyF.InsertValEntry( "8", 0x0028, 0x0101); // Bits Stored
119 copyF.InsertValEntry( "7", 0x0028, 0x0102); // High Bit
120 copyF.InsertValEntry( "0", 0x0028, 0x0103); // Pixel Representation
122 // We assume the value were from 0 to uint16_t max
123 rescaleSize = dataSize / 2;
124 rescaleImage = new uint8_t[dataSize];
126 uint16_t *imageData16 = (uint16_t*)original.GetImageData();
127 uint16_t *tmpImage = imageData16;
128 uint8_t *tmpRescale = rescaleImage;
129 for(unsigned int i=0; i<rescaleSize; i++)
131 *tmpRescale = (*tmpImage)>>8;
138 std::cout << "Same...";
139 rescaleSize = dataSize;
140 rescaleImage = new uint8_t[dataSize];
141 memcpy(rescaleImage,original.GetImageData(),dataSize);
144 copy.SetImageData(rescaleImage, rescaleSize);
146 //////////////// Step 3:
148 copy.SetWriteModeToRGB();
149 if( !copy.WriteDcmExplVR(output) )
151 std::cout << " Failed" << std::endl
152 << " " << output << " not written" << std::endl;
154 delete[] rescaleImage;
159 //////////////// Step 4:
161 gdcm::FileHelper copy2 = gdcm::FileHelper( output );
163 //Is the file written still gdcm parsable ?
164 if ( !copy2.GetFile()->IsReadable() )
166 std::cout << " Failed" << std::endl
167 << " " << output << " not readable" << std::endl;
169 delete[] rescaleImage;
174 //////////////// Step 5:
176 size_t dataSizeWritten = copy2.GetImageDataSize();
177 uint8_t *imageDataWritten = copy2.GetImageData();
179 if (originalF.GetXSize() != copy2.GetFile()->GetXSize() ||
180 originalF.GetYSize() != copy2.GetFile()->GetYSize() ||
181 originalF.GetZSize() != copy2.GetFile()->GetZSize())
183 std::cout << "Failed" << std::endl
184 << " X Size differs: "
185 << "X: " << originalF.GetXSize() << " # "
186 << copy2.GetFile()->GetXSize() << " | "
187 << "Y: " << originalF.GetYSize() << " # "
188 << copy2.GetFile()->GetYSize() << " | "
189 << "Z: " << originalF.GetZSize() << " # "
190 << copy2.GetFile()->GetZSize() << std::endl;
191 delete[] rescaleImage;
196 if (rescaleSize != dataSizeWritten)
198 std::cout << " Failed" << std::endl
199 << " Pixel areas lengths differ: "
200 << dataSize << " # " << dataSizeWritten << std::endl;
202 delete[] rescaleImage;
207 if (int res = memcmp(rescaleImage, imageDataWritten, rescaleSize) !=0)
210 std::cout << " Failed" << std::endl
211 << " Pixel differ (as expanded in memory)." << std::endl;
213 delete[] rescaleImage;
217 std::cout << "OK." << std::endl ;
219 delete[] rescaleImage;
224 // Here we load a gdcmFile and then try to create from scratch a copy of it,
225 // copying field by field the dicom image
227 int TestCopyRescaleDicom(int argc, char *argv[])
231 // The test is specified a specific filename, use it instead of looping
233 const std::string input = argv[1];
234 const std::string reference = argv[2];
235 return CopyRescaleDicom( input, reference );
237 else if ( argc > 3 || argc == 2 )
239 std::cout << " Usage: " << argv[0]
240 << " (no arguments needed)." << std::endl;
241 std::cout << "or Usage: " << argv[0]
242 << " filename.dcm reference.dcm" << std::endl;
247 std::cout << " Description (Test::TestCopyDicom): "
249 std::cout << " For all images in gdcmData (and not blacklisted in "
250 "Test/CMakeLists.txt)"
252 std::cout << " apply the following to each filename.xxx: "
254 std::cout << " step 1: parse the image (as gdcmFile) and call"
255 << " IsReadable(). After that, call GetImageData() and "
256 << "GetImageDataSize() "
258 std::cout << " step 2: create a copy of the readed file and the new"
259 << " pixel data are set to the copy"
261 std::cout << " step 3: write the copy of the image"
263 std::cout << " step 4: read the copy and call IsReadable()"
265 std::cout << " step 5: compare (in memory with memcmp) that the two "
266 << "images " << std::endl
267 << " match (as expanded by gdcm)." << std::endl;
268 std::cout << std::endl;
271 int retVal = 0; //by default this is an error
272 while( gdcmDataImages[i] != 0 )
274 std::string filename = GDCM_DATA_ROOT;
275 filename += "/"; //doh!
276 filename += gdcmDataImages[i];
278 std::string output = "output.dcm";
280 if( CopyRescaleDicom( filename, output ) != 0 )