X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=Testing%2FTestAllReadCompareDicom.cxx;h=d2fb988f5087435919ae0942aed0b78e8483141d;hb=afb4925bd3110d67669db7264f78a238d4fb9333;hp=e2474b959ff96f08cc634e17e4f86b5bd08352a9;hpb=52051b5d16852990a4850c45541e3e88a660b305;p=gdcm.git diff --git a/Testing/TestAllReadCompareDicom.cxx b/Testing/TestAllReadCompareDicom.cxx index e2474b95..d2fb988f 100644 --- a/Testing/TestAllReadCompareDicom.cxx +++ b/Testing/TestAllReadCompareDicom.cxx @@ -1,17 +1,163 @@ -#include "gdcmHeader.h" +/*========================================================================= + + Program: gdcm + Module: $RCSfile: TestAllReadCompareDicom.cxx,v $ + Language: C++ + Date: $Date: 2005/02/03 10:00:06 $ + Version: $Revision: 1.30 $ + + Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de + l'Image). All rights reserved. See Doc/License.txt or + http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details. + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#include "gdcmDirList.h" #include "gdcmFile.h" +#include "gdcmFileHelper.h" + +#include +#include //Generated file: #include "gdcmDataImages.h" -int TestAllReadCompareDicom(int argc, char* argv[]) +int InternalTest(std::string const &filename, + std::string const &referenceFileName ) { - if ( argc > 1 ) + std::cout << " Testing: " << filename << std::endl; + + ////// Step 1: + std::cout << " 1..."; + gdcm::FileHelper *tested = new gdcm::FileHelper( filename ); + if( !tested->GetFile()->IsReadable() ) + { + std::cout << " Failed" << std::endl + << " Image not gdcm compatible:" + << filename << std::endl; + delete tested; + return 1; + } + + ////// Step 2: + ////// Check for existence of reference baseline dicom file: + std::cout << "2..."; + + std::ifstream testFILE( referenceFileName.c_str() ); + if (! testFILE ) + { + uint8_t *testedImageData = tested->GetImageData(); // Kludge + (void)testedImageData; + + tested->SetWriteModeToRGB(); + tested->WriteDcmExplVR( referenceFileName ); + } + testFILE.close(); + + ////// Step 3a: + ////// When reference file is not gdcm readable test is failed: + std::cout << "3a..."; + + gdcm::FileHelper *reference = new gdcm::FileHelper( referenceFileName ); + if( !reference->GetFile()->IsReadable() ) + { + std::cout << " Failed" << std::endl + << " reference image " + << referenceFileName + << " is not gdcm compatible." << std::endl; + delete tested; + delete reference; + return 1; + } + + std::string PixelType = reference->GetFile()->GetPixelType(); + + ////// Step 3b: + std::cout << "3b..."; + int testedDataSize = tested->GetImageDataSize(); + uint8_t *testedImageData = tested->GetImageData(); + + int referenceDataSize = reference->GetImageDataSize(); + uint8_t *referenceImageData = reference->GetImageData(); + + // Test the image size + if (tested->GetFile()->GetXSize() != reference->GetFile()->GetXSize() || + tested->GetFile()->GetYSize() != reference->GetFile()->GetYSize() || + tested->GetFile()->GetZSize() != reference->GetFile()->GetZSize()) + { + std::cout << "Failed" << std::endl + << " Size differs: " + << "X: " << tested->GetFile()->GetXSize() << " # " + << reference->GetFile()->GetXSize() << " | " + << "Y: " << tested->GetFile()->GetYSize() << " # " + << reference->GetFile()->GetYSize() << " | " + << "Z: " << tested->GetFile()->GetZSize() << " # " + << reference->GetFile()->GetZSize() << std::endl; + delete reference; + delete tested; + return 1; + } + + // Test the data size + if (testedDataSize != referenceDataSize) + { + std::cout << " Failed" << std::endl + << " pixel (" + << PixelType + <<") areas lengths differ: " + << testedDataSize << " # " << referenceDataSize + << std::endl; + delete tested; + delete reference; + return 1; + } + + // Test the data content + if (int res = memcmp(testedImageData, referenceImageData, + testedDataSize) != 0 ) + { + (void)res; + std::cout << " Failed" << std::endl + << " pixel (" + << PixelType + << ") differ (as expanded in memory)." + << std::endl; + delete tested; + delete reference; + return 1; + } + + //////////////// Clean up: + delete tested; + delete reference; + + std::cout << "OK." << std::endl; + + return 0; +} + +int TestAllReadCompareDicom(int argc, char *argv[]) +{ + if ( argc == 3 ) + { + // The test is specified a specific filename, use it instead of looping + // over all images + const std::string input = argv[1]; + const std::string reference = argv[2]; + return InternalTest( input, reference ); + } + else if ( argc > 3 || argc == 2 ) { std::cerr << " Usage: " << argv[0] << " (no arguments needed)." << std::endl; + std::cerr << "or Usage: " << argv[0] + << " filename.dcm reference.dcm" << std::endl; return 1; } + // else other cases: std::cout << " Description (Test::TestAllReadCompareDicom): " << std::endl; @@ -20,7 +166,7 @@ int TestAllReadCompareDicom(int argc, char* argv[]) << std::endl; std::cout << " apply the following to each filename.xxx: " << std::endl; - std::cout << " step 1: parse the image (as gdcmHeader) and call" + std::cout << " step 1: parse the image (as gdcmFile) and call" << " IsReadable(). " << std::endl; std::cout << " step 2: find in GDCM_DATA_ROOT/BaselineDicom/filename.dcm" @@ -43,6 +189,7 @@ int TestAllReadCompareDicom(int argc, char* argv[]) << std::endl << std::endl; int i = 0; + int result = 0; while( gdcmDataImages[i] != 0 ) { ////// Check for existence of reference baseline directory @@ -50,120 +197,33 @@ int TestAllReadCompareDicom(int argc, char* argv[]) std::string baseLineDir = GDCM_DATA_ROOT; baseLineDir += "/BaselineDicom/"; - FILE* testFILE = fopen( baseLineDir.c_str(), "r" ); - if (! testFILE ) + if( !gdcm::DirList::IsDirectory(baseLineDir) ) { - std::cerr << " The reference baseline directory " << std::endl - << " " - << baseLineDir << std::endl - << " couldn't be opened." - << std::endl; - return 1; + std::cerr << " The reference baseline directory " << std::endl + << " " + << baseLineDir << std::endl + << " couldn't be opened." + << std::endl; + return 1; } - else - fclose( testFILE ); ////// Step 1 (see above description): - std::string filename = GDCM_DATA_ROOT; - filename += "/"; //doh! + filename += "/"; filename += gdcmDataImages[i]; - - std::cout << " Testing: " << filename << std::endl; - - gdcmFile* tested = new gdcmFile( filename.c_str(), false, true ); - if( !tested->GetHeader()->IsReadable() ) - { - std::cout << " Image not gdcm compatible:" - << filename << std::endl; - delete tested; - return 1; - } - - ////// Step 2: - - ////// Check for existence of reference baseline dicom file: - + std::string referenceFileName = baseLineDir + gdcmDataImages[i++]; std::string::size_type slash_pos = referenceFileName.rfind( "." ); - if ( slash_pos != std::string::npos ) + if( slash_pos != std::string::npos ) { referenceFileName.replace( slash_pos + 1, 3, "dcm" ); } - testFILE = fopen( referenceFileName.c_str(), "r" ); - if (! testFILE ) + if( InternalTest( filename, referenceFileName ) != 0 ) { - ////// Step 3a: - - int testedDataSize = tested->GetImageDataSize(); - (void)testedDataSize; - void* testedImageData = tested->GetImageData(); // Kludge - tested->WriteDcmExplVR( referenceFileName ); - std::cerr << " Creating reference baseline file :" << std::endl - << " " << referenceFileName - << std::endl; - delete tested; - delete (char*)testedImageData; - continue; + result++; } - else - fclose( testFILE ); - - ////// When reference file is not gdcm readable test is failed: - - gdcmFile* reference = new gdcmFile( referenceFileName.c_str(), - false, true ); - if( !reference->GetHeader()->IsReadable() ) - { - std::cout << " Reference image " << std::endl - << " " << referenceFileName <GetImageDataSize(); - void* testedImageData = tested->GetImageData(); - - int referenceDataSize = reference->GetImageDataSize(); - void* referenceImageData = reference->GetImageData(); - - if (testedDataSize != referenceDataSize) - { - std::cout << " Pixel areas lengths differ: " - << testedDataSize << " # " << referenceDataSize - << std::endl; - delete tested; - delete reference; - delete (char*)testedImageData; - delete (char*)referenceImageData; - return 1; - } - - if (int res = memcmp(testedImageData, referenceImageData, - testedDataSize) != 0 ) - { - (void)res; - std::cout << " Pixel differ (as expanded in memory)." - << std::endl; - delete tested; - delete reference; - delete (char*)testedImageData; - delete (char*)referenceImageData; - return 1; - } - std::cout << " Passed." << std::endl ; - - //////////////// Clean up: - delete tested; - delete reference; - delete (char*)testedImageData; - delete (char*)referenceImageData; } - return 0; + return result; }