1 /*=========================================================================
4 Module: $RCSfile: TestCopyDicom.cxx,v $
6 Date: $Date: 2004/11/16 04:28:20 $
7 Version: $Revision: 1.16 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
18 #include "gdcmHeader.h"
20 #include "gdcmDocument.h"
21 #include "gdcmValEntry.h"
22 #include "gdcmBinEntry.h"
25 #include "gdcmDataImages.h"
28 #include <unistd.h> //for access, unlink
30 #include <io.h> //for _access on Win32
33 // return true if the file exists
34 bool FileExists(const char* filename)
37 # define access _access
42 if ( access(filename, R_OK) != 0 )
52 bool RemoveFile(const char* source)
55 #define _unlink unlink
57 return unlink(source) != 0 ? false : true;
60 // Here we load a gdcmFile and then try to create from scratch a copy of it,
61 // copying field by field the dicom image
63 int TestCopyDicom(int , char* [])
66 int retVal = 0; //by default this is an error
67 while( gdcmDataImages[i] != 0 )
69 std::string filename = GDCM_DATA_ROOT;
70 filename += "/"; //doh!
71 filename += gdcmDataImages[i];
72 std::cerr << "Filename: " << filename << std::endl;
74 std::string output = "../Testing/Temporary/output.dcm";
76 if( FileExists( output.c_str() ) )
78 // std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
79 if( !RemoveFile( output.c_str() ) )
81 std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl;
86 gdcm::File *original = new gdcm::File( filename );
87 gdcm::File *copy = new gdcm::File( output );
89 const gdcm::TagDocEntryHT & Ht = original->GetHeader()->GetTagHT();
91 size_t dataSize = original->GetImageDataSize();
92 uint8_t* imageData = original->GetImageData();
94 //First of all copy the header field by field
96 // Warning :Accessor gdcmElementSet::GetEntry() should not exist
97 // It was commented out by Mathieu, that was a *good* idea
98 // (the user does NOT have to know the way we implemented the Header !)
99 // Waiting for a 'clean' solution, I keep the method ...JPRx
103 for (gdcm::TagDocEntryHT::const_iterator tag = Ht.begin(); tag != Ht.end(); ++tag)
106 if ( gdcm::BinEntry* b = dynamic_cast<gdcm::BinEntry*>(d) )
108 copy->GetHeader()->ReplaceOrCreateByNumber(
115 else if ( gdcm::ValEntry* v = dynamic_cast<gdcm::ValEntry*>(d) )
117 copy->GetHeader()->ReplaceOrCreateByNumber(
125 // We skip pb of SQ recursive exploration
126 //std::cout << "Skipped Sequence "
127 // << "------------- " << d->GetVR() << " "<< std::hex
128 // << d->GetGroup() << " " << d->GetElement()
133 copy->SetImageData(imageData, dataSize);
134 original->GetHeader()->SetImageDataSize(dataSize);
136 copy->WriteDcmExplVR( output );
141 copy = new gdcm::File( output );
143 //Is the file written still gdcm parsable ?
144 if ( !copy->GetHeader()->IsReadable() )
147 std::cout << output << " Failed" << std::endl;