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