1 #include "gdcmHeader.h"
3 #include "gdcmDocument.h"
4 #include "gdcmValEntry.h"
5 #include "gdcmBinEntry.h"
8 #include "gdcmDataImages.h"
11 #include <unistd.h> //for access, unlink
14 // return true if the file exists
15 bool FileExists(const char* filename)
18 # define access _access
23 if ( access(filename, R_OK) != 0 )
33 bool RemoveFile(const char* source)
36 #define _unlink unlink
38 return unlink(source) != 0 ? false : true;
41 // Here we load a gdcmFile and then try to create from scratch a copy of it,
42 // copying field by field the dicom image
44 int TestCopyDicom(int , char* [])
47 int retVal = 0; //by default this is an error
48 while( gdcmDataImages[i] != 0 )
50 std::string filename = GDCM_DATA_ROOT;
51 filename += "/"; //doh!
52 filename += gdcmDataImages[i];
53 std::cerr << "Filename: " << filename << std::endl;
55 std::string output = "../Testing/Temporary/output.dcm";
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;
67 gdcmFile *original = new gdcmFile( filename );
68 gdcmFile *copy = new gdcmFile( output );
70 TagDocEntryHT & Ht = original->GetHeader()->GetEntry();
72 size_t dataSize = original->GetImageDataSize();
73 uint8_t* imageData = original->GetImageData();
75 //First of all copy the header field by field
77 // Warning :Accessor gdcmElementSet::GetEntry() should not exist
78 // It was commented out by Mathieu, that was a *good* idea
79 // (the user does NOT have to know the way we implemented the Header !)
80 // Waiting for a 'clean' solution, I keep the method ...JPRx
84 for (TagDocEntryHT::iterator tag = Ht.begin(); tag != Ht.end(); ++tag)
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()
114 copy->SetImageData(imageData, dataSize);
115 original->GetHeader()->SetImageDataSize(dataSize);
117 copy->WriteDcmExplVR( output );
122 copy = new gdcmFile( output );
124 //Is the file written still gdcm parsable ?
125 if ( !copy->GetHeader()->IsReadable() )
128 std::cout << output << " Failed" << std::endl;