1 /*=========================================================================
4 Module: $RCSfile: TestReadWriteJPEGReadCompare.cxx,v $
6 Date: $Date: 2007/09/05 09:55:01 $
7 Version: $Revision: 1.10 $
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"
29 const unsigned int MAX_NUMBER_OF_DIFFERENCE = 10;
31 int nb_of_successJPEG___;
32 int nb_of_failureJPEG___;
33 int nb_of_diffPM1JPEG___;
34 static int CompareInternalJPEG(std::string const &filename, std::string const &output)
36 std::cout << "----------------------------------------------------------------------" << std::endl
37 << " Testing: " << filename << std::endl;
39 //////////////// Step 1 (see above description):
41 GDCM_NAME_SPACE::File *file = GDCM_NAME_SPACE::File::New( );
42 file->SetFileName( filename );
44 if( !file->IsReadable() )
46 std::cout << "Failed" << std::endl
47 << "Test::TestReadWriteJPEGReadCompare: Image not gdcm compatible:"
48 << filename << std::endl;
50 nb_of_failureJPEG___++;
53 std::cout << " step 1...";
55 //////////////// Step 2:
56 GDCM_NAME_SPACE::FileHelper *filehelper = GDCM_NAME_SPACE::FileHelper::New( file );
57 int dataSize = filehelper->GetImageDataRawSize();
58 uint8_t *imageData = filehelper->GetImageDataRaw(); //EXTREMELY IMPORTANT
59 // Sure, it is : It's up to the user to decide if he wants to
60 // GetImageData or if he wants to GetImageDataRaw
61 // (even if we do it by setting a flag, *he* will have to decide)
63 //filehelper->SetImageData(imageData, dataSize);
65 filehelper->SetContentType(GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE); // lossless compression : pixels remain unimpared
66 filehelper->SetWriteModeToRaw();
67 filehelper->SetWriteTypeToJPEG( );
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::TestReadWriteJPEGReadCompare: Could not parse the newly "
83 << "written image:" << filename << std::endl;
87 nb_of_failureJPEG___++;
91 if ( file->GetBitsAllocated()>16 )
93 std::cout << "=============== 32 bits, not checked...OK." << std::endl ;
94 //////////////// Clean up:
101 GDCM_NAME_SPACE::FileHelper *reread = GDCM_NAME_SPACE::FileHelper::New( fileout );
104 // For the next step:
105 int dataSizeWritten = reread->GetImageDataRawSize();
106 uint8_t *imageDataWritten = reread->GetImageDataRaw();
108 //////////////// Step 4:
109 // Test the image size
110 if (file->GetXSize() != reread->GetFile()->GetXSize() ||
111 file->GetYSize() != reread->GetFile()->GetYSize() ||
112 file->GetZSize() != reread->GetFile()->GetZSize())
114 std::cout << "Failed" << std::endl
116 << "X: " << file->GetXSize() << " # "
117 << reread->GetFile()->GetXSize() << " | "
118 << "Y: " << file->GetYSize() << " # "
119 << reread->GetFile()->GetYSize() << " | "
120 << "Z: " << file->GetZSize() << " # "
121 << reread->GetFile()->GetZSize() << std::endl;
123 filehelper->Delete();
126 nb_of_failureJPEG___++;
130 // Test the data size
131 // beware of odd length Pixel Element!
132 if (dataSize != dataSizeWritten)
133 std::cout << std::endl << "dataSize:" << dataSize << " dataSizeWritten:" << dataSizeWritten << std::endl;
135 int dataSizeFixed = dataSize - dataSize%2;
136 int dataSizeWrittenFixed = dataSizeWritten - dataSizeWritten%2;
138 if (dataSizeFixed != dataSizeWrittenFixed)
140 std::cout << "Failed" << std::endl
141 << " Pixel areas lengths differ: "
142 << dataSize << " # " << dataSizeWritten << std::endl;
144 filehelper->Delete();
147 nb_of_failureJPEG___++;
151 // Test the data content
153 unsigned int nbDiff = 0;
154 if (memcmp(imageData, imageDataWritten, dataSizeFixed) !=0)
156 std::string PixelType = filehelper->GetFile()->GetPixelType();
157 std::string ts = filehelper->GetFile()->GetTransferSyntax();
159 for(int i1=0; i1<dataSizeFixed; i1++)
160 if (abs ((int)imageData[i1]-(int)imageDataWritten[i1]) > 2) {
162 // break; // at debug time, keep line commented out; (uncommenting will save CPU time)
167 std::cout << std::endl << filename << " Failed : "
168 << nbDiff/(file->GetBitsAllocated()/8) << " Pixels -amongst "
169 << dataSizeFixed/(file->GetBitsAllocated()/8) << "- ("
170 << PixelType << " bAlloc:" << file->GetBitsAllocated() << " bStored:" << file->GetBitsStored()
171 << ") differ (as expanded in memory)."
174 << GDCM_NAME_SPACE::Global::GetTS()->GetValue(ts) << std::endl;
176 std::cout << " list of the first " << MAX_NUMBER_OF_DIFFERENCE
177 << " bytes differing (pos : original - written) :"
180 for(int i=0, j=0; i<dataSizeFixed && j<MAX_NUMBER_OF_DIFFERENCE; i++)
182 if (abs ((int)imageData[i]-(int)imageDataWritten[i]) > 2)
184 if (j<MAX_NUMBER_OF_DIFFERENCE)
185 std::cout << std::dec << "(" << i << " : "
187 << (int)(imageData[i]) << " - "
188 << (int)(imageDataWritten[i]) << ") "
193 std::cout << std::endl;
196 filehelper->Delete();
199 nb_of_failureJPEG___++;
201 if (nbDiff/2 > 8 ) // last pixel of (DermaColorLossLess.dcm) is diferent. ?!?
202 // I don't want it to break the testsuite
209 std::cout << std::endl << filename << " : some pixels"
211 << PixelType << " bAlloc:" << file->GetBitsAllocated() << " bStored:" << file->GetBitsStored()
212 << ") differ +/-1 (as expanded in memory)."
215 << GDCM_NAME_SPACE::Global::GetTS()->GetValue(ts) << std::endl;
217 std::cout << " list of the first " << MAX_NUMBER_OF_DIFFERENCE
218 << " bytes differing (pos : original - written) :"
221 for(int i=0, j=0; i<dataSizeFixed && j<MAX_NUMBER_OF_DIFFERENCE; i++)
223 if (imageData[i] != imageDataWritten[i])
225 std::cout << std::hex << "(" << i << " : "
226 << std::hex << (int)(imageData[i]) << " - "
227 << std::hex << (int)(imageDataWritten[i]) << ") "
232 std::cout << std::endl;
233 nb_of_diffPM1JPEG___++;
238 nb_of_successJPEG___ ++;
240 std::cout << "=============== 4...OK." << std::endl ;
241 //////////////// Clean up:
243 filehelper->Delete();
250 // -------------------------------------------------------------------------------------------
252 int TestReadWriteJPEGReadCompare(int argc, char *argv[])
255 nb_of_successJPEG___ = 0;
256 nb_of_failureJPEG___ = 0;
257 nb_of_diffPM1JPEG___ = 0;
260 GDCM_NAME_SPACE::Debug::DebugOn();
264 const std::string input = argv[1];
265 const std::string output = argv[2];
266 result += CompareInternalJPEG(input, output);
268 else if( argc > 4 || argc == 2 )
270 std::cout << "Please read the manual" << std::endl;
274 std::cout<< "Test::TestReadWriteJPEGReadCompare: description " << std::endl;
275 std::cout << " For all images in gdcmData (and not blacklisted in "
276 "Test/CMakeLists.txt)" << std::endl;
277 std::cout << " apply the following multistep test: " << std::endl;
278 std::cout << " step 1: parse the image (as gdcmFile) and call"
279 << " IsReadable(). " << std::endl;
280 std::cout << " step 2: write the corresponding image in JPEG DICOM V3 "
281 << "with explicit Value Representation " << std::endl
282 << " in temporary file "
283 << "TestReadWriteJPEGReadCompare.dcm." << std::endl;
284 std::cout << " step 3: read the image written on step2 and call "
285 << " IsReadable(). " << std::endl;
286 std::cout << " step 4: compare (in memory with memcmp) that the two "
287 << "images " << std::endl
288 << " match (as expanded by gdcm)." << std::endl;
292 while( gdcmDataImages[i] != 0 )
294 std::string filename = GDCM_DATA_ROOT;
296 filename += gdcmDataImages[i];
297 res = CompareInternalJPEG(filename, "TestReadWriteJPEGReadCompare.dcm");
301 std::cout << "=============================== Failure on: " << gdcmDataImages[i] << std::endl;
307 std::cout << "==================================" << std::endl;
308 std::cout << "nb of success " << nb_of_successJPEG___ << std::endl;
309 std::cout << "nb of failure " << nb_of_failureJPEG___ << std::endl;
310 std::cout << "nb of diff+/-1 " << nb_of_diffPM1JPEG___ << std::endl;