1 /*=========================================================================
\r
4 Module: $RCSfile: Bmp2Dcm.cxx,v $
\r
6 Date: $Date: 2007/06/26 15:40:38 $
\r
7 Version: $Revision: 1.1 $
\r
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
\r
10 l'Image). All rights reserved. See Doc/License.txt or
\r
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
\r
13 This software is distributed WITHOUT ANY WARRANTY; without even
\r
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
15 PURPOSE. See the above copyright notices for more information.
\r
17 =========================================================================*/
\r
19 #include "gdcmArgMgr.h"
\r
20 #include "gdcmDirList.h"
\r
21 #include "gdcmDebug.h"
\r
22 #include "gdcmUtil.h"
\r
24 #include <vtkImageData.h>
\r
26 #include <vtkBMPReader.h>
\r
27 #include <vtkBMPWriter.h>
\r
28 #include "vtkGdcmWriter.h"
\r
30 #include <vtkImageExtractComponents.h>
\r
31 #include <vtkPointData.h>
\r
32 #include <vtkDataArray.h>
\r
39 //---------------------------------------------------------------------------
\r
41 int main( int argc, char *argv[] )
\r
45 "usage: Bmp2Dcm {filein=inputFileName|dirin=inputDirectoryName} ",
\r
46 " [studyUID = ] [patName = ] [debug] ",
\r
48 " inputFileName : Name of the (single) file user wants to transform ",
\r
49 " inputDirectoryName : user wants to transform *all* the files ",
\r
50 " studyUID : *aware* user wants to add the serie ",
\r
51 " to an already existing study ",
\r
52 " verbose : user wants to run the program in 'verbose mode' ",
\r
53 " debug : *developper* wants to run the program in 'debug mode' ",
\r
57 // ----- Initialize Arguments Manager ------
\r
59 GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
\r
61 if (am->ArgMgrDefined("usage") || argc == 1)
\r
63 am->ArgMgrUsage(usage); // Display 'usage'
\r
68 if (am->ArgMgrDefined("debug"))
\r
69 GDCM_NAME_SPACE::Debug::DebugOn();
\r
71 int verbose = am->ArgMgrDefined("verbose");
\r
73 const char *fileName = am->ArgMgrGetString("filein");
\r
74 const char *dirName = am->ArgMgrGetString("dirin");
\r
76 if ( (fileName == 0 && dirName == 0)
\r
78 (fileName != 0 && dirName != 0) )
\r
80 std::cout <<std::endl
\r
81 << "Either 'filein=' or 'dirin=' must be present;"
\r
82 << std::endl << "Not both" << std::endl;
\r
83 am->ArgMgrUsage(usage); // Display 'usage'
\r
88 std::string patName = am->ArgMgrGetString("patname", dirName);
\r
90 bool userDefinedStudy = am->ArgMgrDefined("studyUID");
\r
91 const char *studyUID = am->ArgMgrGetString("studyUID");
\r
93 // not described *on purpose* in the Usage !
\r
94 bool userDefinedSerie = am->ArgMgrDefined("serieUID");
\r
95 const char *serieUID = am->ArgMgrGetString("serieUID");
\r
97 /* if unused Param we give up */
\r
98 if ( am->ArgMgrPrintUnusedLabels() )
\r
100 am->ArgMgrUsage(usage);
\r
105 delete am; // ------ we don't need Arguments Manager any longer ------
\r
108 std::string nomFich;
\r
110 if ( fileName != 0 ) // ====== Deal with a single file ======
\r
112 vtkBMPReader* Reader = vtkBMPReader::New();
\r
113 if ( Reader->CanReadFile(fileName ) == 0) {
\r
114 // skip 'non BMP' files
\r
117 std::cout << "Sorry, [" << fileName << "] is not a BMP file!" << std::endl;
\r
122 std::cout << "deal with [" << fileName << "]" << std::endl;
\r
125 Reader->SetFileName(fileName);
\r
128 vtkImageExtractComponents* Red = vtkImageExtractComponents::New();
\r
129 Red->SetInput(Reader->GetOutput());
\r
130 Red->SetComponents(0);
\r
133 vtkGdcmWriter* Writer = vtkGdcmWriter::New();
\r
134 Writer->SetInput(Red->GetOutput());
\r
136 nomFich = nomFich+fileName+".acr";
\r
144 else // ====== Deal with a (single Patient) Directory ======
\r
146 std::string strStudyUID;
\r
147 std::string strSerieUID;
\r
149 if (userDefinedStudy)
\r
150 strSerieUID = studyUID;
\r
152 strStudyUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
\r
154 if (userDefinedStudy)
\r
155 strSerieUID = serieUID;
\r
157 strStudyUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
\r
160 std::cout << "dirName [" << dirName << "]" << std::endl;
\r
162 GDCM_NAME_SPACE::DirList dirList(dirName,1); // gets recursively the file list
\r
163 GDCM_NAME_SPACE::DirListType fileList = dirList.GetFilenames();
\r
165 for( GDCM_NAME_SPACE::DirListType::iterator it = fileList.begin();
\r
166 it != fileList.end();
\r
169 if ( GDCM_NAME_SPACE::Util::GetName((*it)).c_str()[0] == '.' )
\r
171 // skip hidden files
\r
175 vtkBMPReader* Reader = vtkBMPReader::New();
\r
177 if ( Reader->CanReadFile(it->c_str() ) == 0) {
\r
178 // skip 'non BMP' files
\r
184 std::cout << "deal with [" << it->c_str() << "]" << std::endl;
\r
186 Reader->SetFileName(it->c_str());
\r
189 dim=Reader->GetOutput()->GetDimensions();
\r
190 vtkImageExtractComponents* Red = vtkImageExtractComponents::New();
\r
191 Red->SetInput(Reader->GetOutput());
\r
192 Red->SetComponents(0);
\r
195 // At least, created files will look like a Serie from a single Study ...
\r
196 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
\r
197 f->InsertEntryString(strStudyUID, 0x0020, 0x000d, "UI");
\r
198 f->InsertEntryString(strSerieUID, 0x0020, 0x000e, "UI");
\r
199 f->InsertEntryString(patName, 0x0010, 0x0010, "PN"); // Patient's Name
\r
201 vtkGdcmWriter* Writer = vtkGdcmWriter::New();
\r
202 Writer->SetInput(Red->GetOutput());
\r
204 nomFich = nomFich+it->c_str()+".acr";
\r
205 Writer->SetFileName(nomFich.c_str());
\r
206 Writer->SetGdcmFile(f);
\r