]> Creatis software - gdcm.git/blob - Example/toBrainVisa.cxx
Some cleaning in examples for extracting Overlays
[gdcm.git] / Example / toBrainVisa.cxx
1 #include "gdcmFile.h"
2 #include "gdcmFileHelper.h"
3 #include "gdcmDocument.h"
4 #include "gdcmDataEntry.h"
5 #include "gdcmArgMgr.h"
6
7    START_USAGE(usage)
8    " \n toBrainVisa :\n                                                       ",
9    "   Extracts user supplied tags (identified by group number-element number)",
10    "   and writes their values in a text file.                                ",
11    "   (file will be used as input by BrainVisa software )                    ",
12    " usage :                                                                  ",
13    " toBrainVisa      filein=inputFileName  (a DICOM file)                    ",
14    "                  fileoutout=outputFileName                               ",
15    "                  tags= list of tags to process                           ",
16    "                  [verbose] [debug]                                       ",
17    "                                                                          ",
18    " e.g. : tags=0020-0052,0008-0021,0018-1030 (no space!)                    ",
19    " dirout : will be created (as a text file) if doesn't exist               ",
20    " verbose  : user wants to run the program in 'verbose mode'               ",
21    " debug    : *developer*  wants to run the program in 'debug mode'         ",
22    FINISH_USAGE
23    
24 int main (int argc , char *argv[])
25 {
26    // ============== Initialize Arguments Manager =============================
27       
28    GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
29   
30    if (argc == 1 || am->ArgMgrDefined("usage")) 
31    {
32       am->ArgMgrUsage(usage); // Display 'usage'
33       delete am;
34       return 0;
35    }
36
37    const char *filein;   
38    filein  = am->ArgMgrGetString("filein","."); 
39
40    const char *fileout;   
41    fileout  = am->ArgMgrGetString("fileout",".");
42          
43    int verbose  = am->ArgMgrDefined("verbose");
44    
45    int nbTags;
46    uint16_t *tags = am->ArgMgrGetXInt16Enum("tags", &nbTags);   
47    
48    // if unused Param we give up
49    if ( am->ArgMgrPrintUnusedLabels() )
50    { 
51       am->ArgMgrUsage(usage);
52       delete am;
53       return 0;
54    }
55    delete am;  // we don't need Argument Manager any longer
56
57    // ==================== Begin Processing =====================================
58
59    FILE *fp;
60    fp=fopen(fileout, "w");
61    if (fp == 0)
62    {
63       std::cout << "Failed to open [" << fileout << "] for writting" << std::endl;
64       return 0;
65    }      
66    GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
67    f->SetLoadMode( GDCM_NAME_SPACE::LD_ALL);
68    f->SetFileName( filein );
69    bool res = f->Load(); 
70          
71    if (!res) 
72    {
73        std::cout << "Sorry, [" << filein << "]  not a gdcm-readable "
74                  << "DICOM / ACR File"
75                  <<std::endl;
76        f->Delete();
77        return 0;
78    }
79    if (verbose)
80       std::cout << "[" << filein << "] is readable " << std::endl;
81    
82    for(int i=0; i<nbTags; i++)
83    {
84       std::string toto = f->GetEntryString(tags[2*i], tags[2*i+1]);
85       fprintf(fp, "%s\n", toto.c_str() );
86       if (verbose)
87          std::cout << "[" << toto << "]" << std::endl;
88    }
89    
90   f->Delete();
91   fclose (fp);        
92    
93 }