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