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