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