1 #include "gdcmHeader.h"
3 #include "gdcmDocument.h"
4 #include "gdcmValEntry.h"
5 #include "gdcmBinEntry.h"
8 #include <unistd.h> //for access, unlink
10 #include <io.h> //for _access
13 // return true if the file exists
14 bool FileExists(const char* filename)
17 # define access _access
22 if ( access(filename, R_OK) != 0 )
32 bool RemoveFile(const char* source)
35 #define _unlink unlink
37 return unlink(source) != 0 ? false : true;
40 // Here we load a gdcmFile and then try to create from scratch a copy of it,
41 // copying field by field the dicom image
43 int main(int argc, char* argv[])
47 std::cerr << "Usage :" << std::endl <<
48 argv[0] << " input_dicom output_dicom" << std::endl;
52 // don't modify identation in order to let this source xdiffable with ../Test
54 std::string filename = argv[1];
55 std::string output = argv[2];
57 if( FileExists( output.c_str() ) )
59 std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
60 if( !RemoveFile( output.c_str() ) )
62 std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl;
66 gdcmFile *original = new gdcmFile( filename );
68 std::cout << "--- Original ----------------------" << std::endl;
69 //original->GetHeader()->Print();
71 gdcmFile *copy = new gdcmFile( output );
73 TagDocEntryHT & Ht = original->GetHeader()->GetEntry();
75 size_t dataSize = original->GetImageDataSize();
76 uint8_t* imageData = original->GetImageData();
78 //First of all copy the header field by field
80 // Warning :Accessor gdcmElementSet::GetEntry() should not exist
81 // It was commented out by Mathieu, that was a *good* idea
82 // (the user does NOT have to know the way we implemented the Header !)
83 // Waiting for a 'clean' solution, I keep the method ...JPRx
87 for (TagDocEntryHT::iterator tag = Ht.begin(); tag != Ht.end(); ++tag)
90 d->Print(); std::cout << std::endl;
91 if ( gdcmBinEntry* b = dynamic_cast<gdcmBinEntry*>(d) )
93 copy->GetHeader()->ReplaceOrCreateByNumber(
100 else if ( gdcmValEntry* v = dynamic_cast<gdcmValEntry*>(d) )
102 copy->GetHeader()->ReplaceOrCreateByNumber(
110 // We skip pb of SQ recursive exploration
111 //std::cout << "Skipped Sequence "
112 // << "------------- " << d->GetVR() << " "<< std::hex
113 // << d->GetGroup() << " " << d->GetElement()
122 //copy->GetImageData();
123 copy->SetImageData(imageData, dataSize);
125 std::cout << "--- Copy ----------------------" << std::endl;
126 std::cout <<std::endl << "DO NOT care about Offset" <<std::endl<<std::endl;;
127 copy->GetHeader()->Print();
128 std::cout << "--- ---- ----------------------" << std::endl;
130 copy->WriteDcmExplVR( output );