]> Creatis software - gdcm.git/blob - Testing/TestAllReadCompareDicom.cxx
FIX: Could not compile with warnings flags on
[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)testedDataSize;
101          void* testedImageData = tested->GetImageData(); // Kludge
102          tested->WriteDcmExplVR( referenceFileName );
103          std::cerr << "      Creating reference baseline file :" << std::endl
104                    << "      " << referenceFileName 
105                    << std::endl;
106          delete tested;
107          delete (char*)testedImageData;
108          continue; 
109       }
110       else
111          fclose( testFILE );
112
113       ////// When reference file is not gdcm readable test is failed:
114   
115       gdcmFile* reference = new gdcmFile( referenceFileName.c_str(),
116                                           false, true );
117       if( !reference->GetHeader()->IsReadable() )
118       {
119          std::cout << "      Reference image " << std::endl
120                    << "      " << referenceFileName <<std::endl
121                    << "      is not gdcm compatible." << std::endl;
122          delete tested;
123          delete reference;
124          return 1;
125       }
126
127       ////// Step 3b:
128
129       int testedDataSize    = tested->GetImageDataSize();
130       void* testedImageData = tested->GetImageData();
131     
132       int    referenceDataSize = reference->GetImageDataSize();
133       void* referenceImageData = reference->GetImageData();
134
135       if (testedDataSize != referenceDataSize)
136       {
137          std::cout << "        Pixel areas lengths differ: "
138                    << testedDataSize << " # " << referenceDataSize
139                    << std::endl;
140          delete tested;
141          delete reference;
142          delete (char*)testedImageData;
143          delete (char*)referenceImageData;
144          return 1;
145       }
146
147       if (int res = memcmp(testedImageData, referenceImageData,
148                            testedDataSize) != 0 )
149       {
150          (void)res;
151          std::cout << "        Pixel differ (as expanded in memory)."
152                    << std::endl;
153          delete tested;
154          delete reference;
155          delete (char*)testedImageData;
156          delete (char*)referenceImageData;
157          return 1;
158       }
159       std::cout << "      Passed." << std::endl ;
160
161       //////////////// Clean up:
162       delete tested;
163       delete reference;
164       delete (char*)testedImageData;
165       delete (char*)referenceImageData;
166    }
167
168    return 0;
169 }