]> Creatis software - gdcm.git/blob - Example/MakeDicomDir.cxx
Use Argument Manager in 'utilities'
[gdcm.git] / Example / MakeDicomDir.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: MakeDicomDir.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/06/07 11:12:10 $
7   Version:   $Revision: 1.4 $
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   * \ingroup Test
42   * \brief   Explores recursively the given directory
43   *          orders the gdcm-readable found Files
44   *          according their Patient/Study/Serie/Image characteristics
45   *          makes the gdcmDicomDir 
46   *          and writes a file named NewDICOMDIR..
47   */  
48
49 int main(int argc, char *argv[]) 
50 {
51    START_USAGE(usage)
52    " \n MakeDicomDir :\n",
53    " Explores recursively the given directory, makes the relevant DICOMDIR",
54    "          and writes it as 'NewDICOMDIR'",
55    " usage: MakeDicomDir dirname=rootDirectoryName [noshadow] [noseq] [debug] ",
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 (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","."); 
73
74    int loadMode;
75    if ( am->ArgMgrDefined("noshadow") && am->ArgMgrDefined("noseq") )
76        loadMode = NO_SEQ | NO_SHADOW;  
77    else if ( am->ArgMgrDefined("noshadow") )
78       loadMode = NO_SHADOW;
79    else if ( am->ArgMgrDefined("noseq") )
80       loadMode = NO_SEQ;
81    else
82       loadMode = 0;
83
84    if (am->ArgMgrDefined("debug"))
85       gdcm::Debug::DebugOn();
86  
87    // if unused Param we give up
88    if ( am->ArgMgrPrintUnusedLabels() )
89    { 
90       am->ArgMgrUsage(usage);
91       delete am;
92       return 0;
93    }
94
95    delete am;  // we don't need Argument Manager any longer
96
97    // ----- Begin Processing -----
98
99    gdcm::DicomDir *dcmdir;
100
101    // we ask for Directory parsing
102
103    dcmdir = new gdcm::DicomDir( );
104    dcmdir->SetParseDir(true);
105
106    dcmdir->SetStartMethod(StartMethod, (void *) NULL);
107    dcmdir->SetEndMethod(EndMethod);
108
109    dcmdir->SetLoadMode(loadMode);
110    dcmdir->Load(dirName);
111
112     // ----- Check the result
113     
114    if ( !dcmdir->GetFirstPatient() ) 
115    {
116       std::cout << "makeDicomDir: no patient found. Exiting."
117                 << std::endl;
118       delete dcmdir;
119       return 1;
120    }
121     
122    // ----- Create the corresponding DicomDir
123
124    dcmdir->WriteDicomDir("NewDICOMDIR");
125    delete dcmdir;
126
127    // Read from disc the just written DicomDir
128    gdcm::DicomDir *newDicomDir = new gdcm::DicomDir("NewDICOMDIR");
129    if( !newDicomDir->IsReadable() )
130    {
131       std::cout<<"          Written DicomDir 'NewDICOMDIR'"
132                <<" is not readable"<<std::endl
133                <<"          ...Failed"<<std::endl;
134
135       delete newDicomDir;
136       return 1;
137    }
138
139    if( !newDicomDir->GetFirstPatient() )
140    {
141       std::cout<<"          Written DicomDir 'NewDICOMDIR'"
142                <<" has no patient"<<std::endl
143                <<"          ...Failed"<<std::endl;
144
145       delete newDicomDir;
146       return(1);
147    }
148
149    std::cout<<std::flush;
150
151    delete newDicomDir;
152    return 0;
153 }