1 /*=========================================================================
4 Module: $RCSfile: TestReadWriteJPEGReadCompare.cxx,v $
6 Date: $Date: 2007/08/30 14:07:33 $
7 Version: $Revision: 1.7 $
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;
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;
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 reain 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;
91 GDCM_NAME_SPACE::FileHelper *reread = GDCM_NAME_SPACE::FileHelper::New( fileout );
95 int dataSizeWritten = reread->GetImageDataRawSize();
96 uint8_t *imageDataWritten = reread->GetImageDataRaw();
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 int dataSizeFixed = dataSize + dataSize%2;
123 int dataSizeWrittenFixed = dataSizeWritten + dataSizeWritten%2;
125 if (dataSizeFixed != dataSizeWrittenFixed)
127 std::cout << "Failed" << std::endl
128 << " Pixel areas lengths differ: "
129 << dataSize << " # " << dataSizeWritten << std::endl;
131 filehelper->Delete();
138 // Test the data content
140 unsigned int nbDiff =0;
141 if (memcmp(imageData, imageDataWritten, dataSizeFixed) !=0)
143 std::string PixelType = filehelper->GetFile()->GetPixelType();
144 std::string ts = filehelper->GetFile()->GetTransferSyntax();
146 for(int i1=0; i1<dataSizeFixed; i1++)
147 if (abs ((int)imageData[i1]-(int)imageDataWritten[i1]) > 2) {
149 // break; // at debug time; keep line commented out; (uncommenting will save CPU time)
154 std::cout << std::endl << filename << " Failed : "
155 << nbDiff/(file->GetBitsAllocated()/8) << " pixels -amongst "
156 << dataSizeFixed/(file->GetBitsAllocated()/8) << "- ("
157 << PixelType << " bAlloc " << file->GetBitsAllocated() << " bStored " << file->GetBitsStored()
158 << ") differ (as expanded in memory)."
161 << GDCM_NAME_SPACE::Global::GetTS()->GetValue(ts) << std::endl;
163 std::cout << " list of the first " << MAX_NUMBER_OF_DIFFERENCE
164 << " bytes differing (pos : original - written) :"
167 for(int i=0, j=0; i<dataSizeFixed && j<MAX_NUMBER_OF_DIFFERENCE; i++)
169 if (abs ((int)imageData[i]-(int)imageDataWritten[i]) > 2)
171 if (j<MAX_NUMBER_OF_DIFFERENCE)
172 std::cout << std::dec << "(" << i << " : "
174 << (int)(imageData[i]) << " - "
175 << (int)(imageDataWritten[i]) << ") "
180 std::cout << std::endl;
183 filehelper->Delete();
191 std::cout << std::endl << filename << " : some pixels"
193 << PixelType << " bAlloc " << file->GetBitsAllocated() << " bStored " << file->GetBitsStored()
194 << ") differ +/-1 (as expanded in memory)."
197 << GDCM_NAME_SPACE::Global::GetTS()->GetValue(ts) << std::endl;
199 std::cout << " list of the first " << MAX_NUMBER_OF_DIFFERENCE
200 << " bytes differing (pos : original - written) :"
203 for(int i=0, j=0; i<dataSizeFixed && j<MAX_NUMBER_OF_DIFFERENCE; i++)
205 if (imageData[i] != imageDataWritten[i])
207 std::cout << std::hex << "(" << i << " : "
208 << std::hex << (int)(imageData[i]) << " - "
209 << std::hex << (int)(imageDataWritten[i]) << ") "
214 std::cout << std::endl;
222 std::cout << "=============== 4...OK." << std::endl ;
223 //////////////// Clean up:
225 filehelper->Delete();
232 // -------------------------------------------------------------------------------------------
234 int TestReadWriteJPEGReadCompare(int argc, char *argv[])
237 nb_of_success___ = 0;
238 nb_of_failure___ = 0;
239 nb_of_diffPM1___ = 0;
242 GDCM_NAME_SPACE::Debug::DebugOn();
246 const std::string input = argv[1];
247 const std::string output = argv[2];
248 result += CompareInternalJPEG(input, output);
250 else if( argc > 4 || argc == 2 )
252 std::cout << "Please read the manual" << std::endl;
256 std::cout<< "Test::TestReadWriteJPEGReadCompare: description " << std::endl;
257 std::cout << " For all images in gdcmData (and not blacklisted in "
258 "Test/CMakeLists.txt)" << std::endl;
259 std::cout << " apply the following multistep test: " << std::endl;
260 std::cout << " step 1: parse the image (as gdcmFile) and call"
261 << " IsReadable(). " << std::endl;
262 std::cout << " step 2: write the corresponding image in JPEG DICOM V3 "
263 << "with explicit Value Representation " << std::endl
264 << " in temporary file "
265 << "TestReadWriteJPEGReadCompare.dcm." << std::endl;
266 std::cout << " step 3: read the image written on step2 and call "
267 << " IsReadable(). " << std::endl;
268 std::cout << " step 4: compare (in memory with memcmp) that the two "
269 << "images " << std::endl
270 << " match (as expanded by gdcm)." << std::endl;
273 while( gdcmDataImages[i] != 0 )
275 std::string filename = GDCM_DATA_ROOT;
277 filename += gdcmDataImages[i++];
278 result += CompareInternalJPEG(filename, "TestReadWriteJPEGReadCompare.dcm");
281 std::cout << "==================================" << std::endl;
282 std::cout << "nb of success " << nb_of_success___ << std::endl;
283 std::cout << "nb of failure " << nb_of_failure___ << std::endl;
284 std::cout << "nb of diff+/-1 " << nb_of_diffPM1___ << std::endl;