1 /*=========================================================================
4 Module: $RCSfile: TestReadWriteReadCompare.cxx,v $
6 Date: $Date: 2008/04/10 12:15:34 $
7 Version: $Revision: 1.38 $
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"
25 #include <stdlib.h> // abs
28 #include "gdcmDataImages.h"
30 const unsigned int MAX_NUMBER_OF_DIFFERENCE = 10;
35 static int CompareInternal(std::string const &filename, std::string const &output)
37 std::cout << "----------------------------------------------------------------------" << std::endl
38 << " Testing: " << filename << std::endl;
40 //////////////// Step 1 (see above description):
42 GDCM_NAME_SPACE::File *file = GDCM_NAME_SPACE::File::New( );
43 file->SetFileName( filename );
45 if( !file->IsReadable() )
47 std::cout << "Failed" << std::endl
48 << "Test::TestReadWriteReadCompare: Image not gdcm compatible:"
49 << filename << std::endl;
54 std::cout << " step 1...";
56 //////////////// Step 2:
57 GDCM_NAME_SPACE::FileHelper *filehelper = GDCM_NAME_SPACE::FileHelper::New( file );
58 int dataSize = filehelper->GetImageDataSize();
59 uint8_t *imageData = filehelper->GetImageData(); //EXTREMELY IMPORTANT
60 // Sure, it is : It's up to the user to decide if he wants to
61 // GetImageData or if he wants to GetImageDataRaw
62 // (even if we do it by setting a flag, *he* will have to decide)
64 //filehelper->SetImageData(imageData, dataSize);
66 filehelper->SetContentType(GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE); // pixels remain unimpared
67 filehelper->SetWriteModeToRGB( );
69 filehelper->SetUserData(imageData,dataSize); // This one ensures the compression
70 filehelper->Write( output );
74 //////////////// Step 3:
75 GDCM_NAME_SPACE::File *fileout = GDCM_NAME_SPACE::File::New();
76 fileout->SetFileName( output );
79 if( !fileout->IsReadable() )
81 std::cout << "Failed" << std::endl
82 << "Test::TestReadWriteReadCompare: Could not parse the newly "
83 << "written image:" << filename << std::endl;
91 GDCM_NAME_SPACE::FileHelper *reread = GDCM_NAME_SPACE::FileHelper::New( fileout );
95 int dataSizeWritten = reread->GetImageDataSize();
96 uint8_t *imageDataWritten = reread->GetImageData();
98 //////////////// Step 4:
99 // Test the image size
100 if (file->GetXSize() != reread->GetFile()->GetXSize() ||
101 file->GetYSize() != reread->GetFile()->GetYSize() ||
102 file->GetZSize() != reread->GetFile()->GetZSize())
104 std::cout << "Failed" << std::endl
106 << "X: " << file->GetXSize() << " # "
107 << reread->GetFile()->GetXSize() << " | "
108 << "Y: " << file->GetYSize() << " # "
109 << reread->GetFile()->GetYSize() << " | "
110 << "Z: " << file->GetZSize() << " # "
111 << reread->GetFile()->GetZSize() << std::endl;
113 filehelper->Delete();
120 // Test the data size
121 // beware of odd length Pixel Element!
122 if (dataSize != dataSizeWritten)
123 std::cout << std::endl << "dataSize:" << dataSize << " dataSizeWritten:" << dataSizeWritten << std::endl;
125 int dataSizeFixed = dataSize - dataSize%2;
126 int dataSizeWrittenFixed = dataSizeWritten - dataSizeWritten%2;
128 if (dataSizeFixed != dataSizeWrittenFixed)
130 std::cout << "Failed" << std::endl
131 << " Pixel areas lengths differ: "
132 << dataSize << " # " << dataSizeWritten << std::endl;
134 filehelper->Delete();
141 // Test the data content
143 unsigned int nbDiff = 0;
144 if (memcmp(imageData, imageDataWritten, dataSizeFixed) !=0)
146 std::string PixelType = filehelper->GetFile()->GetPixelType();
147 std::string ts = filehelper->GetFile()->GetTransferSyntax();
149 for(int i1=0; i1<dataSizeFixed; i1++)
150 if (abs ((int)imageData[i1]-(int)imageDataWritten[i1]) > 2) {
152 // break; // at debug time, keep line commented out; (uncommenting will save CPU time)
157 std::cout << std::endl << filename << " Failed : "
158 << nbDiff/(file->GetBitsAllocated()/8) << " Pixels -amongst "
159 << dataSizeFixed/(file->GetBitsAllocated()/8) << "- ("
160 << PixelType << " bAlloc:" << file->GetBitsAllocated() << " bStored:" << file->GetBitsStored()
161 << ") differ (as expanded in memory)."
164 << GDCM_NAME_SPACE::Global::GetTS()->GetValue(ts) << std::endl;
166 std::cout << " list of the first " << MAX_NUMBER_OF_DIFFERENCE
167 << " bytes differing (pos : original - written) :"
170 for(unsigned int i=0, j1=0; i<dataSizeFixed && j1<MAX_NUMBER_OF_DIFFERENCE; i++)
172 if (abs ((int)imageData[i]-(int)imageDataWritten[i]) > 2)
174 if (j1<MAX_NUMBER_OF_DIFFERENCE)
175 std::cout << std::dec << "(" << i << " : "
177 << (int)(imageData[i]) << " - "
178 << (int)(imageDataWritten[i]) << ") "
183 std::cout << std::endl;
186 filehelper->Delete();
191 if (nbDiff/2 > 8 ) // last pixel of (DermaColorLossLess.dcm) is diferent. ?!?
192 // I don't want it to break the testsuite
199 std::cout << std::endl << filename << " : some pixels"
201 << PixelType << " bAlloc:" << file->GetBitsAllocated() << " bStored:" << file->GetBitsStored()
202 << ") differ +/-1 (as expanded in memory)."
205 << GDCM_NAME_SPACE::Global::GetTS()->GetValue(ts) << std::endl;
207 std::cout << " list of the first " << MAX_NUMBER_OF_DIFFERENCE
208 << " bytes differing (pos : original - written) :"
211 for(unsigned int i=0, j=0; i<dataSizeFixed && j<MAX_NUMBER_OF_DIFFERENCE; i++)
213 if (imageData[i] != imageDataWritten[i])
215 std::cout << std::hex << "(" << i << " : "
216 << std::hex << (int)(imageData[i]) << " - "
217 << std::hex << (int)(imageDataWritten[i]) << ") "
222 std::cout << std::endl;
230 std::cout << "=============== 4...OK." << std::endl ;
231 //////////////// Clean up:
233 filehelper->Delete();
240 // -------------------------------------------------------------------------------------------
242 int TestReadWriteReadCompare(int argc, char *argv[])
245 nb_of_success___ = 0;
246 nb_of_failure___ = 0;
247 nb_of_diffPM1___ = 0;
250 GDCM_NAME_SPACE::Debug::DebugOn();
254 const std::string input = argv[1];
255 const std::string output = argv[2];
256 result += CompareInternal(input, output);
258 else if( argc > 4 || argc == 2 )
260 std::cout << "Please read the manual" << std::endl;
264 std::cout<< "Test::TestReadWriteReadCompare: description " << std::endl;
265 std::cout << " For all images in gdcmData (and not blacklisted in "
266 "Test/CMakeLists.txt)" << std::endl;
267 std::cout << " apply the following multistep test: " << std::endl;
268 std::cout << " step 1: parse the image (as gdcmFile) and call"
269 << " IsReadable(). " << std::endl;
270 std::cout << " step 2: write the corresponding image in DICOM V3 "
271 << "with explicit Value Representation " << std::endl
272 << " in temporary file "
273 << "TestReadWriteReadCompare.dcm." << std::endl;
274 std::cout << " step 3: read the image written on step2 and call "
275 << " IsReadable(). " << std::endl;
276 std::cout << " step 4: compare (in memory with memcmp) that the two "
277 << "images " << std::endl
278 << " match (as expanded by gdcm)." << std::endl;
282 while( gdcmDataImages[i] != 0 )
284 std::string filename = GDCM_DATA_ROOT;
286 filename += gdcmDataImages[i];
287 res = CompareInternal(filename, "TestReadWriteReadCompare.dcm");
291 std::cout << "=============================== Failure on: " << gdcmDataImages[i] << std::endl;
297 std::cout << "==================================" << std::endl;
298 std::cout << "nb of success " << nb_of_success___ << std::endl;
299 std::cout << "nb of failure " << nb_of_failure___ << std::endl;
300 std::cout << "nb of diff+/-1 " << nb_of_diffPM1___ << std::endl;