]> Creatis software - gdcm.git/blob - Example/Rename.cxx
copies and renames the files within a directory as ima-SeriesNumber-ImageNumber.dcm...
[gdcm.git] / Example / Rename.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: Rename.cxx,v $
5   Language:  C++
6   Date:      $Date: 2010/09/01 11:17:32 $
7   Version:   $Revision: 1.1 $
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
19 #include "gdcmFile.h"
20 #include "gdcmDirList.h"
21 #include "gdcmArgMgr.h"
22
23 int main(int argc, char *argv[])
24 {
25    START_USAGE(usage)
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",
29    "                [rec] [fix]                    ",
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  ",   
35       
36    FINISH_USAGE
37    
38    // Initialize Arguments Manager   
39    GDCM_NAME_SPACE::ArgMgr *am= new GDCM_NAME_SPACE::ArgMgr(argc, argv);
40       
41    if (argc == 1 || am->ArgMgrDefined("usage") )
42    {
43       am->ArgMgrUsage(usage); // Display 'usage'
44       delete am;
45       return 1;
46    }
47    
48    const char *dirNameIn   = am->ArgMgrGetString("dirin");
49    const char *dirNameOut  = am->ArgMgrGetString("dirout");   
50    
51    if (am->ArgMgrDefined("debug"))
52       GDCM_NAME_SPACE::Debug::DebugOn();
53
54    if (am->ArgMgrDefined("warning"))
55       GDCM_NAME_SPACE::Debug::WarningOn();
56       
57    bool rec      = ( 0 != am->ArgMgrDefined("rec") );
58    bool fix      = ( 0 != am->ArgMgrDefined("fix") );
59    bool verbose  = ( 0 != am->ArgMgrDefined("verbose") );
60             
61    /* if unused Param we give up */
62    if ( am->ArgMgrPrintUnusedLabels() )
63    {
64       am->ArgMgrUsage(usage);
65       delete am;
66       return 1;
67    } 
68
69    delete am;  // we don't need Argument Manager any longer
70
71    // ----------- End Arguments Manager ---------
72    
73    // =============================== Deal with a Directory =====================
74
75 #ifdef _MSC_VER
76    std::string copyinstr("copy");
77 #else
78    std::string copyinstr("cp");
79 #endif
80    
81    char copy[4096]; // Hope it's enough!
82    
83 /*
84    char format[5]; // "%d" or "%04d"
85
86    if (fix)
87       sprintf(format, "%s", "%04d");
88    else
89       sprintf(format, "%s", "%d");   
90 */      
91   
92    std::cout << "dirNameIn [" << dirNameIn << "]" << std::endl;
93       
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;
97    bool res;
98
99    if (fileList.size() == 0)
100    {
101       std::cout << "No file found in : [" << dirNameIn << "]" << std::endl;
102    }
103
104    // Remember :
105    //0020|0011 [IS]  [Series Number]
106    //0020|0012 [IS]  [Acquisition Number]
107    //0020|0013 [IS]  [Instance Number]
108     
109    std::string  strSeriesNumber;
110    std::string  strAcquisitionNumber;
111    std::string  strInstanceNumber;
112    int SeriesNumber;
113    int AcquisitionNumber;
114    int InstanceNumber;
115       
116    for( GDCM_NAME_SPACE::DirListType::iterator it  = fileList.begin();
117                                  it != fileList.end();
118                                  ++it )
119    {
120       if (verbose)
121          std::cout << std::endl<<" Start processing :[" << it->c_str() << "]"
122                    << std::endl;
123       f = GDCM_NAME_SPACE::File::New();
124       f->SetFileName( it->c_str() );
125       res = f->Load();
126
127       if ( !res )
128       {
129             std::cout << "Cannot process file [" << it->c_str() << "]" 
130                       << std::endl;
131             std::cout << "Either it doesn't exist, or it's read protected " 
132                       << std::endl;
133             std::cout << "or it's not a Dicom File, or its 'header' is bugged" 
134                       << std::endl;
135             std::cout << "use 'PrintFile filein=... debug' "
136                       << "to try to guess the pb"
137                       << std::endl;
138             f->Delete();
139             continue;
140       }
141
142       int number;
143  
144       strSeriesNumber = f->GetEntryString(0x0020,0x0011);
145       if ( strSeriesNumber != GDCM_NAME_SPACE::GDCM_UNFOUND ) 
146       {
147        if (verbose) std::cout << " SeriesNumber [" <<  strSeriesNumber << "]" << std::endl;
148          SeriesNumber = atoi(strSeriesNumber.c_str());
149       }
150     
151       strAcquisitionNumber = f->GetEntryString(0x0020,0x0012);
152       if ( strAcquisitionNumber != GDCM_NAME_SPACE::GDCM_UNFOUND ) 
153       {
154        if (verbose) std::cout << " AcquisitionNumber [" <<  strAcquisitionNumber << "]" << std::endl;
155          AcquisitionNumber = atoi(strAcquisitionNumber.c_str());
156          number = AcquisitionNumber;
157       }  
158
159       strInstanceNumber = f->GetEntryString(0x0020,0x0013);
160       if ( strInstanceNumber != GDCM_NAME_SPACE::GDCM_UNFOUND ) 
161       {
162        if (verbose) std::cout << " InstanceNumber [" <<  strInstanceNumber << "]" << std::endl;
163          InstanceNumber = atoi(strInstanceNumber.c_str());
164          number = InstanceNumber;
165       }
166
167       if (fix) 
168          sprintf (copy, "%s %s %s%cima-%04d-%04d.dcm", 
169                       copyinstr.c_str(),
170                       it->c_str(),
171                       dirNameOut, GDCM_NAME_SPACE::GDCM_FILESEPARATOR, SeriesNumber,  number);
172       else
173          sprintf (copy, "%s %s %s%cima-%d-%d.dcm", 
174                       copyinstr.c_str(),
175                       it->c_str(),
176                       dirNameOut, GDCM_NAME_SPACE::GDCM_FILESEPARATOR, SeriesNumber,  number);
177         
178     
179        if (verbose) std::cout << copy << std::endl;
180        system (copy);      
181    }
182 }      
183       
184