1 /*=========================================================================
4 Module: $RCSfile: exXCoherentFileSet.cxx,v $
6 Date: $Date: 2009/05/28 15:44:34 $
7 Version: $Revision: 1.16 $
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.
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.
17 =========================================================================*/
18 #include "gdcmSerieHelper.h"
20 #include "gdcmDebug.h"
22 #include "gdcmDirList.h"
24 #include "gdcmArgMgr.h"
25 int main(int argc, char *argv[])
28 "\n exXCoherentFileSet :\n ",
29 "Shows the various 'XCoherent' Filesets within a directory ",
30 "Optionaly copies the images in a Directories tree ",
31 "usage: exXCoherentFileSet {dirin=inputDirectoryName} ",
32 " dirout=outputDirectoryName ",
33 " { tag=group-elem | pos | ori } [sort] [write] ",
34 " [ { [noshadowseq] | [noshadow][noseq] } ] [debug] ",
36 " dirin : user wants to analyze *all* the files ",
37 " within the directory ",
38 " write : user wants to create directories ",
39 " dirout : will be created if doesn't exist ",
40 " pos : user wants to split each Single SerieUID Fileset on the ",
41 " 'Image Position ' ",
42 " ori : user wants to split each Single SerieUID Fileset on the ",
43 " 'Image Orientation ' ",
44 " tag : group-elem (in hexa, no space) ",
45 " the user wants to split on ",
46 " sort : user wants FileHelper to sort the images ",
47 " Warning : will probabely crah if sort has no meaning ",
48 " (not only look at image names) ",
49 " noshadowseq: user doesn't want to load Private Sequences ",
50 " noshadow : user doesn't want to load Private groups (odd number) ",
51 " noseq : user doesn't want to load Sequences ",
52 " verbose : user wants to run the program in 'verbose mode' ",
53 " debug : developper wants to run the program in 'debug mode' ",
56 // ----- Initialize Arguments Manager ------
58 GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
60 if (am->ArgMgrDefined("usage") || argc == 1)
62 am->ArgMgrUsage(usage); // Display 'usage'
67 if (am->ArgMgrDefined("debug"))
68 GDCM_NAME_SPACE::Debug::DebugOn();
70 int loadMode = GDCM_NAME_SPACE::LD_ALL;
71 if ( am->ArgMgrDefined("noshadowseq") )
72 loadMode |= GDCM_NAME_SPACE::LD_NOSHADOWSEQ;
75 if ( am->ArgMgrDefined("noshadow") )
76 loadMode |= GDCM_NAME_SPACE::LD_NOSHADOW;
77 if ( am->ArgMgrDefined("noseq") )
78 loadMode |= GDCM_NAME_SPACE::LD_NOSEQ;
81 const char *dirName = am->ArgMgrGetString("dirin");
85 << "'dirin=' must be present;"
87 am->ArgMgrUsage(usage); // Display 'usage'
92 const char *dirNameout;
93 dirNameout = am->ArgMgrGetString("dirout",".");
95 bool pos = ( 0 != am->ArgMgrDefined("pos") );
96 bool ori = ( 0 != am->ArgMgrDefined("ori") );
97 bool sort = ( 0 != am->ArgMgrDefined("sort") );
98 bool write = ( 0 != am->ArgMgrDefined("write") );
99 bool verbose = ( 0 != am->ArgMgrDefined("verbose") );
100 bool tag = ( 0 != am->ArgMgrDefined("tag") );
102 if( (tag && (pos || ori)) || (pos && (tag || ori)) || (ori && (tag || pos)) )
104 std::cout << " POS, ORI and TAG are mutually exclusive" << std::endl;
109 if( (!tag && !pos && !ori))
111 std::cout << " One of POS, ORI and TAG is mandatory!" << std::endl;
119 groupelem = am->ArgMgrGetXInt16Enum("tag", &nb);
122 std::cout << "TAG : one and only one group,elem!" << std::endl;
128 /* if unused Param we give up */
129 if ( am->ArgMgrPrintUnusedLabels() )
131 am->ArgMgrUsage(usage);
136 delete am; // ------ we don't need Arguments Manager any longer ------
138 GDCM_NAME_SPACE::SerieHelper *s;
140 s = GDCM_NAME_SPACE::SerieHelper::New();
141 s->SetLoadMode(GDCM_NAME_SPACE::LD_ALL); // Load everything for each File
143 //GDCM_NAME_SPACE::TagKey t(0x0020,0x0013);
144 //s->AddRestriction(t, "340", GDCM_NAME_SPACE::GDCM_LESS); // Keep only files where
145 // restriction is true
147 s->SetDirectory(dirName, true); // true : recursive exploration
149 // The Dicom file set is splitted into several 'Single SerieUID Files Sets'
150 // (a 'Single SerieUID Files Set' per SerieUID)
151 // In some cases, it's not enough, since, in some cases
152 // we can find scout view with the same SerieUID
155 std::cout << " ---------------------------------------- "
156 << "'Single UID' Filesets found in :["
157 << dirName << "]" << std::endl;
160 std::cout << " ------------------------------------- Result after splitting"
164 std::string systemCommand;
165 std::string filenameout;
168 std::cout << "Check for output directory :[" << dirNameout << "]."
170 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(dirNameout) ) // dirout not found
172 std::string strDirNameout(dirNameout); // to please gcc 4
173 systemCommand = "mkdir " +strDirNameout; // create it!
175 std::cout << systemCommand << std::endl;
176 system (systemCommand.c_str());
177 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(dirNameout) ) // be sure it worked
179 std::cout << "KO : not a dir : [" << dirNameout << "] (creation failure ?)"
186 std::cout << "Directory [" << dirNameout << "] created." << std::endl;
192 std::cout << "Output Directory [" << dirNameout
193 << "] already exists; Used as is."
197 // --> End of checking supposed-to-be-directory names
200 std::string fileName;
202 // For all the Single SerieUID Files Sets of the GDCM_NAME_SPACE::Serie
203 GDCM_NAME_SPACE::FileList *l = s->GetFirstSingleSerieUIDFileSet();
204 GDCM_NAME_SPACE::XCoherentFileSetmap xcm;
206 std::string serieUID;
207 std::string currentSerieWriteDir = "";
208 std::string xCoherentWriteDir = "";
209 std::string xCoherentName = "";
210 std::string serieDirectory;
211 std::string lastFilename;
212 std::string rep("_");
213 int controlCount = 0;
215 while (l) // for each 'Single SerieUID FileSet'
217 nbFiles = l->size() ;
218 if ( l->size() > 0 ) // ignore Series with less than 2 images.
219 // Why not ? Just an example, for testing!
221 serieUID = s->GetCurrentSerieUIDFileSetUID();
222 GDCM_NAME_SPACE::Util::ReplaceSpecChar(serieUID, rep);
227 currentSerieWriteDir = currentSerieWriteDir + dirNameout;
228 unsigned int lg = strlen(dirNameout)-1;
229 if ( dirNameout[lg] != '/' && dirNameout[lg] != '\\')
230 currentSerieWriteDir = currentSerieWriteDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR;
232 currentSerieWriteDir = currentSerieWriteDir + serieUID;
234 std::cout << "[" << currentSerieWriteDir<< "]" << std::endl;
235 // if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(currentSerieWriteDir) )
237 systemCommand = "mkdir " + currentSerieWriteDir;
238 system( systemCommand.c_str());
240 std::cout << "1 " <<systemCommand << std::endl;
245 std::cout << "Split the 'Single SerieUID' FileSet :["
247 << "] " << nbFiles << " long" << std::endl;
248 std::cout << "-----------------------------------" << std::endl;
251 xcm = s->SplitOnOrientation(l);
253 xcm = s->SplitOnPosition(l);
254 else if (groupelem != 0) {
255 xcm = s->SplitOnTagValue(l, groupelem[0],groupelem[1] );
258 for (GDCM_NAME_SPACE::XCoherentFileSetmap::iterator i = xcm.begin();
262 xCoherentName = (*i).first;
264 std::cout << "xCoherentName = " << xCoherentName << std::endl;
265 GDCM_NAME_SPACE::Util::ReplaceSpecChar(serieUID, rep);
269 xCoherentWriteDir = currentSerieWriteDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR+ xCoherentName;
270 // if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(xCoherentWriteDir) )
272 systemCommand = "mkdir " + xCoherentWriteDir;
273 system( systemCommand.c_str());
275 std::cout << systemCommand << std::endl;
281 std::cout << "Orientation : ";
283 std::cout << "Position : ";
284 else if (groupelem != 0)
285 std::cout << "Tag (" << std::hex << groupelem[0]
286 << "|" << groupelem[1] << ") value : ";
287 std::cout << "[" << (*i).first << "]" << std::endl;
290 std::cout << "xCoherentName [" << xCoherentName << "]" << std::endl;
292 // Within a 'just to see' program,
293 // OrderFileList() causes trouble, since some files
294 // (eg:MIP views) don't have 'Position', now considered as mandatory
295 // Commented out for the moment.
298 s->OrderFileList((*i).second); // sort the XCoherent Fileset
299 std::cout << "ZSpacing for the file set " << s->GetZSpacing()
303 for (GDCM_NAME_SPACE::FileList::iterator it = ((*i).second)->begin();
304 it != ((*i).second)->end();
308 fileName = (*it)->GetFileName();
309 std::cout << " " << fileName << std::endl;
314 lastFilename = GDCM_NAME_SPACE::Util::GetName( fileName );
315 filenameout = xCoherentWriteDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR+ lastFilename;
316 systemCommand = "cp " + fileName + " " + filenameout;
317 system( systemCommand.c_str());
319 std::cout << "3 " << systemCommand << std::endl;
323 std::cout << std::endl;
326 l = s->GetNextSingleSerieUIDFileSet();
329 if ( controlCount == 0 )
330 std::cout << "No suitable file was found!" << std::endl;