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