1 /*=========================================================================
4 Module: $RCSfile: TestAllReadCompareDicom.cxx,v $
6 Date: $Date: 2004/12/03 20:43:36 $
7 Version: $Revision: 1.22 $
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"
25 #include "gdcmDataImages.h"
27 int InternalTest(std::string const & filename,
28 std::string const & referenceFileName )
30 std::cout << " Testing: " << filename << std::endl;
34 gdcm::File* tested = new gdcm::File( filename );
35 if( !tested->GetHeader()->IsReadable() )
37 std::cout << " Failed" << std::endl
38 << " Image not gdcm compatible:"
39 << filename << std::endl;
45 ////// Check for existence of reference baseline dicom file:
48 //FILE* testFILE = fopen( referenceFileName.c_str(), "r" );
49 std::ifstream testFILE( referenceFileName.c_str() );
52 uint8_t* testedImageData = tested->GetImageData(); // Kludge
53 (void)testedImageData;
55 tested->SetWriteModeToRGB();
56 tested->WriteDcmExplVR( referenceFileName );
65 ////// When reference file is not gdcm readable test is failed:
68 gdcm::File* reference = new gdcm::File( referenceFileName );
69 if( !reference->GetHeader()->IsReadable() )
71 std::cout << " Failed" << std::endl
72 << " reference image "
74 << " is not gdcm compatible." << std::endl;
82 int testedDataSize = tested->GetImageDataSize();
83 uint8_t* testedImageData = tested->GetImageData();
85 int referenceDataSize = reference->GetImageDataSize();
86 uint8_t* referenceImageData = reference->GetImageData();
88 // Test the image size
89 if (tested->GetHeader()->GetXSize() != reference->GetHeader()->GetXSize() ||
90 tested->GetHeader()->GetYSize() != reference->GetHeader()->GetYSize() ||
91 tested->GetHeader()->GetZSize() != reference->GetHeader()->GetZSize())
93 std::cout << "Failed" << std::endl
95 << "X: " << tested->GetHeader()->GetXSize() << " # "
96 << reference->GetHeader()->GetXSize() << " | "
97 << "Y: " << tested->GetHeader()->GetYSize() << " # "
98 << reference->GetHeader()->GetYSize() << " | "
99 << "Z: " << tested->GetHeader()->GetZSize() << " # "
100 << reference->GetHeader()->GetZSize() << std::endl;
106 // Test the data size
107 if (testedDataSize != referenceDataSize)
109 std::cout << " Failed" << std::endl
110 << " pixel areas lengths differ: "
111 << testedDataSize << " # " << referenceDataSize
118 // Test the data content
119 if (int res = memcmp(testedImageData, referenceImageData,
120 testedDataSize) != 0 )
123 std::cout << " Failed" << std::endl
124 << " pixel differ (as expanded in memory)."
131 //////////////// Clean up:
135 std::cout << "OK." << std::endl;
140 int TestAllReadCompareDicom(int argc, char* argv[])
144 // The test is specified a specific filename, use it instead of looping
146 const std::string input = argv[1];
147 const std::string reference = argv[2];
148 return InternalTest( input, reference );
150 else if ( argc > 3 || argc == 2 )
152 std::cerr << " Usage: " << argv[0]
153 << " (no arguments needed)." << std::endl;
154 std::cerr << "or Usage: " << argv[0]
155 << " filename.dcm reference.dcm" << std::endl;
160 std::cout << " Description (Test::TestAllReadCompareDicom): "
162 std::cout << " For all images in gdcmData (and not blacklisted in "
163 "Test/CMakeLists.txt)"
165 std::cout << " apply the following to each filename.xxx: "
167 std::cout << " step 1: parse the image (as gdcmHeader) and call"
170 std::cout << " step 2: find in GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
172 << " (with format DICOM V3, explicit Value"
175 std::cout << " step 3a: when image NOT found on step 2, write "
177 << " GDCM_DATA_ROOT/BaselineDicom/filename.dcm"
179 << " (with format DICOM V3, explicit Value"
182 std::cout << " step 3b: when image found on step 2, and when IsReadable()"
184 << " compare it (in memory with memcmp) with the"
186 << " image we are testing (the one of step 1). "
187 << std::endl << std::endl;
191 while( gdcmDataImages[i] != 0 )
193 ////// Check for existence of reference baseline directory
195 std::string baseLineDir = GDCM_DATA_ROOT;
196 baseLineDir += "/BaselineDicom/";
198 std::ifstream* testDIR = new std::ifstream(baseLineDir.c_str(), std::ios::in | std::ios::binary);
201 std::cerr << " The reference baseline directory " << std::endl
203 << baseLineDir << std::endl
204 << " couldn't be opened."
213 ////// Step 1 (see above description):
214 std::string filename = GDCM_DATA_ROOT;
216 filename += gdcmDataImages[i];
218 std::string referenceFileName = baseLineDir + gdcmDataImages[i++];
219 std::string::size_type slash_pos = referenceFileName.rfind( "." );
220 if ( slash_pos != std::string::npos )
222 referenceFileName.replace( slash_pos + 1, 3, "dcm" );
225 if( InternalTest( filename, referenceFileName ) != 0 )