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