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