1 #include "gdcmHeader.h"
3 #include "gdcmDocument.h"
4 #include "gdcmValEntry.h"
7 #include "gdcmDataImages.h"
9 // return true if the file exists
10 bool FileExists(const char* filename)
13 # define access _access
18 if ( access(filename, R_OK) != 0 )
28 bool RemoveFile(const char* source)
31 #define _unlink unlink
33 return unlink(source) != 0 ? false : true;
36 // Here we load a gdcmFile and then try to create from scratch a copy of it,
37 // copying field by field the dicom image
39 int TestCopyDicom(int , char* [])
42 int retVal = 0; //by default this is an error
43 while( gdcmDataImages[i] != 0 )
45 std::string filename = GDCM_DATA_ROOT;
46 filename += "/"; //doh!
47 filename += gdcmDataImages[i];
48 std::cerr << "Filename: " << filename << std::endl;
50 std::string output = "../Testing/Temporary/output.dcm";
52 if( FileExists( output.c_str() ) )
54 std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
55 if( !RemoveFile( output.c_str() ) )
57 std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl;
62 gdcmFile *original = new gdcmFile( filename );
63 gdcmFile *copy = new gdcmFile( output );
65 //First of all copy the header field by field
66 TagNameHT & nameHt = original->GetHeader()->GetPubDict()->GetEntriesByName();
67 for (TagNameHT::iterator tag = nameHt.begin(); tag != nameHt.end(); ++tag)
69 if (tag->second->GetVR() == "SQ") //to skip pb of SQ recursive exploration
72 //According to JPR I should also skip those:
73 // if (tag->second->GetVR() == "unkn") //to skip pb of SQ recursive exploration
76 //no clue what to do with this one
77 //std::cerr << "Reading: " << tag->second->GetVR() << std::endl;
78 std::string value = ((gdcmValEntry*)(tag->second))->GetValue();
79 // if( value.find( "gdcm::NotLoaded" ) == 0 )
82 //no clue what to do with this one
83 if( value.find( "gdcm::Loaded" ) == 0 )
86 copy->GetHeader()->ReplaceOrCreateByNumber(
88 tag->second->GetGroup(),
89 tag->second->GetElement() );
92 size_t dataSize = original->GetImageDataSize();
93 void *imageData = original->GetImageData();
95 copy->SetImageData(imageData, dataSize);
96 //original->GetHeader()->SetImageDataSize(dataSize);
98 //copy->GetHeader()->PrintEntry();
100 //copy->WriteDcmExplVR( output );
102 //Is the file written still gdcm parsable ?
103 gdcmFile check( output );
104 retVal += !check.GetHeader()->IsReadable();