]> Creatis software - gdcm.git/blob - Testing/TestReadWriteJPEGReadCompare.cxx
Still trying to make a full JPEG Writer test.
[gdcm.git] / Testing / TestReadWriteJPEGReadCompare.cxx
1 /*=========================================================================
2
3   Program:   gdcm
4   Module:    $RCSfile: TestReadWriteJPEGReadCompare.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/08/28 16:57:00 $
7   Version:   $Revision: 1.2 $
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
32 static int CompareInternalJPEG(std::string const &filename, std::string const &output)
33 {
34    std::cout << "----------------------------------------------------------------------" << std::endl
35              << "   Testing: " << filename << std::endl;
36
37    //////////////// Step 1 (see above description):
38
39    GDCM_NAME_SPACE::File *file = GDCM_NAME_SPACE::File::New( );
40    file->SetFileName( filename );
41    file->Load ();
42    if( !file->IsReadable() )
43    {
44       std::cout << "Failed" << std::endl
45                 << "Test::TestReadWriteJPEGReadCompare: Image not gdcm compatible:"
46                 << filename << std::endl;
47       file->Delete();
48       return 1;
49    }
50    std::cout << "           step 1...";
51
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)
59
60    //filehelper->SetImageData(imageData, dataSize);
61
62          // Just to be sure the further Write() doesn't corrupt imageData ...
63          std::cout << std::endl;    
64          int i,j;
65          for(i=0, j=0;i<dataSize && j<MAX_NUMBER_OF_DIFFERENCE;i++)
66          {
67                std::cout << std::hex << "(" << i << " : " 
68                          << std::hex << (int)(imageData[i]) << ") "
69                          << std::dec;
70                ++j;
71          }
72          std::cout << std::endl;
73
74    filehelper->SetWriteModeToRGB();
75   
76    filehelper->SetWriteTypeToJPEG(  ); 
77    filehelper->SetUserData(imageData,dataSize); // This one ensures the compression
78    filehelper->Write( output ); 
79
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++)
82          {
83                std::cout << std::hex << "(" << i << " : " 
84                          << std::hex << (int)(imageData[i]) << ") "
85                          << std::dec;
86                ++j;
87          }
88          std::cout << std::endl << std::endl;
89  
90    std::cout << "2...";
91
92    //////////////// Step 3:
93    GDCM_NAME_SPACE::File *fileout = GDCM_NAME_SPACE::File::New();
94    fileout->SetFileName( output );
95    fileout->Load();
96
97    if( !fileout->IsReadable() )
98    {
99       std::cout << "Failed" << std::endl
100                 << "Test::TestReadWriteReadCompare: Could not parse the newly "
101                 << "written image:" << filename << std::endl;
102       file->Delete();
103       filehelper->Delete();
104       fileout->Delete();
105       return 1;
106    }
107
108    GDCM_NAME_SPACE::FileHelper *reread = GDCM_NAME_SPACE::FileHelper::New( fileout );
109
110    std::cout << "3...";
111    // For the next step:
112    int     dataSizeWritten   = reread->GetImageDataSize();
113    uint8_t *imageDataWritten = reread->GetImageData();
114    
115          // Just to see
116          std::cout << std::endl;
117          for(i=0, j=0;i<dataSize && j<MAX_NUMBER_OF_DIFFERENCE;i++)
118          {
119                std::cout << std::hex << "(" << i << " : " 
120                          << std::hex << (int)(imageDataWritten[i]) << ") "
121                          << std::dec;
122                ++j;
123          }
124          std::cout << std::endl << std::endl;
125  
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())
131    {
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;
140       file->Delete();
141       filehelper->Delete();
142       fileout->Delete();
143       reread->Delete();
144       return 1;
145    }
146
147    // Test the data size
148    // beware of odd length Pixel Element!
149    int dataSizeFixed = dataSize + dataSize%2;
150    int dataSizeWrittenFixed = dataSizeWritten + dataSizeWritten%2;
151
152    if (dataSizeFixed != dataSizeWrittenFixed)
153    {
154       std::cout << "Failed" << std::endl
155          << "        Pixel areas lengths differ: "
156          << dataSize << " # " << dataSizeWritten << std::endl;
157       file->Delete();
158       filehelper->Delete();
159       fileout->Delete();
160       reread->Delete();
161       return 1;
162    }
163
164    // Test the data content
165    
166    if (memcmp(imageData, imageDataWritten, dataSize) !=0)
167    {
168          std::string PixelType = filehelper->GetFile()->GetPixelType();
169          std::string ts        = filehelper->GetFile()->GetTransferSyntax();
170
171          std::cout << " Failed" << std::endl
172                    << "        pixel (" 
173                    << PixelType
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                    << " pixels differing (pos : original - written) :" 
181                    << std::endl;
182   
183          int i;
184          unsigned int j;
185          for(i=0, j=0;i<dataSize && j<MAX_NUMBER_OF_DIFFERENCE;i++)
186          {
187             //if(imageData[i]!=imageDataWritten[i])
188             if (abs ((int)imageData[i]-(int)imageDataWritten[i]) > 2)
189               {
190                std::cout << std::hex << "(" << i << " : " 
191                          << std::hex << (int)(imageData[i]) << " - "
192                          << std::hex << (int)(imageDataWritten[i]) << ") "
193                          << std::dec;
194                ++j;
195               }
196          }
197          std::cout << std::endl;
198    
199      if (j !=0 ) { 
200       file->Delete();
201       filehelper->Delete();
202       fileout->Delete();
203       reread->Delete();
204       return 1;
205      }
206    }
207    std::cout << "========================================= 4...OK." << std::endl ;
208
209    //////////////// Clean up:
210    file->Delete();
211    filehelper->Delete();
212    fileout->Delete();
213    reread->Delete();
214
215    return 0;
216 }
217
218 // -------------------------------------------------------------------------------------------
219
220 int TestReadWriteJPEGReadCompare(int argc, char *argv[]) 
221 {
222    int result = 0;
223
224    if (argc == 4)
225       GDCM_NAME_SPACE::Debug::DebugOn();
226
227    if (argc >= 3)
228    {
229       const std::string input  = argv[1];
230       const std::string output = argv[2];
231       result += CompareInternalJPEG(input, output);
232    }
233    else if( argc > 4 || argc == 2 )
234    {
235       std::cout << "Please read the manual" << std::endl;
236    }
237    else
238    {
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;
254    
255       int i = 0;
256       while( gdcmDataImages[i] != 0 )
257       {
258          std::string filename = GDCM_DATA_ROOT;
259          filename += "/";
260          filename += gdcmDataImages[i++];
261          result += CompareInternalJPEG(filename, "TestReadWriteJPEGReadCompare.dcm"); 
262       }
263    }
264    return result;
265 }