Bmp2Dcm.cxx

Go to the documentation of this file.
00001 /*=========================================================================
00002                                                                                 
00003   Program:   gdcm
00004   Module:    $RCSfile: Bmp2Dcm.cxx,v $
00005   Language:  C++
00006   Date:      $Date: 2007/06/27 08:43:25 $
00007   Version:   $Revision: 1.2 $
00008                                                                                 
00009   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
00010   l'Image). All rights reserved. See Doc/License.txt or
00011   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
00012                                                                                 
00013      This software is distributed WITHOUT ANY WARRANTY; without even
00014      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00015      PURPOSE.  See the above copyright notices for more information.
00016                                                                                 
00017 =========================================================================*/
00018 
00019 #include "gdcmArgMgr.h"
00020 #include "gdcmDirList.h"
00021 #include "gdcmDebug.h"
00022 #include "gdcmUtil.h"
00023 
00024 #include <vtkImageData.h>
00025 
00026 #include <vtkBMPReader.h>
00027 #include <vtkBMPWriter.h>
00028 #include "vtkGdcmWriter.h"
00029 
00030 #include <vtkImageExtractComponents.h>
00031 #include <vtkPointData.h>
00032 #include <vtkDataArray.h>
00033 
00034 #include <stdio.h>
00035 #include <string.h>
00036 #include <stdlib.h>
00037 #include <math.h>
00038 
00039 //---------------------------------------------------------------------------
00040 
00041 int main( int argc, char *argv[] )
00042 {
00043    START_USAGE(usage)
00044    "\n Bmp2Dcm :\n                                                            ",  
00045    "usage: Bmp2Dcm {filein=inputFileName|dirin=inputDirectoryName}            ",
00046    "              [studyUID = ] [patName = ] [debug]                          ", 
00047    "                                                                          ",
00048    " inputFileName : Name of the (single) file user wants to transform        ",
00049    " inputDirectoryName : user wants to transform *all* the files             ",
00050    " studyUID   : *aware* user wants to add the serie                         ",
00051    "                                             to an already existing study ", 
00052    " verbose    : user wants to run the program in 'verbose mode'             ",       
00053    " debug      : *developper* wants to run the program in 'debug mode'       ",
00054    
00055    FINISH_USAGE
00056 
00057    // ----- Initialize Arguments Manager ------
00058   
00059    GDCM_NAME_SPACE::ArgMgr *am = new GDCM_NAME_SPACE::ArgMgr(argc, argv);
00060   
00061    if (am->ArgMgrDefined("usage") || argc == 1) 
00062    {
00063       am->ArgMgrUsage(usage); // Display 'usage'
00064       delete am;
00065       return 0;
00066    }
00067 
00068    if (am->ArgMgrDefined("debug"))
00069       GDCM_NAME_SPACE::Debug::DebugOn();
00070       
00071    int verbose  = am->ArgMgrDefined("verbose");
00072          
00073    const char *fileName = am->ArgMgrGetString("filein");
00074    const char *dirName  = am->ArgMgrGetString("dirin");
00075 
00076    if ( (fileName == 0 && dirName == 0)
00077         ||
00078         (fileName != 0 && dirName != 0) )
00079    {
00080        std::cout <<std::endl
00081                  << "Either 'filein=' or 'dirin=' must be present;" 
00082                  << std::endl << "Not both" << std::endl;
00083        am->ArgMgrUsage(usage); // Display 'usage'  
00084        delete am;
00085        return 0;
00086  }
00087  
00088    std::string patName  = am->ArgMgrGetString("patname", dirName);
00089     
00090    bool userDefinedStudy = am->ArgMgrDefined("studyUID");
00091    const char *studyUID;
00092    if (userDefinedStudy)
00093       studyUID  = am->ArgMgrGetString("studyUID");  
00094 
00095    // not described *on purpose* in the Usage !    
00096    bool userDefinedSerie = am->ArgMgrDefined("serieUID");   
00097    const char *serieUID;
00098    if(userDefinedSerie)
00099       serieUID = am->ArgMgrGetString("serieUID");
00100        
00101     /* if unused Param we give up */
00102    if ( am->ArgMgrPrintUnusedLabels() )
00103    {
00104       am->ArgMgrUsage(usage);
00105       delete am;
00106       return 0;
00107    } 
00108  
00109    delete am;  // ------ we don't need Arguments Manager any longer ------
00110 
00111 
00112    // ----- Begin Processing -----
00113    
00114    int *dim;
00115    std::string nomFich;
00116    
00117    if ( fileName != 0 ) // ====== Deal with a single file ======
00118    { 
00119      vtkBMPReader* Reader = vtkBMPReader::New();
00120      if ( Reader->CanReadFile(fileName ) == 0) {
00121          // skip 'non BMP' files   
00122         Reader->Delete();
00123         if (verbose)
00124             std::cout << "Sorry, [" << fileName << "] is not a BMP file!" << std::endl;
00125         return 0;
00126     }
00127     
00128     if (verbose)
00129        std::cout << "deal with [" <<  fileName << "]" << std::endl;
00130      //Read BMP file
00131 
00132      Reader->SetFileName(fileName);
00133      Reader->Update();
00134 
00135      vtkImageExtractComponents* Red = vtkImageExtractComponents::New();
00136      Red->SetInput(Reader->GetOutput());
00137      Red->SetComponents(0);
00138      Red->Update();
00139 
00140      vtkGdcmWriter* Writer = vtkGdcmWriter::New();
00141      Writer->SetInput(Red->GetOutput());
00142      nomFich = "";
00143      nomFich = nomFich+fileName+".acr";   
00144      Writer->Write();
00145    
00146      Reader->Delete();
00147      Red->Delete();
00148      Writer->Delete();
00149    
00150      }
00151      else  // ====== Deal with a (single Patient) Directory ======
00152      { 
00153      
00154         if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(dirName) )
00155         {
00156           std::cout << "KO : [" << dirName << "] is not a Directory." << std::endl;
00157           return 0;
00158         }
00159         else
00160         {
00161           if (verbose)
00162             std::cout << "OK : [" << dirName << "] is a Directory." << std::endl;
00163         } 
00164         std::string strStudyUID;
00165         std::string strSerieUID;
00166 
00167         if (userDefinedStudy)
00168            strSerieUID =  studyUID;
00169         else
00170            strStudyUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();
00171    
00172         if (userDefinedStudy)
00173           strSerieUID =  serieUID;
00174         else
00175            strStudyUID =  GDCM_NAME_SPACE::Util::CreateUniqueUID();    
00176 
00177        if(verbose)
00178            std::cout << "dirName [" << dirName << "]" << std::endl;
00179        
00180         GDCM_NAME_SPACE::DirList dirList(dirName,1); // gets recursively the file list
00181         GDCM_NAME_SPACE::DirListType fileList = dirList.GetFilenames();
00182 
00183         for( GDCM_NAME_SPACE::DirListType::iterator it  = fileList.begin();
00184                                    it != fileList.end();
00185                                    ++it )
00186         {
00187            if ( GDCM_NAME_SPACE::Util::GetName((*it)).c_str()[0] == '.' ) 
00188            {
00189               // skip hidden files
00190               continue;
00191            }
00192       
00193            vtkBMPReader* Reader = vtkBMPReader::New();
00194   
00195            if ( Reader->CanReadFile(it->c_str() ) == 0) {
00196               // skip 'non BMP' files  
00197                Reader->Delete();
00198                continue;
00199             }
00200    
00201            if (verbose)
00202              std::cout << "deal with [" <<  it->c_str() << "]" << std::endl;  
00203    
00204            Reader->SetFileName(it->c_str());    
00205            Reader->Update();
00206    
00207            dim=Reader->GetOutput()->GetDimensions();
00208            vtkImageExtractComponents* Red = vtkImageExtractComponents::New();
00209            Red->SetInput(Reader->GetOutput());
00210            Red->SetComponents(0);
00211            Red->Update();
00212 
00213            // At least, created files will look like a Serie from a single Study ...  
00214            GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
00215            f->InsertEntryString(strStudyUID, 0x0020, 0x000d, "UI");      
00216            f->InsertEntryString(strSerieUID, 0x0020, 0x000e, "UI");
00217            f->InsertEntryString(patName,     0x0010, 0x0010, "PN");   // Patient's Name 
00218 
00219            vtkGdcmWriter* Writer = vtkGdcmWriter::New();
00220            Writer->SetInput(Red->GetOutput());
00221            nomFich = "";
00222            nomFich = nomFich+it->c_str()+".acr";
00223            Writer->SetFileName(nomFich.c_str());
00224            Writer->SetGdcmFile(f);
00225            Writer->Write();
00226    
00227            f->Delete(); 
00228         
00229       }
00230    }   
00231    return 0;   
00232 }

Generated on Fri Aug 24 12:59:28 2007 for gdcm by  doxygen 1.4.6