1 #include "gdcmHeader.h"
3 #include "gdcmDocument.h"
4 #include "gdcmValEntry.h"
6 // return true if the file exists
7 bool FileExists(const char* filename)
10 # define access _access
15 if ( access(filename, R_OK) != 0 )
25 bool RemoveFile(const char* source)
28 #define _unlink unlink
30 return unlink(source) != 0 ? false : true;
33 // Here we load a gdcmFile and then try to create from scratch a copy of it,
34 // copying field by field the dicom image
36 int main(int argc, char* argv[])
40 std::cerr << "Usage :" << std::endl <<
41 argv[0] << " input_dicom output_dicom" << std::endl;
45 if( FileExists( argv[2] ) )
47 std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
48 if( !RemoveFile( argv[2] ) )
50 std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl;
54 gdcmFile *original = new gdcmFile( argv[1] );
56 std::cout << "--- Original ----------------------" << std::endl;
57 original->GetHeader()->Print();
59 gdcmFile *copy = new gdcmFile( argv[2] );
61 //First of all copy the header field by field
63 // Warning :Accessor gdcmElementSet::GetEntry() should not exist
64 //It was commented out by Mathieu, that was a *good* idea
65 // (the user does NOT have to know the way we implemented the Header !)
67 TagDocEntryHT & Ht = original->GetHeader()->GetEntry();
69 for (TagDocEntryHT::iterator tag = Ht.begin(); tag != Ht.end(); ++tag)
71 //std::cerr << "Reading: " << tag->second->GetVR() << std::endl;
72 tag->second->Print(); std::cout << std::endl;
74 if (tag->second->GetVR() == "SQ") //to skip pb of SQ recursive exploration
76 // Well ... Should have dynamic cast here
77 copy->GetHeader()->ReplaceOrCreateByNumber(
78 ((gdcmValEntry*)(tag->second))->GetValue(),
79 tag->second->GetGroup(),
80 tag->second->GetElement() );
82 // todo : Setting Offset to 0 to avoid further missprint
85 size_t dataSize = original->GetImageDataSize();
86 void *imageData = original->GetImageData();
88 copy->SetImageData(imageData, dataSize);
90 std::cout << "--- Copy ----------------------" << std::endl;
91 std::cout <<std::endl << "DO NOT care about Offset" <<std::endl<<std::endl;;
92 copy->GetHeader()->Print();
93 std::cout << "--- ---- ----------------------" << std::endl;
95 copy->WriteDcmExplVR( argv[2] );