1 /*=========================================================================
4 Module: $RCSfile: Bmp2Dcm.cxx,v $
6 Date: $Date: 2008/01/28 12:47:07 $
7 Version: $Revision: 1.4 $
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 =========================================================================*/
19 #include "gdcmArgMgr.h"
20 #include "gdcmDirList.h"
21 #include "gdcmDebug.h"
24 #include <vtkImageData.h>
26 #include <vtkBMPReader.h>
27 #include <vtkBMPWriter.h>
28 #include "vtkGdcmWriter.h"
30 #include <vtkImageExtractComponents.h>
31 #include <vtkPointData.h>
32 #include <vtkDataArray.h>
39 //---------------------------------------------------------------------------
41 int main( int argc, char *argv[] )
45 "usage: Bmp2Dcm {filein=inputFileName|dirin=inputDirectoryName} ",
46 " [studyUID = ] [patName = ] [debug] ",
48 " inputFileName : Name of the (single) file user wants to transform ",
49 " inputDirectoryName : user wants to transform *all* the files ",
50 " studyUID : *aware* user wants to add the serie ",
51 " to an already existing study ",
52 " verbose : user wants to run the program in 'verbose mode' ",
53 " debug : *developper* wants to run the program in 'debug mode' ",
57 // ----- Initialize Arguments Manager ------
59 GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
61 if (am->ArgMgrDefined("usage") || argc == 1)
63 am->ArgMgrUsage(usage); // Display 'usage'
68 if (am->ArgMgrDefined("debug"))
69 GDCM_NAME_SPACE::Debug::DebugOn();
71 bool verbose = ( 0 != am->ArgMgrDefined("verbose") );
73 const char *fileName = am->ArgMgrGetString("filein");
74 const char *dirName = am->ArgMgrGetString("dirin");
76 if ( (fileName == 0 && dirName == 0)
78 (fileName != 0 && dirName != 0) )
81 << "Either 'filein=' or 'dirin=' must be present;"
82 << std::endl << "Not both" << std::endl;
83 am->ArgMgrUsage(usage); // Display 'usage'
88 std::string patName = am->ArgMgrGetString("patname", dirName);
89 bool userDefinedStudy = ( 0 != am->ArgMgrDefined("studyUID") );
92 studyUID = am->ArgMgrGetString("studyUID");
94 // not described *on purpose* in the Usage !
95 bool userDefinedSerie = ( 0 != am->ArgMgrDefined("serieUID") );
98 serieUID = am->ArgMgrGetString("serieUID");
100 /* if unused Param we give up */
101 if ( am->ArgMgrPrintUnusedLabels() )
103 am->ArgMgrUsage(usage);
108 delete am; // ------ we don't need Arguments Manager any longer ------
111 // ----- Begin Processing -----
116 if ( fileName != 0 ) // ====== Deal with a single file ======
118 vtkBMPReader* Reader = vtkBMPReader::New();
119 if ( Reader->CanReadFile(fileName ) == 0) {
120 // skip 'non BMP' files
123 std::cout << "Sorry, [" << fileName << "] is not a BMP file!" << std::endl;
128 std::cout << "deal with [" << fileName << "]" << std::endl;
131 Reader->SetFileName(fileName);
134 vtkImageExtractComponents* Red = vtkImageExtractComponents::New();
135 Red->SetInput(Reader->GetOutput());
136 Red->SetComponents(0);
139 vtkGdcmWriter* Writer = vtkGdcmWriter::New();
140 Writer->SetInput(Red->GetOutput());
142 nomFich = nomFich+fileName+".acr";
150 else // ====== Deal with a (single Patient) Directory ======
153 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(dirName) )
155 std::cout << "KO : [" << dirName << "] is not a Directory." << std::endl;
161 std::cout << "OK : [" << dirName << "] is a Directory." << std::endl;
163 std::string strStudyUID;
164 std::string strSerieUID;
166 if (userDefinedStudy)
167 strSerieUID = studyUID;
169 strStudyUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
171 if (userDefinedStudy)
172 strSerieUID = serieUID;
174 strStudyUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
177 std::cout << "dirName [" << dirName << "]" << std::endl;
179 GDCM_NAME_SPACE::DirList dirList(dirName,1); // gets recursively the file list
180 GDCM_NAME_SPACE::DirListType fileList = dirList.GetFilenames();
182 for( GDCM_NAME_SPACE::DirListType::iterator it = fileList.begin();
183 it != fileList.end();
186 if ( GDCM_NAME_SPACE::Util::GetName((*it)).c_str()[0] == '.' )
192 vtkBMPReader* Reader = vtkBMPReader::New();
194 if ( Reader->CanReadFile(it->c_str() ) == 0) {
195 // skip 'non BMP' files
201 std::cout << "deal with [" << it->c_str() << "]" << std::endl;
203 Reader->SetFileName(it->c_str());
206 dim=Reader->GetOutput()->GetDimensions();
207 vtkImageExtractComponents* Red = vtkImageExtractComponents::New();
208 Red->SetInput(Reader->GetOutput());
209 Red->SetComponents(0);
212 // At least, created files will look like a Serie from a single Study ...
213 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
214 f->InsertEntryString(strStudyUID, 0x0020, 0x000d, "UI");
215 f->InsertEntryString(strSerieUID, 0x0020, 0x000e, "UI");
216 f->InsertEntryString(patName, 0x0010, 0x0010, "PN"); // Patient's Name
218 vtkGdcmWriter* Writer = vtkGdcmWriter::New();
219 Writer->SetInput(Red->GetOutput());
221 nomFich = nomFich+it->c_str()+".acr";
222 Writer->SetFileName(nomFich.c_str());
223 Writer->SetGdcmFile(f);