]> Creatis software - gdcm.git/blob - Testing/TestAllReadCompareDicom.cxx
0c0595c80123b4d4d13039b8459e09182280d3ef
[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, true );
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(),
120                                           false, true );
121       if( !reference->GetHeader()->IsReadable() )
122       {
123          std::cout << "      Reference image " << std::endl
124                    << "      " << referenceFileName <<std::endl
125                    << "      is not gdcm compatible." << std::endl;
126          delete tested;
127          delete reference;
128          return 1;
129       }
130
131       ////// Step 3b:
132
133       int testedDataSize    = tested->GetImageDataSize();
134       void* testedImageData = tested->GetImageData();
135     
136       int    referenceDataSize = reference->GetImageDataSize();
137       void* referenceImageData = reference->GetImageData();
138
139       if (testedDataSize != referenceDataSize)
140       {
141          std::cout << "        Pixel areas lengths differ: "
142                    << testedDataSize << " # " << referenceDataSize
143                    << std::endl;
144          delete tested;
145          delete reference;
146          delete (char*)testedImageData;
147          delete (char*)referenceImageData;
148          return 1;
149       }
150
151       if (int res = memcmp(testedImageData, referenceImageData,
152                            testedDataSize) != 0 )
153       {
154          (void)res;
155          std::cout << "        Pixel differ (as expanded in memory)."
156                    << std::endl;
157          delete tested;
158          delete reference;
159          delete (char*)testedImageData;
160          delete (char*)referenceImageData;
161          return 1;
162       }
163       std::cout << "      Passed." << std::endl ;
164
165       //////////////// Clean up:
166       delete tested;
167       delete reference;
168       delete (char*)testedImageData;
169       delete (char*)referenceImageData;
170    }
171
172    return 0;
173 }