1 /*=========================================================================
4 Module: $RCSfile: exExtractTag.cxx,v $
6 Date: $Date: 2007/06/06 13:03:09 $
7 Version: $Revision: 1.5 $
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 "gdcmCommon.h"
21 #include "gdcmDebug.h"
22 #include "gdcmDocEntry.h"
23 #include "gdcmDataEntry.h"
27 int main(int argc, char *argv[])
29 GDCM_NAME_SPACE::File *f;
33 std::cerr << "Usage :" << argv[0] << " input.dcm group element outputfile" << std::endl;
34 std::cerr << " Ex: " << argv[0] << " /tmp/bla.dcm 0029 2110 /tmp/out.raw" << std::endl;
37 std::string fileName = argv[1];
39 // Find the dicom tag, and extract the string
41 std::istringstream convert;
42 convert.str( argv[2] );
43 convert >> std::hex >> group;
44 convert.clear(); //important
45 convert.str( argv[3] );
46 convert >> std::hex >> elem;
48 std::cout << fileName << std::endl;
49 // ============================================================
50 // Read the input image.
51 // ============================================================
53 f = GDCM_NAME_SPACE::File::New( );
55 //f->SetLoadMode(GDCM_NAME_SPACE::LD_NOSEQ | GDCM_NAME_SPACE::LD_NOSHADOW);
56 f->SetFileName( fileName );
57 // Make sure the Data Element will be loaded
58 f->AddForceLoadElement ( group, elem);
62 if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
64 std::cout << "---------------------------------------------" << std::endl;
66 std::cout << "---------------------------------------------" << std::endl;
69 std::cerr << "Sorry, " << fileName << " not a gdcm-readable "
75 std::cout << " ... is readable " << std::endl;
77 // (Find the dicom tag, and) extract the string
79 std::cout << "Extracting tag: (0x" << std::hex << std::setw(4) << std::setfill('0')
80 << group << ",0x" << std::setw(4) << std::setfill('0') << elem << ")" << std::endl;
81 std::string dicom_tag_value = f->GetEntryString(group, elem);
82 if (dicom_tag_value == GDCM_NAME_SPACE::GDCM_UNFOUND)
84 GDCM_NAME_SPACE::DictEntry *dictEntry = f->GetPubDict()->GetEntry( group, elem);
85 std::cerr << "Image doesn't contain any tag: " << dictEntry->GetName() << std::endl;
90 GDCM_NAME_SPACE::DocEntry *dicom_tag_doc = f->GetDocEntry(group, elem);
91 GDCM_NAME_SPACE::DataEntry *dicom_tag = dynamic_cast<GDCM_NAME_SPACE::DataEntry *>(dicom_tag_doc);
94 std::cerr << "Sorry DataEntry only please" << std::endl;
99 // Write out the data as a file:
100 std::ofstream o(argv[4]);
103 std::cerr << "Problem opening file: " << argv[4] << std::endl;
107 o.write((char*)dicom_tag->GetBinArea(), dicom_tag->GetLength());