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