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