]> Creatis software - gdcm.git/blob - Example/exExtractTag.cxx
* Minor coding-style clean up
[gdcm.git] / Example / exExtractTag.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exExtractTag.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/18 08:35:44 $
7   Version:   $Revision: 1.2 $
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::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    std::cout << fileName << std::endl;
40 // ============================================================
41 //   Read the input image.
42 // ============================================================
43
44    f = new gdcm::File( );
45
46    //f->SetLoadMode(gdcm::LD_NOSEQ | gdcm::LD_NOSHADOW);
47    f->SetFileName( fileName );
48    bool res = f->Load();  
49
50    if( gdcm::Debug::GetDebugFlag() )
51    {
52       std::cout << "---------------------------------------------" << std::endl;
53       f->Print();
54       std::cout << "---------------------------------------------" << std::endl;
55    }
56    if (!res) {
57        std::cerr << "Sorry, " << fileName << " not a gdcm-readable "
58            << "DICOM / ACR File"
59            << std::endl;
60       delete f;
61       return 1;
62    }
63    std::cout << " ... is readable " << std::endl;
64
65    // Find the dicom tag, and extract the string
66    uint16_t group, elem;
67    std::istringstream convert;
68    convert.str( argv[2] );
69    convert >> std::hex >> group;
70    convert.clear(); //important
71    convert.str( argv[3] );
72    convert >> std::hex >> elem;
73    std::cout << "Extracting tag: (0x" << std::hex << std::setw(4) << std::setfill('0')
74      << group << ",0x" << std::setw(4) << std::setfill('0') << elem << ")" << std::endl;
75    std::string dicom_tag_value = f->GetEntryString(group, elem);
76    if (dicom_tag_value == gdcm::GDCM_UNFOUND)
77    {
78      gdcm::DictEntry *dictEntry = f->GetPubDict()->GetEntry( group, elem);
79      std::cerr << "Image doesn't contain any tag: " << dictEntry->GetName() << std::endl;
80      delete f;
81      return 1;
82    }
83
84    gdcm::DocEntry *dicom_tag_doc = f->GetDocEntry(group, elem);
85    gdcm::DataEntry *dicom_tag = dynamic_cast<gdcm::DataEntry *>(dicom_tag_doc);
86    if( !dicom_tag )
87    {
88       std::cerr << "Sorry DataEntry only please" << std::endl;
89       delete f;
90       return 1;
91    }
92
93    // Write out the data as a file:
94    std::ofstream o(argv[4]);
95    if( !o )
96    {
97       std::cerr << "Problem opening file: " << argv[4] << std::endl;
98       delete f;
99       return 1;
100    }
101    o.write((char*)dicom_tag->GetBinArea(), dicom_tag->GetLength());
102    o.close();
103
104    delete f;
105    return 0;
106 }
107
108