]> Creatis software - gdcm.git/blobdiff - Example/PrintDicomDir.cxx
Unify user interface
[gdcm.git] / Example / PrintDicomDir.cxx
index 89ba25c00246e9f7a436814162d2065f83864979..7025c18d1b050e9a59c62a40465f464184a70b89 100644 (file)
-#include <iostream>
-#include "gdcm.h"
+/*=========================================================================
+                                                                                
+  Program:   gdcm
+  Module:    $RCSfile: PrintDicomDir.cxx,v $
+  Language:  C++
+  Date:      $Date: 2005/07/21 04:55:50 $
+  Version:   $Revision: 1.28 $
+                                                                                
+  Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
+  l'Image). All rights reserved. See Doc/License.txt or
+  http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
+                                                                                
+     This software is distributed WITHOUT ANY WARRANTY; without even
+     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+     PURPOSE.  See the above copyright notices for more information.
+                                                                                
+=========================================================================*/
 #include "gdcmDocument.h"
 #include "gdcmDicomDir.h"
 #include "gdcmValEntry.h"
+#include "gdcmDicomDirPatient.h"
+#include "gdcmDicomDirStudy.h"
+#include "gdcmDicomDirVisit.h"
+#include "gdcmDicomDirSerie.h"
+#include "gdcmDicomDirImage.h"
+#include "gdcmTS.h"
+#include "gdcmDebug.h"
 
-#include <fstream>
+#include "gdcmArgMgr.h"
 
-using namespace std;
+#include <fstream>
+#include <iostream>
 
 int main(int argc, char* argv[])
