]> Creatis software - gdcm.git/blob - Example/PrintDicomDir.cxx
* Some classes inherit now from gdcm::RefCounter
[gdcm.git] / Example / PrintDicomDir.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: PrintDicomDir.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/25 14:52:27 $
7   Version:   $Revision: 1.30 $
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 "gdcmDicomDirPatient.h"
21 #include "gdcmDicomDirStudy.h"
22 #include "gdcmDicomDirVisit.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 || am->ArgMgrDefined("usage") )
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::DicomDirVisit *vs;
62    gdcm::DicomDirImage *im;
63   
64    char *fileName;
65    fileName  = am->ArgMgrWantString("filein",usage); 
66
67    int level  = am->ArgMgrGetInt("level", 2);
68
69    int detailLevel = am->ArgMgrGetInt("detail", 2);
70
71    if (am->ArgMgrDefined("debug"))
72       gdcm::Debug::DebugOn();
73
74    /* if unused Param we give up */
75    if ( am->ArgMgrPrintUnusedLabels() )
76    { 
77       am->ArgMgrUsage(usage);
78       delete am;
79       return 0;
80    } 
81
82    // new gdcm2 style 
83
84    f = gdcm::DicomDir::New();
85    f->SetFileName ( fileName );
86    f->Load( );
87
88    // Test if the DicomDir is readable
89    if( !f->IsReadable() )
90    {
91       std::cout<<"          DicomDir '"<<fileName
92                <<"' is not readable"<<std::endl
93                <<"          ...Failed"<<std::endl;
94
95       f->Delete();
96       return 1;
97    }
98
99    f->SetPrintLevel(level);
100
101    // Test if the DicomDir contains any Patient
102    pa = f->GetFirstPatient();
103    if ( pa == 0)
104    {
105       std::cout<<"          DicomDir '"<<fileName
106                <<" has no patient"<<std::endl
107                <<"          ...Failed"<<std::endl;
108
109       f->Delete();
110       return 1;
111    }
112
113 // Structure use Examples 
114
115    switch (detailLevel)
116    { 
117       case 1:
118          std::cout << std::endl << std::endl  
119          << " =  PATIENT List ==========================================" 
120          << std::endl<< std::endl;
121
122          pa = f->GetFirstPatient();
123          while (pa) 
124          {
125             std::cout << "Pat.Name:[" << pa->GetEntryString(0x0010, 0x0010) <<"]"; // Patient's Name
126             std::cout << " Pat.ID:[";
127             std::cout << pa->GetEntryString(0x0010, 0x0020) << "]" << std::endl; // Patient ID
128             pa = f->GetNextPatient();    
129          }
130          break;
131
132       case 2:    
133          std::cout << std::endl << std::endl  
134          << " = PATIENT/STUDY List =======================================" 
135          << std::endl<< std::endl;
136
137          pa = f->GetFirstPatient();
138          while ( pa ) // on degouline les PATIENT de ce DICOMDIR
139          {  
140             std::cout << "Pat.Name:[" << pa->GetEntryString(0x0010, 0x0010) <<"]"; // Patient's Name
141             std::cout << " Pat.ID:[";
142             std::cout << pa->GetEntryString(0x0010, 0x0020) << "]" << std::endl; // Patient ID
143             st = pa->GetFirstStudy();
144             while ( st ) 
145             { // on degouline les STUDY de ce patient
146                std::cout << "--- Stud.descr:["    << st->GetEntryString(0x0008, 0x1030) << "]"; // Study Description 
147                std::cout << " Stud.ID:["          << st->GetEntryString(0x0020, 0x0010) << "]"; // Study ID
148                std::cout << std::endl; 
149                st = pa->GetNextStudy();
150             }
151             pa = f->GetNextPatient();    
152          }   
153          break;
154
155       case 3: 
156          std::cout << std::endl << std::endl  
157          << " =  PATIENT/STUDY/SERIE List ==================================" 
158          << std::endl<< std::endl;
159
160          pa = f->GetFirstPatient(); 
161          while ( pa )   // on degouline les PATIENT de ce DICOMDIR
162          {
163             // Patient's Name, Patient ID 
164             std::cout << "Pat.Name:[" << pa->GetEntryString(0x0010, 0x0010) <<"]"; // Patient's Name
165             std::cout << " Pat.ID:[";
166             std::cout << pa->GetEntryString(0x0010, 0x0020) << "]" << std::endl; // Patient ID
167
168             st = pa->GetFirstStudy();
169             while ( st ) 
170             { // on degouline les STUDY de ce patient
171                std::cout << "--- Stud.descr:["    << st->GetEntryString(0x0008, 0x1030) << "]"; // Study Description 
172                std::cout << " Stud.ID:["          << st->GetEntryString(0x0020, 0x0010) << "]"; // Study ID
173                std::cout << std::endl;
174
175                se = st->GetFirstSerie();
176                while ( se ) 
177                { // on degouline les SERIES de cette study
178                   std::cout << "--- --- Ser.Descr:["<< se->GetEntryString(0x0008, 0x103e)<< "]";  // Series Description
179                   std::cout << " Ser.nb:["          << se->GetEntryString(0x0020, 0x0011);        // Series number
180                   std::cout << "] Mod.:["           << se->GetEntryString(0x0008, 0x0060) << "]"; // Modality
181                   std::cout << std::endl;    
182                   se = st->GetNextSerie();   
183                }
184
185                vs = st->GetFirstVisit();
186                while ( vs ) 
187                { // on degouline les VISIT de cette study
188                   std::cout << "--- --- VISIT: ";
189                   std::cout << " Ref. File ID :[" << vs->GetEntryString(0x0004, 0x1500) << "]"; // Referenced File ID
190                   std::cout << " Inst.Name:["    << vs->GetEntryString(0x0008,0x0080)   << "]"; // Institution Name
191                   std::cout << " Adm.ID:["      << vs->GetEntryString(0x0038, 0x0010)   << "]"; // Admission ID
192                   std::cout << " Adm. date:["   << vs->GetEntryString(0x0038, 0x0020)   << "]"; // Admitting Date
193                   std::cout << std::endl;    
194                   vs = st->GetNextVisit();    
195                }
196
197                st = pa->GetNextStudy();
198             }
199             pa = f->GetNextPatient();    
200          } 
201          break;
202
203       case 4:  
204          std::cout << std::endl << std::endl  
205          << " = PATIENT/STUDY/SERIE/IMAGE List ============================" 
206          << std::endl<< std::endl;
207
208          pa = f->GetFirstPatient(); 
209          while ( pa )
210          {  // les PATIENT de ce DICOMDIR
211             // Patient's Name, Patient ID 
212             std::cout << "Pat.Name:[" << pa->GetEntryString(0x0010, 0x0010) <<"]"; // Patient's Name
213             std::cout << " Pat.ID:[";
214             std::cout << pa->GetEntryString(0x0010, 0x0020) << "]" << std::endl; // Patient ID
215
216             st = pa->GetFirstStudy();
217             while ( st ) 
218             { // on degouline les STUDY de ce patient
219                std::cout << "--- Stud.descr:["    << st->GetEntryString(0x0008, 0x1030) << "]"; // Study Description 
220                std::cout << " Stud.ID:["          << st->GetEntryString(0x0020, 0x0010) << "]"; // Study ID
221                std::cout << std::endl;
222
223                vs = st->GetFirstVisit();
224                while ( vs ) 
225                { // on degouline les VISIT de cette study
226                   std::cout << "--- --- VISIT: ";
227                   std::cout << " Ref. File ID :[" << vs->GetEntryString(0x0004, 0x1500) << "]"; // Referenced File ID
228                   std::cout << " Inst.Name:["    << vs->GetEntryString(0x0008,0x0080)   << "]"; // Institution Name
229                   std::cout << " Adm.ID:["      << vs->GetEntryString(0x0038, 0x0010)   << "]"; // Admission ID
230                   std::cout << " Adm. date:["   << vs->GetEntryString(0x0038, 0x0020)   << "]"; // Admitting Date
231                   std::cout << std::endl;    
232                   vs = st->GetNextVisit();    
233                }
234
235                se = st->GetFirstSerie();
236                while ( se ) 
237                { // on degouline les SERIES de cette study
238                   std::cout << "--- --- Ser.Descr:["<< se->GetEntryString(0x0008, 0x103e)<< "]";  // Series Description
239                   std::cout << " Ser.nb:["         <<  se->GetEntryString(0x0020, 0x0011);        // Series number
240                   std::cout << "] Mod.:["          <<  se->GetEntryString(0x0008, 0x0060) << "]"; // Modality
241                   std::cout << std::endl;    
242
243                   im = se->GetFirstImage();
244                   while ( im ) 
245                   { // on degouline les Images de cette serie
246                      std::cout << "--- --- --- "<< " IMAGE Ref. File ID :[" << im->GetEntryString(0x0004, 0x1500) 
247                                << "]" << std::endl; // File name (Referenced File ID)
248                      im = se->GetNextImage();   
249                   }
250                   se = st->GetNextSerie();   
251                }
252                st = pa->GetNextStudy();
253             }     
254             pa = f->GetNextPatient();    
255          }
256          break;
257
258       case 5:
259          std::cout << std::endl << std::endl  
260          << " = DICOMDIR full content ==========================================" 
261          << std::endl<< std::endl;
262          f->Print();
263          break;
264
265    }  // end switch
266
267
268  /*
269    // Previous code.
270    // Kept as an example. Please don't remove
271  
272    gdcm::ListDicomDirPatient::const_iterator  itPatient;
273    gdcm::ListDicomDirStudy::const_iterator    itStudy;
274    gdcm::ListDicomDirSerie::const_iterator    itSerie;
275    gdcm::ListDicomDirImage::const_iterator    itImage;
276    cout << std::endl << std::endl
277         << " = Liste des PATIENT/STUDY/SERIE/IMAGE ===================================" 
278         << std::endl<< std::endl;
279  
280    itPatient = f->GetDicomDirPatients().begin();
281    while ( itPatient != f->GetDicomDirPatients().end() ) {  // on degouline les PATIENT de ce DICOMDIR
282       std::cout << (*itPatient)->GetEntryString(0x0010, 0x0010) << std::endl; // Patient's Name
283       itStudy = ((*itPatient)->GetDicomDirStudies()).begin();
284       while (itStudy != (*itPatient)->GetDicomDirStudies().end() ) { // on degouline les STUDY de ce patient
285          std::cout << "--- "<< (*itStudy)->GetEntryString(0x0008, 0x1030) << std::endl; // Study Description
286          itSerie = ((*itStudy)->GetDicomDirSeries()).begin();
287          while (itSerie != (*itStudy)->GetDicomDirSeries().end() ) { // on degouline les SERIES de cette study
288             std::cout << "--- --- "<< (*itSerie)->GetEntryString(0x0008, 0x103e) << std::endl; // Serie Description
289             itImage = ((*itSerie)->GetDicomDirImages()).begin();
290             while (itImage != (*itSerie)->GetDicomDirImages().end() ) { // on degouline les IMAGES de cette serie
291                std::cout << "--- --- --- "<< (*itImage)->GetEntryString(0x0004, 0x1500) << std::endl; // File name
292                ++itImage;   
293             }
294             ++itSerie;   
295          }
296          ++itStudy;
297       }  
298       itPatient ++;    
299    }   
300  */  
301
302
303    if(f->IsReadable())
304       std::cout <<std::endl<<fileName<<" is Readable"<<std::endl;
305    else
306       std::cout <<std::endl<<fileName<<" is NOT Readable"<<std::endl;
307    std::cout<<std::flush;
308    f->Delete();
309
310    return(0);
311 }