]> Creatis software - gdcm.git/blob - Testing/TestAllReadCompareDicom.cxx
* src/gdcmDocument.cxx : now, when using the ReplaceOrCreateByNumber to
[gdcm.git] / Testing / TestAllReadCompareDicom.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestAllReadCompareDicom.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/11/17 10:20:06 $
7   Version:   $Revision: 1.16 $
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       gdcm::File* tested = new gdcm::File( filename );
31       if( !tested->GetHeader()->IsReadable() )
32       {
33         std::cout << "      Image not gdcm compatible:"
34                   << filename << std::endl;
35         delete tested;
36         return 1;
37       }
38
39       ////// Step 2:
40
41       ////// Check for existence of reference baseline dicom file:
42
43       FILE* testFILE = fopen( referenceFileName.c_str(), "r" );
44       if (! testFILE )
45       {
46       ////// Step 3a:
47          uint8_t* testedImageData = tested->GetImageData(); // Kludge
48          (void)testedImageData;
49
50          tested->WriteDcmExplVR( referenceFileName );
51          std::cerr << "      Creating reference baseline file :" << std::endl
52                    << "      " << referenceFileName 
53                    << std::endl;
54          delete tested;
55          //delete (char*)testedImageData;
56          return 0;
57       }
58       else
59       {
60          fclose( testFILE );
61       }
62
63       ////// When reference file is not gdcm readable test is failed:
64   
65       gdcm::File* reference = new gdcm::File( referenceFileName );
66       if( !reference->GetHeader()->IsReadable() )
67       {
68          std::cout << "      Failed: reference image " << std::endl
69                    << "              " << referenceFileName <<std::endl
70                    << "              is not gdcm compatible." << std::endl;
71          delete tested;
72          delete reference;
73          return 1;
74       }
75
76       ////// Step 3b:
77
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: pixel areas lengths differ: "
87                    << testedDataSize << " # " << referenceDataSize
88                    << std::endl;
89          delete tested;
90          delete reference;
91          return 1;
92       }
93
94       if (int res = memcmp(testedImageData, referenceImageData,
95                            testedDataSize) != 0 )
96       {
97          (void)res;
98          std::cout << "      Failed: pixel differ (as expanded in memory)."
99                    << std::endl;
100          delete tested;
101          delete reference;
102          return 1;
103       }
104
105       //////////////// Clean up:
106       delete tested;
107       delete reference;
108
109       std::cout << "      Passed..." << std::endl;
110       
111       return 0;
112 }
113
114 int TestAllReadCompareDicom(int argc, char* argv[]) 
115 {
116    if ( argc == 3 )
117    {
118       // The test is specified a specific filename, use it instead of looping
119       // over all images
120       const std::string input = argv[1];
121       const std::string reference = argv[2];
122       return InternalTest( input, reference );
123    }
124    else if ( argc > 3 || argc == 2 )
125    {
126       std::cerr << "   Usage: " << argv[0]
127                 << " (no arguments needed)." << std::endl;
128       std::cerr << "or   Usage: " << argv[0]
129                 << " filename.dcm reference.dcm" << std::endl;
130       return 1;
131    }
132    // else other cases:
133    
134    std::cout << "   Description (Test::TestAllReadCompareDicom): "
135              << std::endl;
136    std::cout << "   For all images in gdcmData (and not blacklisted in "
137                 "Test/CMakeLists.txt)"
138              << std::endl;
139    std::cout << "   apply the following to each filename.xxx: "
140              << std::endl;
141    std::cout << "   step 1: parse the image (as gdcmHeader) and call"
142              << " IsReadable(). "
143              << std::endl;
144    std::cout << "   step 2: find in GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
145              << std::endl
146              << "           (with format DICOM V3, explicit Value"
147              << "Representation)"
148              << std::endl;
149    std::cout << "   step 3a: when image NOT found on step 2, write "
150              << std::endl
151              << "            GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
152              << std::endl
153              << "           (with format DICOM V3, explicit Value"
154              << "Representation)"
155              << std::endl;
156    std::cout << "   step 3b: when image found on step 2, and when IsReadable()"
157              << std::endl
158              << "            compare it (in memory with memcmp) with the"
159              << std::endl
160              << "            image we are testing (the one of step 1). "
161              << std::endl << std::endl;
162
163    int i = 0;
164    int result = 0;
165    while( gdcmDataImages[i] != 0 )
166    {
167       ////// Check for existence of reference baseline directory
168
169       std::string baseLineDir = GDCM_DATA_ROOT;
170       baseLineDir += "/BaselineDicom/";
171
172       std::ifstream* testDIR = new std::ifstream(baseLineDir.c_str(), std::ios::in | std::ios::binary);
173       if (!testDIR )
174       {
175          std::cerr << "   The reference baseline directory " << std::endl
176                    << "      "
177                    << baseLineDir << std::endl
178                    << "   couldn't be opened."
179                    << std::endl;
180          return 1;
181       }
182       else
183       {
184          testDIR->close();
185       }
186
187       ////// Step 1 (see above description):
188       std::string filename = GDCM_DATA_ROOT;
189       filename += "/";
190       filename += gdcmDataImages[i];
191       
192       std::string referenceFileName = baseLineDir + gdcmDataImages[i++];
193       std::string::size_type slash_pos = referenceFileName.rfind( "." );
194       if ( slash_pos != std::string::npos )
195       {
196          referenceFileName.replace( slash_pos + 1, 3, "dcm" );
197       }
198
199       if( InternalTest( filename, referenceFileName ) != 0 )
200       {
201          result++;
202       }
203    }
204
205    return result;
206 }