]> Creatis software - gdcm.git/blob - Testing/TestAllReadCompareDicom.cxx
* src/jpeg/libijg12/jdhuff12.c:
[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         fclose( testFILE );
65
66       ////// Step 1 (see above description):
67
68       std::string filename = GDCM_DATA_ROOT;
69       filename += "/";  //doh!
70       filename += gdcmDataImages[i];
71    
72       std::cout << "   Testing: " << filename << std::endl;
73
74       gdcmFile* tested = new gdcmFile( filename.c_str(), false, true );
75       if( !tested->GetHeader()->IsReadable() )
76       {
77         std::cout << "      Image not gdcm compatible:"
78                   << filename << std::endl;
79         delete tested;
80         return 1;
81       }
82
83       ////// Step 2:
84
85       ////// Check for existence of reference baseline dicom file:
86
87       std::string referenceFileName = baseLineDir + gdcmDataImages[i++];
88       std::string::size_type slash_pos = referenceFileName.rfind( "." );
89       if ( slash_pos != std::string::npos )
90       {
91          referenceFileName.replace( slash_pos + 1, 3, "dcm" );
92       }
93
94       testFILE = fopen( referenceFileName.c_str(), "r" );
95       if (! testFILE )
96       {
97       ////// Step 3a:
98
99          int testedDataSize    = tested->GetImageDataSize();
100          void* testedImageData = tested->GetImageData(); // Kludge
101          tested->WriteDcmExplVR( referenceFileName );
102          std::cerr << "      Creating reference baseline file :" << std::endl
103                    << "      " << referenceFileName 
104                    << std::endl;
105          delete tested;
106          delete (char*)testedImageData;
107          continue; 
108       }
109       else
110          fclose( testFILE );
111
112       ////// When reference file is not gdcm readable test is failed:
113   
114       gdcmFile* reference = new gdcmFile( referenceFileName.c_str(),
115                                           false, true );
116       if( !reference->GetHeader()->IsReadable() )
117       {
118          std::cout << "      Reference image " << std::endl
119                    << "      " << referenceFileName <<std::endl
120                    << "      is not gdcm compatible." << std::endl;
121          delete tested;
122          delete reference;
123          return 1;
124       }
125
126       ////// Step 3b:
127
128       int testedDataSize    = tested->GetImageDataSize();
129       void* testedImageData = tested->GetImageData();
130     
131       int    referenceDataSize = reference->GetImageDataSize();
132       void* referenceImageData = reference->GetImageData();
133
134       if (testedDataSize != referenceDataSize)
135       {
136          std::cout << "        Pixel areas lengths differ: "
137                    << testedDataSize << " # " << referenceDataSize
138                    << std::endl;
139          delete tested;
140          delete reference;
141          delete (char*)testedImageData;
142          delete (char*)referenceImageData;
143          return 1;
144       }
145
146       if (int res = memcmp(testedImageData, referenceImageData,
147                            testedDataSize) != 0 )
148       {
149          std::cout << "        Pixel differ (as expanded in memory)."
150                    << std::endl;
151          delete tested;
152          delete reference;
153          delete (char*)testedImageData;
154          delete (char*)referenceImageData;
155          return 1;
156       }
157       std::cout << "      Passed." << std::endl ;
158
159       //////////////// Clean up:
160       delete tested;
161       delete reference;
162       delete (char*)testedImageData;
163       delete (char*)referenceImageData;
164    }
165
166    return 0;
167 }