1 /*=========================================================================
4 Module: $RCSfile: TestCopyRescaleDicom.cxx,v $
6 Date: $Date: 2005/01/08 15:03:58 $
7 Version: $Revision: 1.4 $
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 =========================================================================*/
18 #include "gdcmHeader.h"
20 #include "gdcmValEntry.h"
21 #include "gdcmBinEntry.h"
24 #include "gdcmDataImages.h"
26 bool FileExists(const char* filename);
28 bool RemoveFile(const char* source);
30 int CopyRescaleDicom(std::string const & filename,
31 std::string const & output )
33 std::cout << " Testing: " << filename << std::endl;
34 if( FileExists( output.c_str() ) )
36 // std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
37 if( !RemoveFile( output.c_str() ) )
39 std::cout << "Ouch, the file exist, but I cannot remove it" << std::endl;
44 //////////////// Step 1:
46 gdcm::Header *originalH = new gdcm::Header( filename );
47 gdcm::Header *copyH = new gdcm::Header( );
49 //First of all copy the header field by field
51 // Warning :Accessor gdcmElementSet::GetEntry() should not exist
52 // It was commented out by Mathieu, that was a *good* idea
53 // (the user does NOT have to know the way we implemented the Header !)
54 // Waiting for a 'clean' solution, I keep the method ...JPRx
57 //////////////// Step 2:
59 originalH->Initialize();
60 gdcm::DocEntry* d = originalH->GetNextEntry();
62 // Copy of the header content
65 if ( gdcm::BinEntry* b = dynamic_cast<gdcm::BinEntry*>(d) )
67 copyH->ReplaceOrCreate(
74 else if ( gdcm::ValEntry* v = dynamic_cast<gdcm::ValEntry*>(d) )
76 copyH->ReplaceOrCreate(
84 // We skip pb of SQ recursive exploration
87 d=originalH->GetNextEntry();
90 gdcm::File *original = new gdcm::File( originalH );
91 gdcm::File *copy = new gdcm::File( copyH );
93 size_t dataSize = original->GetImageDataSize();
96 uint8_t *rescaleImage;
98 const std::string & bitsStored = originalH->GetEntry(0x0028,0x0101);
99 if( bitsStored == "16" )
101 std::cout << "Rescale...";
102 copyH->ReplaceOrCreate( "8", 0x0028, 0x0100); // BitsAllocated
103 copyH->ReplaceOrCreate( "8", 0x0028, 0x0101); // BitsStored
104 copyH->ReplaceOrCreate( "7", 0x0028, 0x0102); // HighBit
105 copyH->ReplaceOrCreate( "0", 0x0028, 0x0103); //Pixel Representation
107 // We assume the value were from 0 to uint16_t max
108 rescaleSize = dataSize / 2;
109 rescaleImage = new uint8_t[dataSize];
111 uint16_t* imageData16 = (uint16_t*)original->GetImageData();
112 for(unsigned int i=0; i<rescaleSize; i++)
114 rescaleImage[i] = imageData16[i]/256;
119 std::cout << "Same...";
120 rescaleSize = dataSize;
121 rescaleImage = new uint8_t[dataSize];
122 memcpy(rescaleImage,original->GetImageData(),dataSize);
125 copy->SetImageData(rescaleImage, rescaleSize);
127 //////////////// Step 3:
129 copy->SetWriteModeToRGB();
130 if( !copy->WriteDcmExplVR(output) )
132 std::cout << " Failed" << std::endl
133 << " " << output << " not written" << std::endl;
139 delete[] rescaleImage;
147 //////////////// Step 4:
149 copy = new gdcm::File( output );
151 //Is the file written still gdcm parsable ?
152 if ( !copy->GetHeader()->IsReadable() )
154 std::cout << " Failed" << std::endl
155 << " " << output << " not readable" << std::endl;
159 delete[] rescaleImage;
164 //////////////// Step 5:
166 size_t dataSizeWritten = copy->GetImageDataSize();
167 uint8_t* imageDataWritten = copy->GetImageData();
169 if (originalH->GetXSize() != copy->GetHeader()->GetXSize() ||
170 originalH->GetYSize() != copy->GetHeader()->GetYSize() ||
171 originalH->GetZSize() != copy->GetHeader()->GetZSize())
173 std::cout << "Failed" << std::endl
174 << " X Size differs: "
175 << "X: " << originalH->GetXSize() << " # "
176 << copy->GetHeader()->GetXSize() << " | "
177 << "Y: " << originalH->GetYSize() << " # "
178 << copy->GetHeader()->GetYSize() << " | "
179 << "Z: " << originalH->GetZSize() << " # "
180 << copy->GetHeader()->GetZSize() << std::endl;
184 delete[] rescaleImage;
189 if (rescaleSize != dataSizeWritten)
191 std::cout << " Failed" << std::endl
192 << " Pixel areas lengths differ: "
193 << dataSize << " # " << dataSizeWritten << std::endl;
198 delete[] rescaleImage;
203 if (int res = memcmp(rescaleImage, imageDataWritten, rescaleSize) !=0)
206 std::cout << " Failed" << std::endl
207 << " Pixel differ (as expanded in memory)." << std::endl;
212 delete[] rescaleImage;
216 std::cout << "OK." << std::endl ;
221 delete[] rescaleImage;
226 // Here we load a gdcmFile and then try to create from scratch a copy of it,
227 // copying field by field the dicom image
229 int TestCopyRescaleDicom(int argc, char* argv[])
233 // The test is specified a specific filename, use it instead of looping
235 const std::string input = argv[1];
236 const std::string reference = argv[2];
237 return CopyRescaleDicom( input, reference );
239 else if ( argc > 3 || argc == 2 )
241 std::cout << " Usage: " << argv[0]
242 << " (no arguments needed)." << std::endl;
243 std::cout << "or Usage: " << argv[0]
244 << " filename.dcm reference.dcm" << std::endl;
249 std::cout << " Description (Test::TestCopyDicom): "
251 std::cout << " For all images in gdcmData (and not blacklisted in "
252 "Test/CMakeLists.txt)"
254 std::cout << " apply the following to each filename.xxx: "
256 std::cout << " step 1: parse the image (as gdcmHeader) and call"
257 << " IsReadable(). After that, call GetImageData() and "
258 << "GetImageDataSize() "
260 std::cout << " step 2: create a copy of the readed file and the new"
261 << " pixel datas are set to the copy"
263 std::cout << " step 3: write the copy of the image"
265 std::cout << " step 4: read the copy and call IsReadable()"
267 std::cout << " step 5: compare (in memory with memcmp) that the two "
268 << "images " << std::endl
269 << " match (as expanded by gdcm)." << std::endl;
270 std::cout << std::endl;
273 int retVal = 0; //by default this is an error
274 while( gdcmDataImages[i] != 0 )
276 std::string filename = GDCM_DATA_ROOT;
277 filename += "/"; //doh!
278 filename += gdcmDataImages[i];
280 std::string output = "output.dcm";
282 if( CopyRescaleDicom( filename, output ) != 0 )