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