1 /*=========================================================================
4 Module: $RCSfile: TestReadWriteReadCompare.cxx,v $
6 Date: $Date: 2007/10/19 15:14:05 $
7 Version: $Revision: 1.35 $
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 "gdcmDebug.h"
22 #include "gdcmGlobal.h"
26 #include "gdcmDataImages.h"
28 const unsigned int MAX_NUMBER_OF_DIFFERENCE = 10;
33 static int CompareInternal(std::string const &filename, std::string const &output)
35 std::cout << "----------------------------------------------------------------------" << std::endl
36 << " Testing: " << filename << std::endl;
38 //////////////// Step 1 (see above description):
40 GDCM_NAME_SPACE::File *file = GDCM_NAME_SPACE::File::New( );
41 file->SetFileName( filename );
43 if( !file->IsReadable() )
45 std::cout << "Failed" << std::endl
46 << "Test::TestReadWriteReadCompare: Image not gdcm compatible:"
47 << filename << std::endl;
52 std::cout << " step 1...";
54 //////////////// Step 2:
55 GDCM_NAME_SPACE::FileHelper *filehelper = GDCM_NAME_SPACE::FileHelper::New( file );
56 int dataSize = filehelper->GetImageDataSize();
57 uint8_t *imageData = filehelper->GetImageData(); //EXTREMELY IMPORTANT
58 // Sure, it is : It's up to the user to decide if he wants to
59 // GetImageData or if he wants to GetImageDataRaw
60 // (even if we do it by setting a flag, *he* will have to decide)
62 //filehelper->SetImageData(imageData, dataSize);
64 filehelper->SetContentType(GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE); // pixels remain unimpared
65 filehelper->SetWriteModeToRGB( );
67 filehelper->SetUserData(imageData,dataSize); // This one ensures the compression
68 filehelper->Write( output );
72 //////////////// Step 3:
73 GDCM_NAME_SPACE::File *fileout = GDCM_NAME_SPACE::File::New();
74 fileout->SetFileName( output );
77 if( !fileout->IsReadable() )
79 std::cout << "Failed" << std::endl
80 << "Test::TestReadWriteReadCompare: Could not parse the newly "
81 << "written image:" << filename << std::endl;
89 GDCM_NAME_SPACE::FileHelper *reread = GDCM_NAME_SPACE::FileHelper::New( fileout );
93 int dataSizeWritten = reread->GetImageDataSize();
94 uint8_t *imageDataWritten = reread->GetImageData();
96 //////////////// Step 4:
97 // Test the image size
98 if (file->GetXSize() != reread->GetFile()->GetXSize() ||
99 file->GetYSize() != reread->GetFile()->GetYSize() ||
100 file->GetZSize() != reread->GetFile()->GetZSize())
102 std::cout << "Failed" << std::endl
104 << "X: " << file->GetXSize() << " # "
105 << reread->GetFile()->GetXSize() << " | "
106 << "Y: " << file->GetYSize() << " # "
107 << reread->GetFile()->GetYSize() << " | "
108 << "Z: " << file->GetZSize() << " # "
109 << reread->GetFile()->GetZSize() << std::endl;
111 filehelper->Delete();
118 // Test the data size
119 // beware of odd length Pixel Element!
120 if (dataSize != dataSizeWritten)
121 std::cout << std::endl << "dataSize:" << dataSize << " dataSizeWritten:" << dataSizeWritten << std::endl;
123 int dataSizeFixed = dataSize - dataSize%2;
124 int dataSizeWrittenFixed = dataSizeWritten - dataSizeWritten%2;
126 if (dataSizeFixed != dataSizeWrittenFixed)
128 std::cout << "Failed" << std::endl
129 << " Pixel areas lengths differ: "
130 << dataSize << " # " << dataSizeWritten << std::endl;
132 filehelper->Delete();
139 // Test the data content
141 unsigned int nbDiff = 0;
142 if (memcmp(imageData, imageDataWritten, dataSizeFixed) !=0)
144 std::string PixelType = filehelper->GetFile()->GetPixelType();
145 std::string ts = filehelper->GetFile()->GetTransferSyntax();
147 for(int i1=0; i1<dataSizeFixed; i1++)
148 if (abs ((int)imageData[i1]-(int)imageDataWritten[i1]) > 2) {
150 // break; // at debug time, keep line commented out; (uncommenting will save CPU time)
155 std::cout << std::endl << filename << " Failed : "
156 << nbDiff/(file->GetBitsAllocated()/8) << " Pixels -amongst "
157 << dataSizeFixed/(file->GetBitsAllocated()/8) << "- ("
158 << PixelType << " bAlloc:" << file->GetBitsAllocated() << " bStored:" << file->GetBitsStored()
159 << ") differ (as expanded in memory)."
162 << GDCM_NAME_SPACE::Global::GetTS()->GetValue(ts) << std::endl;
164 std::cout << " list of the first " << MAX_NUMBER_OF_DIFFERENCE
165 << " bytes differing (pos : original - written) :"
168 for(int i=0, j1=0; i<dataSizeFixed && j1<MAX_NUMBER_OF_DIFFERENCE; i++)
170 if (abs ((int)imageData[i]-(int)imageDataWritten[i]) > 2)
172 if (j1<MAX_NUMBER_OF_DIFFERENCE)
173 std::cout << std::dec << "(" << i << " : "
175 << (int)(imageData[i]) << " - "
176 << (int)(imageDataWritten[i]) << ") "
181 std::cout << std::endl;
184 filehelper->Delete();
189 if (nbDiff/2 > 8 ) // last pixel of (DermaColorLossLess.dcm) is diferent. ?!?
190 // I don't want it to break the testsuite
197 std::cout << std::endl << filename << " : some pixels"
199 << PixelType << " bAlloc:" << file->GetBitsAllocated() << " bStored:" << file->GetBitsStored()
200 << ") differ +/-1 (as expanded in memory)."
203 << GDCM_NAME_SPACE::Global::GetTS()->GetValue(ts) << std::endl;
205 std::cout << " list of the first " << MAX_NUMBER_OF_DIFFERENCE
206 << " bytes differing (pos : original - written) :"
209 for(int i=0, j=0; i<dataSizeFixed && j<MAX_NUMBER_OF_DIFFERENCE; i++)
211 if (imageData[i] != imageDataWritten[i])
213 std::cout << std::hex << "(" << i << " : "
214 << std::hex << (int)(imageData[i]) << " - "
215 << std::hex << (int)(imageDataWritten[i]) << ") "
220 std::cout << std::endl;
228 std::cout << "=============== 4...OK." << std::endl ;
229 //////////////// Clean up:
231 filehelper->Delete();
238 // -------------------------------------------------------------------------------------------
240 int TestReadWriteReadCompare(int argc, char *argv[])
243 nb_of_success___ = 0;
244 nb_of_failure___ = 0;
245 nb_of_diffPM1___ = 0;
248 GDCM_NAME_SPACE::Debug::DebugOn();
252 const std::string input = argv[1];
253 const std::string output = argv[2];
254 result += CompareInternal(input, output);
256 else if( argc > 4 || argc == 2 )
258 std::cout << "Please read the manual" << std::endl;
262 std::cout<< "Test::TestReadWriteReadCompare: description " << std::endl;
263 std::cout << " For all images in gdcmData (and not blacklisted in "
264 "Test/CMakeLists.txt)" << std::endl;
265 std::cout << " apply the following multistep test: " << std::endl;
266 std::cout << " step 1: parse the image (as gdcmFile) and call"
267 << " IsReadable(). " << std::endl;
268 std::cout << " step 2: write the corresponding image in DICOM V3 "
269 << "with explicit Value Representation " << std::endl
270 << " in temporary file "
271 << "TestReadWriteReadCompare.dcm." << std::endl;
272 std::cout << " step 3: read the image written on step2 and call "
273 << " IsReadable(). " << std::endl;
274 std::cout << " step 4: compare (in memory with memcmp) that the two "
275 << "images " << std::endl
276 << " match (as expanded by gdcm)." << std::endl;
280 while( gdcmDataImages[i] != 0 )
282 std::string filename = GDCM_DATA_ROOT;
284 filename += gdcmDataImages[i];
285 res = CompareInternal(filename, "TestReadWriteReadCompare.dcm");
289 std::cout << "=============================== Failure on: " << gdcmDataImages[i] << std::endl;
295 std::cout << "==================================" << std::endl;
296 std::cout << "nb of success " << nb_of_success___ << std::endl;
297 std::cout << "nb of failure " << nb_of_failure___ << std::endl;
298 std::cout << "nb of diff+/-1 " << nb_of_diffPM1___ << std::endl;