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