]> Creatis software - gdcm.git/blob - Testing/TestMakeDicomDir.cxx
use GDCM_NAME_SPACE:: instead of gdcm::, even in Examples ...
[gdcm.git] / Testing / TestMakeDicomDir.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestMakeDicomDir.cxx,v $
5   Language:  C++
6   Date:      $Date: 2008/09/15 15:49:21 $
7   Version:   $Revision: 1.14 $
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 "gdcmCommandManager.h"
23 #include "gdcmDebug.h"
24
25 // ---
26 class CommandStart : public GDCM_NAME_SPACE::Command
27 {
28    gdcmTypeMacro(CommandStart);
29    gdcmNewMacro(CommandStart);
30
31 public :
32    virtual void Execute()
33    {
34       std::cerr << "Start parsing" << std::endl;
35    }
36
37 protected :
38    CommandStart() {}
39 };
40
41 class CommandEnd : public GDCM_NAME_SPACE::Command
42 {
43    gdcmTypeMacro(CommandEnd);
44    gdcmNewMacro(CommandEnd);
45
46 public :
47    virtual void Execute()
48    {
49       std::cerr << "End parsing" << std::endl;
50    }
51
52 protected :
53    CommandEnd() {}
54 };
55
56 class CommandProgress : public GDCM_NAME_SPACE::Command
57 {
58    gdcmTypeMacro(CommandProgress);
59    gdcmNewMacro(CommandProgress);
60
61 public :
62    virtual void Execute()
63    {
64       GDCM_NAME_SPACE::DicomDir *dd=dynamic_cast<GDCM_NAME_SPACE::DicomDir *>(GetObject());
65
66       if(dd)
67          std::cerr << "Progress parsing (" << dd->GetProgress() << ")" << std::endl;
68       else
69          std::cerr << "Progress parsing (NULL)" << std::endl;
70    }
71
72 protected :
73    CommandProgress() {}
74 };
75
76 void EndMethod(void *endMethod) 
77 {
78   (void)endMethod;
79    std::cout<<"End parsing"<<std::endl;
80 }
81 // ---
82
83 /**
84   * \brief   - Explores recursively the given directory 
85   *            (or GDCM_DATA_ROOT by default)
86   *          - Orders the gdcm-readable found Files
87   *             according their Patient/Study/Serie/Image characteristics
88   *          - Makes the GDCM_NAME_SPACE::DicomDir. 
89   *          - Writes a file named "NewDICOMDIR".
90   *          - Reads "NewDICOMDIR" file.
91   */  
92
93 int TestMakeDicomDir(int argc, char *argv[])
94 {
95    //GDCM_NAME_SPACE::Debug::DebugOn();
96    std::string dirName;   
97
98    if (argc > 1)
99    {
100       dirName = argv[1];
101    }
102    else
103    {
104       dirName = GDCM_DATA_ROOT;
105    }
106  
107    GDCM_NAME_SPACE::DicomDir *dcmdir;
108
109    // new style (user is allowed no to load Sequences an/or Shadow Groups)
110    dcmdir = GDCM_NAME_SPACE::DicomDir::New( );
111  
112    GDCM_NAME_SPACE::Command *cmd;
113    cmd = CommandStart::New();
114    GDCM_NAME_SPACE::CommandManager::SetCommand(dcmdir,GDCM_NAME_SPACE::CMD_STARTPROGRESS,cmd);
115    cmd->Delete();
116    cmd = CommandProgress::New();
117    GDCM_NAME_SPACE::CommandManager::SetCommand(dcmdir,GDCM_NAME_SPACE::CMD_PROGRESS,cmd);
118    cmd->Delete();
119    cmd = CommandEnd::New();
120    GDCM_NAME_SPACE::CommandManager::SetCommand(dcmdir,GDCM_NAME_SPACE::CMD_ENDPROGRESS,cmd);
121    cmd->Delete();
122
123    // dcmdir->SetLoadMode(GDCM_NAME_SPACE::LD_NOSEQ | GDCM_NAME_SPACE::LD_NOSHADOW);
124    // some images have a wrong length for element 0x0000 of private groups
125    dcmdir->SetLoadMode(GDCM_NAME_SPACE::LD_NOSEQ);
126    dcmdir->SetDirectoryName(dirName);
127    dcmdir->Load();
128
129    if ( !dcmdir->GetFirstPatient() ) 
130    {
131       std::cout << "makeDicomDir: no patient found. Exiting."
132                 << std::endl;
133
134       dcmdir->Delete();
135       return 1;
136    }
137     
138    // Create the corresponding DicomDir
139    dcmdir->Write("NewDICOMDIR");
140    dcmdir->Delete();
141
142    // Read from disc the just written DicomDir
143    GDCM_NAME_SPACE::DicomDir *newDicomDir = GDCM_NAME_SPACE::DicomDir::New();
144    newDicomDir->SetFileName("NewDICOMDIR");
145    newDicomDir->Load();
146
147    if( !newDicomDir->IsReadable() )
148    {
149       std::cout<<"          Written DicomDir 'NewDICOMDIR'"
150                <<" is not readable"<<std::endl
151                <<"          ...Failed"<<std::endl;
152
153       newDicomDir->Delete();
154       return 1;
155    }
156
157    if( !newDicomDir->GetFirstPatient() )
158    {
159       std::cout<<"          Written DicomDir 'NewDICOMDIR'"
160                <<" has no patient"<<std::endl
161                <<"          ...Failed"<<std::endl;
162
163       newDicomDir->Delete();
164       return(1);
165    }
166
167    std::cout<<std::flush;
168    newDicomDir->Delete();
169    return 0;
170 }