]> Creatis software - gdcm.git/blob - Testing/TestAllReadCompareDicom.cxx
COMP: Borland could not compile gdcm... BTW there is something fishy with the protect...
[gdcm.git] / Testing / TestAllReadCompareDicom.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestAllReadCompareDicom.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/12/03 20:43:36 $
7   Version:   $Revision: 1.22 $
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
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::File* tested = new gdcm::File( 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::File* reference = new gdcm::File( 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       ////// Step 3b:
81       std::cout << "3b...";
82       int testedDataSize    = tested->GetImageDataSize();
83       uint8_t* testedImageData = tested->GetImageData();
84     
85       int    referenceDataSize = reference->GetImageDataSize();
86       uint8_t* referenceImageData = reference->GetImageData();
87
88       // Test the image size
89       if (tested->GetHeader()->GetXSize() != reference->GetHeader()->GetXSize() ||
90           tested->GetHeader()->GetYSize() != reference->GetHeader()->GetYSize() ||
91           tested->GetHeader()->GetZSize() != reference->GetHeader()->GetZSize())
92       {
93          std::cout << "Failed" << std::endl
94             << "        Size differs: "
95             << "X: " << tested->GetHeader()->GetXSize() << " # " 
96                      << reference->GetHeader()->GetXSize() << " | "
97             << "Y: " << tested->GetHeader()->GetYSize() << " # " 
98                      << reference->GetHeader()->GetYSize() << " | "
99             << "Z: " << tested->GetHeader()->GetZSize() << " # " 
100                      << reference->GetHeader()->GetZSize() << std::endl;
101          delete reference;
102          delete tested;
103          return 1;
104       }
105
106       // Test the data size
107       if (testedDataSize != referenceDataSize)
108       {
109          std::cout << " Failed" << std::endl
110                    << "        pixel areas lengths differ: "
111                    << testedDataSize << " # " << referenceDataSize
112                    << std::endl;
113          delete tested;
114          delete reference;
115          return 1;
116       }
117
118       // Test the data content
119       if (int res = memcmp(testedImageData, referenceImageData,
120                            testedDataSize) != 0 )
121       {
122          (void)res;
123          std::cout << " Failed" << std::endl
124                    << "        pixel differ (as expanded in memory)."
125                    << std::endl;
126          delete tested;
127          delete reference;
128          return 1;
129       }
130
131       //////////////// Clean up:
132       delete tested;
133       delete reference;
134
135       std::cout << "OK." << std::endl;
136       
137       return 0;
138 }
139
140 int TestAllReadCompareDicom(int argc, char* argv[]) 
141 {
142    if ( argc == 3 )
143    {
144       // The test is specified a specific filename, use it instead of looping
145       // over all images
146       const std::string input = argv[1];
147       const std::string reference = argv[2];
148       return InternalTest( input, reference );
149    }
150    else if ( argc > 3 || argc == 2 )
151    {
152       std::cerr << "   Usage: " << argv[0]
153                 << " (no arguments needed)." << std::endl;
154       std::cerr << "or   Usage: " << argv[0]
155                 << " filename.dcm reference.dcm" << std::endl;
156       return 1;
157    }
158    // else other cases:
159    
160    std::cout << "   Description (Test::TestAllReadCompareDicom): "
161              << std::endl;
162    std::cout << "   For all images in gdcmData (and not blacklisted in "
163                 "Test/CMakeLists.txt)"
164              << std::endl;
165    std::cout << "   apply the following to each filename.xxx: "
166              << std::endl;
167    std::cout << "   step 1: parse the image (as gdcmHeader) and call"
168              << " IsReadable(). "
169              << std::endl;
170    std::cout << "   step 2: find in GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
171              << std::endl
172              << "           (with format DICOM V3, explicit Value"
173              << "Representation)"
174              << std::endl;
175    std::cout << "   step 3a: when image NOT found on step 2, write "
176              << std::endl
177              << "            GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
178              << std::endl
179              << "           (with format DICOM V3, explicit Value"
180              << "Representation)"
181              << std::endl;
182    std::cout << "   step 3b: when image found on step 2, and when IsReadable()"
183              << std::endl
184              << "            compare it (in memory with memcmp) with the"
185              << std::endl
186              << "            image we are testing (the one of step 1). "
187              << std::endl << std::endl;
188
189    int i = 0;
190    int result = 0;
191    while( gdcmDataImages[i] != 0 )
192    {
193       ////// Check for existence of reference baseline directory
194
195       std::string baseLineDir = GDCM_DATA_ROOT;
196       baseLineDir += "/BaselineDicom/";
197
198       std::ifstream* testDIR = new std::ifstream(baseLineDir.c_str(), std::ios::in | std::ios::binary);
199       if (!testDIR )
200       {
201          std::cerr << "   The reference baseline directory " << std::endl
202                    << "      "
203                    << baseLineDir << std::endl
204                    << "   couldn't be opened."
205                    << std::endl;
206          return 1;
207       }
208       else
209       {
210          testDIR->close();
211       }
212
213       ////// Step 1 (see above description):
214       std::string filename = GDCM_DATA_ROOT;
215       filename += "/";
216       filename += gdcmDataImages[i];
217       
218       std::string referenceFileName = baseLineDir + gdcmDataImages[i++];
219       std::string::size_type slash_pos = referenceFileName.rfind( "." );
220       if ( slash_pos != std::string::npos )
221       {
222          referenceFileName.replace( slash_pos + 1, 3, "dcm" );
223       }
224
225       if( InternalTest( filename, referenceFileName ) != 0 )
226       {
227          result++;
228       }
229    }
230
231    return result;
232 }