1 /*=========================================================================
4 Module: $RCSfile: SplitIntoXCoherentDirectoriesIgnoreSerieUID.cxx,v $
6 Date: $Date: 2011/03/29 07:35:58 $
7 Version: $Revision: 1.3 $
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 SplitIntoCoherentFileSetIgnoreSerieUID :\n ",
32 "Shows the various 'XCoherent' Filesets within a directory ",
33 "Optionaly copis the images in a Directories tree ",
34 "usage: SplitIntoCoherentFileSetIgnoreSerieUID{dirin=inputDirectoryName} ",
35 " dirout=outputDirectoryName ",
36 " { tag=group-elem | pos | ori } [sort] ",
37 " [{ write | copy }] ",
38 " [ { [noshadowseq] | [noshadow][noseq] } ] [debug] ",
40 " dirin : user wants to analyze *all* the files ",
41 " within the directory ",
42 " write : user wants to create directories ",
43 " dirout : will be created if doesn't exist ",
44 " pos : user wants to split each Single SerieUID Fileset on the ",
45 " 'Image Position ' ",
46 " ori : user wants to split each Single SerieUID Fileset on the ",
47 " 'Image Orientation ' ",
48 " tag : group-elem (in hexa, no space) ",
49 " the user wants to split on ",
50 " sort : user wants FileHelper to sort the images ",
51 " Warning : will probabely crash if sort has no meaning ",
52 " (not only look at image names) ",
53 " studyUID : *aware* user wants to add the serie ",
54 " to an already existing study ",
55 " verbose : user wants to run the program in 'verbose mode' ",
56 " debug : developper wants to run the program in 'debug mode' ",
59 // ----- Initialize Arguments Manager ------
61 GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
63 if (am->ArgMgrDefined("usage") || argc == 1)
65 am->ArgMgrUsage(usage); // Display 'usage'
70 if (am->ArgMgrDefined("debug"))
71 GDCM_NAME_SPACE::Debug::DebugOn();
73 int loadMode = GDCM_NAME_SPACE::LD_ALL;
75 const char *dirName = am->ArgMgrGetString("dirin");
79 << "'dirin=' must be present;"
81 am->ArgMgrUsage(usage); // Display 'usage'
86 const char *dirNameout;
87 dirNameout = am->ArgMgrGetString("dirout",".");
89 bool pos = ( 0 != am->ArgMgrDefined("pos") );
90 bool ori = ( 0 != am->ArgMgrDefined("ori") );
91 bool sort = ( 0 != am->ArgMgrDefined("sort") );
92 bool copy = ( 0 != am->ArgMgrDefined("copy") );
93 bool write = ( 0 != am->ArgMgrDefined("write") );
94 bool verbose = ( 0 != am->ArgMgrDefined("verbose") );
95 bool tag = ( 0 != am->ArgMgrDefined("tag") );
99 std::cout << "COPY and WRITE are mutually exclusive" << std::endl;
104 if( (tag && (pos || ori)) || (pos && (tag || ori)) || (ori && (tag || pos)) )
106 std::cout << " POS, ORI and TAG are mutually exclusive" << std::endl;
111 if( (!tag && !pos && !ori))
113 std::cout << " One of POS, ORI and TAG is mandatory!" << std::endl;
121 groupelem = am->ArgMgrGetXInt16Enum("tag", &nb);
124 std::cout << "TAG : one and only one group,elem!" << std::endl;
130 bool userDefinedStudy = ( 0 != am->ArgMgrDefined("userDefinedStudy") );
131 const char *studyUID = am->ArgMgrGetString("studyUID");
133 /* if unused Param we give up */
134 if ( am->ArgMgrPrintUnusedLabels() )
136 am->ArgMgrUsage(usage);
141 delete am; // ------ we don't need Arguments Manager any longer ------
144 GDCM_NAME_SPACE::SerieHelper *s;
145 s = GDCM_NAME_SPACE::SerieHelper::New();
147 GDCM_NAME_SPACE::File *f;
149 GDCM_NAME_SPACE::DirList dirlist(dirName, true); // recursive exploration
150 GDCM_NAME_SPACE::DirListType fileNames = dirlist.GetFilenames();
152 GDCM_NAME_SPACE::FileList *l = new GDCM_NAME_SPACE::FileList;
153 // Loop on all the gdcm-readable files
154 for (GDCM_NAME_SPACE::DirListType::iterator it = fileNames.begin();
155 it != fileNames.end();
159 std::cout << *it << std::endl;
160 f = GDCM_NAME_SPACE::File::New();
161 f->SetLoadMode(loadMode); // load any DataElement
162 f->SetMaxSizeLoadEntry(0x7fff); // load any length
163 f->SetFileName( *it );
169 /* ===========================================================================================*/
170 std::string systemCommand;
171 std::string filenameout;
174 std::cout << "Check for output directory :[" << dirNameout << "]."
176 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(dirNameout) ) // dirout not found
178 std::string strDirNameout(dirNameout); // to please gcc 4
179 systemCommand = "mkdir " +strDirNameout; // create it!
181 std::cout << systemCommand << std::endl;
182 system (systemCommand.c_str());
183 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(dirNameout) ) // be sure it worked
185 std::cout << "KO : not a dir : [" << dirNameout << "] (creation failure ?)"
192 std::cout << "Directory [" << dirNameout << "] created." << std::endl;
198 std::cout << "Output Directory [" << dirNameout
199 << "] already exists; Used as is."
203 // --> End of checking supposed-to-be-directory names
206 std::string fileName;
208 // For all the Single SerieUID Files Sets of the GDCM_NAME_SPACE::Serie
210 // GDCM_NAME_SPACE::FileList *l = s->GetFirstSingleSerieUIDFileSet();//===> Ignore 'Serie UID"
212 GDCM_NAME_SPACE::XCoherentFileSetmap xcm;
214 std::string serieUID;
215 std::string currentSerieWriteDir = "";
216 std::string xCoherentWriteDir = "";
217 std::string xCoherentName = "";
218 std::string serieDirectory;
219 std::string lastFilename;
220 std::string rep("_");
221 int controlCount = 0;
223 // 'Study Instance UID'
224 // The user is allowed to create his own Study,
225 // keeping the same 'Study Instance UID' for various images
226 // The user may add images to a 'Manufacturer Study',
227 // adding new Series to an already existing Study
228 std::string strStudyUID;
229 if ( !userDefinedStudy )
230 strStudyUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
232 strStudyUID = studyUID;
235 // while (l) // for each 'Single SerieUID FileSet' //===> Ignore 'Serie UID"
237 nbFiles = l->size() ;
238 if ( l->size() > 2 ) // ignore a Directory with less than 2 images.
239 // Why not ? Just an example, for testing!
241 // Just not to make too many modif in the code
242 //serieUID = "SingleSerie"; // s->GetCurrentSerieUIDFileSetUID();
243 serieUID = s->GetCurrentSerieUIDFileSetUID();
244 GDCM_NAME_SPACE::Util::ReplaceSpecChar(serieUID, rep);
249 currentSerieWriteDir = currentSerieWriteDir + dirNameout;
250 unsigned int l = strlen(dirNameout)-1;
251 if ( dirNameout[l] != '/' && dirNameout[l] != '\\')
252 currentSerieWriteDir = currentSerieWriteDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR;
254 currentSerieWriteDir = currentSerieWriteDir + serieUID;
256 std::cout << "[" << currentSerieWriteDir<< "]" << std::endl;
257 // if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(currentSerieWriteDir) )
259 systemCommand = "mkdir " + currentSerieWriteDir;
260 system( systemCommand.c_str());
262 std::cout << "1 " <<systemCommand << std::endl;
267 std::cout << "Split the 'Single SerieUID' FileSet :["
269 << "] " << nbFiles << " long" << std::endl;
270 std::cout << "-----------------------------------" << std::endl;
273 xcm = s->SplitOnOrientation(l);
275 xcm = s->SplitOnPosition(l);
276 else if (groupelem != 0) {
278 std:: cout << GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->GetEntry(groupelem[0],groupelem[1])->GetName() << std::endl;
280 xcm = s->SplitOnTagValue(l, groupelem[0],groupelem[1] );
283 GDCM_NAME_SPACE::FileHelper *fh;
285 for (GDCM_NAME_SPACE::XCoherentFileSetmap::iterator i = xcm.begin();
289 xCoherentName = (*i).first;
291 std::cout << "xCoherentName = " << xCoherentName << std::endl;
292 GDCM_NAME_SPACE::Util::ReplaceSpecChar(xCoherentName, rep);
296 xCoherentWriteDir = currentSerieWriteDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR+ xCoherentName;
297 // if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(xCoherentWriteDir) )
299 systemCommand = "mkdir " + xCoherentWriteDir;
300 system( systemCommand.c_str());
302 std::cout << "2 " << systemCommand << std::endl;
308 std::cout << "Orientation : ";
310 std::cout << "Position : ";
311 else if (groupelem != 0)
312 std::cout << "Tag (" << std::hex << groupelem[0]
313 << "|" << groupelem[1] << ") value : ";
314 std::cout << "[" << (*i).first << "]" << std::endl;
317 std::cout << "xCoherentName [" << xCoherentName << "]" << std::endl;
319 // Within a 'just to see' program,
320 // OrderFileList() causes trouble, since some files
321 // (eg:MIP views) don't have 'Position', now considered as mandatory
322 // --> Activated on user demand.
325 s->OrderFileList((*i).second); // sort the XCoherent Fileset
326 std::cout << "ZSpacing for the file set " << s->GetZSpacing()
330 std::string strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
332 for (GDCM_NAME_SPACE::FileList::iterator it2 = ((*i).second)->begin();
333 it2 != ((*i).second)->end();
337 fileName = (*it2)->GetFileName();
338 std::cout << " " << fileName << std::endl;
343 fh = GDCM_NAME_SPACE::FileHelper::New( (*it2) );
344 fh->SetKeepOverlays( true );
345 fh->InsertEntryString(strSerieUID,0x0020,0x000e,"UI");
346 unsigned int dataSize = fh->GetImageDataRawSize();
347 uint8_t *imageData = fh->GetImageDataRaw();// somewhat important : Loads the Pixels in memory !
349 std::cout << "fail to read [" << (*it2)->GetFileName() << std::endl;
350 lastFilename = GDCM_NAME_SPACE::Util::GetName( fileName );
351 filenameout = xCoherentWriteDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR+ lastFilename;
352 //systemCommand = "cp " + fileName + " " + filenameout;
353 //system( systemCommand.c_str());
355 fh->SetWriteTypeToDcmExplVR();
356 fh->SetContentType(GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE);
357 if (!fh->Write(filenameout))
359 std::cout << "Fail to write :[" << filenameout << "]"
363 std::cout << "3 " << systemCommand << std::endl;
368 std::cout << std::endl;
371 // l = s->GetNextSingleSerieUIDFileSet(); //===> Ignore 'Serie UID"
374 if ( controlCount == 0 )
375 std::cout << "No suitable file was found!" << std::endl;
378 /*===============================================================================================*/