1 /*=========================================================================
4 Module: $RCSfile: TestAllReadCompareDicom.cxx,v $
6 Date: $Date: 2004/11/17 10:20:06 $
7 Version: $Revision: 1.16 $
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: pixel areas lengths differ: "
87 << testedDataSize << " # " << referenceDataSize
94 if (int res = memcmp(testedImageData, referenceImageData,
95 testedDataSize) != 0 )
98 std::cout << " Failed: pixel differ (as expanded in memory)."
105 //////////////// Clean up:
109 std::cout << " Passed..." << std::endl;
114 int TestAllReadCompareDicom(int argc, char* argv[])
118 // The test is specified a specific filename, use it instead of looping
120 const std::string input = argv[1];
121 const std::string reference = argv[2];
122 return InternalTest( input, reference );
124 else if ( argc > 3 || argc == 2 )
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;
134 std::cout << " Description (Test::TestAllReadCompareDicom): "
136 std::cout << " For all images in gdcmData (and not blacklisted in "
137 "Test/CMakeLists.txt)"
139 std::cout << " apply the following to each filename.xxx: "
141 std::cout << " step 1: parse the image (as gdcmHeader) and call"
144 std::cout << " step 2: find in GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
146 << " (with format DICOM V3, explicit Value"
149 std::cout << " step 3a: when image NOT found on step 2, write "
151 << " GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
153 << " (with format DICOM V3, explicit Value"
156 std::cout << " step 3b: when image found on step 2, and when IsReadable()"
158 << " compare it (in memory with memcmp) with the"
160 << " image we are testing (the one of step 1). "
161 << std::endl << std::endl;
165 while( gdcmDataImages[i] != 0 )
167 ////// Check for existence of reference baseline directory
169 std::string baseLineDir = GDCM_DATA_ROOT;
170 baseLineDir += "/BaselineDicom/";
172 std::ifstream* testDIR = new std::ifstream(baseLineDir.c_str(), std::ios::in | std::ios::binary);
175 std::cerr << " The reference baseline directory " << std::endl
177 << baseLineDir << std::endl
178 << " couldn't be opened."
187 ////// Step 1 (see above description):
188 std::string filename = GDCM_DATA_ROOT;
190 filename += gdcmDataImages[i];
192 std::string referenceFileName = baseLineDir + gdcmDataImages[i++];
193 std::string::size_type slash_pos = referenceFileName.rfind( "." );
194 if ( slash_pos != std::string::npos )
196 referenceFileName.replace( slash_pos + 1, 3, "dcm" );
199 if( InternalTest( filename, referenceFileName ) != 0 )