]> Creatis software - gdcm.git/blob - Example/exXCoherentFileSet.cxx
ENH: Finally I got ArgMgr to be const string instead of char*... strcasecmp is POSIX...
[gdcm.git] / Example / exXCoherentFileSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exXCoherentFileSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2006/01/26 15:52:43 $
7   Version:   $Revision: 1.7 $
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 "gdcmDebug.h"
21 #include <iostream>
22
23 #include "gdcmArgMgr.h"
24
25 int main(int argc, char *argv[])
26 {  
27
28    START_USAGE(usage)
29    "\n exXCoherentFileSet :\n                                                 ",
30    "Shows the various 'XCoherent' Filesets within a directory                 ",
31    "usage: exXCoherentFileSet {dirin=inputDirectoryName}                      ",
32    "                       { tag= group-elem | pos | ori }                    ",
33    "                       [ { [noshadowseq] | [noshadow][noseq] } ] [debug]  ",
34    "                                                                          ",
35    "       inputDirectoryName : user wants to analyze *all* the files         ",
36    "                            within the directory                          ",
37    "       pos  : user wants to split each Single SerieUID Fileset on the     ",
38    "                         'Image Position '                                ",
39    "       ori  : user wants to split each Single SerieUID Fileset on the     ",
40    "                         'Image Orientation '                             ",
41    "       tag : group-elem    (in hexa, no space)                            ",
42    "                       the user wants to split on                         ",
43    "       noshadowseq: user doesn't want to load Private Sequences           ",
44    "       noshadow   : user doesn't want to load Private groups (odd number) ",
45    "       noseq      : user doesn't want to load Sequences                   ",
46    "       debug      : user wants to run the program in 'debug mode'         ",
47    FINISH_USAGE
48    
49    // ----- Initialize Arguments Manager ------
50   
51    gdcm::ArgMgr *am = new gdcm::ArgMgr(argc, argv);
52   
53    if (am->ArgMgrDefined("usage") || argc == 1) 
54    {
55       am->ArgMgrUsage(usage); // Display 'usage'
56       delete am;
57       return 0;
58    }
59
60    if (am->ArgMgrDefined("debug"))
61       gdcm::Debug::DebugOn();
62       
63    int loadMode = gdcm::LD_ALL;
64    if ( am->ArgMgrDefined("noshadowseq") )
65       loadMode |= gdcm::LD_NOSHADOWSEQ;
66    else 
67    {
68       if ( am->ArgMgrDefined("noshadow") )
69          loadMode |= gdcm::LD_NOSHADOW;
70       if ( am->ArgMgrDefined("noseq") )
71          loadMode |= gdcm::LD_NOSEQ;
72    }
73
74    const char *dirName  = am->ArgMgrGetString("dirin");
75    if (dirName == 0)
76    {
77        std::cout <<std::endl
78                  << "'dirin=' must be present;" 
79                  <<  std::endl;
80        am->ArgMgrUsage(usage); // Display 'usage'  
81        delete am;
82        return 0;
83  }
84
85    // FIXME : check only one of them is set !
86
87    int pos = am->ArgMgrDefined("pos");
88    int ori = am->ArgMgrDefined("ori");
89    
90    int nb;
91    uint16_t *groupelem;
92    groupelem = am->ArgMgrGetXInt16Enum("tag", &nb);
93
94    if (groupelem != 0)
95    {
96       if (nb != 1)
97       {
98          std::cout << "TAG : one and only one group,elem!" << std::endl;
99          delete am;
100          return 0;
101       }
102    }      
103      
104        
105    /* if unused Param we give up */
106    if ( am->ArgMgrPrintUnusedLabels() )
107    {
108       am->ArgMgrUsage(usage);
109       delete am;
110       return 0;
111    } 
112  
113    delete am;  // ------ we don't need Arguments Manager any longer ------
114    
115                  
116    gdcm::SerieHelper *s;
117   
118    s = gdcm::SerieHelper::New();
119    s->SetLoadMode(gdcm::LD_ALL);     // Load everything for each File
120    //gdcm::TagKey t(0x0020,0x0013);
121    //s->AddRestriction(t, "340", gdcm::GDCM_LESS); // Keep only files where
122                                               // restriction is true
123    s->SetDirectory(dirName, true); // true : recursive exploration
124
125 /*
126    std::cout << " ---------------------------------------- "
127              << "'Single UID' Filesets found in :["
128              << dirName << "]" << std::endl;
129
130    s->Print();
131    std::cout << " ------------------------------------- Result after splitting"
132              << std::endl;
133 */
134    int nbFiles;
135    std::string fileName;
136    // For all the Single SerieUID Files Sets of the gdcm::Serie
137    gdcm::FileList *l = s->GetFirstSingleSerieUIDFileSet();
138    
139    gdcm::XCoherentFileSetmap xcm;
140    while (l)
141    { 
142       nbFiles = l->size() ;
143       if ( l->size() > 3 ) // Why not ? Just an example, for testing
144       {
145          std::cout << "Split the 'Single SerieUID' FileSet :[" 
146                    << s->GetCurrentSerieUIDFileSetUID()
147                    << "]  " << nbFiles << " long" << std::endl;
148          std::cout << "-----------------------------------" << std::endl;
149   
150          if (ori) 
151             xcm = s->SplitOnOrientation(l);
152          if (pos)
153             xcm = s->SplitOnPosition(l);
154          if (groupelem != 0)
155             xcm = s->SplitOnTagValue(l, groupelem[0],groupelem[1] );
156     
157          for (gdcm::XCoherentFileSetmap::iterator i = xcm.begin();
158                                                   i != xcm.end();
159                                                 ++i)
160          {
161             if (ori) 
162                std::cout << "Orientation : ";
163             if (pos) 
164                std::cout << "Position : ";
165             if (groupelem != 0)    
166                std::cout << "Tag (" << std::hex << groupelem[0] 
167                                  << "|" << groupelem[1] << ") value : ";
168     
169              std::cout << "[" << (*i).first << "]" << std::endl;
170     
171            // Nowadays OrderFileList() causes trouble, since some files
172            // (MIP views) don't have 'Position', now considered as mandatory
173            // Commented out for the moment.
174    
175            //s->OrderFileList((*i).second);  // sort the XCoherent Fileset
176     
177             for (std::vector<gdcm::File* >::iterator it =  ((*i).second)->begin();
178                                                      it != ((*i).second)->end();
179                                                    ++it)
180             {
181                fileName = (*it)->GetFileName();
182                std::cout << "    " << fileName << std::endl;
183             } 
184             std::cout << std::endl;   
185          }
186       }
187       l = s->GetNextSingleSerieUIDFileSet();
188    } 
189   
190    s->Delete();
191
192    return 0;
193 }