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