1 /*=========================================================================
4 Module: $RCSfile: TestCopyRescaleDicom.cxx,v $
6 Date: $Date: 2005/01/14 11:28:29 $
7 Version: $Revision: 1.5 $
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 // Copy of the header content
60 originalH->InitTraversal();
61 gdcm::DocEntry* d = originalH->GetNextEntry();
64 if ( gdcm::BinEntry* b = dynamic_cast<gdcm::BinEntry*>(d) )
66 copyH->ReplaceOrCreate(
73 else if ( gdcm::ValEntry* v = dynamic_cast<gdcm::ValEntry*>(d) )
75 copyH->ReplaceOrCreate(
83 // We skip pb of SQ recursive exploration
86 d=originalH->GetNextEntry();
89 gdcm::File *original = new gdcm::File( originalH );
90 gdcm::File *copy = new gdcm::File( copyH );
92 size_t dataSize = original->GetImageDataSize();
95 uint8_t *rescaleImage;
97 const std::string & bitsStored = originalH->GetEntry(0x0028,0x0101);
98 if( bitsStored == "16" )
100 std::cout << "Rescale...";
101 copyH->ReplaceOrCreate( "8", 0x0028, 0x0100); // BitsAllocated
102 copyH->ReplaceOrCreate( "8", 0x0028, 0x0101); // BitsStored
103 copyH->ReplaceOrCreate( "7", 0x0028, 0x0102); // HighBit
104 copyH->ReplaceOrCreate( "0", 0x0028, 0x0103); //Pixel Representation
106 // We assume the value were from 0 to uint16_t max
107 rescaleSize = dataSize / 2;
108 rescaleImage = new uint8_t[dataSize];
110 uint16_t* imageData16 = (uint16_t*)original->GetImageData();
111 for(unsigned int i=0; i<rescaleSize; i++)
113 rescaleImage[i] = imageData16[i]/256;
118 std::cout << "Same...";
119 rescaleSize = dataSize;
120 rescaleImage = new uint8_t[dataSize];
121 memcpy(rescaleImage,original->GetImageData(),dataSize);
124 copy->SetImageData(rescaleImage, rescaleSize);
126 //////////////// Step 3:
128 copy->SetWriteModeToRGB();
129 if( !copy->WriteDcmExplVR(output) )
131 std::cout << " Failed" << std::endl
132 << " " << output << " not written" << std::endl;
138 delete[] rescaleImage;
146 //////////////// Step 4:
148 copy = new gdcm::File( output );
150 //Is the file written still gdcm parsable ?
151 if ( !copy->GetHeader()->IsReadable() )
153 std::cout << " Failed" << std::endl
154 << " " << output << " not readable" << std::endl;
158 delete[] rescaleImage;
163 //////////////// Step 5:
165 size_t dataSizeWritten = copy->GetImageDataSize();
166 uint8_t* imageDataWritten = copy->GetImageData();
168 if (originalH->GetXSize() != copy->GetHeader()->GetXSize() ||
169 originalH->GetYSize() != copy->GetHeader()->GetYSize() ||
170 originalH->GetZSize() != copy->GetHeader()->GetZSize())
172 std::cout << "Failed" << std::endl
173 << " X Size differs: "
174 << "X: " << originalH->GetXSize() << " # "
175 << copy->GetHeader()->GetXSize() << " | "
176 << "Y: " << originalH->GetYSize() << " # "
177 << copy->GetHeader()->GetYSize() << " | "
178 << "Z: " << originalH->GetZSize() << " # "
179 << copy->GetHeader()->GetZSize() << std::endl;
183 delete[] rescaleImage;
188 if (rescaleSize != dataSizeWritten)
190 std::cout << " Failed" << std::endl
191 << " Pixel areas lengths differ: "
192 << dataSize << " # " << dataSizeWritten << std::endl;
197 delete[] rescaleImage;
202 if (int res = memcmp(rescaleImage, imageDataWritten, rescaleSize) !=0)
205 std::cout << " Failed" << std::endl
206 << " Pixel differ (as expanded in memory)." << std::endl;
211 delete[] rescaleImage;
215 std::cout << "OK." << std::endl ;
220 delete[] rescaleImage;
225 // Here we load a gdcmFile and then try to create from scratch a copy of it,
226 // copying field by field the dicom image
228 int TestCopyRescaleDicom(int argc, char* argv[])
232 // The test is specified a specific filename, use it instead of looping
234 const std::string input = argv[1];
235 const std::string reference = argv[2];
236 return CopyRescaleDicom( input, reference );
238 else if ( argc > 3 || argc == 2 )
240 std::cout << " Usage: " << argv[0]
241 << " (no arguments needed)." << std::endl;
242 std::cout << "or Usage: " << argv[0]
243 << " filename.dcm reference.dcm" << std::endl;
248 std::cout << " Description (Test::TestCopyDicom): "
250 std::cout << " For all images in gdcmData (and not blacklisted in "
251 "Test/CMakeLists.txt)"
253 std::cout << " apply the following to each filename.xxx: "
255 std::cout << " step 1: parse the image (as gdcmHeader) and call"
256 << " IsReadable(). After that, call GetImageData() and "
257 << "GetImageDataSize() "
259 std::cout << " step 2: create a copy of the readed file and the new"
260 << " pixel datas are set to the copy"
262 std::cout << " step 3: write the copy of the image"
264 std::cout << " step 4: read the copy and call IsReadable()"
266 std::cout << " step 5: compare (in memory with memcmp) that the two "
267 << "images " << std::endl
268 << " match (as expanded by gdcm)." << std::endl;
269 std::cout << std::endl;
272 int retVal = 0; //by default this is an error
273 while( gdcmDataImages[i] != 0 )
275 std::string filename = GDCM_DATA_ROOT;
276 filename += "/"; //doh!
277 filename += gdcmDataImages[i];
279 std::string output = "output.dcm";
281 if( CopyRescaleDicom( filename, output ) != 0 )