X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=Testing%2FTestCopyDicom.cxx;h=62e1a727e9a1a68e0c51a0520786bcdf13132076;hb=879f56a62d0772c95e92d0657882eb1886b4153d;hp=0225453919dc50341f12ec1d33418a71f5663b89;hpb=4511c4982e71b3a90859cc7d7cb44e9a6cb588bc;p=gdcm.git diff --git a/Testing/TestCopyDicom.cxx b/Testing/TestCopyDicom.cxx index 02254539..62e1a727 100644 --- a/Testing/TestCopyDicom.cxx +++ b/Testing/TestCopyDicom.cxx @@ -1,5 +1,17 @@ #include "gdcmHeader.h" #include "gdcmFile.h" +#include "gdcmDocument.h" +#include "gdcmValEntry.h" +#include "gdcmBinEntry.h" + +//Generated file: +#include "gdcmDataImages.h" + +#ifndef _WIN32 +#include //for access, unlink +#else +#include //for _access on Win32 +#endif // return true if the file exists bool FileExists(const char* filename) @@ -31,49 +43,94 @@ bool RemoveFile(const char* source) // Here we load a gdcmFile and then try to create from scratch a copy of it, // copying field by field the dicom image -int TestCopyDicom(int argc, char* argv[]) +int TestCopyDicom(int , char* []) { - if (argc < 3) + int i =0; + int retVal = 0; //by default this is an error + while( gdcmDataImages[i] != 0 ) { - std::cerr << "Usage :" << std::endl << - argv[0] << " input_dicom output_dicom" << std::endl; - return 1; - } + std::string filename = GDCM_DATA_ROOT; + filename += "/"; //doh! + filename += gdcmDataImages[i]; + std::cerr << "Filename: " << filename << std::endl; - if( FileExists( argv[2] ) ) - { - std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl; - if( !RemoveFile( argv[2] ) ) + std::string output = "../Testing/Temporary/output.dcm"; + + if( FileExists( output.c_str() ) ) { - std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl; - return 1; + // std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl; + if( !RemoveFile( output.c_str() ) ) + { + std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl; + return 1; + } } - } - gdcmFile *original = new gdcmFile( argv[1] ); - gdcmFile *copy = new gdcmFile( argv[2] ); - //First of all copy the header field by field - TagNameHT & nameHt = original->GetHeader()->GetPubDict()->GetEntriesByName(); - for (TagNameHT::iterator tag = nameHt.begin(); tag != nameHt.end(); ++tag) - { - std::cerr << "Reading: " << tag->second->GetVR() << std::endl; + gdcmFile *original = new gdcmFile( filename ); + gdcmFile *copy = new gdcmFile( output ); - copy->GetHeader()->ReplaceOrCreateByNumber( tag->second->GetVR(), - tag->second->GetGroup(), tag->second->GetElement() ); - } + TagDocEntryHT & Ht = original->GetHeader()->GetEntry(); - size_t dataSize = original->GetImageDataSize(); - void *imageData = original->GetImageData(); + size_t dataSize = original->GetImageDataSize(); + uint8_t* imageData = original->GetImageData(); - copy->SetImageData(imageData, dataSize); - //original->GetHeader()->SetImageDataSize(dataSize); + //First of all copy the header field by field + + // Warning :Accessor gdcmElementSet::GetEntry() should not exist + // It was commented out by Mathieu, that was a *good* idea + // (the user does NOT have to know the way we implemented the Header !) + // Waiting for a 'clean' solution, I keep the method ...JPRx - //copy->GetHeader()->PrintEntry(); + gdcmDocEntry* d; - copy->WriteDcmExplVR( argv[2] ); + for (TagDocEntryHT::iterator tag = Ht.begin(); tag != Ht.end(); ++tag) + { + d = tag->second; + if ( gdcmBinEntry* b = dynamic_cast(d) ) + { + copy->GetHeader()->ReplaceOrCreateByNumber( + b->GetBinArea(), + b->GetLength(), + b->GetGroup(), + b->GetElement(), + b->GetVR() ); + } + else if ( gdcmValEntry* v = dynamic_cast(d) ) + { + copy->GetHeader()->ReplaceOrCreateByNumber( + v->GetValue(), + v->GetGroup(), + v->GetElement(), + v->GetVR() ); + } + else + { + // We skip pb of SQ recursive exploration + //std::cout << "Skipped Sequence " + // << "------------- " << d->GetVR() << " "<< std::hex + // << d->GetGroup() << " " << d->GetElement() + // << std::endl; + } + } - return 0; -} + copy->SetImageData(imageData, dataSize); + original->GetHeader()->SetImageDataSize(dataSize); + + copy->WriteDcmExplVR( output ); + delete original; + delete copy; + copy = new gdcmFile( output ); + + //Is the file written still gdcm parsable ? + if ( !copy->GetHeader()->IsReadable() ) + { + retVal +=1; + std::cout << output << " Failed" << std::endl; + } + i++; + } + return retVal; +}