1 /*=========================================================================
4 Module: $RCSfile: TestAllReadCompareDicom.cxx,v $
6 Date: $Date: 2004/11/30 14:17:52 $
7 Version: $Revision: 1.20 $
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;
32 gdcm::File* tested = new gdcm::File( filename );
33 if( !tested->GetHeader()->IsReadable() )
35 std::cout << " Failed" << std::endl
36 << " Image not gdcm compatible:"
37 << filename << std::endl;
43 ////// Check for existence of reference baseline dicom file:
46 FILE* testFILE = fopen( referenceFileName.c_str(), "r" );
49 uint8_t* testedImageData = tested->GetImageData(); // Kludge
50 (void)testedImageData;
52 tested->SetWriteModeToRGB();
53 tested->WriteDcmExplVR( referenceFileName );
61 ////// When reference file is not gdcm readable test is failed:
64 gdcm::File* reference = new gdcm::File( referenceFileName );
65 if( !reference->GetHeader()->IsReadable() )
67 std::cout << " Failed" << std::endl
68 << " reference image "
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 // Test the image size
85 if (tested->GetHeader()->GetXSize() != reference->GetHeader()->GetXSize() ||
86 tested->GetHeader()->GetYSize() != reference->GetHeader()->GetYSize() ||
87 tested->GetHeader()->GetZSize() != reference->GetHeader()->GetZSize())
89 std::cout << "Failed" << std::endl
91 << "X: " << tested->GetHeader()->GetXSize() << " # "
92 << reference->GetHeader()->GetXSize() << " | "
93 << "Y: " << tested->GetHeader()->GetYSize() << " # "
94 << reference->GetHeader()->GetYSize() << " | "
95 << "Z: " << tested->GetHeader()->GetZSize() << " # "
96 << reference->GetHeader()->GetZSize() << std::endl;
102 // Test the data size
103 if (testedDataSize != referenceDataSize)
105 std::cout << " Failed" << std::endl
106 << " pixel areas lengths differ: "
107 << testedDataSize << " # " << referenceDataSize
114 // Test the data content
115 if (int res = memcmp(testedImageData, referenceImageData,
116 testedDataSize) != 0 )
119 std::cout << " Failed" << std::endl
120 << " pixel differ (as expanded in memory)."
127 //////////////// Clean up:
131 std::cout << "OK." << std::endl;
136 int TestAllReadCompareDicom(int argc, char* argv[])
140 // The test is specified a specific filename, use it instead of looping
142 const std::string input = argv[1];
143 const std::string reference = argv[2];
144 return InternalTest( input, reference );
146 else if ( argc > 3 || argc == 2 )
148 std::cerr << " Usage: " << argv[0]
149 << " (no arguments needed)." << std::endl;
150 std::cerr << "or Usage: " << argv[0]
151 << " filename.dcm reference.dcm" << std::endl;
156 std::cout << " Description (Test::TestAllReadCompareDicom): "
158 std::cout << " For all images in gdcmData (and not blacklisted in "
159 "Test/CMakeLists.txt)"
161 std::cout << " apply the following to each filename.xxx: "
163 std::cout << " step 1: parse the image (as gdcmHeader) and call"
166 std::cout << " step 2: find in GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
168 << " (with format DICOM V3, explicit Value"
171 std::cout << " step 3a: when image NOT found on step 2, write "
173 << " GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
175 << " (with format DICOM V3, explicit Value"
178 std::cout << " step 3b: when image found on step 2, and when IsReadable()"
180 << " compare it (in memory with memcmp) with the"
182 << " image we are testing (the one of step 1). "
183 << std::endl << std::endl;
187 while( gdcmDataImages[i] != 0 )
189 ////// Check for existence of reference baseline directory
191 std::string baseLineDir = GDCM_DATA_ROOT;
192 baseLineDir += "/BaselineDicom/";
194 std::ifstream* testDIR = new std::ifstream(baseLineDir.c_str(), std::ios::in | std::ios::binary);
197 std::cerr << " The reference baseline directory " << std::endl
199 << baseLineDir << std::endl
200 << " couldn't be opened."
209 ////// Step 1 (see above description):
210 std::string filename = GDCM_DATA_ROOT;
212 filename += gdcmDataImages[i];
214 std::string referenceFileName = baseLineDir + gdcmDataImages[i++];
215 std::string::size_type slash_pos = referenceFileName.rfind( "." );
216 if ( slash_pos != std::string::npos )
218 referenceFileName.replace( slash_pos + 1, 3, "dcm" );
221 if( InternalTest( filename, referenceFileName ) != 0 )