1 #include "gdcmHeader.h"
3 #include "gdcmDocument.h"
4 #include "gdcmValEntry.h"
5 #include "gdcmBinEntry.h"
8 #include <unistd.h> //for access, unlink
11 // return true if the file exists
12 bool FileExists(const char* filename)
15 # define access _access
20 if ( access(filename, R_OK) != 0 )
30 bool RemoveFile(const char* source)
33 #define _unlink unlink
35 return unlink(source) != 0 ? false : true;
38 // Here we load a gdcmFile and then try to create from scratch a copy of it,
39 // copying field by field the dicom image
41 int main(int argc, char* argv[])
45 std::cerr << "Usage :" << std::endl <<
46 argv[0] << " input_dicom output_dicom" << std::endl;
50 std::string filename = argv[1];
51 std::string output = argv[2];
53 if( FileExists( output.c_str() ) )
55 std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
56 if( !RemoveFile( output.c_str() ) )
58 std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl;
62 gdcmFile *original = new gdcmFile( filename );
64 std::cout << "--- Original ----------------------" << std::endl;
65 //original->GetHeader()->Print();
67 gdcmFile *copy = new gdcmFile( output );
69 TagDocEntryHT & Ht = original->GetHeader()->GetEntry();
71 size_t dataSize = original->GetImageDataSize();
72 void *imageData = original->GetImageData();
74 //First of all copy the header field by field
76 // Warning :Accessor gdcmElementSet::GetEntry() should not exist
77 // It was commented out by Mathieu, that was a *good* idea
78 // (the user does NOT have to know the way we implemented the Header !)
79 // Waiting for a 'clean' solution, I keep the method ...JPRx
83 for (TagDocEntryHT::iterator tag = Ht.begin(); tag != Ht.end(); ++tag)
86 d->Print(); std::cout << std::endl;
87 if ( gdcmBinEntry* b = dynamic_cast<gdcmBinEntry*>(d) )
89 copy->GetHeader()->ReplaceOrCreateByNumber(
96 else if ( gdcmValEntry* v = dynamic_cast<gdcmValEntry*>(d) )
98 copy->GetHeader()->ReplaceOrCreateByNumber(
106 // We skip pb of SQ recursive exploration
107 //std::cout << "Skipped Sequence "
108 // << "------------- " << d->GetVR() << " "<< std::hex
109 // << d->GetGroup() << " " << d->GetElement()
118 //copy->GetImageData();
119 copy->SetImageData(imageData, dataSize);
121 std::cout << "--- Copy ----------------------" << std::endl;
122 std::cout <<std::endl << "DO NOT care about Offset" <<std::endl<<std::endl;;
123 copy->GetHeader()->Print();
124 std::cout << "--- ---- ----------------------" << std::endl;
126 copy->WriteDcmExplVR( output );