]> Creatis software - gdcm.git/blob - Testing/TestReadWriteJPEGReadCompare.cxx
First version of a general JPEG-writer tester
[gdcm.git] / Testing / TestReadWriteJPEGReadCompare.cxx
1 /*=========================================================================
2
3   Program:   gdcm
4   Module:    $RCSfile: TestReadWriteJPEGReadCompare.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/08/27 16:09:06 $
7   Version:   $Revision: 1.1 $
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 //Generated file:
23 #include "gdcmDataImages.h"
24
25 static int CompareInternalJPEG(std::string const &filename, std::string const &output)
26 {
27    std::cout << "   Testing: " << filename << std::endl;
28
29    //////////////// Step 1 (see above description):
30
31    GDCM_NAME_SPACE::File *file = GDCM_NAME_SPACE::File::New( );
32    file->SetFileName( filename );
33    file->Load ();
34    if( !file->IsReadable() )
35    {
36       std::cout << "Failed" << std::endl
37                 << "Test::TestReadWriteJPEGReadCompare: Image not gdcm compatible:"
38                 << filename << std::endl;
39       file->Delete();
40       return 1;
41    }
42    std::cout << "           step 1...";
43
44    //////////////// Step 2:
45    GDCM_NAME_SPACE::FileHelper *filehelper = GDCM_NAME_SPACE::FileHelper::New( file );
46    int dataSize    = filehelper->GetImageDataSize();
47    uint8_t *imageData = filehelper->GetImageData(); //EXTREMELY IMPORTANT
48           // Sure, it is : It's up to the user to decide if he wants to
49           // GetImageData or if he wants to GetImageDataRaw
50           // (even if we do it by setting a flag, *he* will have to decide)
51
52    //filehelper->SetImageData(imageData, dataSize);
53
54    filehelper->SetWriteModeToRGB();
55   
56    filehelper->SetWriteTypeToJPEG(  ); 
57    filehelper->SetUserData(imageData,dataSize); 
58    filehelper->Write( output );  
59    std::cout << "2...";
60
61    //////////////// Step 3:
62    GDCM_NAME_SPACE::File *fileout = GDCM_NAME_SPACE::File::New();
63    fileout->SetFileName( output );
64    fileout->Load();
65   // gdcm::FileHelper *reread = new gdcm::FileHelper( output ); // deprecated
66
67    if( !fileout->IsReadable() )
68    {
69       std::cout << "Failed" << std::endl
70                 << "Test::TestReadWriteReadCompare: Could not parse the newly "
71                 << "written image:" << filename << std::endl;
72       file->Delete();
73       filehelper->Delete();
74       fileout->Delete();
75       return 1;
76    }
77
78    GDCM_NAME_SPACE::FileHelper *reread = GDCM_NAME_SPACE::FileHelper::New( fileout );
79
80    std::cout << "3...";
81    // For the next step:
82    int    dataSizeWritten = reread->GetImageDataSize();
83    uint8_t *imageDataWritten = reread->GetImageData();
84
85    //////////////// Step 4:
86    // Test the image size
87    if (file->GetXSize() != reread->GetFile()->GetXSize() ||
88        file->GetYSize() != reread->GetFile()->GetYSize() ||
89        file->GetZSize() != reread->GetFile()->GetZSize())
90    {
91       std::cout << "Failed" << std::endl
92          << "        X Size differs: "
93          << "X: " << file->GetXSize() << " # "
94                   << reread->GetFile()->GetXSize() << " | "
95          << "Y: " << file->GetYSize() << " # "
96                   << reread->GetFile()->GetYSize() << " | "
97          << "Z: " << file->GetZSize() << " # "
98                   << reread->GetFile()->GetZSize() << std::endl;
99       file->Delete();
100       filehelper->Delete();
101       fileout->Delete();
102       reread->Delete();
103       return 1;
104    }
105
106    // Test the data size
107    // beware of odd length Pixel Element!
108    int dataSizeFixed = dataSize + dataSize%2;
109    int dataSizeWrittenFixed = dataSizeWritten + dataSizeWritten%2;
110
111    if (dataSizeFixed != dataSizeWrittenFixed)
112    {
113       std::cout << "Failed" << std::endl
114          << "        Pixel areas lengths differ: "
115          << dataSize << " # " << dataSizeWritten << std::endl;
116       file->Delete();
117       filehelper->Delete();
118       fileout->Delete();
119       reread->Delete();
120       return 1;
121    }
122
123    // Test the data's content
124    if (memcmp(imageData, imageDataWritten, dataSize) !=0)
125    {
126       std::cout << "Failed" << std::endl
127          << "        Pixel differ (as expanded in memory)." << std::endl;
128       file->Delete();
129       filehelper->Delete();
130       fileout->Delete();
131       reread->Delete();
132       return 1;
133    }
134    std::cout << "4...OK." << std::endl ;
135
136    //////////////// Clean up:
137    file->Delete();
138    filehelper->Delete();
139    fileout->Delete();
140    reread->Delete();
141
142    return 0;
143 }
144
145 int TestReadWriteJPEGReadCompare(int argc, char *argv[]) 
146 {
147    int result = 0;
148
149    if (argc == 4)
150       GDCM_NAME_SPACE::Debug::DebugOn();
151
152    if (argc >= 3)
153    {
154       const std::string input  = argv[1];
155       const std::string output = argv[2];
156       result += CompareInternalJPEG(input, output);
157    }
158    else if( argc > 4 || argc == 2 )
159    {
160       std::cout << "Please read the manual" << std::endl;
161    }
162    else
163    {
164       std::cout<< "Test::TestReadWriteJPEGReadCompare: description " << std::endl;
165       std::cout << "   For all images in gdcmData (and not blacklisted in "
166                    "Test/CMakeLists.txt)" << std::endl;
167       std::cout << "   apply the following multistep test: " << std::endl;
168       std::cout << "   step 1: parse the image (as gdcmFile) and call"
169                 << " IsReadable(). " << std::endl;
170       std::cout << "   step 2: write the corresponding image in JPEG DICOM V3 "
171                 << "with explicit" << std::endl
172                 << "           Value Representation in temporary file "
173                 << "TestReadWriteReadCompare.dcm." << std::endl;
174       std::cout << "   step 3: read the image written on step2 and call "
175                 << " IsReadable(). " << std::endl;
176       std::cout << "   step 4: compare (in memory with memcmp) that the two "
177                 << "images " << std::endl
178                 << "           match (as expanded by gdcm)." << std::endl;
179    
180       int i = 0;
181       while( gdcmDataImages[i] != 0 )
182       {
183          std::string filename = GDCM_DATA_ROOT;
184          filename += "/";
185          filename += gdcmDataImages[i++];
186          result += CompareInternalJPEG(filename, "TestReadWriteJPEGReadCompare.dcm"); 
187       }
188    }
189    return result;
190 }