1 /*=========================================================================
4 Module: $RCSfile: TestCopyDicom.cxx,v $
6 Date: $Date: 2005/10/25 14:52:27 $
7 Version: $Revision: 1.32 $
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 "gdcmDataEntry.h"
24 #include <unistd.h> //for access, unlink
26 #include <io.h> //for _access
29 // return true if the file exists
30 bool FileExists(const char *filename)
33 # define access _access
38 if ( access(filename, R_OK) != 0 )
48 bool RemoveFile(const char *source)
51 #define _unlink unlink
53 return unlink(source) != 0 ? false : true;
56 // Here we load a gdcmFile and then try to create from scratch a copy of it,
57 // copying field by field the dicom image
59 int main(int argc, char *argv[])
63 std::cerr << "Usage :" << std::endl <<
64 argv[0] << " input_dicom output_dicom" << std::endl;
68 // don't modify identation in order to let this source xdiffable with ../Test
69 std::string filename = argv[1];
70 std::string output = argv[2];
72 if( FileExists( output.c_str() ) )
74 std::cerr << "Don't try to cheat, I am removing the file anyway"
76 if( !RemoveFile( output.c_str() ) )
78 std::cerr << "Ouch, the file exist, but I cannot remove it"
83 gdcm::File *fileOr = gdcm::File::New();
84 fileOr->SetFileName( filename );
86 gdcm::FileHelper *original = gdcm::FileHelper::New( fileOr );
88 std::cout << "--- Original ----------------------" << std::endl;
90 gdcm::FileHelper *copy = gdcm::FileHelper::New( );
91 copy->SetFileName( output );
96 imageData = original->GetImageData(); // VERY important :
97 // brings pixels into memory !
99 std::cout << imageData << std::endl; // to avoid warning ?
101 //First of all copy the header field by field
103 gdcm::DocEntry *d = original->GetFile()->GetFirstEntry();
106 if ( gdcm::DataEntry *de = dynamic_cast<gdcm::DataEntry *>(d) )
108 copy->GetFile()->InsertEntryBinArea( de->GetBinArea(),de->GetLength(),
109 de->GetGroup(),de->GetElement(),
114 // We skip pb of SQ recursive exploration
115 std::cout << "Skipped Sequence "
116 << "------------- " << d->GetVR() << " "<< std::hex
117 << d->GetGroup() << "," << d->GetElement()
121 d=original->GetFile()->GetNextEntry();
124 std::cout << "--- Copy ----------------------" << std::endl;
125 std::cout <<std::endl << "DO NOT care about Offset"
126 <<std::endl << std::endl;;
127 copy->GetFile()->Print();
128 std::cout << "--- ---- ----------------------" << std::endl;
130 copy->WriteDcmExplVR( output );
132 fileOr->Delete(); // File
133 original->Delete(); // FileHelper
134 copy->Delete(); // FileHelper