]> Creatis software - gdcm.git/blob - Example/exXCoherentFileSet.cxx
Update comments
[gdcm.git] / Example / exXCoherentFileSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exXCoherentFileSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/06/15 13:18:51 $
7   Version:   $Revision: 1.10 $
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 }  sort              ",
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    "       sort :  user wants FileHelper to sort the images                   ",
44    "               Warning : will probabely crah if sort has no meaning       ",
45    "       noshadowseq: user doesn't want to load Private Sequences           ",
46    "       noshadow   : user doesn't want to load Private groups (odd number) ",
47    "       noseq      : user doesn't want to load Sequences                   ",
48    "       debug      : user wants to run the program in 'debug mode'         ",
49    FINISH_USAGE
50    
51    // ----- Initialize Arguments Manager ------
52   
53    GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
54   
55    if (am->ArgMgrDefined("usage") || argc == 1) 
56    {
57       am->ArgMgrUsage(usage); // Display 'usage'
58       delete am;
59       return 0;
60    }
61
62    if (am->ArgMgrDefined("debug"))
63       GDCM_NAME_SPACE::Debug::DebugOn();
64       
65    int loadMode = GDCM_NAME_SPACE::LD_ALL;
66    if ( am->ArgMgrDefined("noshadowseq") )
67       loadMode |= GDCM_NAME_SPACE::LD_NOSHADOWSEQ;
68    else 
69    {
70       if ( am->ArgMgrDefined("noshadow") )
71          loadMode |= GDCM_NAME_SPACE::LD_NOSHADOW;
72       if ( am->ArgMgrDefined("noseq") )
73          loadMode |= GDCM_NAME_SPACE::LD_NOSEQ;
74    }
75
76    const char *dirName  = am->ArgMgrGetString("dirin");
77    if (dirName == 0)
78    {
79        std::cout <<std::endl
80                  << "'dirin=' must be present;" 
81                  <<  std::endl;
82        am->ArgMgrUsage(usage); // Display 'usage'  
83        delete am;
84        return 0;
85  }
86
87    // FIXME : check only one of them is set !
88
89    int pos  = am->ArgMgrDefined("pos");
90    int ori  = am->ArgMgrDefined("ori");
91    int sort = am->ArgMgrDefined("sort");
92       
93    int nb;
94    uint16_t *groupelem;
95    groupelem = am->ArgMgrGetXInt16Enum("tag", &nb);
96
97    if (groupelem != 0)
98    {
99       if (nb != 1)
100       {
101          std::cout << "TAG : one and only one group,elem!" << std::endl;
102          delete am;
103          return 0;
104       }
105    }      
106      
107        
108    /* if unused Param we give up */
109    if ( am->ArgMgrPrintUnusedLabels() )
110    {
111       am->ArgMgrUsage(usage);
112       delete am;
113       return 0;
114    } 
115  
116    delete am;  // ------ we don't need Arguments Manager any longer ------
117    
118                  
119    GDCM_NAME_SPACE::SerieHelper *s;
120   
121    s = GDCM_NAME_SPACE::SerieHelper::New();
122    s->SetLoadMode(GDCM_NAME_SPACE::LD_ALL);     // Load everything for each File
123    //GDCM_NAME_SPACE::TagKey t(0x0020,0x0013);
124    //s->AddRestriction(t, "340", GDCM_NAME_SPACE::GDCM_LESS); // Keep only files where
125                                               // restriction is true
126    s->SetDirectory(dirName, true); // true : recursive exploration
127
128    // The Dicom file set is splitted into several 'Single SerieUID Files Sets'
129    // (a 'Single SerieUID Files Set' per SerieUID)
130    // In some cases, it's not enough, since, in some cases
131    // we can find scout view with the same SerieUID
132    
133 /*
134    std::cout << " ---------------------------------------- "
135              << "'Single UID' Filesets found in :["
136              << dirName << "]" << std::endl;
137
138    s->Print();
139    std::cout << " ------------------------------------- Result after splitting"
140              << std::endl;
141 */
142    int nbFiles;
143    std::string fileName;
144    // For all the Single SerieUID Files Sets of the GDCM_NAME_SPACE::Serie
145    GDCM_NAME_SPACE::FileList *l = s->GetFirstSingleSerieUIDFileSet();
146    
147    GDCM_NAME_SPACE::XCoherentFileSetmap xcm;
148    while (l) // for each 'Single SerieUID FileSet'
149    { 
150       nbFiles = l->size() ;
151       if ( l->size() > 3 ) // Why not ? Just an example, for testing
152       {
153          std::cout << "Split the 'Single SerieUID' FileSet :[" 
154                    << s->GetCurrentSerieUIDFileSetUID()
155                    << "]  " << nbFiles << " long" << std::endl;
156          std::cout << "-----------------------------------" << std::endl;
157   
158          if (ori) 
159             xcm = s->SplitOnOrientation(l);
160          if (pos)
161             xcm = s->SplitOnPosition(l);
162          if (groupelem != 0)
163             xcm = s->SplitOnTagValue(l, groupelem[0],groupelem[1] );
164     
165          for (GDCM_NAME_SPACE::XCoherentFileSetmap::iterator i = xcm.begin();
166                                                   i != xcm.end();
167                                                 ++i)
168          {
169             if (ori) 
170                std::cout << "Orientation : ";
171             if (pos) 
172                std::cout << "Position : ";
173             if (groupelem != 0)    
174                std::cout << "Tag (" << std::hex << groupelem[0] 
175                                  << "|" << groupelem[1] << ") value : ";
176     
177              std::cout << "[" << (*i).first << "]" << std::endl;
178     
179            // Within a 'just to see' program, 
180            // OrderFileList() causes trouble, since some files
181            // (eg:MIP views) don't have 'Position', now considered as mandatory
182            // Commented out for the moment.
183            
184            if (sort) {   
185               s->OrderFileList((*i).second);  // sort the XCoherent Fileset
186               std::cout << "ZSpacing for the file set " << s->GetZSpacing()
187                         << std::endl;
188             } 
189
190             for (GDCM_NAME_SPACE::FileList::iterator it =  ((*i).second)->begin();
191                                           it != ((*i).second)->end();
192                                         ++it)
193             {
194                fileName = (*it)->GetFileName();
195                std::cout << "    " << fileName << std::endl;
196             } 
197             std::cout << std::endl;   
198          }
199       }
200       l = s->GetNextSingleSerieUIDFileSet();
201    } 
202   
203    s->Delete();
204
205    return 0;
206 }