-{  
-   gdcmDicomDir *e1;
-   
-   ListDicomDirPatient::iterator  itPatient;
-   ListDicomDirStudy::iterator    itStudy;
-   ListDicomDirSerie::iterator    itSerie;
-   ListDicomDirImage::iterator    itImage;
-   TSKey v;
-    
-   std::string file; 
-   if (argc > 1) 
-      file = argv[1];    
-   else {
-      file += GDCM_DATA_ROOT;
-      file += "/DICOMDIR";
+{
+   START_USAGE(usage)
+   " \n PrintDicomDir :\n",
+   " Display the tree-like structure of a DICOMDIR File",
+   " usage: PrintDicomDir filein=fileName [level=n] [debug] ",
+   "        detail = 1 : Patients, 2 : Studies, 3 : Series, 4 : Images ",
+   "                 5 : Full Content ",
+   "        level = 0,1,2 : depending on user (what he wants to see)",
+   "        debug    : user wants to run the program in 'debug mode' ",
+   FINISH_USAGE
+
+   // Initialize Arguments Manager   
+   gdcm::ArgMgr *am= new gdcm::ArgMgr(argc, argv);
+  
+   if (argc == 1 || am->ArgMgrDefined("usage") )
+   {
+      am->ArgMgrUsage(usage); // Display 'usage'
+      delete am;
+      return 0;
    }
+  
+   gdcm::DicomDir *f;
+   gdcm::TSKey v;
+
+   gdcm::DicomDirPatient *pa;
+   gdcm::DicomDirStudy *st;
+   gdcm::DicomDirSerie *se;
+   gdcm::DicomDirVisit *vs;
+   gdcm::DicomDirImage *im;
+  
+   char *fileName;
+   fileName  = am->ArgMgrWantString("filein",usage); 
+
+   int level  = am->ArgMgrGetInt("level", 2);
+
+   int detailLevel = am->ArgMgrGetInt("detail", 2);
+
+   if (am->ArgMgrDefined("debug"))
+      gdcm::Debug::DebugOn();
+
+   /* if unused Param we give up */
+   if ( am->ArgMgrPrintUnusedLabels() )
+   { 
+      am->ArgMgrUsage(usage);
+      delete am;
+      return 0;
+   } 
+
+   // new gdcm2 style 
 
-   e1 = new gdcmDicomDir( file );
+   f = new gdcm::DicomDir();
+   f->SetFileName ( fileName );
+   f->Load( );
 
-   if (argc > 2) {
-      int level = atoi(argv[2]);   
-      e1->SetPrintLevel(level);
+   // Test if the DicomDir is readable
+   if( !f->IsReadable() )
+   {
+      std::cout<<"          DicomDir '"<<fileName
+               <<"' is not readable"<<std::endl
+               <<"          ...Failed"<<std::endl;
+
+      delete f;
+      return 1;
    }
 
-   if(e1->GetDicomDirPatients().begin() == e1->GetDicomDirPatients().end() )
+   f->SetPrintLevel(level);
+
+   // Test if the DicomDir contains any Patient
+   pa = f->GetFirstPatient();
+   if ( pa == 0)
    {
-      std::cout<<"Empty list"<<std::endl;
-      return(1);
+      std::cout<<"          DicomDir '"<<fileName
+               <<" has no patient"<<std::endl
+               <<"          ...Failed"<<std::endl;
+
+      delete f;
+      return 1;
    }
 
 // Structure use Examples 
-// DON'T REMOVE neither the following code, nor the commented out lines
 
-/*
-  cout << std::endl << std::endl  
+   switch (detailLevel)
+  { 
+  case 1:
+     std::cout << std::endl << std::endl  
        << " =  PATIENT List ==========================================" 
        << std::endl<< std::endl;
 
-   itPatient = e1->GetDicomDirPatients().begin();
-   while ( itPatient != e1->GetDicomDirPatients().end() ) {  // on degouline la liste de PATIENT
-      std::cout << (*itPatient)->GetEntryByNumber(0x0010, 0x0010) << std::endl; // Patient's Name   
-      itPatient ++;    
-   }
-*/
+      pa = f->GetFirstPatient();
+      while (pa) 
+      {
+         std::cout << "Pat.Name:[" << pa->GetEntryValue(0x0010, 0x0010) <<"]"; // Patient's Name
+         std::cout << " Pat.ID:[";
+         std::cout << pa->GetEntryValue(0x0010, 0x0020) << "]" << std::endl; // Patient ID
+         pa = f->GetNextPatient();    
+      }
+      break;
 
-/*       
-   cout << std::endl << std::endl  
+   case 2:    
+      std::cout << std::endl << std::endl  
         << " = PATIENT/STUDY List =======================================" 
         << std::endl<< std::endl;
 
-   itPatient = e1->GetDicomDirPatients().begin();
-   while ( itPatient != e1->GetDicomDirPatients().end() ) {  // on degouline la liste de PATIENT
-      std::cout << (*itPatient)->GetEntryByNumber(0x0010, 0x0010) << std::endl; // Patient's Name 
-      itStudy = ((*itPatient)->GetDicomDirStudies()).begin();
-      while (itStudy != (*itPatient)->GetDicomDirStudies().end() ) { // on degouline les STUDY de ce patient
-         std::cout << "--- "<< (*itStudy)->GetEntryByNumber(0x0008, 0x1030) << std::endl; // Study Description
-         ++itStudy;
-      }
-      itPatient ++;    
-   }   
-
-*/
+      pa = f->GetFirstPatient();
+      while ( pa ) // on degouline les PATIENT de ce DICOMDIR
+      {  
+         std::cout << "Pat.Name:[" << pa->GetEntryValue(0x0010, 0x0010) <<"]"; // Patient's Name
+         std::cout << " Pat.ID:[";
+         std::cout << pa->GetEntryValue(0x0010, 0x0020) << "]" << std::endl; // Patient ID
+         st = pa->GetFirstStudy();
+         while ( st ) { // on degouline les STUDY de ce patient
+            std::cout << "--- Stud.descr:["    << st->GetEntryValue(0x0008, 0x1030) << "]"; // Study Description 
+            std::cout << " Stud.ID:["          << st->GetEntryValue(0x0020, 0x0010) << "]"; // Study ID
+            std::cout << std::endl; 
+            st = pa->GetNextStudy();
+         }
+         pa = f->GetNextPatient();    
+      }   
+      break;
 
-   cout << std::endl << std::endl  
+   case 3: 
+      std::cout << std::endl << std::endl  
         << " =  PATIENT/STUDY/SERIE List ==================================" 
         << std::endl<< std::endl;
-   itPatient = e1->GetDicomDirPatients().begin();
-   while ( itPatient != e1->GetDicomDirPatients().end() ) {  // on degouline la liste de PATIENT
+
+      pa = f->GetFirstPatient(); 
+      while ( pa )   // on degouline les PATIENT de ce DICOMDIR
+      {
        // Patient's Name, Patient ID 
-      std::cout << "Pat.Name:[" << (*itPatient)->GetEntryByNumber(0x0010, 0x0010) <<"]"; // Patient's Name
-      std::cout << " Pat.ID:[";
-      std::cout << (*itPatient)->GetEntryByNumber(0x0010, 0x0020) << "]" << std::endl; // Patient ID
-      itStudy = ((*itPatient)->GetDicomDirStudies()).begin();
-      while (itStudy != (*itPatient)->GetDicomDirStudies().end() ) { // on degouline les STUDY de ce patient
-         std::cout << "--- Stud.descr:["    << (*itStudy)->GetEntryByNumber(0x0008, 0x1030) << "]";// Study Description 
-         std::cout << " Stud.ID:["          << (*itStudy)->GetEntryByNumber(0x0020, 0x0010);       // Study ID
-         std::cout << "]" << std::endl;
-         itSerie = ((*itStudy)->GetDicomDirSeries()).begin();
-         while (itSerie != (*itStudy)->GetDicomDirSeries().end() ) { // on degouline les SERIES de cette study
-            std::cout << "--- --- Ser.Descr:["<< (*itSerie)->GetEntryByNumber(0x0008, 0x103e)<< "]";  // Series Description
-            std::cout << " Ser.nb:["         <<  (*itSerie)->GetEntryByNumber(0x0020, 0x0011);        // Series number
-            std::cout << "] Mod.:["          <<  (*itSerie)->GetEntryByNumber(0x0008, 0x0060) << "]"; // Modality
-            std::cout << std::endl;    
-            ++itSerie;   
+         std::cout << "Pat.Name:[" << pa->GetEntryValue(0x0010, 0x0010) <<"]"; // Patient's Name
+         std::cout << " Pat.ID:[";
+         std::cout << pa->GetEntryValue(0x0010, 0x0020) << "]" << std::endl; // Patient ID
+
+         st = pa->GetFirstStudy();
+         while ( st ) { // on degouline les STUDY de ce patient
+            std::cout << "--- Stud.descr:["    << st->GetEntryValue(0x0008, 0x1030) << "]"; // Study Description 
+            std::cout << " Stud.ID:["          << st->GetEntryValue(0x0020, 0x0010) << "]"; // Study ID
+            std::cout << std::endl;
+
+            se = st->GetFirstSerie();
+            while ( se ) { // on degouline les SERIES de cette study
+               std::cout << "--- --- Ser.Descr:["<< se->GetEntryValue(0x0008, 0x103e)<< "]";  // Series Description
+               std::cout << " Ser.nb:["          << se->GetEntryValue(0x0020, 0x0011);        // Series number
+               std::cout << "] Mod.:["           << se->GetEntryValue(0x0008, 0x0060) << "]"; // Modality
+               std::cout << std::endl;    
+               se = st->GetNextSerie();   
+            }
+  
+            vs = st->GetFirstVisit();
+            while ( vs ) { // on degouline les VISIT de cette study
+               std::cout << "--- --- VISIT: ";
+               std::cout << " Ref. File ID :[" << vs->GetEntryValue(0x0004, 0x1500) << "]"; // Referenced File ID
+               std::cout << " Inst.Name:["    << vs->GetEntryValue(0x0008,0x0080)   << "]"; // Institution Name
+               std::cout << " Adm.ID:["      << vs->GetEntryValue(0x0038, 0x0010)   << "]"; // Admission ID
+               std::cout << " Adm. date:["   << vs->GetEntryValue(0x0038, 0x0020)   << "]"; // Admitting Date
+               std::cout << std::endl;    
+               vs = st->GetNextVisit();    
+            }
+
+            st = pa->GetNextStudy();
          }
-         ++itStudy;
-      }
-      itPatient ++;    
-   } 
+         pa = f->GetNextPatient();    
+      } 
+      break;
+
+   case 4:  
+      std::cout << std::endl << std::endl  
+           << " = PATIENT/STUDY/SERIE/IMAGE List ============================" 
+           << std::endl<< std::endl;
+      pa = f->GetFirstPatient(); 
+      while ( pa ) {  // les PATIENT de ce DICOMDIR
+       // Patient's Name, Patient ID 
+         std::cout << "Pat.Name:[" << pa->GetEntryValue(0x0010, 0x0010) <<"]"; // Patient's Name
+         std::cout << " Pat.ID:[";
+         std::cout << pa->GetEntryValue(0x0010, 0x0020) << "]" << std::endl; // Patient ID
+
+         st = pa->GetFirstStudy();
+         while ( st ) { // on degouline les STUDY de ce patient
+            std::cout << "--- Stud.descr:["    << st->GetEntryValue(0x0008, 0x1030) << "]"; // Study Description 
+            std::cout << " Stud.ID:["          << st->GetEntryValue(0x0020, 0x0010) << "]"; // Study ID
+            std::cout << std::endl;
   
-    
+            vs = st->GetFirstVisit();
+            while ( vs ) { // on degouline les VISIT de cette study
+               std::cout << "--- --- VISIT: ";
+               std::cout << " Ref. File ID :[" << vs->GetEntryValue(0x0004, 0x1500) << "]"; // Referenced File ID
+               std::cout << " Inst.Name:["    << vs->GetEntryValue(0x0008,0x0080)   << "]"; // Institution Name
+               std::cout << " Adm.ID:["      << vs->GetEntryValue(0x0038, 0x0010)   << "]"; // Admission ID
+               std::cout << " Adm. date:["   << vs->GetEntryValue(0x0038, 0x0020)   << "]"; // Admitting Date
+               std::cout << std::endl;    
+               vs = st->GetNextVisit();    
+            }
+
+            se = st->GetFirstSerie();
+            while ( se ) { // on degouline les SERIES de cette study
+               std::cout << "--- --- Ser.Descr:["<< se->GetEntryValue(0x0008, 0x103e)<< "]";  // Series Description
+               std::cout << " Ser.nb:["         <<  se->GetEntryValue(0x0020, 0x0011);        // Series number
+               std::cout << "] Mod.:["          <<  se->GetEntryValue(0x0008, 0x0060) << "]"; // Modality
+               std::cout << std::endl;    
+
+               im = se->GetFirstImage();
+               while ( im ) { // on degouline les Images de cette serie
+                  std::cout << "--- --- --- "<< " IMAGE Ref. File ID :[" << im->GetEntryValue(0x0004, 0x1500) 
+                            << "]" << std::endl; // File name (Referenced File ID)
+                  im = se->GetNextImage();   
+               }
+               se = st->GetNextSerie();   
+           }
+            st = pa->GetNextStudy();
+        }     
+        pa = f->GetNextPatient();    
+      }
+      break;
+
+   case 5:
+      std::cout << std::endl << std::endl  
+           << " = DICOMDIR full content ==========================================" 
+           << std::endl<< std::endl;
+      f->Print();
+      break;
+
+   }  // end switch
+
+
  /*
-   cout << std::endl << std::endl  
-        << " = PATIENT/STUDY/SERIE/IMAGE List ============================" 
+   // Previous code.
+   // Kept as an example. Please don't remove
+   gdcm::ListDicomDirPatient::const_iterator  itPatient;
+   gdcm::ListDicomDirStudy::const_iterator    itStudy;
+   gdcm::ListDicomDirSerie::const_iterator    itSerie;
+   gdcm::ListDicomDirImage::const_iterator    itImage;
+   cout << std::endl << std::endl
+        << " = Liste des PATIENT/STUDY/SERIE/IMAGE ===================================" 
         << std::endl<< std::endl;
  
-   itPatient = e1->GetDicomDirPatients().begin();
-   while ( itPatient != e1->GetDicomDirPatients().end() ) {  // on degouline la liste de PATIENT
-      std::cout << (*itPatient)->GetEntryByNumber(0x0010, 0x0010) << std::endl; // Patient's Name
+   itPatient = f->GetDicomDirPatients().begin();
+   while ( itPatient != f->GetDicomDirPatients().end() ) {  // on degouline les PATIENT de ce DICOMDIR
+      std::cout << (*itPatient)->GetEntryValue(0x0010, 0x0010) << std::endl; // Patient's Name
       itStudy = ((*itPatient)->GetDicomDirStudies()).begin();
       while (itStudy != (*itPatient)->GetDicomDirStudies().end() ) { // on degouline les STUDY de ce patient
-         std::cout << "--- "<< (*itStudy)->GetEntryByNumber(0x0008, 0x1030) << std::endl; // Study Description
+         std::cout << "--- "<< (*itStudy)->GetEntryValue(0x0008, 0x1030) << std::endl; // Study Description
          itSerie = ((*itStudy)->GetDicomDirSeries()).begin();
          while (itSerie != (*itStudy)->GetDicomDirSeries().end() ) { // on degouline les SERIES de cette study
-            std::cout << "--- --- "<< (*itSerie)->GetEntryByNumber(0x0008, 0x103e) << std::endl; // Serie Description
+            std::cout << "--- --- "<< (*itSerie)->GetEntryValue(0x0008, 0x103e) << std::endl; // Serie Description
             itImage = ((*itSerie)->GetDicomDirImages()).begin();
-            while (itImage != (*itSerie)->GetDicomDirImages().end() ) { // on degouline les SERIES de cette study
-               std::cout << "--- --- --- "<< (*itImage)->GetEntryByNumber(0x0004, 0x1500) << std::endl; // File name
+            while (itImage != (*itSerie)->GetDicomDirImages().end() ) { // on degouline les IMAGES de cette serie
+               std::cout << "--- --- --- "<< (*itImage)->GetEntryValue(0x0004, 0x1500) << std::endl; // File name
                ++itImage;   
             }
             ++itSerie;   
@@ -129,14 +291,13 @@ int main(int argc, char* argv[])
    }   
  */  
 
-/*
-   cout << std::endl << std::endl  
-        << " = DICOMDIR full content ==========================================" 
-        << std::endl<< std::endl;
-   e1->Print();
-*/   
+
+   if(f->IsReadable())
+      std::cout <<std::endl<<fileName<<" is Readable"<<std::endl;
+   else
+      std::cout <<std::endl<<fileName<<" is NOT Readable"<<std::endl;
    std::cout<<std::flush;
-   delete e1;
+   delete f;
 
    return(0);
 }