]> Creatis software - gdcm.git/blob - Testing/TestReadWriteJPEG2000ReadCompare.cxx
d5e30aba2eddbc5bb644dd27db42e6ced0fa31ee
[gdcm.git] / Testing / TestReadWriteJPEG2000ReadCompare.cxx
1 /*=========================================================================
2
3   Program:   gdcm
4   Module:    $RCSfile: TestReadWriteJPEG2000ReadCompare.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/09/04 14:44:45 $
7   Version:   $Revision: 1.5 $
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    if ( file->GetBitsAllocated()>16 )
92    {
93       std::cout << "=============== 32 bits, not checked...OK." << std::endl ;
94       //////////////// Clean up:
95       file->Delete();
96       filehelper->Delete();
97       fileout->Delete();
98       return 0;   
99    }
100    
101    
102    GDCM_NAME_SPACE::FileHelper *reread = GDCM_NAME_SPACE::FileHelper::New( fileout );
103
104    std::cout << "3...";
105    // For the next step:
106    int     dataSizeWritten   = reread->GetImageDataRawSize();
107    uint8_t *imageDataWritten = reread->GetImageDataRaw();
108  
109    //////////////// Step 4:
110    // Test the image size
111    if (file->GetXSize() != reread->GetFile()->GetXSize() ||
112        file->GetYSize() != reread->GetFile()->GetYSize() ||
113        file->GetZSize() != reread->GetFile()->GetZSize())
114    {
115       std::cout << "Failed" << std::endl
116          << "        Size differs: "
117          << "X: " << file->GetXSize() << " # "
118                   << reread->GetFile()->GetXSize() << " | "
119          << "Y: " << file->GetYSize() << " # "
120                   << reread->GetFile()->GetYSize() << " | "
121          << "Z: " << file->GetZSize() << " # "
122                   << reread->GetFile()->GetZSize() << std::endl;
123       file->Delete();
124       filehelper->Delete();
125       fileout->Delete();
126       reread->Delete();
127       nb_of_failure2000___++;      
128       return 1;
129    }
130
131    // Test the data size
132    // beware of odd length Pixel Element!
133    if (dataSize != dataSizeWritten)
134       std::cout << std::endl << "dataSize:" << dataSize << " dataSizeWritten:" << dataSizeWritten << std::endl;
135       
136    int dataSizeFixed = dataSize - dataSize%2;
137    int dataSizeWrittenFixed = dataSizeWritten - dataSizeWritten%2;
138
139    if (dataSizeFixed != dataSizeWrittenFixed)
140    {
141       std::cout << "Failed" << std::endl
142          << "        Pixel areas lengths differ: "
143          << dataSize << " # " << dataSizeWritten << std::endl;
144       file->Delete();
145       filehelper->Delete();
146       fileout->Delete();
147       reread->Delete();
148       nb_of_failure2000___++;      
149       return 1;
150    }
151
152    // Test the data content
153
154    
155    unsigned int j  =0;
156    unsigned int nbDiff =0;  
157    if (memcmp(imageData, imageDataWritten, dataSizeFixed) !=0)
158    {   
159       std::string PixelType = filehelper->GetFile()->GetPixelType();
160       std::string ts        = filehelper->GetFile()->GetTransferSyntax();
161
162        for(int i1=0; i1<dataSizeFixed; i1++)
163          if (abs ((int)imageData[i1]-(int)imageDataWritten[i1]) > 2) {
164             nbDiff++;
165            // break; // at debug time, keep line commented out; (uncommenting will save CPU time)
166          }
167
168        if (nbDiff!=0)
169        {
170           std::cout << std::endl << filename << " Failed : "
171                     << nbDiff/(file->GetBitsAllocated()/8) << " pixels -amongst "
172                     << dataSizeFixed/(file->GetBitsAllocated()/8) << "- (" 
173                     << PixelType << " bAlloc:" << file->GetBitsAllocated() << " bStored:" << file->GetBitsStored()
174                     << ") differ (as expanded in memory)."
175                     << std::endl
176                     << "        compression : " 
177                     << GDCM_NAME_SPACE::Global::GetTS()->GetValue(ts) << std::endl;
178
179           std::cout << "   list of the first " << MAX_NUMBER_OF_DIFFERENCE
180                     << " bytes differing (pos : original - written) :"
181                     << std::endl;
182
183           for(int i=0, j=0; i<dataSizeFixed && j<MAX_NUMBER_OF_DIFFERENCE; i++)
184           {
185              if (abs ((int)imageData[i]-(int)imageDataWritten[i]) > 2)
186              {
187                 if (j<MAX_NUMBER_OF_DIFFERENCE)
188                    std::cout << std::dec << "(" << i << " : "
189                      << std::hex
190                      << (int)(imageData[i]) << " - "
191                      << (int)(imageDataWritten[i]) << ") "
192                      << std::dec;
193                 ++j;
194               }
195           }
196           std::cout << std::endl;
197
198           file->Delete();
199           filehelper->Delete();
200           fileout->Delete();
201           reread->Delete();
202           nb_of_failure2000___++;
203   
204           if (nbDiff>1)  // last pixel of (DermaColorLossLess.dcm) is diferent. ?!?
205                          // I don't want it to break the testsuite
206              return 1;
207           else
208              return 0;
209        }      
210        else
211        {
212           std::cout << std::endl << filename << " : some pixels"
213                     << "  ("
214                     << PixelType << " bAlloc:" << file->GetBitsAllocated() << " bStored:" << file->GetBitsStored()
215                     << ") differ +/-1 (as expanded in memory)."
216                     << std::endl
217                     << "        compression : "
218                     << GDCM_NAME_SPACE::Global::GetTS()->GetValue(ts) << std::endl;
219
220           std::cout << "   list of the first " << MAX_NUMBER_OF_DIFFERENCE
221                     << " bytes differing (pos : original - written) :"
222                     << std::endl;
223
224           for(int i=0, j=0; i<dataSizeFixed && j<MAX_NUMBER_OF_DIFFERENCE; i++)
225           {
226              if (imageData[i] != imageDataWritten[i])
227              {
228                 std::cout << std::hex << "(" << i << " : " 
229                          << std::hex << (int)(imageData[i]) << " - "
230                          << std::hex << (int)(imageDataWritten[i]) << ") "
231                          << std::dec;
232                 ++j;
233               }
234           }
235           std::cout << std::endl;
236           nb_of_diffPM12000___++;
237        }
238    }
239    else
240    {
241       nb_of_success2000___ ++;
242    }
243    std::cout << "=============== 4...OK." << std::endl ;
244    //////////////// Clean up:
245    file->Delete();
246    filehelper->Delete();
247    fileout->Delete();
248    reread->Delete();
249
250    return 0;
251 }
252
253 // -------------------------------------------------------------------------------------------
254
255 int TestReadWriteJPEG2000ReadCompare(int argc, char *argv[]) 
256 {
257    int result = 0;
258    nb_of_success2000___ = 0;
259    nb_of_failure2000___ = 0;
260    nb_of_diffPM12000___ = 0;
261    
262    if (argc == 4)
263       GDCM_NAME_SPACE::Debug::DebugOn();
264
265    if (argc >= 3)
266    {
267       const std::string input  = argv[1];
268       const std::string output = argv[2];
269       result += CompareInternalJPEG2000(input, output);
270    }
271    else if( argc > 4 || argc == 2 )
272    {
273       std::cout << "Please read the manual" << std::endl;
274    }
275    else
276    {
277       std::cout<< "Test::TestReadWriteJPEG2000ReadCompare: description " << std::endl;
278       std::cout << "   For all images in gdcmData (and not blacklisted in "
279                    "Test/CMakeLists.txt)" << std::endl;
280       std::cout << "   apply the following multistep test: " << std::endl;
281       std::cout << "   step 1: parse the image (as gdcmFile) and call"
282                 << " IsReadable(). " << std::endl;
283       std::cout << "   step 2: write the corresponding image in JPEG2000 DICOM V3 "
284                 << "with explicit Value Representation " << std::endl
285                 << "            in temporary file "
286                 << "TestReadWriteJPEG2000ReadCompare.dcm." << std::endl;
287       std::cout << "   step 3: read the image written on step2 and call "
288                 << " IsReadable(). " << std::endl;
289       std::cout << "   step 4: compare (in memory with memcmp) that the two "
290                 << "images " << std::endl
291                 << "           match (as expanded by gdcm)." << std::endl;
292    
293       int i = 0;
294       while( gdcmDataImages[i] != 0 )
295       {
296          std::string filename = GDCM_DATA_ROOT;
297          filename += "/";
298          filename += gdcmDataImages[i++];
299          result += CompareInternalJPEG2000(filename, "TestReadWriteJPEG2000ReadCompare.dcm");
300       }
301    }
302    std::cout << "==================================" << std::endl;
303    std::cout << "nb of success  " << nb_of_success2000___ << std::endl;
304    std::cout << "nb of failure  " << nb_of_failure2000___ << std::endl;
305    std::cout << "nb of diff+/-1 " << nb_of_diffPM12000___ << std::endl;   
306    return result;
307 }