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 if (tag->second->GetVR() == "SQ") //to skip pb of SQ recursive exploration
78 std::string value = ((gdcmValEntry*)(tag->second))->GetValue();
79 //According to JPR I should also skip those:
80 // if (tag->second->GetVR() == "unkn") //to skip pb of SQ recursive exploration
83 // if( value.find( "gdcm::NotLoaded" ) == 0 )
86 if( value.find( "gdcm::Loaded" ) == 0 )
89 //std::cerr << "Reading: " << tag->second->GetVR() << std::endl;
90 //tag->second->Print(); std::cout << std::endl;
92 std::cerr << "Reading: " << value << std::endl;
94 // Well ... Should have dynamic cast here
95 copy->GetHeader()->ReplaceOrCreateByNumber(
97 tag->second->GetGroup(),
98 tag->second->GetElement() );
100 // todo : Setting Offset to 0 to avoid further missprint
103 size_t dataSize = original->GetImageDataSize();
104 void *imageData = original->GetImageData();
106 //copy->GetImageData();
107 copy->SetImageData(imageData, dataSize);
109 std::cout << "--- Copy ----------------------" << std::endl;
110 std::cout <<std::endl << "DO NOT care about Offset" <<std::endl<<std::endl;;
111 //copy->GetHeader()->Print();
112 std::cout << "--- ---- ----------------------" << std::endl;
114 copy->WriteDcmExplVR( argv[2] );