]> Creatis software - gdcm.git/blob - vtk/Bmp2Dcm.cxx
Fix mistypings
[gdcm.git] / vtk / Bmp2Dcm.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: Bmp2Dcm.cxx,v $
5   Language:  C++
6   Date:      $Date: 2008/01/28 12:47:07 $
7   Version:   $Revision: 1.4 $
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 "gdcmArgMgr.h"
20 #include "gdcmDirList.h"
21 #include "gdcmDebug.h"
22 #include "gdcmUtil.h"
23
24 #include <vtkImageData.h>
25
26 #include <vtkBMPReader.h>
27 #include <vtkBMPWriter.h>
28 #include "vtkGdcmWriter.h"
29
30 #include <vtkImageExtractComponents.h>
31 #include <vtkPointData.h>
32 #include <vtkDataArray.h>
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <math.h>
38
39 //---------------------------------------------------------------------------
40
41 int main( int argc, char *argv[] )
42 {
43    START_USAGE(usage)
44    "\n Bmp2Dcm :\n                                                            ",  
45    "usage: Bmp2Dcm {filein=inputFileName|dirin=inputDirectoryName}            ",
46    "              [studyUID = ] [patName = ] [debug]                          ", 
47    "                                                                          ",
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'       ",
54    
55    FINISH_USAGE
56
57    // ----- Initialize Arguments Manager ------
58   
59    GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
60   
61    if (am->ArgMgrDefined("usage") || argc == 1) 
62    {
63       am->ArgMgrUsage(usage); // Display 'usage'
64       delete am;
65       return 0;
66    }
67
68    if (am->ArgMgrDefined("debug"))
69       GDCM_NAME_SPACE::Debug::DebugOn();
70
71    bool verbose = ( 0 != am->ArgMgrDefined("verbose") );
72
73    const char *fileName = am->ArgMgrGetString("filein");
74    const char *dirName  = am->ArgMgrGetString("dirin");
75
76    if ( (fileName == 0 && dirName == 0)
77         ||
78         (fileName != 0 && dirName != 0) )
79    {
80        std::cout <<std::endl
81                  << "Either 'filein=' or 'dirin=' must be present;"
82                  << std::endl << "Not both" << std::endl;
83        am->ArgMgrUsage(usage); // Display 'usage'
84        delete am;
85        return 0;
86  }
87
88    std::string patName  = am->ArgMgrGetString("patname", dirName);
89    bool userDefinedStudy = ( 0 != am->ArgMgrDefined("studyUID") );
90    const char *studyUID;
91    if (userDefinedStudy)
92       studyUID  = am->ArgMgrGetString("studyUID");
93
94    // not described *on purpose* in the Usage !
95    bool userDefinedSerie = ( 0 != am->ArgMgrDefined("serieUID") );
96    const char *serieUID;
97    if(userDefinedSerie)
98       serieUID = am->ArgMgrGetString("serieUID");
99
100     /* if unused Param we give up */
101    if ( am->ArgMgrPrintUnusedLabels() )
102    {
103       am->ArgMgrUsage(usage);
104       delete am;
105       return 0;
106    }
107
108    delete am;  // ------ we don't need Arguments Manager any longer ------
109
110
111    // ----- Begin Processing -----
112
113    int *dim;
114    std::string nomFich;
115
116    if ( fileName != 0 ) // ====== Deal with a single file ======
117    {
118      vtkBMPReader* Reader = vtkBMPReader::New();
119      if ( Reader->CanReadFile(fileName ) == 0) {
120          // skip 'non BMP' files
121         Reader->Delete();
122         if (verbose)
123             std::cout << "Sorry, [" << fileName << "] is not a BMP file!" << std::endl;
124         return 0;
125     }
126
127     if (verbose)
128        std::cout << "deal with [" <<  fileName << "]" << std::endl;
129      //Read BMP file
130
131      Reader->SetFileName(fileName);
132      Reader->Update();
133
134      vtkImageExtractComponents* Red = vtkImageExtractComponents::New();
135      Red->SetInput(Reader->GetOutput());
136      Red->SetComponents(0);
137      Red->Update();
138
139      vtkGdcmWriter* Writer = vtkGdcmWriter::New();
140      Writer->SetInput(Red->GetOutput());
141      nomFich = "";
142      nomFich = nomFich+fileName+".acr";   
143      Writer->Write();
144    
145      Reader->Delete();
146      Red->Delete();
147      Writer->Delete();
148    
149      }
150      else  // ====== Deal with a (single Patient) Directory ======
151      { 
152      
153         if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(dirName) )
154         {
155           std::cout << "KO : [" << dirName << "] is not a Directory." << std::endl;
156           return 0;
157         }
158         else
159         {
160           if (verbose)
161             std::cout << "OK : [" << dirName << "] is a Directory." << std::endl;
162         } 
163         std::string strStudyUID;
164         std::string strSerieUID;
165
166         if (userDefinedStudy)
167            strSerieUID =  studyUID;
168         else
169            strStudyUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
170    
171         if (userDefinedStudy)
172           strSerieUID =  serieUID;
173         else
174            strStudyUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();    
175
176        if(verbose)
177            std::cout << "dirName [" << dirName << "]" << std::endl;
178        
179         GDCM_NAME_SPACE::DirList dirList(dirName,1); // gets recursively the file list
180         GDCM_NAME_SPACE::DirListType fileList = dirList.GetFilenames();
181
182         for( GDCM_NAME_SPACE::DirListType::iterator it  = fileList.begin();
183                                    it != fileList.end();
184                                    ++it )
185         {
186            if ( GDCM_NAME_SPACE::Util::GetName((*it)).c_str()[0] == '.' ) 
187            {
188               // skip hidden files
189               continue;
190            }
191       
192            vtkBMPReader* Reader = vtkBMPReader::New();
193   
194            if ( Reader->CanReadFile(it->c_str() ) == 0) {
195               // skip 'non BMP' files  
196                Reader->Delete();
197                continue;
198             }
199    
200            if (verbose)
201              std::cout << "deal with [" <<  it->c_str() << "]" << std::endl;  
202    
203            Reader->SetFileName(it->c_str());    
204            Reader->Update();
205    
206            dim=Reader->GetOutput()->GetDimensions();
207            vtkImageExtractComponents* Red = vtkImageExtractComponents::New();
208            Red->SetInput(Reader->GetOutput());
209            Red->SetComponents(0);
210            Red->Update();
211
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 
217
218            vtkGdcmWriter* Writer = vtkGdcmWriter::New();
219            Writer->SetInput(Red->GetOutput());
220            nomFich = "";
221            nomFich = nomFich+it->c_str()+".acr";
222            Writer->SetFileName(nomFich.c_str());
223            Writer->SetGdcmFile(f);
224            Writer->Write();
225    
226            f->Delete(); 
227         
228       }
229    }   
230    return 0;   
231 }