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