]> Creatis software - gdcm.git/blob - Example/exSerieHelper.cxx
minor improvements
[gdcm.git] / Example / exSerieHelper.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exSerieHelper.cxx,v $
5   Language:  C++
6   Date:      $Date: 2009/05/28 15:44:34 $
7   Version:   $Revision: 1.18 $
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 "gdcmSerieHelper.h"
19 #include "gdcmFile.h"
20 #include "gdcmDirList.h" // for FileList
21 #include "gdcmDebug.h"
22 #include <iostream>
23 #include "gdcmArgMgr.h"
24
25 int main(int argc, char *argv[])
26 {
27    START_USAGE(usage)
28    "\n exSerieHelper :\n                                                      ",
29    "Example on how to use the methodes of gdcm::SerieHelper                   ",
30    "usage: exSerieHelper {dirin=inputDirectoryName}                           ",
31    "                       [ { [noshadowseq] | [noshadow][noseq] } ] [debug]  ",
32    "                                                                          ",
33    "       dirin : user wants to analyze *all* the files                      ",
34    "                            within the directory                          ",
35    "       noshadowseq: user doesn't want to load Private Sequences           ",
36    "       noshadow   : user doesn't want to load Private groups (odd number) ",
37    "       noseq      : user doesn't want to load Sequences                   ",
38    "       verbose    : user wants to run the program in 'verbose mode'       ",
39    "       warning    : user wants to run the program in 'warning mode'       ",   
40    "       debug      : developper wants to run the program in 'debug mode'   ",
41    FINISH_USAGE
42
43    // ----- Initialize Arguments Manager ------
44   
45    GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
46   
47    if (am->ArgMgrDefined("usage") || argc == 1) 
48    {
49       am->ArgMgrUsage(usage); // Display 'usage'
50       delete am;
51       return 0;
52    }
53
54    if (am->ArgMgrDefined("debug"))
55       GDCM_NAME_SPACE::Debug::DebugOn();
56       
57    if (am->ArgMgrDefined("warning"))
58       GDCM_NAME_SPACE::Debug::WarningOn();      
59
60    bool verbose = ( 0 != am->ArgMgrDefined("verbose") );
61       
62    int loadMode = GDCM_NAME_SPACE::LD_ALL;
63    if ( am->ArgMgrDefined("noshadowseq") )
64       loadMode |= GDCM_NAME_SPACE::LD_NOSHADOWSEQ;
65    else
66    {
67       if ( am->ArgMgrDefined("noshadow") )
68          loadMode |= GDCM_NAME_SPACE::LD_NOSHADOW;
69       if ( am->ArgMgrDefined("noseq") )
70          loadMode |= GDCM_NAME_SPACE::LD_NOSEQ;
71    }
72    
73    const char *dirName  = am->ArgMgrGetString("dirin");
74    if (dirName == 0)
75    {
76        std::cout <<std::endl
77                  << "'dirin=' must be present;" 
78                  <<  std::endl;
79        am->ArgMgrUsage(usage); // Display 'usage'  
80        delete am;
81        return 0;
82    }
83
84   
85    std::cout << "Dir Name :[" << dirName << "]" << std::endl;
86    //   
87    // Sometimes using only SerieHelper is not enought !
88    // See also exXcoherentFileSet
89    //
90    
91    
92    GDCM_NAME_SPACE::SerieHelper *s;
93    s = GDCM_NAME_SPACE::SerieHelper::New();
94    s->SetLoadMode(GDCM_NAME_SPACE::LD_ALL);     // Load everything for each File
95    //GDCM_NAME_SPACE::TagKey t(0x0020,0x0013);
96    //s->AddRestriction(t, "340", GDCM_NAME_SPACE::GDCM_LESS); // Keep only files where
97                                               // restriction is true
98    s->SetDirectory(dirName, true); // true : recursive exploration
99
100    if (verbose) {
101       std::cout << " ---------------------------------------- Finish parsing :["
102                 << dirName << "]" << std::endl;
103
104       s->Print();
105      std::cout << " ---------------------------------------- Finish printing (1)"
106                 << std::endl;
107    }
108
109    GDCM_NAME_SPACE::FileList::const_iterator it;
110    GDCM_NAME_SPACE::FileList *l;
111    std::cout << std::endl << " ---------------------------------------- Recap"
112              << std::endl;  
113    l = s->GetFirstSingleSerieUIDFileSet();
114    while (l)
115    { 
116       it = l->begin();
117       std::cout << "SerieUID [" <<  (*it)->GetEntryString(0x0020,0x000e) <<"]   Serie Description ["
118                 << (*it)->GetEntryString(0x0008,0x103e) << "] "  
119                 << " : " << l->size() << " files" << std::endl;
120       l = s->GetNextSingleSerieUIDFileSet();
121    } 
122     std::cout << " ----------------------------------------End Recap"
123              << std::endl << std::endl;
124
125    int nbFiles;
126    double zspacing = 0.;
127    // For all the Single SerieUID Files Sets of the GDCM_NAME_SPACE::Serie
128    l = s->GetFirstSingleSerieUIDFileSet();
129    while (l)
130    { 
131       nbFiles = l->size() ;
132       if ( nbFiles > 5 ) // Why not ? Just an example, for testing
133       {
134          std::cout << "List to sort : " << nbFiles << " long" << std::endl;  
135          //---------------------------------------------------------
136          s->OrderFileList(l);  // sort the list (and compute ZSpacing !)
137          //---------------------------------------------------------
138          std::cout << "List after sorting : " << l->size() << " long" << std::endl;
139            
140           zspacing = s->GetZSpacing();
141          // Just to show : GetZSpacing from a GDCM_NAME_SPACE::SerieHelper is right  
142          std::cout << "GetZSpacing() of sorted SingleSerieUIDFileSet "
143                    << "from GDCM_NAME_SPACE::SerieHelper: " << zspacing << std::endl;
144          std::cout << " ('-1' means all the files have the same position)" << std::endl;
145          
146          // Check the vector content
147          int fileCount = 0;
148       // for (std::vector<GDCM_NAME_SPACE::File* >::iterator it2 =  l->begin();
149          for (GDCM_NAME_SPACE::FileList::const_iterator it2 = l->begin();
150                                             it2 != l->end();
151                                           ++it2)
152          {
153           // Just to show : GetZSpacing from a GDCM_NAME_SPACE::File may be different        
154              std::cout << (*it2)->GetFileName() << " -->  Get{X/Y/Z}Spacing() from GDCM_NAME_SPACE::File : " 
155                        << (*it2)->GetXSpacing() << " " 
156                        << (*it2)->GetYSpacing() << " " 
157                        << (*it2)->GetZSpacing() << std::endl; 
158            fileCount++;      
159          }
160          std::cout << "Iterate trough vector, nb of files : " << fileCount << std::endl;  
161
162          //break; // we only deal with the first one ... Why not ?
163       }
164       l = s->GetNextSingleSerieUIDFileSet();
165    } 
166    std::cout << std::endl
167              << " ------------------Prints all the Single SerieUID File Sets (sorted or not) -----"
168              << std::endl;
169    s->Print(); // Prints all the Single SerieUID File Sets (sorted or not)
170    std::cout << " -------------------------------------------- Finish printing"
171              << std::endl;
172      
173    s->Delete();
174
175    return 0;
176 }