]> Creatis software - gdcm.git/blob - Testing/TestReadWriteJPEG2000ReadCompare.cxx
(try to) help debugger
[gdcm.git] / Testing / TestReadWriteJPEG2000ReadCompare.cxx
1 /*=========================================================================
2
3   Program:   gdcm
4   Module:    $RCSfile: TestReadWriteJPEG2000ReadCompare.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/08/30 14:07:33 $
7   Version:   $Revision: 1.3 $
8
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.
12
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.
16
17 =========================================================================*/
18 #include "gdcmFile.h"
19 #include "gdcmFileHelper.h"
20 #include "gdcmDebug.h"
21
22 #include "gdcmGlobal.h"
23 #include "gdcmTS.h"
24
25 //Generated file:
26 #include "gdcmDataImages.h"
27
28
29 const unsigned int MAX_NUMBER_OF_DIFFERENCE = 10;
30
31 int nb_of_success2000___;
32 int nb_of_failure2000___;
33 int nb_of_diffPM12000___;
34 static int CompareInternalJPEG2000(std::string const &filename, std::string const &output)
35 {
36    std::cout << "----------------------------------------------------------------------" << std::endl
37              << "   Testing: " << filename << std::endl;
38
39    //////////////// Step 1 (see above description):
40
41    GDCM_NAME_SPACE::File *file = GDCM_NAME_SPACE::File::New( );
42    file->SetFileName( filename );
43    file->Load ();
44    if( !file->IsReadable() )
45    {
46       std::cout << "Failed" << std::endl
47                 << "Test::TestReadWriteJPEG2000ReadCompare: Image not gdcm compatible:"
48                 << filename << std::endl;
49       file->Delete();
50       nb_of_failure2000___++;
51       return 1;
52    }
53    std::cout << "           step 1...";
54
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)
62
63    //filehelper->SetImageData(imageData, dataSize);
64  
65    filehelper->SetContentType(GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE); // lossless compression : pixels reain unimpared
66    filehelper->SetWriteModeToRaw();  
67    filehelper->SetWriteTypeToJPEG2000(  ); 
68    
69    filehelper->SetUserData(imageData,dataSize); // This one ensures the compression
70    filehelper->Write( output ); 
71
72    std::cout << "2...";
73
74    //////////////// Step 3:
75    GDCM_NAME_SPACE::File *fileout = GDCM_NAME_SPACE::File::New();
76    fileout->SetFileName( output );
77    fileout->Load();
78
79    if( !fileout->IsReadable() )
80    {
81       std::cout << "Failed" << std::endl
82                 << "Test::TestReadWriteJPEG2000ReadCompare: Could not parse the newly "
83                 << "written image:" << filename << std::endl;
84       file->Delete();
85       filehelper->Delete();
86       fileout->Delete();
87       nb_of_failure2000___++;
88       return 1;
89    }
90
91    GDCM_NAME_SPACE::FileHelper *reread = GDCM_NAME_SPACE::FileHelper::New( fileout );
92
93    std::cout << "3...";
94    // For the next step:
95    int     dataSizeWritten   = reread->GetImageDataRawSize();
96    uint8_t *imageDataWritten = reread->GetImageDataRaw();
97  
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())
103    {
104       std::cout << "Failed" << std::endl
105          << "        Size differs: "
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;
112       file->Delete();
113       filehelper->Delete();
114       fileout->Delete();
115       reread->Delete();
116       nb_of_failure2000___++;      
117       return 1;
118    }
119
120    // Test the data size
121    // beware of odd length Pixel Element!
122    int dataSizeFixed = dataSize + dataSize%2;
123    int dataSizeWrittenFixed = dataSizeWritten + dataSizeWritten%2;
124
125    if (dataSizeFixed != dataSizeWrittenFixed)
126    {
127       std::cout << "Failed" << std::endl
128          << "        Pixel areas lengths differ: "
129          << dataSize << " # " << dataSizeWritten << std::endl;
130       file->Delete();
131       filehelper->Delete();
132       fileout->Delete();
133       reread->Delete();
134       nb_of_failure2000___++;      
135       return 1;
136    }
137
138    // Test the data content
139    unsigned int j  =0;
140    unsigned int nbDiff =0;  
141    if (memcmp(imageData, imageDataWritten, dataSizeFixed) !=0)
142    {   
143       std::string PixelType = filehelper->GetFile()->GetPixelType();
144       std::string ts        = filehelper->GetFile()->GetTransferSyntax();
145       
146        for(int i1=0; i1<dataSizeFixed; i1++)
147          if (abs ((int)imageData[i1]-(int)imageDataWritten[i1]) > 2) {
148             nbDiff++;
149            // break; // at debug time; keep line commented out; (uncommenting will save CPU time)
150          }
151
152        if (nbDiff!=0)
153        {
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)."
159                     << std::endl
160                     << "        compression : " 
161                     << GDCM_NAME_SPACE::Global::GetTS()->GetValue(ts) << std::endl;
162
163           std::cout << "   list of the first " << MAX_NUMBER_OF_DIFFERENCE
164                     << " bytes differing (pos : original - written) :"
165                     << std::endl;
166
167           for(int i=0, j=0; i<dataSizeFixed && j<MAX_NUMBER_OF_DIFFERENCE; i++)
168           {
169              if (abs ((int)imageData[i]-(int)imageDataWritten[i]) > 2)
170              {
171                 if (j<MAX_NUMBER_OF_DIFFERENCE)
172                    std::cout << std::dec << "(" << i << " : "
173                      << std::hex
174                      << (int)(imageData[i]) << " - "
175                      << (int)(imageDataWritten[i]) << ") "
176                      << std::dec;
177                 ++j;
178               }
179           }
180           std::cout << std::endl;
181
182           file->Delete();
183           filehelper->Delete();
184           fileout->Delete();
185           reread->Delete();
186           nb_of_failure2000___++;
187           return 1;
188        }
189        else
190        {
191           std::cout << std::endl << filename << " : some pixels"
192                     << "  ("
193                     << PixelType << " bAlloc " << file->GetBitsAllocated() << " bStored " << file->GetBitsStored()
194                     << ") differ +/-1 (as expanded in memory)."
195                     << std::endl
196                     << "        compression : "
197                     << GDCM_NAME_SPACE::Global::GetTS()->GetValue(ts) << std::endl;
198
199           std::cout << "   list of the first " << MAX_NUMBER_OF_DIFFERENCE
200                     << " bytes differing (pos : original - written) :"
201                     << std::endl;
202
203           for(int i=0, j=0; i<dataSizeFixed && j<MAX_NUMBER_OF_DIFFERENCE; i++)
204           {
205              if (imageData[i] != imageDataWritten[i])
206              {
207                 std::cout << std::hex << "(" << i << " : " 
208                          << std::hex << (int)(imageData[i]) << " - "
209                          << std::hex << (int)(imageDataWritten[i]) << ") "
210                          << std::dec;
211                 ++j;
212               }
213           }
214           std::cout << std::endl;
215           nb_of_diffPM12000___++;
216        }
217    }
218    else
219    {
220       nb_of_success2000___ ++;
221    }
222    std::cout << "=============== 4...OK." << std::endl ;
223    //////////////// Clean up:
224    file->Delete();
225    filehelper->Delete();
226    fileout->Delete();
227    reread->Delete();
228
229    return 0;
230 }
231
232 // -------------------------------------------------------------------------------------------
233
234 int TestReadWriteJPEG2000ReadCompare(int argc, char *argv[]) 
235 {
236    int result = 0;
237    nb_of_success2000___ = 0;
238    nb_of_failure2000___ = 0;
239    nb_of_diffPM12000___ = 0;
240    
241    if (argc == 4)
242       GDCM_NAME_SPACE::Debug::DebugOn();
243
244    if (argc >= 3)
245    {
246       const std::string input  = argv[1];
247       const std::string output = argv[2];
248       result += CompareInternalJPEG2000(input, output);
249    }
250    else if( argc > 4 || argc == 2 )
251    {
252       std::cout << "Please read the manual" << std::endl;
253    }
254    else
255    {
256       std::cout<< "Test::TestReadWriteJPEG2000ReadCompare: 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 JPEG2000 DICOM V3 "
263                 << "with explicit Value Representation " << std::endl
264                 << "            in temporary file "
265                 << "TestReadWriteJPEG2000ReadCompare.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;
271    
272       int i = 0;
273       while( gdcmDataImages[i] != 0 )
274       {
275          std::string filename = GDCM_DATA_ROOT;
276          filename += "/";
277          filename += gdcmDataImages[i++];
278          result += CompareInternalJPEG2000(filename, "TestReadWriteJPEG2000ReadCompare.dcm");
279       }
280    }
281    std::cout << "==================================" << std::endl;
282    std::cout << "nb of success  " << nb_of_success2000___ << std::endl;
283    std::cout << "nb of failure  " << nb_of_failure2000___ << std::endl;
284    std::cout << "nb of diff+/-1 " << nb_of_diffPM12000___ << std::endl;   
285    return result;
286 }