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