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