1 /*=========================================================================
4 Module: $RCSfile: TestAllReadCompareDicom.cxx,v $
6 Date: $Date: 2004/11/24 10:23:46 $
7 Version: $Revision: 1.17 $
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.
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.
17 =========================================================================*/
18 #include "gdcmHeader.h"
23 #include "gdcmDataImages.h"
25 int InternalTest(std::string const & filename,
26 std::string const & referenceFileName )
28 std::cout << " Testing: " << filename << std::endl;
30 gdcm::File* tested = new gdcm::File( filename );
31 if( !tested->GetHeader()->IsReadable() )
33 std::cout << " Image not gdcm compatible:"
34 << filename << std::endl;
41 ////// Check for existence of reference baseline dicom file:
43 FILE* testFILE = fopen( referenceFileName.c_str(), "r" );
47 uint8_t* testedImageData = tested->GetImageData(); // Kludge
48 (void)testedImageData;
50 tested->WriteDcmExplVR( referenceFileName );
51 std::cerr << " Creating reference baseline file :" << std::endl
52 << " " << referenceFileName
55 //delete (char*)testedImageData;
63 ////// When reference file is not gdcm readable test is failed:
65 gdcm::File* reference = new gdcm::File( referenceFileName );
66 if( !reference->GetHeader()->IsReadable() )
68 std::cout << " Failed: reference image " << std::endl
69 << " " << referenceFileName <<std::endl
70 << " is not gdcm compatible." << std::endl;
78 int testedDataSize = tested->GetImageDataSize();
79 uint8_t* testedImageData = tested->GetImageData();
81 int referenceDataSize = reference->GetImageDataSize();
82 uint8_t* referenceImageData = reference->GetImageData();
84 if (testedDataSize != referenceDataSize)
86 std::cout << " Failed" << std::endl
87 << " pixel areas lengths differ: "
88 << testedDataSize << " # " << referenceDataSize
95 if (int res = memcmp(testedImageData, referenceImageData,
96 testedDataSize) != 0 )
99 std::cout << " Failed" << std::endl
100 << " pixel differ (as expanded in memory)."
107 //////////////// Clean up:
111 std::cout << " Passed..." << std::endl;
116 int TestAllReadCompareDicom(int argc, char* argv[])
120 // The test is specified a specific filename, use it instead of looping
122 const std::string input = argv[1];
123 const std::string reference = argv[2];
124 return InternalTest( input, reference );
126 else if ( argc > 3 || argc == 2 )
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;
136 std::cout << " Description (Test::TestAllReadCompareDicom): "
138 std::cout << " For all images in gdcmData (and not blacklisted in "
139 "Test/CMakeLists.txt)"
141 std::cout << " apply the following to each filename.xxx: "
143 std::cout << " step 1: parse the image (as gdcmHeader) and call"
146 std::cout << " step 2: find in GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
148 << " (with format DICOM V3, explicit Value"
151 std::cout << " step 3a: when image NOT found on step 2, write "
153 << " GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
155 << " (with format DICOM V3, explicit Value"
158 std::cout << " step 3b: when image found on step 2, and when IsReadable()"
160 << " compare it (in memory with memcmp) with the"
162 << " image we are testing (the one of step 1). "
163 << std::endl << std::endl;
167 while( gdcmDataImages[i] != 0 )
169 ////// Check for existence of reference baseline directory
171 std::string baseLineDir = GDCM_DATA_ROOT;
172 baseLineDir += "/BaselineDicom/";
174 std::ifstream* testDIR = new std::ifstream(baseLineDir.c_str(), std::ios::in | std::ios::binary);
177 std::cerr << " The reference baseline directory " << std::endl
179 << baseLineDir << std::endl
180 << " couldn't be opened."
189 ////// Step 1 (see above description):
190 std::string filename = GDCM_DATA_ROOT;
192 filename += gdcmDataImages[i];
194 std::string referenceFileName = baseLineDir + gdcmDataImages[i++];
195 std::string::size_type slash_pos = referenceFileName.rfind( "." );
196 if ( slash_pos != std::string::npos )
198 referenceFileName.replace( slash_pos + 1, 3, "dcm" );
201 if( InternalTest( filename, referenceFileName ) != 0 )