]> Creatis software - gdcm.git/blob - Example/PrintDicomDir.cxx
Use Argument Manager in 'utilities'
[gdcm.git] / Example / PrintDicomDir.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: PrintDicomDir.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/06/07 11:12:10 $
7   Version:   $Revision: 1.22 $
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 "gdcmDocument.h"
19 #include "gdcmDicomDir.h"
20 #include "gdcmValEntry.h"
21 #include "gdcmDicomDirPatient.h"
22 #include "gdcmDicomDirStudy.h"
23 #include "gdcmDicomDirSerie.h"
24 #include "gdcmDicomDirImage.h"
25 #include "gdcmTS.h"
26 #include "gdcmDebug.h"
27
28 #include "gdcmArgMgr.h"
29
30 #include <fstream>
31 #include <iostream>
32
33 int main(int argc, char* argv[])
34 {
35    START_USAGE(usage)
36    " \n PrintDicomDir :\n",
37    " Display the tree-like structure of a DICOMDIR File",
38    " usage: PrintDicomDir filein=fileName [level=n] [debug] ",
39    "        detail = 1 : Patients, 2 : Studies, 3 : Series, 4 : Images ",
40    "                 5 : Full Content ",
41    "        level = 0,1,2 : depending on user (what he wants to see)",
42    "        debug    : user wants to run the program in 'debug mode' ",
43    FINISH_USAGE
44
45    // Initialize Arguments Manager   
46    gdcm::ArgMgr *am= new gdcm::ArgMgr(argc, argv);
47   
48    if (argc == 1) 
49    {
50       am->ArgMgrUsage(usage); // Display 'usage'
51       delete am;
52       return 0;
53    }
54   
55    gdcm::DicomDir *e1;
56    gdcm::TSKey v;
57
58    gdcm::DicomDirPatient *pa;
59    gdcm::DicomDirStudy *st;
60    gdcm::DicomDirSerie *se;
61    gdcm::DicomDirImage *im;
62   
63    char *fileName;
64    fileName  = am->ArgMgrWantString("filein",usage); 
65
66    int level  = am->ArgMgrGetInt("level", 2);
67
68    int detailLevel = am->ArgMgrGetInt("detail", 2);
69
70    if (am->ArgMgrDefined("debug"))
71       gdcm::Debug::DebugOn();
72
73    /* if unused Param we give up */
74    if ( am->ArgMgrPrintUnusedLabels() )
75    { 
76       am->ArgMgrUsage(usage);
77       delete e1;
78       delete am;
79       return 0;
80    } 
81
82    // new style is useless, since it has no effect for *reading* a DICOMDIR
83    // (only meaningfull when *creating* a DICOMDIR)
84
85    e1 = new gdcm::DicomDir( fileName );
86
87    //e1 = new gdcm::DicomDir();
88    //e1->SetParseDir(false);
89    //e1->Load(  fileName );
90
91    // Test if the DicomDir is readable
92    if( !e1->IsReadable() )
93    {
94       std::cout<<"          DicomDir '"<<fileName
95                <<"' is not readable"<<std::endl
96                <<"          ...Failed"<<std::endl;
97
98       delete e1;
99       return 1;
100    }
101
102    e1->SetPrintLevel(level);
103
104    // Test if the DicomDir contains any Patient
105    pa = e1->GetFirstPatient();
106    if ( pa  == 0)
107    {
108       std::cout<<"          DicomDir '"<<fileName
109                <<" has no patient"<<std::endl
110                <<"          ...Failed"<<std::endl;
111
112       delete e1;
113       return 1;
114    }
115
116 // Structure use Examples 
117
118    switch (detailLevel)
119   { 
120   case 1:
121      std::cout << std::endl << std::endl  
122        << " =  PATIENT List ==========================================" 
123        << std::endl<< std::endl;
124
125       pa = e1->GetFirstPatient();
126       while (pa) 
127       {
128          std::cout << pa->GetEntryValue(0x0010, 0x0010) << std::endl; // Patient's Name   
129          pa = e1->GetNextPatient();    
130       }
131       break;
132
133    case 2:    
134       std::cout << std::endl << std::endl  
135         << " = PATIENT/STUDY List =======================================" 
136         << std::endl<< std::endl;
137
138       pa = e1->GetFirstPatient();
139       while ( pa ) // on degouline les PATIENT de ce DICOMDIR
140       {  
141          std::cout << pa->GetEntryValue(0x0010, 0x0010) << std::endl; // Patient's Name 
142
143          st = pa->GetFirstStudy();
144          while ( st ) { // on degouline les STUDY de ce patient
145             std::cout << "--- "<< st->GetEntryValue(0x0008, 0x1030) << std::endl; // Study Description
146             st = pa->GetNextStudy();
147          }
148          pa = e1->GetNextPatient();    
149       }   
150       break;
151
152    case 3: 
153       std::cout << std::endl << std::endl  
154         << " =  PATIENT/STUDY/SERIE List ==================================" 
155         << std::endl<< std::endl;
156
157       pa = e1->GetFirstPatient(); 
158       while ( pa )   // on degouline les PATIENT de ce DICOMDIR
159       {
160        // Patient's Name, Patient ID 
161          std::cout << "Pat.Name:[" << pa->GetEntryValue(0x0010, 0x0010) <<"]"; // Patient's Name
162          std::cout << " Pat.ID:[";
163          std::cout << pa->GetEntryValue(0x0010, 0x0020) << "]" << std::endl; // Patient ID
164
165          st = pa->GetFirstStudy();
166          while ( st ) { // on degouline les STUDY de ce patient
167             std::cout << "--- Stud.descr:["    << st->GetEntryValue(0x0008, 0x1030) << "]";// Study Description 
168             std::cout << " Stud.ID:["          << st->GetEntryValue(0x0020, 0x0010);       // Study ID
169             std::cout << "]" << std::endl;
170
171             se = st->GetFirstSerie();
172             while ( se ) { // on degouline les SERIES de cette study
173                std::cout << "--- --- Ser.Descr:["<< se->GetEntryValue(0x0008, 0x103e)<< "]";  // Series Description
174                std::cout << " Ser.nb:["         <<  se->GetEntryValue(0x0020, 0x0011);        // Series number
175                std::cout << "] Mod.:["          <<  se->GetEntryValue(0x0008, 0x0060) << "]"; // Modality
176                std::cout << std::endl;    
177                se = st->GetNextSerie();   
178             }
179             st = pa->GetNextStudy();
180          }
181          pa = e1->GetNextPatient();    
182       } 
183       break;
184
185    case 4:  
186       std::cout << std::endl << std::endl  
187            << " = PATIENT/STUDY/SERIE/IMAGE List ============================" 
188            << std::endl<< std::endl;
189  
190       pa = e1->GetFirstPatient(); 
191       while ( pa ) {  // les PATIENT de ce DICOMDIR
192        // Patient's Name, Patient ID 
193          std::cout << "Pat.Name:[" << pa->GetEntryValue(0x0010, 0x0010) <<"]"; // Patient's Name
194          std::cout << " Pat.ID:[";
195          std::cout << pa->GetEntryValue(0x0010, 0x0020) << "]" << std::endl; // Patient ID
196
197          st = pa->GetFirstStudy();
198          while ( st ) { // on degouline les STUDY de ce patient
199             std::cout << "--- Stud.descr:["    << st->GetEntryValue(0x0008, 0x1030) << "]";// Study Description 
200             std::cout << " Stud.ID:["          << st->GetEntryValue(0x0020, 0x0010);       // Study ID
201             std::cout << "]" << std::endl;
202
203             se = st->GetFirstSerie();
204             while ( se ) { // on degouline les SERIES de cette study
205                std::cout << "--- --- Ser.Descr:["<< se->GetEntryValue(0x0008, 0x103e)<< "]";  // Series Description
206                std::cout << " Ser.nb:["         <<  se->GetEntryValue(0x0020, 0x0011);        // Series number
207                std::cout << "] Mod.:["          <<  se->GetEntryValue(0x0008, 0x0060) << "]"; // Modality
208                std::cout << std::endl;    
209
210                im = se->GetFirstImage();
211                while ( im ) { // on degouline les Images de cette serie
212                   std::cout << "--- --- --- "<< im->GetEntryValue(0x0004, 0x1500) << std::endl; // File name
213                   im = se->GetNextImage();   
214                }
215                se = st->GetNextSerie();   
216            }
217             st = pa->GetNextStudy();
218         }     
219         pa = e1->GetNextPatient();    
220       }
221       break;
222
223    case 5:
224       std::cout << std::endl << std::endl  
225            << " = DICOMDIR full content ==========================================" 
226            << std::endl<< std::endl;
227       e1->Print();
228       break;
229
230    }  // end switch
231
232
233  /*
234    // Previous code.
235    // Kept as an example. Please don't remove
236  
237    gdcm::ListDicomDirPatient::const_iterator  itPatient;
238    gdcm::ListDicomDirStudy::const_iterator    itStudy;
239    gdcm::ListDicomDirSerie::const_iterator    itSerie;
240    gdcm::ListDicomDirImage::const_iterator    itImage;
241    cout << std::endl << std::endl
242         << " = Liste des PATIENT/STUDY/SERIE/IMAGE ===================================" 
243         << std::endl<< std::endl;
244  
245    itPatient = e1->GetDicomDirPatients().begin();
246    while ( itPatient != e1->GetDicomDirPatients().end() ) {  // on degouline les PATIENT de ce DICOMDIR
247       std::cout << (*itPatient)->GetEntryValue(0x0010, 0x0010) << std::endl; // Patient's Name
248       itStudy = ((*itPatient)->GetDicomDirStudies()).begin();
249       while (itStudy != (*itPatient)->GetDicomDirStudies().end() ) { // on degouline les STUDY de ce patient
250          std::cout << "--- "<< (*itStudy)->GetEntryValue(0x0008, 0x1030) << std::endl; // Study Description
251          itSerie = ((*itStudy)->GetDicomDirSeries()).begin();
252          while (itSerie != (*itStudy)->GetDicomDirSeries().end() ) { // on degouline les SERIES de cette study
253             std::cout << "--- --- "<< (*itSerie)->GetEntryValue(0x0008, 0x103e) << std::endl; // Serie Description
254             itImage = ((*itSerie)->GetDicomDirImages()).begin();
255             while (itImage != (*itSerie)->GetDicomDirImages().end() ) { // on degouline les IMAGES de cette serie
256                std::cout << "--- --- --- "<< (*itImage)->GetEntryValue(0x0004, 0x1500) << std::endl; // File name
257                ++itImage;   
258             }
259             ++itSerie;   
260          }
261          ++itStudy;
262       }  
263       itPatient ++;    
264    }   
265  */  
266
267
268    if(e1->IsReadable())
269       std::cout <<std::endl<<fileName<<" is Readable"<<std::endl;
270    else
271       std::cout <<std::endl<<fileName<<" is NOT Readable"<<std::endl;
272    std::cout<<std::flush;
273    delete e1;
274
275    return(0);
276 }