]> Creatis software - gdcm.git/blob - Example/exExtractTag.cxx
Fix mistypings
[gdcm.git] / Example / exExtractTag.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exExtractTag.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/06/06 13:03:09 $
7   Version:   $Revision: 1.5 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18 #include "gdcmFile.h"
19 #include "gdcmFileHelper.h"
20 #include "gdcmCommon.h"
21 #include "gdcmDebug.h"
22 #include "gdcmDocEntry.h"
23 #include "gdcmDataEntry.h"
24
25 #include <iomanip>
26
27 int main(int argc, char *argv[])
28 {  
29    GDCM_NAME_SPACE::File *f;
30  
31    if( argc < 5 )
32    {
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;
35       return 1;
36    }
37    std::string fileName = argv[1];
38    
39       // Find the dicom tag, and extract the string
40    uint16_t group, elem;
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;
47
48    std::cout << fileName << std::endl;
49 // ============================================================
50 //   Read the input image.
51 // ============================================================
52
53    f = GDCM_NAME_SPACE::File::New( );
54
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);
59       
60    bool res = f->Load();  
61
62    if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
63    {
64       std::cout << "---------------------------------------------" << std::endl;
65       f->Print();
66       std::cout << "---------------------------------------------" << std::endl;
67    }
68    if (!res) {
69        std::cerr << "Sorry, " << fileName << " not a gdcm-readable "
70            << "DICOM / ACR File"
71            << std::endl;
72       f->Delete();
73       return 1;
74    }
75    std::cout << " ... is readable " << std::endl;
76
77      // (Find the dicom tag, and) extract the string
78      
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)
83    {
84      GDCM_NAME_SPACE::DictEntry *dictEntry = f->GetPubDict()->GetEntry( group, elem);
85      std::cerr << "Image doesn't contain any tag: " << dictEntry->GetName() << std::endl;
86      f->Delete();
87      return 1;
88    }
89
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);
92    if( !dicom_tag )
93    {
94       std::cerr << "Sorry DataEntry only please" << std::endl;
95       f->Delete();
96       return 1;
97    }
98
99    // Write out the data as a file:
100    std::ofstream o(argv[4]);
101    if( !o )
102    {
103       std::cerr << "Problem opening file: " << argv[4] << std::endl;
104       f->Delete();
105       return 1;
106    }
107    o.write((char*)dicom_tag->GetBinArea(), dicom_tag->GetLength());
108    o.close();
109
110    f->Delete();
111    return 0;
112 }
113
114