1 /*=========================================================================
4 Module: $RCSfile: TestCopyDicom.cxx,v $
6 Date: $Date: 2005/07/19 15:19:25 $
7 Version: $Revision: 1.26 $
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 =========================================================================*/
19 #include "gdcmFileHelper.h"
20 #include "gdcmDocument.h"
21 #include "gdcmValEntry.h"
22 #include "gdcmBinEntry.h"
25 #include <unistd.h> //for access, unlink
27 #include <io.h> //for _access
30 // return true if the file exists
31 bool FileExists(const char *filename)
34 # define access _access
39 if ( access(filename, R_OK) != 0 )
49 bool RemoveFile(const char *source)
52 #define _unlink unlink
54 return unlink(source) != 0 ? false : true;
57 // Here we load a gdcmFile and then try to create from scratch a copy of it,
58 // copying field by field the dicom image
60 int main(int argc, char *argv[])
64 std::cerr << "Usage :" << std::endl <<
65 argv[0] << " input_dicom output_dicom" << std::endl;
69 // don't modify identation in order to let this source xdiffable with ../Test
71 std::string filename = argv[1];
72 std::string output = argv[2];
74 if( FileExists( output.c_str() ) )
76 std::cerr << "Don't try to cheat, I am removing the file anyway" << std::endl;
77 if( !RemoveFile( output.c_str() ) )
79 std::cerr << "Ouch, the file exist, but I cannot remove it" << std::endl;
83 gdcm::File *fileOr = new gdcm::File();
84 fileOr->SetFileName( filename );
86 gdcm::FileHelper *original = new gdcm::FileHelper( fileOr );
88 std::cout << "--- Original ----------------------" << std::endl;
89 //original->GetFile()->Print();
91 gdcm::FileHelper *copy = new gdcm::FileHelper( );
92 copy->SetFileName( output );
95 size_t dataSize = original->GetImageDataSize();
96 uint8_t *imageData = original->GetImageData();
100 //First of all copy the header field by field
102 gdcm::DocEntry *d = original->GetFile()->GetFirstEntry();
105 if ( gdcm::BinEntry *b = dynamic_cast<gdcm::BinEntry*>(d) )
107 copy->GetFile()->InsertBinEntry( b->GetBinArea(),b->GetLength(),
108 b->GetGroup(),b->GetElement(),
111 else if ( gdcm::ValEntry *v = dynamic_cast<gdcm::ValEntry*>(d) )
113 copy->GetFile()->InsertValEntry( v->GetValue(),
114 v->GetGroup(),v->GetElement(),
119 // We skip pb of SQ recursive exploration
120 std::cout << "Skipped Sequence "
121 << "------------- " << d->GetVR() << " "<< std::hex
122 << d->GetGroup() << "," << d->GetElement()
126 d=original->GetFile()->GetNextEntry();
129 //copy->GetImageData();
130 //copy->SetImageData(imageData, dataSize);
132 std::cout << "--- Copy ----------------------" << std::endl;
133 std::cout <<std::endl << "DO NOT care about Offset" <<std::endl<<std::endl;;
134 copy->GetFile()->Print();
135 std::cout << "--- ---- ----------------------" << std::endl;
137 copy->WriteDcmExplVR( output );