]> Creatis software - gdcm.git/blob - Testing/TestAllReadCompareDicom.cxx
* src/gdcmFile.[h|cxx] : bug fix for ACR-LIBIDO files when writting the file
[gdcm.git] / Testing / TestAllReadCompareDicom.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestAllReadCompareDicom.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/11/30 14:17:52 $
7   Version:   $Revision: 1.20 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18 #include "gdcmHeader.h"
19 #include "gdcmFile.h"
20 #include <stdio.h>
21
22 //Generated file:
23 #include "gdcmDataImages.h"
24
25 int InternalTest(std::string const & filename, 
26                  std::string const & referenceFileName )
27 {
28       std::cout << "   Testing: " << filename << std::endl;
29
30       ////// Step 1:
31       std::cout << "      1...";
32       gdcm::File* tested = new gdcm::File( filename );
33       if( !tested->GetHeader()->IsReadable() )
34       {
35         std::cout << " Failed" << std::endl
36                    << "      Image not gdcm compatible:"
37                   << filename << std::endl;
38         delete tested;
39         return 1;
40       }
41
42       ////// Step 2:
43       ////// Check for existence of reference baseline dicom file:
44       std::cout << "2...";
45
46       FILE* testFILE = fopen( referenceFileName.c_str(), "r" );
47       if (! testFILE )
48       {
49          uint8_t* testedImageData = tested->GetImageData(); // Kludge
50          (void)testedImageData;
51
52          tested->SetWriteModeToRGB();
53          tested->WriteDcmExplVR( referenceFileName );
54       }
55       else
56       {
57          fclose( testFILE );
58       }
59
60       ////// Step 3a:
61       ////// When reference file is not gdcm readable test is failed:
62       std::cout << "3a...";
63
64       gdcm::File* reference = new gdcm::File( referenceFileName );
65       if( !reference->GetHeader()->IsReadable() )
66       {
67          std::cout << " Failed" << std::endl
68                    << "              reference image " 
69                    << referenceFileName 
70                    << " is not gdcm compatible." << std::endl;
71          delete tested;
72          delete reference;
73          return 1;
74       }
75
76       ////// Step 3b:
77       std::cout << "3b...";
78       int testedDataSize    = tested->GetImageDataSize();
79       uint8_t* testedImageData = tested->GetImageData();
80     
81       int    referenceDataSize = reference->GetImageDataSize();
82       uint8_t* referenceImageData = reference->GetImageData();
83
84       // Test the image size
85       if (tested->GetHeader()->GetXSize() != reference->GetHeader()->GetXSize() ||
86           tested->GetHeader()->GetYSize() != reference->GetHeader()->GetYSize() ||
87           tested->GetHeader()->GetZSize() != reference->GetHeader()->GetZSize())
88       {
89          std::cout << "Failed" << std::endl
90             << "        Size differs: "
91             << "X: " << tested->GetHeader()->GetXSize() << " # " 
92                      << reference->GetHeader()->GetXSize() << " | "
93             << "Y: " << tested->GetHeader()->GetYSize() << " # " 
94                      << reference->GetHeader()->GetYSize() << " | "
95             << "Z: " << tested->GetHeader()->GetZSize() << " # " 
96                      << reference->GetHeader()->GetZSize() << std::endl;
97          delete reference;
98          delete tested;
99          return 1;
100       }
101
102       // Test the data size
103       if (testedDataSize != referenceDataSize)
104       {
105          std::cout << " Failed" << std::endl
106                    << "        pixel areas lengths differ: "
107                    << testedDataSize << " # " << referenceDataSize
108                    << std::endl;
109          delete tested;
110          delete reference;
111          return 1;
112       }
113
114       // Test the data content
115       if (int res = memcmp(testedImageData, referenceImageData,
116                            testedDataSize) != 0 )
117       {
118          (void)res;
119          std::cout << " Failed" << std::endl
120                    << "        pixel differ (as expanded in memory)."
121                    << std::endl;
122          delete tested;
123          delete reference;
124          return 1;
125       }
126
127       //////////////// Clean up:
128       delete tested;
129       delete reference;
130
131       std::cout << "OK." << std::endl;
132       
133       return 0;
134 }
135
136 int TestAllReadCompareDicom(int argc, char* argv[]) 
137 {
138    if ( argc == 3 )
139    {
140       // The test is specified a specific filename, use it instead of looping
141       // over all images
142       const std::string input = argv[1];
143       const std::string reference = argv[2];
144       return InternalTest( input, reference );
145    }
146    else if ( argc > 3 || argc == 2 )
147    {
148       std::cerr << "   Usage: " << argv[0]
149                 << " (no arguments needed)." << std::endl;
150       std::cerr << "or   Usage: " << argv[0]
151                 << " filename.dcm reference.dcm" << std::endl;
152       return 1;
153    }
154    // else other cases:
155    
156    std::cout << "   Description (Test::TestAllReadCompareDicom): "
157              << std::endl;
158    std::cout << "   For all images in gdcmData (and not blacklisted in "
159                 "Test/CMakeLists.txt)"
160              << std::endl;
161    std::cout << "   apply the following to each filename.xxx: "
162              << std::endl;
163    std::cout << "   step 1: parse the image (as gdcmHeader) and call"
164              << " IsReadable(). "
165              << std::endl;
166    std::cout << "   step 2: find in GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
167              << std::endl
168              << "           (with format DICOM V3, explicit Value"
169              << "Representation)"
170              << std::endl;
171    std::cout << "   step 3a: when image NOT found on step 2, write "
172              << std::endl
173              << "            GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
174              << std::endl
175              << "           (with format DICOM V3, explicit Value"
176              << "Representation)"
177              << std::endl;
178    std::cout << "   step 3b: when image found on step 2, and when IsReadable()"
179              << std::endl
180              << "            compare it (in memory with memcmp) with the"
181              << std::endl
182              << "            image we are testing (the one of step 1). "
183              << std::endl << std::endl;
184
185    int i = 0;
186    int result = 0;
187    while( gdcmDataImages[i] != 0 )
188    {
189       ////// Check for existence of reference baseline directory
190
191       std::string baseLineDir = GDCM_DATA_ROOT;
192       baseLineDir += "/BaselineDicom/";
193
194       std::ifstream* testDIR = new std::ifstream(baseLineDir.c_str(), std::ios::in | std::ios::binary);
195       if (!testDIR )
196       {
197          std::cerr << "   The reference baseline directory " << std::endl
198                    << "      "
199                    << baseLineDir << std::endl
200                    << "   couldn't be opened."
201                    << std::endl;
202          return 1;
203       }
204       else
205       {
206          testDIR->close();
207       }
208
209       ////// Step 1 (see above description):
210       std::string filename = GDCM_DATA_ROOT;
211       filename += "/";
212       filename += gdcmDataImages[i];
213       
214       std::string referenceFileName = baseLineDir + gdcmDataImages[i++];
215       std::string::size_type slash_pos = referenceFileName.rfind( "." );
216       if ( slash_pos != std::string::npos )
217       {
218          referenceFileName.replace( slash_pos + 1, 3, "dcm" );
219       }
220
221       if( InternalTest( filename, referenceFileName ) != 0 )
222       {
223          result++;
224       }
225    }
226
227    return result;
228 }