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 // don't modify identation in order to let this source xdiffable with ../Test
52 std::string filename = argv[1];
53 std::string output = argv[2];
55 if( FileExists( output.c_str() ) )
57 std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
58 if( !RemoveFile( output.c_str() ) )
60 std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl;
64 gdcmFile *original = new gdcmFile( filename );
66 std::cout << "--- Original ----------------------" << std::endl;
67 //original->GetHeader()->Print();
69 gdcmFile *copy = new gdcmFile( output );
71 TagDocEntryHT & Ht = original->GetHeader()->GetEntry();
73 size_t dataSize = original->GetImageDataSize();
74 uint8_t* imageData = original->GetImageData();
76 //First of all copy the header field by field
78 // Warning :Accessor gdcmElementSet::GetEntry() should not exist
79 // It was commented out by Mathieu, that was a *good* idea
80 // (the user does NOT have to know the way we implemented the Header !)
81 // Waiting for a 'clean' solution, I keep the method ...JPRx
85 for (TagDocEntryHT::iterator tag = Ht.begin(); tag != Ht.end(); ++tag)
88 d->Print(); std::cout << std::endl;
89 if ( gdcmBinEntry* b = dynamic_cast<gdcmBinEntry*>(d) )
91 copy->GetHeader()->ReplaceOrCreateByNumber(
98 else if ( gdcmValEntry* v = dynamic_cast<gdcmValEntry*>(d) )
100 copy->GetHeader()->ReplaceOrCreateByNumber(
108 // We skip pb of SQ recursive exploration
109 //std::cout << "Skipped Sequence "
110 // << "------------- " << d->GetVR() << " "<< std::hex
111 // << d->GetGroup() << " " << d->GetElement()
120 //copy->GetImageData();
121 copy->SetImageData(imageData, dataSize);
123 std::cout << "--- Copy ----------------------" << std::endl;
124 std::cout <<std::endl << "DO NOT care about Offset" <<std::endl<<std::endl;;
125 copy->GetHeader()->Print();
126 std::cout << "--- ---- ----------------------" << std::endl;
128 copy->WriteDcmExplVR( output );