]> Creatis software - gdcm.git/blob - Example/MakeDicomDir.cxx
Begin of kosherization of Example
[gdcm.git] / Example / MakeDicomDir.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: MakeDicomDir.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/07/07 17:31:53 $
7   Version:   $Revision: 1.8 $
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    dcmdir->SetParseDir(true);
106
107    dcmdir->SetStartMethod(StartMethod, (void *) NULL);
108    dcmdir->SetEndMethod(EndMethod);
109
110    dcmdir->SetLoadMode(loadMode);
111    dcmdir->SetFileName(dirName);
112
113    dcmdir->Load();
114
115     // ----- Check the result
116     
117    if ( !dcmdir->GetFirstPatient() ) 
118    {
119       std::cout << "makeDicomDir: no patient found. Exiting."
120                 << std::endl;
121       delete dcmdir;
122       return 1;
123    }
124     
125    // ----- Create the corresponding DicomDir
126
127    dcmdir->WriteDicomDir("NewDICOMDIR");
128    delete dcmdir;
129
130    // Read from disc the just written DicomDir
131    gdcm::DicomDir *newDicomDir = new gdcm::DicomDir("NewDICOMDIR");
132    if( !newDicomDir->IsReadable() )
133    {
134       std::cout<<"          Written DicomDir 'NewDICOMDIR'"
135                <<" is not readable"<<std::endl
136                <<"          ...Failed"<<std::endl;
137
138       delete newDicomDir;
139       return 1;
140    }
141
142    if( !newDicomDir->GetFirstPatient() )
143    {
144       std::cout<<"          Written DicomDir 'NewDICOMDIR'"
145                <<" has no patient"<<std::endl
146                <<"          ...Failed"<<std::endl;
147
148       delete newDicomDir;
149       return(1);
150    }
151
152    std::cout<<std::flush;
153
154    delete newDicomDir;
155    return 0;
156 }