]> Creatis software - gdcm.git/blob - Example/MakeDicomDir.cxx
Use new style for DicomDir loading
[gdcm.git] / Example / MakeDicomDir.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: MakeDicomDir.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/07/08 10:15:04 $
7   Version:   $Revision: 1.9 $
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 "gdcmDocEntry.h"
19 #include "gdcmDicomDir.h"
20 #include "gdcmDicomDirPatient.h"
21 #include "gdcmDirList.h"
22 #include "gdcmDebug.h"
23
24 #include "gdcmArgMgr.h"
25
26 #include <iostream>
27
28 // ---
29 void StartMethod(void *toto) {
30   (void)toto;
31    std::cout<<"Start parsing"<<std::endl;
32 }
33
34 void EndMethod(void *toto) {
35   (void)toto;
36    std::cout<<"End parsing"<<std::endl;
37 }
38 // ---
39
40 /**
41   * \brief   Explores recursively the given directory
42   *          orders the gdcm-readable found Files
43   *          according their Patient/Study/Serie/Image characteristics
44   *          makes the gdcmDicomDir 
45   *          and writes a file named NewDICOMDIR..
46   */  
47
48 int main(int argc, char *argv[]) 
49 {
50    START_USAGE(usage)
51    " \n MakeDicomDir :\n",
52    " Explores recursively the given directory, makes the relevant DICOMDIR",
53    "          and writes it as 'NewDICOMDIR'",
54    " usage: MakeDicomDir dirname=rootDirectoryName [noshadowseq][noshadow][noseq] [debug] ",
55    "        noshadowseq: user doesn't want to load Private Sequences",
56    "        noshadow : user doesn't want to load Private groups (odd number)",
57    "        noseq    : user doesn't want to load Sequences ",
58    "        debug    : user wants to run the program in 'debug mode' ",
59    FINISH_USAGE
60
61    // ----- Initialize Arguments Manager ------   
62    gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
63   
64    if (argc == 1 || am->ArgMgrDefined("usage")) 
65    {
66       am->ArgMgrUsage(usage); // Display 'usage'
67       delete am;
68       return 0;
69    }
70
71    char *dirName;   
72    dirName  = am->ArgMgrGetString("dirName",(char *)"."); 
73
74    int loadMode = 0x00000000;
75    if ( am->ArgMgrDefined("noshadowseq") )
76       loadMode |= NO_SHADOWSEQ;
77    else 
78    {
79    if ( am->ArgMgrDefined("noshadow") )
80          loadMode |= NO_SHADOW;
81       if ( am->ArgMgrDefined("noseq") )
82          loadMode |= NO_SEQ;
83    }
84
85    if (am->ArgMgrDefined("debug"))
86       gdcm::Debug::DebugOn();
87  
88    // if unused Param we give up
89    if ( am->ArgMgrPrintUnusedLabels() )
90    { 
91       am->ArgMgrUsage(usage);
92       delete am;
93       return 0;
94    }
95
96    delete am;  // we don't need Argument Manager any longer
97
98    // ----- Begin Processing -----
99
100    gdcm::DicomDir *dcmdir;
101
102    // we ask for Directory parsing
103
104    dcmdir = new gdcm::DicomDir( );
105
106    dcmdir->SetStartMethod(StartMethod, (void *) NULL);
107    dcmdir->SetEndMethod(EndMethod);
108
109    dcmdir->SetLoadMode(loadMode);
110    dcmdir->SetDirectoryName(dirName);
111 dcmdir->SetParseDir(true);
112    dcmdir->Load();
113
114     // ----- Check the result
115     
116    if ( !dcmdir->GetFirstPatient() ) 
117    {
118       std::cout << "makeDicomDir: no patient found. Exiting."
119                 << std::endl;
120       delete dcmdir;
121       return 1;
122    }
123     
124    // ----- Create the corresponding DicomDir
125
126    dcmdir->WriteDicomDir("NewDICOMDIR");
127    delete dcmdir;
128
129    // Read from disc the just written DicomDir
130    gdcm::DicomDir *newDicomDir = new gdcm::DicomDir("NewDICOMDIR");
131    if( !newDicomDir->IsReadable() )
132    {
133       std::cout<<"          Written DicomDir 'NewDICOMDIR'"
134                <<" is not readable"<<std::endl
135                <<"          ...Failed"<<std::endl;
136
137       delete newDicomDir;
138       return 1;
139    }
140
141    if( !newDicomDir->GetFirstPatient() )
142    {
143       std::cout<<"          Written DicomDir 'NewDICOMDIR'"
144                <<" has no patient"<<std::endl
145                <<"          ...Failed"<<std::endl;
146
147       delete newDicomDir;
148       return(1);
149    }
150
151    std::cout<<std::flush;
152
153    delete newDicomDir;
154    return 0;
155 }