]> Creatis software - gdcm.git/blob - Testing/TestReadWriteJPEGReadCompare.cxx
Typo
[gdcm.git] / Testing / TestReadWriteJPEGReadCompare.cxx
1 /*=========================================================================
2
3   Program:   gdcm
4   Module:    $RCSfile: TestReadWriteJPEGReadCompare.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/08/29 15:56:41 $
7   Version:   $Revision: 1.4 $
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_success___;
32 int nb_of_failure___;
33
34 static int CompareInternalJPEG(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::TestReadWriteJPEGReadCompare: Image not gdcm compatible:"
48                 << filename << std::endl;
49       file->Delete();
50       nb_of_failure___++;
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->SetWriteTypeToJPEG(  ); 
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::TestReadWriteJPEGReadCompare: Could not parse the newly "
83                 << "written image:" << filename << std::endl;
84       file->Delete();
85       filehelper->Delete();
86       fileout->Delete();
87       nb_of_failure___++;
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_failure___++;      
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_failure___++;      
135       return 1;
136    }
137
138    // Test the data content
139    
140    if (memcmp(imageData, imageDataWritten, dataSizeFixed) !=0)
141    {
142       unsigned int j =0;
143       for(int i1=0; i1<dataSizeFixed; i1++)
144       {
145          if (abs ((int)imageData[i1]-(int)imageDataWritten[i1]) > 2)
146          {
147             j=1;
148             break;
149          }
150        }
151        
152        if (j!=0)
153        {           
154           std::string PixelType = filehelper->GetFile()->GetPixelType();
155           std::string ts        = filehelper->GetFile()->GetTransferSyntax();
156
157           std::cout << " Failed" << std::endl
158                     << "        pixel (" 
159                     << PixelType
160                     << ") differ (as expanded in memory)."
161                     << std::endl
162                     << "        compression : " 
163                     << GDCM_NAME_SPACE::Global::GetTS()->GetValue(ts) << std::endl;
164
165           std::cout << "        list of the first " << MAX_NUMBER_OF_DIFFERENCE
166                     << " pixels differing (pos : original - written) :" 
167                     << std::endl;
168
169           for(int i=0, j=0;i<dataSizeFixed && j<MAX_NUMBER_OF_DIFFERENCE;i++)
170           {
171              if (abs ((int)imageData[i]-(int)imageDataWritten[i]) > 2)
172              {
173                 std::cout << std::hex << "(" << i << " : " 
174                          << std::hex << (int)(imageData[i]) << " - "
175                          << std::hex << (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_failure___++;  
187           return 1;
188        }
189    }
190    std::cout << "=============== 4...OK." << std::endl ;
191    nb_of_success___ ++;
192    //////////////// Clean up:
193    file->Delete();
194    filehelper->Delete();
195    fileout->Delete();
196    reread->Delete();
197
198    return 0;
199 }
200
201 // -------------------------------------------------------------------------------------------
202
203 int TestReadWriteJPEGReadCompare(int argc, char *argv[]) 
204 {
205    int result = 0;
206    nb_of_success___ =0;
207    nb_of_failure___ =0;
208
209    if (argc == 4)
210       GDCM_NAME_SPACE::Debug::DebugOn();
211
212    if (argc >= 3)
213    {
214       const std::string input  = argv[1];
215       const std::string output = argv[2];
216       result += CompareInternalJPEG(input, output);
217    }
218    else if( argc > 4 || argc == 2 )
219    {
220       std::cout << "Please read the manual" << std::endl;
221    }
222    else
223    {
224       std::cout<< "Test::TestReadWriteJPEGReadCompare: description " << std::endl;
225       std::cout << "   For all images in gdcmData (and not blacklisted in "
226                    "Test/CMakeLists.txt)" << std::endl;
227       std::cout << "   apply the following multistep test: " << std::endl;
228       std::cout << "   step 1: parse the image (as gdcmFile) and call"
229                 << " IsReadable(). " << std::endl;
230       std::cout << "   step 2: write the corresponding image in JPEG DICOM V3 "
231                 << "with explicit Value Representation " << std::endl
232                 << "            in temporary file "
233                 << "TestReadWriteJPEGReadCompare.dcm." << std::endl;
234       std::cout << "   step 3: read the image written on step2 and call "
235                 << " IsReadable(). " << std::endl;
236       std::cout << "   step 4: compare (in memory with memcmp) that the two "
237                 << "images " << std::endl
238                 << "           match (as expanded by gdcm)." << std::endl;
239    
240       int i = 0;
241       while( gdcmDataImages[i] != 0 )
242       {
243          std::string filename = GDCM_DATA_ROOT;
244          filename += "/";
245          filename += gdcmDataImages[i++];
246          result += CompareInternalJPEG(filename, "TestReadWriteJPEGReadCompare.dcm"); 
247       }
248    }
249    std::cout << "==================================" << std::endl;
250    std::cout << "nb of success " << nb_of_success___ << std::endl;
251    std::cout << "nb of failure " << nb_of_failure___ << std::endl;
252    
253    return result;
254 }