#include "gdcm.h" #include "gdcmHeader.h" #include "gdcmDictEntry.h" #include "gdcmDocEntry.h" #include "gdcmValEntry.h" int TestFromScratch(int argc, char *argv[]) { gdcm::File *f1 = new gdcm::File( "/home/malaterre/Creatis/gdcmData/012345.002.050.dcm" ); gdcm::Header *h1 = f1->GetHeader(); int dataSize = f1->GetImageDataSize(); std::cout << "DataSize: " << dataSize << std::endl; // Since we know the image is 16bits: uint8_t* imageData = f1->GetImageData(); // Hopefully default to something gdcm::Header *h2 = new gdcm::Header(); const gdcm::TagDocEntryHT &nameHt = h1->GetTagHT(); //const gdcm::TagDocEntryHT &nameHt = h2->GetTagHT(); seems to be empty ?? for (gdcm::TagDocEntryHT::const_iterator tag = nameHt.begin(); tag != nameHt.end(); ++tag) { //Copy string only: if (tag->second->GetVR().find("SQ") == 0) { // skip DICOM SeQuence, otherwise following cast will crash continue; } std::string temp = ((gdcm::ValEntry*)(tag->second))->GetValue(); if( tag->second->GetName() != "unkn" && temp.find( "gdcm::NotLoaded" ) != 0 && temp.find( "gdcm::Binary" ) != 0 && temp.find( "gdcm::Loaded" ) != 0 ) { const std::string &name = tag->second->GetName(); const std::string &value = ((gdcm::ValEntry*)(tag->second))->GetValue(); std::cout << name << "," << value << std::endl; gdcm::DictEntry *dictEntry = h2->GetPubDict()->GetDictEntryByName(name); h2->ReplaceOrCreateByNumber( value, dictEntry->GetGroup(), dictEntry->GetElement()); } } h2->Print( std::cout ); gdcm::File *f2 = new gdcm::File( h2 ); f2->SetImageData(imageData, dataSize); f2->WriteDcmExplVR( "output.dcm" ); delete f1; delete f2; delete h2; return 0; }