1 /*=========================================================================
4 Module: $RCSfile: SplitIntoXCoherentDirectories.cxx,v $
6 Date: $Date: 2007/10/19 11:53:42 $
7 Version: $Revision: 1.2 $
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"
19 #include "gdcmFileHelper.h"
21 #include "gdcmDebug.h"
23 #include "gdcmDirList.h"
25 #include "gdcmArgMgr.h"
26 #include "gdcmDictSet.h" // for GetName
27 int main(int argc, char *argv[])
31 "\n exXCoherentFileSet :\n ",
32 "Shows the various 'XCoherent' Filesets within a directory ",
33 "Optionaly copis the images in a Directories tree ",
34 "usage: exXCoherentFileSet {dirin=inputDirectoryName} ",
35 " dirout=outputDirectoryName ",
36 " { tag=group-elem | pos | ori } [sort] ",
37 " [{ write | copy }] [studyUID = ] ",
38 " [ { [noshadowseq] | [noshadow][noseq] } ] [debug] ",
40 " dirin : user wants to analyze *all* the files ",
41 " within the directory ",
42 " copy : user wants to copy the files into a directories tree ",
43 " write : user wants to rewrite the files into a directories tree ",
44 " each directory with the same 'Series Instance UID' ",
45 " dirout : will be created if doesn't exist ",
46 " pos : user wants to split each Single SerieUID Fileset on the ",
47 " 'Image Position ' ",
48 " ori : user wants to split each Single SerieUID Fileset on the ",
49 " 'Image Orientation ' ",
50 " tag : group-elem (in hexa, no space) ",
51 " the user wants to split on ",
52 " sort : user wants FileHelper to sort the images ",
53 " Warning : will probabely crah if sort has no meaning ",
54 " (not only look at image names) ",
55 " studyUID : *aware* user wants to add the serie ",
56 " to an already existing study ",
57 " verbose : user wants to run the program in 'verbose mode' ",
58 " debug : developper wants to run the program in 'debug mode' ",
61 // ----- Initialize Arguments Manager ------
63 GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
65 if (am->ArgMgrDefined("usage") || argc == 1)
67 am->ArgMgrUsage(usage); // Display 'usage'
72 if (am->ArgMgrDefined("debug"))
73 GDCM_NAME_SPACE::Debug::DebugOn();
78 const char *dirName = am->ArgMgrGetString("dirin");
82 << "'dirin=' must be present;"
84 am->ArgMgrUsage(usage); // Display 'usage'
89 const char *dirNameout;
90 dirNameout = am->ArgMgrGetString("dirout",".");
92 bool pos = ( 0 != am->ArgMgrDefined("pos") );
93 bool ori = ( 0 != am->ArgMgrDefined("ori") );
94 bool sort = ( 0 != am->ArgMgrDefined("sort") );
95 bool copy = ( 0 != am->ArgMgrDefined("copy") );
96 bool write = ( 0 != am->ArgMgrDefined("write") );
97 bool verbose = ( 0 != am->ArgMgrDefined("verbose") );
98 bool tag = ( 0 != am->ArgMgrDefined("tag") );
102 std::cout << "COPY and WRITE are mutually exclusive" << std::endl;
107 if( (tag && (pos || ori)) || (pos && (tag || ori)) || (ori && (tag || pos)) )
109 std::cout << " POS, ORI and TAG are mutually exclusive" << std::endl;
114 if( (!tag && !pos && !ori))
116 std::cout << " One of POS, ORI and TAG is mandatory!" << std::endl;
124 groupelem = am->ArgMgrGetXInt16Enum("tag", &nb);
127 std::cout << "TAG : one and only one group,elem!" << std::endl;
133 bool userDefinedStudy = ( 0 != am->ArgMgrDefined("userDefinedStudy") );
134 const char *studyUID = am->ArgMgrGetString("studyUID");
136 /* if unused Param we give up */
137 if ( am->ArgMgrPrintUnusedLabels() )
139 am->ArgMgrUsage(usage);
144 delete am; // ------ we don't need Arguments Manager any longer ------
148 GDCM_NAME_SPACE::SerieHelper *s;
149 s = GDCM_NAME_SPACE::SerieHelper::New();
151 GDCM_NAME_SPACE::File *f;
153 GDCM_NAME_SPACE::DirList dirlist(dirName, true); // recursive exploration
154 GDCM_NAME_SPACE::DirListType fileNames = dirlist.GetFilenames();
156 GDCM_NAME_SPACE::FileList *l = new GDCM_NAME_SPACE::FileList;
157 // Loop on all the gdcm-readable files
158 for (GDCM_NAME_SPACE::DirListType::iterator it = fileNames.begin();
159 it != fileNames.end();
163 std::cout << *it << std::endl;
166 loadMode = GDCM_NAME_SPACE::LD_ALL; // load any DataElement
167 maxSize = 0x7fff; // load any length
169 loadMode = GDCM_NAME_SPACE::LD_NOSEQ | GDCM_NAME_SPACE::LD_NOSHADOW ;
173 f = GDCM_NAME_SPACE::File::New();
174 f->SetLoadMode(loadMode);
175 f->SetMaxSizeLoadEntry(maxSize);
176 f->SetFileName( *it );
181 std::string systemCommand;
182 std::string filenameout;
185 std::cout << "Check for output directory :[" << dirNameout << "]."
187 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(dirNameout) ) // dirout not found
189 std::string strDirNameout(dirNameout); // to please gcc 4
190 systemCommand = "mkdir " +strDirNameout; // create it!
192 std::cout << systemCommand << std::endl;
193 system (systemCommand.c_str());
194 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(dirNameout) ) // be sure it worked
196 std::cout << "KO : not a dir : [" << dirNameout << "] (creation failure ?)"
203 std::cout << "Directory [" << dirNameout << "] created." << std::endl;
209 std::cout << "Output Directory [" << dirNameout
210 << "] already exists; Used as is."
214 // --> End of checking supposed-to-be-directory names
217 std::string fileName;
219 // For all the Single SerieUID Files Sets of the GDCM_NAME_SPACE::Serie
221 //GDCM_NAME_SPACE::FileList *l = s->GetFirstSingleSerieUIDFileSet();//===> Ignore 'Serie UID"
223 GDCM_NAME_SPACE::XCoherentFileSetmap xcm;
225 std::string serieUID;
226 std::string currentSerieWriteDir = "";
227 std::string xCoherentWriteDir = "";
228 std::string xCoherentName = "";
229 std::string serieDirectory;
230 std::string lastFilename;
231 std::string rep("_");
232 int controlCount = 0;
234 // 'Study Instance UID'
235 // The user is allowed to create his own Study,
236 // keeping the same 'Study Instance UID' for various images
237 // The user may add images to a 'Manufacturer Study',
238 // adding new Series to an already existing Study
239 std::string strStudyUID;
241 if ( !userDefinedStudy )
242 strStudyUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
244 strStudyUID = studyUID;
247 // while (l) // for each 'Single SerieUID FileSet' //===> Ignore 'Serie UID"
249 nbFiles = l->size() ;
250 if ( l->size() > 2 ) // ignore a Directory with less than 2 images.
251 // Why not ? Just an example, for testing!
253 // Just not to make too many modif in the code
254 serieUID = "SingleSerie"; //s->GetCurrentSerieUIDFileSetUID();
255 GDCM_NAME_SPACE::Util::ReplaceSpecChar(serieUID, rep);
260 currentSerieWriteDir = currentSerieWriteDir + dirNameout;
261 unsigned int l = strlen(dirNameout)-1;
262 if ( dirNameout[l] != '/' && dirNameout[l] != '\\')
263 currentSerieWriteDir = currentSerieWriteDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR;
265 currentSerieWriteDir = currentSerieWriteDir + serieUID;
267 std::cout << "[" << currentSerieWriteDir<< "]" << std::endl;
268 // if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(currentSerieWriteDir) )
270 systemCommand = "mkdir " + currentSerieWriteDir;
271 system( systemCommand.c_str());
273 std::cout << "1 " <<systemCommand << std::endl;
278 std::cout << "Split the 'Single SerieUID' FileSet :["
280 << "] " << nbFiles << " long" << std::endl;
281 std::cout << "-----------------------------------" << std::endl;
283 GDCM_NAME_SPACE::Debug::DebugOn();
287 xcm = s->SplitOnOrientation(l);
289 xcm = s->SplitOnPosition(l);
290 else if (groupelem != 0) {
292 std:: cout << GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->GetEntry(groupelem[0],groupelem[1])->GetName() << std::endl;
294 xcm = s->SplitOnTagValue(l, groupelem[0],groupelem[1] );
297 GDCM_NAME_SPACE::FileHelper *fh;
299 for (GDCM_NAME_SPACE::XCoherentFileSetmap::iterator i = xcm.begin();
303 xCoherentName = (*i).first;
305 std::cout << "xCoherentName = " << xCoherentName << std::endl;
306 GDCM_NAME_SPACE::Util::ReplaceSpecChar(xCoherentName, rep);
310 xCoherentWriteDir = currentSerieWriteDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR+ xCoherentName;
311 // if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(xCoherentWriteDir) )
313 systemCommand = "mkdir " + xCoherentWriteDir;
314 system( systemCommand.c_str());
316 std::cout << "2 " << systemCommand << std::endl;
322 std::cout << "Orientation : ";
324 std::cout << "Position : ";
325 else if (groupelem != 0)
326 std::cout << "Tag (" << std::hex << groupelem[0]
327 << "|" << groupelem[1] << ") value : ";
328 std::cout << "[" << (*i).first << "]" << std::endl;
331 std::cout << "xCoherentName [" << xCoherentName << "]" << std::endl;
333 // Within a 'just to see' program,
334 // OrderFileList() causes trouble, since some files
335 // (eg:MIP views) don't have 'Position', now considered as mandatory
336 // --> Activated on user demand.
339 s->OrderFileList((*i).second); // sort the XCoherent Fileset
340 std::cout << "ZSpacing for the file set " << s->GetZSpacing()
344 std::string strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
346 for (GDCM_NAME_SPACE::FileList::iterator it2 = ((*i).second)->begin();
347 it2 != ((*i).second)->end();
351 fileName = (*it2)->GetFileName();
352 std::cout << " " << fileName << std::endl;
355 lastFilename = GDCM_NAME_SPACE::Util::GetName( fileName );
356 filenameout = xCoherentWriteDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR+ lastFilename;
359 fh = GDCM_NAME_SPACE::FileHelper::New( (*it2) );
360 fh->SetKeepOverlays( true );
361 fh->InsertEntryString(strSerieUID,0x0020,0x000e,"UI");
362 unsigned int dataSize = fh->GetImageDataRawSize();
363 uint8_t *imageData = fh->GetImageDataRaw();// somewhat important : Loads the Pixels in memory !
365 std::cout << "fail to read [" << (*it2)->GetFileName() << std::endl;
366 fh->SetWriteTypeToDcmExplVR();
367 fh->SetContentType(GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE);
368 if (!fh->Write(filenameout))
370 std::cout << "Fail to write :[" << filenameout << "]"
377 systemCommand = "cp " + fileName + " " + filenameout;
378 system( systemCommand.c_str());
381 std::cout << "3 " << systemCommand << std::endl;
384 std::cout << std::endl;
387 // l = s->GetNextSingleSerieUIDFileSet(); //===> Ignore 'Serie UID"
389 if ( controlCount == 0 )
390 std::cout << "No suitable file was found!" << std::endl;