]> Creatis software - gdcm.git/blob - Example/PrintDicomDir.cxx
3fd8d415a72e0895e7309ce048f8a2a2de07fac0
[gdcm.git] / Example / PrintDicomDir.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: PrintDicomDir.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/07/08 10:15:04 $
7   Version:   $Revision: 1.25 $
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 *f;
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 am;
78       return 0;
79    } 
80
81    // new gdcm2 style 
82
83    f = new gdcm::DicomDir();
84    f->SetFileName ( fileName );
85    f->Load( );
86
87    // Test if the DicomDir is readable
88    if( !f->IsReadable() )
89    {
90       std::cout<<"          DicomDir '"<<fileName
91                <<"' is not readable"<<std::endl
92                <<"          ...Failed"<<std::endl;
93
94       delete f;
95       return 1;
96    }
97
98    f->SetPrintLevel(level);
99
100    // Test if the DicomDir contains any Patient
101    pa = f->GetFirstPatient();
102    if ( pa == 0)
103    {
104       std::cout<<"          DicomDir '"<<fileName
105                <<" has no patient"<<std::endl
106                <<"          ...Failed"<<std::endl;
107
108       delete f;
109       return 1;
110    }
111
112 // Structure use Examples 
113
114    switch (detailLevel)
115   { 
116   case 1:
117      std::cout << std::endl << std::endl  
118        << " =  PATIENT List ==========================================" 
119        << std::endl<< std::endl;
120
121       pa = f->GetFirstPatient();
122       while (pa) 
123       {
124          std::cout << pa->GetEntryValue(0x0010, 0x0010) << std::endl; // Patient's Name   
125          pa = f->GetNextPatient();    
126       }
127       break;
128
129    case 2:    
130       std::cout << std::endl << std::endl  
131         << " = PATIENT/STUDY List =======================================" 
132         << std::endl<< std::endl;
133
134       pa = f->GetFirstPatient();
135       while ( pa ) // on degouline les PATIENT de ce DICOMDIR
136       {  
137          std::cout << pa->GetEntryValue(0x0010, 0x0010) << std::endl; // Patient's Name 
138
139          st = pa->GetFirstStudy();
140          while ( st ) { // on degouline les STUDY de ce patient
141             std::cout << "--- "<< st->GetEntryValue(0x0008, 0x1030) << std::endl; // Study Description
142             st = pa->GetNextStudy();
143          }
144          pa = f->GetNextPatient();    
145       }   
146       break;
147
148    case 3: 
149       std::cout << std::endl << std::endl  
150         << " =  PATIENT/STUDY/SERIE List ==================================" 
151         << std::endl<< std::endl;
152
153       pa = f->GetFirstPatient(); 
154       while ( pa )   // on degouline les PATIENT de ce DICOMDIR
155       {
156        // Patient's Name, Patient ID 
157          std::cout << "Pat.Name:[" << pa->GetEntryValue(0x0010, 0x0010) <<"]"; // Patient's Name
158          std::cout << " Pat.ID:[";
159          std::cout << pa->GetEntryValue(0x0010, 0x0020) << "]" << std::endl; // Patient ID
160
161          st = pa->GetFirstStudy();
162          while ( st ) { // on degouline les STUDY de ce patient
163             std::cout << "--- Stud.descr:["    << st->GetEntryValue(0x0008, 0x1030) << "]";// Study Description 
164             std::cout << " Stud.ID:["          << st->GetEntryValue(0x0020, 0x0010);       // Study ID
165             std::cout << "]" << std::endl;
166
167             se = st->GetFirstSerie();
168             while ( se ) { // on degouline les SERIES de cette study
169                std::cout << "--- --- Ser.Descr:["<< se->GetEntryValue(0x0008, 0x103e)<< "]";  // Series Description
170                std::cout << " Ser.nb:["         <<  se->GetEntryValue(0x0020, 0x0011);        // Series number
171                std::cout << "] Mod.:["          <<  se->GetEntryValue(0x0008, 0x0060) << "]"; // Modality
172                std::cout << std::endl;    
173                se = st->GetNextSerie();   
174             }
175             st = pa->GetNextStudy();
176          }
177          pa = f->GetNextPatient();    
178       } 
179       break;
180
181    case 4:  
182       std::cout << std::endl << std::endl  
183            << " = PATIENT/STUDY/SERIE/IMAGE List ============================" 
184            << std::endl<< std::endl;
185  
186       pa = f->GetFirstPatient(); 
187       while ( pa ) {  // les PATIENT de ce DICOMDIR
188        // Patient's Name, Patient ID 
189          std::cout << "Pat.Name:[" << pa->GetEntryValue(0x0010, 0x0010) <<"]"; // Patient's Name
190          std::cout << " Pat.ID:[";
191          std::cout << pa->GetEntryValue(0x0010, 0x0020) << "]" << std::endl; // Patient ID
192
193          st = pa->GetFirstStudy();
194          while ( st ) { // on degouline les STUDY de ce patient
195             std::cout << "--- Stud.descr:["    << st->GetEntryValue(0x0008, 0x1030) << "]";// Study Description 
196             std::cout << " Stud.ID:["          << st->GetEntryValue(0x0020, 0x0010);       // Study ID
197             std::cout << "]" << std::endl;
198
199             se = st->GetFirstSerie();
200             while ( se ) { // on degouline les SERIES de cette study
201                std::cout << "--- --- Ser.Descr:["<< se->GetEntryValue(0x0008, 0x103e)<< "]";  // Series Description
202                std::cout << " Ser.nb:["         <<  se->GetEntryValue(0x0020, 0x0011);        // Series number
203                std::cout << "] Mod.:["          <<  se->GetEntryValue(0x0008, 0x0060) << "]"; // Modality
204                std::cout << std::endl;    
205
206                im = se->GetFirstImage();
207                while ( im ) { // on degouline les Images de cette serie
208                   std::cout << "--- --- --- "<< im->GetEntryValue(0x0004, 0x1500) << std::endl; // File name
209                   im = se->GetNextImage();   
210                }
211                se = st->GetNextSerie();   
212            }
213             st = pa->GetNextStudy();
214         }     
215         pa = f->GetNextPatient();    
216       }
217       break;
218
219    case 5:
220       std::cout << std::endl << std::endl  
221            << " = DICOMDIR full content ==========================================" 
222            << std::endl<< std::endl;
223       f->Print();
224       break;
225
226    }  // end switch
227
228
229  /*
230    // Previous code.
231    // Kept as an example. Please don't remove
232  
233    gdcm::ListDicomDirPatient::const_iterator  itPatient;
234    gdcm::ListDicomDirStudy::const_iterator    itStudy;
235    gdcm::ListDicomDirSerie::const_iterator    itSerie;
236    gdcm::ListDicomDirImage::const_iterator    itImage;
237    cout << std::endl << std::endl
238         << " = Liste des PATIENT/STUDY/SERIE/IMAGE ===================================" 
239         << std::endl<< std::endl;
240  
241    itPatient = f->GetDicomDirPatients().begin();
242    while ( itPatient != f->GetDicomDirPatients().end() ) {  // on degouline les PATIENT de ce DICOMDIR
243       std::cout << (*itPatient)->GetEntryValue(0x0010, 0x0010) << std::endl; // Patient's Name
244       itStudy = ((*itPatient)->GetDicomDirStudies()).begin();
245       while (itStudy != (*itPatient)->GetDicomDirStudies().end() ) { // on degouline les STUDY de ce patient
246          std::cout << "--- "<< (*itStudy)->GetEntryValue(0x0008, 0x1030) << std::endl; // Study Description
247          itSerie = ((*itStudy)->GetDicomDirSeries()).begin();
248          while (itSerie != (*itStudy)->GetDicomDirSeries().end() ) { // on degouline les SERIES de cette study
249             std::cout << "--- --- "<< (*itSerie)->GetEntryValue(0x0008, 0x103e) << std::endl; // Serie Description
250             itImage = ((*itSerie)->GetDicomDirImages()).begin();
251             while (itImage != (*itSerie)->GetDicomDirImages().end() ) { // on degouline les IMAGES de cette serie
252                std::cout << "--- --- --- "<< (*itImage)->GetEntryValue(0x0004, 0x1500) << std::endl; // File name
253                ++itImage;   
254             }
255             ++itSerie;   
256          }
257          ++itStudy;
258       }  
259       itPatient ++;    
260    }   
261  */  
262
263
264    if(f->IsReadable())
265       std::cout <<std::endl<<fileName<<" is Readable"<<std::endl;
266    else
267       std::cout <<std::endl<<fileName<<" is NOT Readable"<<std::endl;
268    std::cout<<std::flush;
269    delete f;
270
271    return(0);
272 }