1 /*=========================================================================
4 Module: $RCSfile: Rename.cxx,v $
6 Date: $Date: 2010/09/01 11:17:32 $
7 Version: $Revision: 1.1 $
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 =========================================================================*/
20 #include "gdcmDirList.h"
21 #include "gdcmArgMgr.h"
23 int main(int argc, char *argv[])
26 " \n copies and renames the files within a directory as ima-SeriesNumber-ImageNumber.dcm (usefull for unaware users!)\n",
27 " usage: Rename dirin=inputDirectoryName ",
28 " dirout=outputDirectoryName",
30 " rec : user wants to parse recursively the directory ",
31 " fix : user want fix length (4) for SeriesNumber/ ImageNumber ",
32 " debug : user wants to run the program in 'debug mode' ",
33 " warning : user wants to be warned about any oddity in the File ",
34 " verose : user wants to be warned about the programm processing ",
38 // Initialize Arguments Manager
39 GDCM_NAME_SPACE::ArgMgr *am= new GDCM_NAME_SPACE::ArgMgr(argc, argv);
41 if (argc == 1 || am->ArgMgrDefined("usage") )
43 am->ArgMgrUsage(usage); // Display 'usage'
48 const char *dirNameIn = am->ArgMgrGetString("dirin");
49 const char *dirNameOut = am->ArgMgrGetString("dirout");
51 if (am->ArgMgrDefined("debug"))
52 GDCM_NAME_SPACE::Debug::DebugOn();
54 if (am->ArgMgrDefined("warning"))
55 GDCM_NAME_SPACE::Debug::WarningOn();
57 bool rec = ( 0 != am->ArgMgrDefined("rec") );
58 bool fix = ( 0 != am->ArgMgrDefined("fix") );
59 bool verbose = ( 0 != am->ArgMgrDefined("verbose") );
61 /* if unused Param we give up */
62 if ( am->ArgMgrPrintUnusedLabels() )
64 am->ArgMgrUsage(usage);
69 delete am; // we don't need Argument Manager any longer
71 // ----------- End Arguments Manager ---------
73 // =============================== Deal with a Directory =====================
76 std::string copyinstr("copy");
78 std::string copyinstr("cp");
81 char copy[4096]; // Hope it's enough!
84 char format[5]; // "%d" or "%04d"
87 sprintf(format, "%s", "%04d");
89 sprintf(format, "%s", "%d");
92 std::cout << "dirNameIn [" << dirNameIn << "]" << std::endl;
94 GDCM_NAME_SPACE::DirList dirList(dirNameIn, rec); // gets recursively (or not) the file list
95 GDCM_NAME_SPACE::DirListType fileList = dirList.GetFilenames();
96 GDCM_NAME_SPACE::File *f;
99 if (fileList.size() == 0)
101 std::cout << "No file found in : [" << dirNameIn << "]" << std::endl;
105 //0020|0011 [IS] [Series Number]
106 //0020|0012 [IS] [Acquisition Number]
107 //0020|0013 [IS] [Instance Number]
109 std::string strSeriesNumber;
110 std::string strAcquisitionNumber;
111 std::string strInstanceNumber;
113 int AcquisitionNumber;
116 for( GDCM_NAME_SPACE::DirListType::iterator it = fileList.begin();
117 it != fileList.end();
121 std::cout << std::endl<<" Start processing :[" << it->c_str() << "]"
123 f = GDCM_NAME_SPACE::File::New();
124 f->SetFileName( it->c_str() );
129 std::cout << "Cannot process file [" << it->c_str() << "]"
131 std::cout << "Either it doesn't exist, or it's read protected "
133 std::cout << "or it's not a Dicom File, or its 'header' is bugged"
135 std::cout << "use 'PrintFile filein=... debug' "
136 << "to try to guess the pb"
144 strSeriesNumber = f->GetEntryString(0x0020,0x0011);
145 if ( strSeriesNumber != GDCM_NAME_SPACE::GDCM_UNFOUND )
147 if (verbose) std::cout << " SeriesNumber [" << strSeriesNumber << "]" << std::endl;
148 SeriesNumber = atoi(strSeriesNumber.c_str());
151 strAcquisitionNumber = f->GetEntryString(0x0020,0x0012);
152 if ( strAcquisitionNumber != GDCM_NAME_SPACE::GDCM_UNFOUND )
154 if (verbose) std::cout << " AcquisitionNumber [" << strAcquisitionNumber << "]" << std::endl;
155 AcquisitionNumber = atoi(strAcquisitionNumber.c_str());
156 number = AcquisitionNumber;
159 strInstanceNumber = f->GetEntryString(0x0020,0x0013);
160 if ( strInstanceNumber != GDCM_NAME_SPACE::GDCM_UNFOUND )
162 if (verbose) std::cout << " InstanceNumber [" << strInstanceNumber << "]" << std::endl;
163 InstanceNumber = atoi(strInstanceNumber.c_str());
164 number = InstanceNumber;
168 sprintf (copy, "%s %s %s%cima-%04d-%04d.dcm",
171 dirNameOut, GDCM_NAME_SPACE::GDCM_FILESEPARATOR, SeriesNumber, number);
173 sprintf (copy, "%s %s %s%cima-%d-%d.dcm",
176 dirNameOut, GDCM_NAME_SPACE::GDCM_FILESEPARATOR, SeriesNumber, number);
179 if (verbose) std::cout << copy << std::endl;