1 /*=========================================================================
4 Module: $RCSfile: TestAllReadCompareDicom.cxx,v $
6 Date: $Date: 2004/11/16 04:28:20 $
7 Version: $Revision: 1.14 $
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 tested->WriteDcmExplVR( referenceFileName );
49 std::cerr << " Creating reference baseline file :" << std::endl
50 << " " << referenceFileName
53 //delete (char*)testedImageData;
61 ////// When reference file is not gdcm readable test is failed:
63 gdcm::File* reference = new gdcm::File( referenceFileName );
64 if( !reference->GetHeader()->IsReadable() )
66 std::cout << " Reference image " << std::endl
67 << " " << referenceFileName <<std::endl
68 << " is not gdcm compatible." << std::endl;
76 int testedDataSize = tested->GetImageDataSize();
77 uint8_t* testedImageData = tested->GetImageData();
79 int referenceDataSize = reference->GetImageDataSize();
80 uint8_t* referenceImageData = reference->GetImageData();
82 if (testedDataSize != referenceDataSize)
84 std::cout << " Pixel areas lengths differ: "
85 << testedDataSize << " # " << referenceDataSize
92 if (int res = memcmp(testedImageData, referenceImageData,
93 testedDataSize) != 0 )
96 std::cout << " Pixel differ (as expanded in memory)."
102 std::cout << " Passed..." << std::endl;
104 //////////////// Clean up:
107 std::cout << " Passed clean up." << std::endl ;
112 int TestAllReadCompareDicom(int argc, char* argv[])
116 // The test is specified a specific filename, use it instead of looping
118 const std::string input = argv[1];
119 const std::string reference = argv[2];
120 return InternalTest( input, reference );
122 else if ( argc > 3 || argc == 2 )
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;
132 std::cout << " Description (Test::TestAllReadCompareDicom): "
134 std::cout << " For all images in gdcmData (and not blacklisted in "
135 "Test/CMakeLists.txt)"
137 std::cout << " apply the following to each filename.xxx: "
139 std::cout << " step 1: parse the image (as gdcmHeader) and call"
142 std::cout << " step 2: find in GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
144 << " (with format DICOM V3, explicit Value"
147 std::cout << " step 3a: when image NOT found on step 2, write "
149 << " GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
151 << " (with format DICOM V3, explicit Value"
154 std::cout << " step 3b: when image found on step 2, and when IsReadable()"
156 << " compare it (in memory with memcmp) with the"
158 << " image we are testing (the one of step 1). "
159 << std::endl << std::endl;
163 while( gdcmDataImages[i] != 0 )
165 ////// Check for existence of reference baseline directory
167 std::string baseLineDir = GDCM_DATA_ROOT;
168 baseLineDir += "/BaselineDicom/";
170 std::ifstream* testDIR = new std::ifstream(baseLineDir.c_str(), std::ios::in | std::ios::binary);
173 std::cerr << " The reference baseline directory " << std::endl
175 << baseLineDir << std::endl
176 << " couldn't be opened."
185 ////// Step 1 (see above description):
186 std::string filename = GDCM_DATA_ROOT;
188 filename += gdcmDataImages[i];
190 std::string referenceFileName = baseLineDir + gdcmDataImages[i++];
191 std::string::size_type slash_pos = referenceFileName.rfind( "." );
192 if ( slash_pos != std::string::npos )
194 referenceFileName.replace( slash_pos + 1, 3, "dcm" );
197 if( InternalTest( filename, referenceFileName ) != 0 )