]> Creatis software - gdcm.git/blob - Testing/TestAllReadCompareDicom.cxx
ENH: gdcm now compiles on borland
[gdcm.git] / Testing / TestAllReadCompareDicom.cxx
1 #include "gdcmHeader.h"
2 #include "gdcmFile.h"
3 #include <stdio.h>
4
5 //Generated file:
6 #include "gdcmDataImages.h"
7
8 int InternalTest(std::string const & filename, 
9                  std::string const & referenceFileName )
10 {
11       std::cout << "   Testing: " << filename << std::endl;
12
13       gdcm::File* tested = new gdcm::File( filename );
14       if( !tested->GetHeader()->IsReadable() )
15       {
16         std::cout << "      Image not gdcm compatible:"
17                   << filename << std::endl;
18         delete tested;
19         return 1;
20       }
21
22       ////// Step 2:
23
24       ////// Check for existence of reference baseline dicom file:
25
26       FILE* testFILE = fopen( referenceFileName.c_str(), "r" );
27       if (! testFILE )
28       {
29       ////// Step 3a:
30
31          int testedDataSize    = tested->GetImageDataSize();
32          (void)testedDataSize;
33          void* testedImageData = tested->GetImageData(); // Kludge
34          tested->WriteDcmExplVR( referenceFileName );
35          std::cerr << "      Creating reference baseline file :" << std::endl
36                    << "      " << referenceFileName 
37                    << std::endl;
38          delete tested;
39          delete (char*)testedImageData;
40          return 0;
41       }
42       else
43       {
44          fclose( testFILE );
45       }
46
47       ////// When reference file is not gdcm readable test is failed:
48   
49       gdcm::File* reference = new gdcm::File( referenceFileName );
50       if( !reference->GetHeader()->IsReadable() )
51       {
52          std::cout << "      Reference image " << std::endl
53                    << "      " << referenceFileName <<std::endl
54                    << "      is not gdcm compatible." << std::endl;
55          delete tested;
56          delete reference;
57          return 1;
58       }
59
60       ////// Step 3b:
61
62       int testedDataSize    = tested->GetImageDataSize();
63       void* testedImageData = tested->GetImageData();
64     
65       int    referenceDataSize = reference->GetImageDataSize();
66       void* referenceImageData = reference->GetImageData();
67
68       if (testedDataSize != referenceDataSize)
69       {
70          std::cout << "        Pixel areas lengths differ: "
71                    << testedDataSize << " # " << referenceDataSize
72                    << std::endl;
73          delete tested;
74          delete reference;
75          delete (char*)testedImageData;
76          delete (char*)referenceImageData;
77          return 1;
78       }
79
80       if (int res = memcmp(testedImageData, referenceImageData,
81                            testedDataSize) != 0 )
82       {
83          (void)res;
84          std::cout << "        Pixel differ (as expanded in memory)."
85                    << std::endl;
86          delete tested;
87          delete reference;
88          delete (char*)testedImageData;
89          delete (char*)referenceImageData;
90          return 1;
91       }
92       std::cout << "      Passed." << std::endl ;
93
94       //////////////// Clean up:
95       delete tested;
96       delete reference;
97       delete (char*)testedImageData;
98       delete (char*)referenceImageData;
99       
100       return 0;
101 }
102
103 int TestAllReadCompareDicom(int argc, char* argv[]) 
104 {
105    if ( argc == 3 )
106    {
107       // The test is specified a specific filename, use it instead of looping
108       // over all images
109       const std::string input = argv[1];
110       const std::string reference = argv[2];
111       return InternalTest( input, reference );
112    }
113    else if ( argc > 3 || argc == 2 )
114    {
115       std::cerr << "   Usage: " << argv[0]
116                 << " (no arguments needed)." << std::endl;
117       std::cerr << "or   Usage: " << argv[0]
118                 << " filename.dcm reference.dcm" << std::endl;
119       return 1;
120    }
121    // else other cases:
122    
123    std::cout << "   Description (Test::TestAllReadCompareDicom): "
124              << std::endl;
125    std::cout << "   For all images in gdcmData (and not blacklisted in "
126                 "Test/CMakeLists.txt)"
127              << std::endl;
128    std::cout << "   apply the following to each filename.xxx: "
129              << std::endl;
130    std::cout << "   step 1: parse the image (as gdcmHeader) and call"
131              << " IsReadable(). "
132              << std::endl;
133    std::cout << "   step 2: find in GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
134              << std::endl
135              << "           (with format DICOM V3, explicit Value"
136              << "Representation)"
137              << std::endl;
138    std::cout << "   step 3a: when image NOT found on step 2, write "
139              << std::endl
140              << "            GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
141              << std::endl
142              << "           (with format DICOM V3, explicit Value"
143              << "Representation)"
144              << std::endl;
145    std::cout << "   step 3b: when image found on step 2, and when IsReadable()"
146              << std::endl
147              << "            compare it (in memory with memcmp) with the"
148              << std::endl
149              << "            image we are testing (the one of step 1). "
150              << std::endl << std::endl;
151
152    int i = 0;
153    int result = 0;
154    while( gdcmDataImages[i] != 0 )
155    {
156       ////// Check for existence of reference baseline directory
157
158       std::string baseLineDir = GDCM_DATA_ROOT;
159       baseLineDir += "/BaselineDicom/";
160
161       std::ifstream* testDIR = new std::ifstream(baseLineDir.c_str(), std::ios::in | std::ios::binary);
162       if (!testDIR )
163       {
164          std::cerr << "   The reference baseline directory " << std::endl
165                    << "      "
166                    << baseLineDir << std::endl
167                    << "   couldn't be opened."
168                    << std::endl;
169          return 1;
170       }
171       else
172       {
173          testDIR->close();
174       }
175
176 /*      FILE* testFILE = fopen( baseLineDir.c_str(), "r" );
177       if (!testFILE )
178       {
179          std::cerr << "   The reference baseline directory " << std::endl
180                    << "      "
181                    << baseLineDir << std::endl
182                    << "   couldn't be opened."
183                    << std::endl;
184          return 1;
185       }
186       else
187       {
188          fclose( testFILE );
189       }*/
190
191       ////// Step 1 (see above description):
192
193       std::string filename = GDCM_DATA_ROOT;
194       filename += "/";
195       filename += gdcmDataImages[i];
196       
197       std::string referenceFileName = baseLineDir + gdcmDataImages[i++];
198       std::string::size_type slash_pos = referenceFileName.rfind( "." );
199       if ( slash_pos != std::string::npos )
200       {
201          referenceFileName.replace( slash_pos + 1, 3, "dcm" );
202       }
203
204       if( InternalTest( filename, referenceFileName ) != 0 )
205       {
206          result++;
207       }
208    }
209
210    return result;
211 }