1 #include "gdcmHeader.h"
3 #include "gdcmDocument.h"
4 #include "gdcmValEntry.h"
10 // return true if the file exists
11 bool FileExists(const char* filename)
14 # define access _access
19 if ( access(filename, R_OK) != 0 )
29 bool RemoveFile(const char* source)
32 #define _unlink unlink
34 return unlink(source) != 0 ? false : true;
37 // Here we load a gdcmFile and then try to create from scratch a copy of it,
38 // copying field by field the dicom image
40 int main(int argc, char* argv[])
44 std::cerr << "Usage :" << std::endl <<
45 argv[0] << " input_dicom output_dicom" << std::endl;
49 if( FileExists( argv[2] ) )
51 std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
52 if( !RemoveFile( argv[2] ) )
54 std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl;
58 gdcmFile *original = new gdcmFile( argv[1] );
60 std::cout << "--- Original ----------------------" << std::endl;
61 original->GetHeader()->Print();
63 gdcmFile *copy = new gdcmFile( argv[2] );
65 //First of all copy the header field by field
67 // Warning :Accessor gdcmElementSet::GetEntry() should not exist
68 //It was commented out by Mathieu, that was a *good* idea
69 // (the user does NOT have to know the way we implemented the Header !)
71 TagDocEntryHT & Ht = original->GetHeader()->GetEntry();
73 for (TagDocEntryHT::iterator tag = Ht.begin(); tag != Ht.end(); ++tag)
75 //std::cerr << "Reading: " << tag->second->GetVR() << std::endl;
76 tag->second->Print(); std::cout << std::endl;
78 if (tag->second->GetVR() == "SQ") //to skip pb of SQ recursive exploration
80 // Well ... Should have dynamic cast here
81 copy->GetHeader()->ReplaceOrCreateByNumber(
82 ((gdcmValEntry*)(tag->second))->GetValue(),
83 tag->second->GetGroup(),
84 tag->second->GetElement() );
86 // todo : Setting Offset to 0 to avoid further missprint
89 size_t dataSize = original->GetImageDataSize();
90 void *imageData = original->GetImageData();
92 copy->SetImageData(imageData, dataSize);
94 std::cout << "--- Copy ----------------------" << std::endl;
95 std::cout <<std::endl << "DO NOT care about Offset" <<std::endl<<std::endl;;
96 copy->GetHeader()->Print();
97 std::cout << "--- ---- ----------------------" << std::endl;
99 copy->WriteDcmExplVR( argv[2] );