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
13 #include <io.h> //for _access on Win32
16 // return true if the file exists
17 bool FileExists(const char* filename)
20 # define access _access
25 if ( access(filename, R_OK) != 0 )
35 bool RemoveFile(const char* source)
38 #define _unlink unlink
40 return unlink(source) != 0 ? false : true;
43 // Here we load a gdcmFile and then try to create from scratch a copy of it,
44 // copying field by field the dicom image
46 int TestCopyDicom(int , char* [])
49 int retVal = 0; //by default this is an error
50 while( gdcmDataImages[i] != 0 )
52 std::string filename = GDCM_DATA_ROOT;
53 filename += "/"; //doh!
54 filename += gdcmDataImages[i];
55 std::cerr << "Filename: " << filename << std::endl;
57 std::string output = "../Testing/Temporary/output.dcm";
59 if( FileExists( output.c_str() ) )
61 // std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
62 if( !RemoveFile( output.c_str() ) )
64 std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl;
69 gdcmFile *original = new gdcmFile( filename );
70 gdcmFile *copy = new gdcmFile( output );
72 TagDocEntryHT & Ht = original->GetHeader()->GetEntry();
74 size_t dataSize = original->GetImageDataSize();
75 uint8_t* imageData = original->GetImageData();
77 //First of all copy the header field by field
79 // Warning :Accessor gdcmElementSet::GetEntry() should not exist
80 // It was commented out by Mathieu, that was a *good* idea
81 // (the user does NOT have to know the way we implemented the Header !)
82 // Waiting for a 'clean' solution, I keep the method ...JPRx
86 for (TagDocEntryHT::iterator tag = Ht.begin(); tag != Ht.end(); ++tag)
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()
116 copy->SetImageData(imageData, dataSize);
117 original->GetHeader()->SetImageDataSize(dataSize);
119 copy->WriteDcmExplVR( output );
124 copy = new gdcmFile( output );
126 //Is the file written still gdcm parsable ?
127 if ( !copy->GetHeader()->IsReadable() )
130 std::cout << output << " Failed" << std::endl;