1 /*=========================================================================
4 Module: $RCSfile: TestReadWriteJPEGReadCompare.cxx,v $
6 Date: $Date: 2007/08/28 16:57:00 $
7 Version: $Revision: 1.2 $
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;
32 static int CompareInternalJPEG(std::string const &filename, std::string const &output)
34 std::cout << "----------------------------------------------------------------------" << std::endl
35 << " Testing: " << filename << std::endl;
37 //////////////// Step 1 (see above description):
39 GDCM_NAME_SPACE::File *file = GDCM_NAME_SPACE::File::New( );
40 file->SetFileName( filename );
42 if( !file->IsReadable() )
44 std::cout << "Failed" << std::endl
45 << "Test::TestReadWriteJPEGReadCompare: Image not gdcm compatible:"
46 << filename << std::endl;
50 std::cout << " step 1...";
52 //////////////// Step 2:
53 GDCM_NAME_SPACE::FileHelper *filehelper = GDCM_NAME_SPACE::FileHelper::New( file );
54 int dataSize = filehelper->GetImageDataSize();
55 uint8_t *imageData = filehelper->GetImageData(); //EXTREMELY IMPORTANT
56 // Sure, it is : It's up to the user to decide if he wants to
57 // GetImageData or if he wants to GetImageDataRaw
58 // (even if we do it by setting a flag, *he* will have to decide)
60 //filehelper->SetImageData(imageData, dataSize);
62 // Just to be sure the further Write() doesn't corrupt imageData ...
63 std::cout << std::endl;
65 for(i=0, j=0;i<dataSize && j<MAX_NUMBER_OF_DIFFERENCE;i++)
67 std::cout << std::hex << "(" << i << " : "
68 << std::hex << (int)(imageData[i]) << ") "
72 std::cout << std::endl;
74 filehelper->SetWriteModeToRGB();
76 filehelper->SetWriteTypeToJPEG( );
77 filehelper->SetUserData(imageData,dataSize); // This one ensures the compression
78 filehelper->Write( output );
80 // Just to be sure the previous Write() didn't corrupt imageData ..
81 for(i=0, j=0;i<dataSize && j<MAX_NUMBER_OF_DIFFERENCE;i++)
83 std::cout << std::hex << "(" << i << " : "
84 << std::hex << (int)(imageData[i]) << ") "
88 std::cout << std::endl << std::endl;
92 //////////////// Step 3:
93 GDCM_NAME_SPACE::File *fileout = GDCM_NAME_SPACE::File::New();
94 fileout->SetFileName( output );
97 if( !fileout->IsReadable() )
99 std::cout << "Failed" << std::endl
100 << "Test::TestReadWriteReadCompare: Could not parse the newly "
101 << "written image:" << filename << std::endl;
103 filehelper->Delete();
108 GDCM_NAME_SPACE::FileHelper *reread = GDCM_NAME_SPACE::FileHelper::New( fileout );
111 // For the next step:
112 int dataSizeWritten = reread->GetImageDataSize();
113 uint8_t *imageDataWritten = reread->GetImageData();
116 std::cout << std::endl;
117 for(i=0, j=0;i<dataSize && j<MAX_NUMBER_OF_DIFFERENCE;i++)
119 std::cout << std::hex << "(" << i << " : "
120 << std::hex << (int)(imageDataWritten[i]) << ") "
124 std::cout << std::endl << std::endl;
126 //////////////// Step 4:
127 // Test the image size
128 if (file->GetXSize() != reread->GetFile()->GetXSize() ||
129 file->GetYSize() != reread->GetFile()->GetYSize() ||
130 file->GetZSize() != reread->GetFile()->GetZSize())
132 std::cout << "Failed" << std::endl
133 << " X Size differs: "
134 << "X: " << file->GetXSize() << " # "
135 << reread->GetFile()->GetXSize() << " | "
136 << "Y: " << file->GetYSize() << " # "
137 << reread->GetFile()->GetYSize() << " | "
138 << "Z: " << file->GetZSize() << " # "
139 << reread->GetFile()->GetZSize() << std::endl;
141 filehelper->Delete();
147 // Test the data size
148 // beware of odd length Pixel Element!
149 int dataSizeFixed = dataSize + dataSize%2;
150 int dataSizeWrittenFixed = dataSizeWritten + dataSizeWritten%2;
152 if (dataSizeFixed != dataSizeWrittenFixed)
154 std::cout << "Failed" << std::endl
155 << " Pixel areas lengths differ: "
156 << dataSize << " # " << dataSizeWritten << std::endl;
158 filehelper->Delete();
164 // Test the data content
166 if (memcmp(imageData, imageDataWritten, dataSize) !=0)
168 std::string PixelType = filehelper->GetFile()->GetPixelType();
169 std::string ts = filehelper->GetFile()->GetTransferSyntax();
171 std::cout << " Failed" << std::endl
174 << ") differ (as expanded in memory)."
177 << GDCM_NAME_SPACE::Global::GetTS()->GetValue(ts) << std::endl;
179 std::cout << " list of the first " << MAX_NUMBER_OF_DIFFERENCE
180 << " pixels differing (pos : original - written) :"
185 for(i=0, j=0;i<dataSize && j<MAX_NUMBER_OF_DIFFERENCE;i++)
187 //if(imageData[i]!=imageDataWritten[i])
188 if (abs ((int)imageData[i]-(int)imageDataWritten[i]) > 2)
190 std::cout << std::hex << "(" << i << " : "
191 << std::hex << (int)(imageData[i]) << " - "
192 << std::hex << (int)(imageDataWritten[i]) << ") "
197 std::cout << std::endl;
201 filehelper->Delete();
207 std::cout << "========================================= 4...OK." << std::endl ;
209 //////////////// Clean up:
211 filehelper->Delete();
218 // -------------------------------------------------------------------------------------------
220 int TestReadWriteJPEGReadCompare(int argc, char *argv[])
225 GDCM_NAME_SPACE::Debug::DebugOn();
229 const std::string input = argv[1];
230 const std::string output = argv[2];
231 result += CompareInternalJPEG(input, output);
233 else if( argc > 4 || argc == 2 )
235 std::cout << "Please read the manual" << std::endl;
239 std::cout<< "Test::TestReadWriteJPEGReadCompare: description " << std::endl;
240 std::cout << " For all images in gdcmData (and not blacklisted in "
241 "Test/CMakeLists.txt)" << std::endl;
242 std::cout << " apply the following multistep test: " << std::endl;
243 std::cout << " step 1: parse the image (as gdcmFile) and call"
244 << " IsReadable(). " << std::endl;
245 std::cout << " step 2: write the corresponding image in JPEG DICOM V3 "
246 << "with explicit Value Representation " << std::endl
247 << " in temporary file "
248 << "TestReadWriteJPEGReadCompare.dcm." << std::endl;
249 std::cout << " step 3: read the image written on step2 and call "
250 << " IsReadable(). " << std::endl;
251 std::cout << " step 4: compare (in memory with memcmp) that the two "
252 << "images " << std::endl
253 << " match (as expanded by gdcm)." << std::endl;
256 while( gdcmDataImages[i] != 0 )
258 std::string filename = GDCM_DATA_ROOT;
260 filename += gdcmDataImages[i++];
261 result += CompareInternalJPEG(filename, "TestReadWriteJPEGReadCompare.dcm");