]> Creatis software - gdcm.git/blob - Example/MakeDicomDir.cxx
Unify user interface
[gdcm.git] / Example / MakeDicomDir.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: MakeDicomDir.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/07/21 04:55:50 $
7   Version:   $Revision: 1.10 $
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    "                                                                          ", 
55    " usage: MakeDicomDir dirname=rootDirectoryName                            ",
56    "        [noshadowseq][noshadow][noseq] [debug]                            ",
57    "                                                                          ",
58    "        noshadowseq: user doesn't want to load Private Sequences          ",
59    "        noshadow : user doesn't want to load Private groups (odd number)  ",
60    "        noseq    : user doesn't want to load Sequences                    ",
61    "        debug    : user wants to run the program in 'debug mode'          ",
62    FINISH_USAGE
63
64    // ----- Initialize Arguments Manager ------   
65    gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
66   
67    if (argc == 1 || am->ArgMgrDefined("usage")) 
68    {
69       am->ArgMgrUsage(usage); // Display 'usage'
70       delete am;
71       return 0;
72    }
73
74    char *dirName;   
75    dirName  = am->ArgMgrGetString("dirName",(char *)"."); 
76
77    int loadMode = 0x00000000;
78    if ( am->ArgMgrDefined("noshadowseq") )
79       loadMode |= NO_SHADOWSEQ;
80    else 
81    {
82    if ( am->ArgMgrDefined("noshadow") )
83          loadMode |= NO_SHADOW;
84       if ( am->ArgMgrDefined("noseq") )
85          loadMode |= NO_SEQ;
86    }
87
88    if (am->ArgMgrDefined("debug"))
89       gdcm::Debug::DebugOn();
90  
91    // if unused Param we give up
92    if ( am->ArgMgrPrintUnusedLabels() )
93    { 
94       am->ArgMgrUsage(usage);
95       delete am;
96       return 0;
97    }
98
99    delete am;  // we don't need Argument Manager any longer
100
101    // ----- Begin Processing -----
102
103    gdcm::DicomDir *dcmdir;
104
105    // we ask for Directory parsing
106
107    dcmdir = new gdcm::DicomDir( );
108
109    dcmdir->SetStartMethod(StartMethod, (void *) NULL);
110    dcmdir->SetEndMethod(EndMethod);
111
112    dcmdir->SetLoadMode(loadMode);
113    dcmdir->SetDirectoryName(dirName);
114    //dcmdir->SetParseDir(true);
115    dcmdir->Load();
116
117     // ----- Check the result
118     
119    if ( !dcmdir->GetFirstPatient() ) 
120    {
121       std::cout << "makeDicomDir: no patient found. Exiting."
122                 << std::endl;
123       delete dcmdir;
124       return 1;
125    }
126     
127    // ----- Create the corresponding DicomDir
128
129    dcmdir->WriteDicomDir("NewDICOMDIR");
130    delete dcmdir;
131
132    // Read from disc the just written DicomDir
133    gdcm::DicomDir *newDicomDir = new gdcm::DicomDir("NewDICOMDIR");
134    if( !newDicomDir->IsReadable() )
135    {
136       std::cout<<"          Written DicomDir 'NewDICOMDIR'"
137                <<" is not readable"<<std::endl
138                <<"          ...Failed"<<std::endl;
139
140       delete newDicomDir;
141       return 1;
142    }
143
144    if( !newDicomDir->GetFirstPatient() )
145    {
146       std::cout<<"          Written DicomDir 'NewDICOMDIR'"
147                <<" has no patient"<<std::endl
148                <<"          ...Failed"<<std::endl;
149
150       delete newDicomDir;
151       return(1);
152    }
153
154    std::cout<<std::flush;
155
156    delete newDicomDir;
157    return 0;
158 }