]> Creatis software - gdcm.git/blob - Example/MakeDicomDir.cxx
User may choose the name of the generated dicomdir
[gdcm.git] / Example / MakeDicomDir.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: MakeDicomDir.cxx,v $
5   Language:  C++
6   Date:      $Date: 2006/01/18 10:20:56 $
7   Version:   $Revision: 1.19 $
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   * \brief   Explores recursively the given directory
29   *          orders the gdcm-readable found Files
30   *          according their Patient/Study/Serie/Image characteristics
31   *          makes the gdcmDicomDir 
32   *          and writes a file named DICOMDIR. (user may choose an other name)
33   */  
34
35 int main(int argc, char *argv[]) 
36 {
37    START_USAGE(usage)
38    " \n MakeDicomDir :\n                                                      ",
39    " Explores recursively the given directory, makes the relevant DICOMDIR    ",
40    "          and writes it as 'NewDICOMDIR'                                  ",
41    "                                                                          ", 
42    " usage: MakeDicomDir dirname=rootDirectoryName                            ",
43    "                     name=DICOMDIR file name                              ",
44    "        [noshadowseq][noshadow][noseq] [debug] [check]                    ",
45    "                                                                          ",
46    "        name : the default name for the generated dicomdir is 'DICOMDIR'  ",
47    "        noshadowseq: user doesn't want to load Private Sequences          ",
48    "        noshadow : user doesn't want to load Private groups (odd number)  ",
49    "        noseq    : user doesn't want to load Sequences                    ",
50    "        debug    : user wants to run the program in 'debug mode'          ",
51    "        check    : the dicomdir is checked as 'gdcm readable'             ",
52    FINISH_USAGE
53
54    // ----- Initialize Arguments Manager ------   
55    gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
56   
57    if (argc == 1 || am->ArgMgrDefined("usage")) 
58    {
59       am->ArgMgrUsage(usage); // Display 'usage'
60       delete am;
61       return 0;
62    }
63
64    char *dirName;   
65    dirName  = am->ArgMgrGetString("dirName",(char *)"."); 
66
67    char *name;
68    name  = am->ArgMgrGetString("name",(char *)"DICOMDIR");
69    
70    int loadMode = gdcm::LD_ALL;
71    if ( am->ArgMgrDefined("noshadowseq") )
72       loadMode |= gdcm::LD_NOSHADOWSEQ; 
73    else 
74    {
75    if ( am->ArgMgrDefined("noshadow") )
76          loadMode |= gdcm::LD_NOSHADOW;
77       if ( am->ArgMgrDefined("noseq") )
78          loadMode |= gdcm::LD_NOSEQ;
79    }
80
81    if (am->ArgMgrDefined("debug"))
82       gdcm::Debug::DebugOn();
83       
84    int check = am->ArgMgrDefined("check"); 
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 = gdcm::DicomDir::New( );
103
104    dcmdir->SetLoadMode(loadMode);
105    dcmdir->SetDirectoryName(dirName);
106    //dcmdir->SetParseDir(true);
107    dcmdir->Load();
108
109    if ( gdcm::Debug::GetDebugFlag() )
110       std::cout << "======================= End Parsing Directory" << std::endl;
111       
112     // ----- Check the result
113     
114    if ( !dcmdir->GetFirstPatient() ) 
115    {
116       std::cout << "makeDicomDir: no patient found. Exiting."
117                 << std::endl;
118       dcmdir->Delete();
119       return 1;
120    }
121     
122    // ----- Create the corresponding DicomDir
123
124    dcmdir->Write(name);
125    dcmdir->Delete();
126    
127    if (check) 
128    {
129       if ( gdcm::Debug::GetDebugFlag() )
130          std::cout << "======================= End Writting DICOMDIR" 
131                    << std::endl;
132
133      // Read from disc the just written DicomDir
134     
135       gdcm::DicomDir *newDicomDir = gdcm::DicomDir::New();
136       newDicomDir->SetFileName( name );
137       newDicomDir->Load();
138       if ( gdcm::Debug::GetDebugFlag() )
139          std::cout << "======================= End Parsing DICOMDIR" 
140                    << std::endl;   
141       if( !newDicomDir->IsReadable() )
142       {
143          std::cout<<"          Written DicomDir [" << name << "] "
144                   <<" is not readable"<<std::endl
145                   <<"          ...Failed"<<std::endl;
146
147          newDicomDir->Delete();
148          return 1;
149       }
150
151       if( !newDicomDir->GetFirstPatient() )
152       {
153          std::cout <<"          Written DicomDir [" << name << "] "
154                    <<" has no patient"<<std::endl
155                    <<"          ...Failed"<<std::endl;
156
157          newDicomDir->Delete();
158          return(1);
159       } 
160       std::cout<<std::flush;
161       newDicomDir->Delete();        
162    }
163    return 0;
164 }