creaImageIODicomImageReader.cpp

Go to the documentation of this file.
00001 #include <creaImageIODicomImageReader.h>
00002 
00003 #include <vtkGdcmReader.h>
00004 
00005 
00006 
00007 #include <creaImageIOSystem.h>
00008 #include "boost/filesystem/path.hpp"
00009 
00010 #include <creaImageIOTreeAttributeDescriptor.h>
00011 #include <vtkStringArray.h>
00012 #include <creaImageIOGimmick.h>
00013 #ifdef _DEBUG
00014 #define new DEBUG_NEW
00015 #endif
00016 namespace creaImageIO
00017 {
00018 
00019   //=====================================================================
00020   DicomImageReader::DicomImageReader()
00021   {
00022     mReader = vtkGdcmReader::New();
00023 //EED   mReader->SetFlipY(false);
00024     SetName ( "Dicom" );
00025 
00026   };
00027   //=====================================================================
00028   
00029   //=====================================================================
00030   DicomImageReader::~DicomImageReader()
00031   {
00032     mReader->Delete();
00033   }
00034   //=====================================================================
00035 
00036   //=====================================================================  
00037   bool DicomImageReader::CanRead(const std::string& filename)
00038   { 
00039     GDCM_NAME_SPACE::Document*doc;
00040     GDCM_NAME_SPACE::File* file = GDCM_NAME_SPACE::File::New();
00041     file->SetLoadMode( GDCM_NAME_SPACE::LD_ALL);
00042     file->SetFileName(filename.c_str());
00043     file->Load();
00044     bool ok = file->IsReadable();
00045         if(!ok)
00046         {
00047                 doc = (GDCM_NAME_SPACE::Document*)file; 
00048                 ok = doc->IsReadable();
00049         }
00050     file->Delete();
00051     return ok;
00052   }
00053   //=====================================================================
00054 
00055   //=====================================================================
00056   vtkImageData* DicomImageReader::ReadImage(const std::string& filename)
00057   {
00058     vtkImageData* im = 0;
00059     try
00060       {
00061         mReader->SetFileName(filename.c_str());
00062         mReader->Update();
00063         im = vtkImageData::New();
00064         im->ShallowCopy(mReader->GetOutput());
00065       }
00066     catch (...)
00067       {
00068         if (im!=0) im->Delete();
00069           im = 0;
00070       }
00071     return im;
00072   }
00073 
00074   //=====================================================================
00075   void DicomImageReader::PushBackExtensions(std::vector<std::string>& v)
00076   {
00077     v.push_back("dcm");
00078     v.push_back("");
00079   }
00080   //=====================================================================
00081   
00082   //========================================================================
00083   std::string irclean(const std::string& str)
00084   {
00085           if(str.size() > 0)
00086           {
00087                 if (str == "GDCM::Unfound") 
00088                   {
00089                 return "";
00090                   }
00091                 if (str[str.size()-1]==' ')
00092                   {
00093                 return irclean(str.substr(0,str.size()-1));
00094                   }
00095                 if (str[str.size()-1]==0)
00096                   {
00097                 return irclean(str.substr(0,str.size()-1));
00098                   }
00099           }
00100     
00101     return str;
00102   }
00103   //========================================================================
00104   void DicomImageReader::getAttributes(const std::string filename,
00105                 std::map <std::string , std::string> &infos, std::vector<std::string> i_attr)
00106         {
00107                 std::vector<std::string>::iterator it = i_attr.begin();
00108                 for(; it != i_attr.end(); it++)
00109                 {
00110                         infos[(*it)] = "";
00111                 }
00112                 ReadAttributes(filename, infos);
00113         }
00114   //=====================================================================
00115   void DicomImageReader::ReadAttributes(const std::string& filename, 
00116                       std::map<std::string,std::string>& attr)
00117   {
00118     GimmickMessage(2,"Reading attributes from DICOM file '"
00119                    <<filename<<"'"<<std::endl);
00120 
00121     GDCM_NAME_SPACE::File* file = GDCM_NAME_SPACE::File::New();
00122         //boost::shared_ptr<GDCM_NAME_SPACE::File> file(GDCM_NAME_SPACE::File::New());//, DicomImageReader::deleter());
00123 
00124     GDCM_NAME_SPACE::Document *doc= GDCM_NAME_SPACE::File::New();
00125     doc->SetLoadMode( GDCM_NAME_SPACE::LD_ALL);
00126     doc->SetFileName(filename.c_str());
00127     doc->Load();
00128     file->SetLoadMode( GDCM_NAME_SPACE::LD_ALL);
00129     file->SetFileName(filename.c_str());
00130     file->Load();
00131     if (file->IsReadable())// ||((GDCM_NAME_SPACE::Document*) file)->IsReadable())
00132       {
00133         std::map<std::string,std::string>::iterator i;
00134         for (i=attr.begin();i!=attr.end();++i)
00135           {
00136             if ( i->first == "D0004_1500" )
00137               {
00138                 boost::filesystem::path full_path(filename);
00139                 std::string f = full_path.leaf();
00140                 i->second = f;
00141               }
00142             else if ( i->first == "FullFileName" )
00143               {
00144                 i->second = filename;
00145               }
00146                   else if ( i->first == "FullFileDirectory" )
00147               {
00148                           std::string::size_type last_pos = filename.find_last_of("//");
00149                           //find first separator
00150                           i->second = filename.substr(0, last_pos);
00151               }
00152             else
00153               {
00154                 uint16_t el;
00155                 uint16_t gr;
00156                 
00157                 tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
00158                 if ( ( gr!=0 ) && ( el!=0 ) )
00159                   {
00160                     std::string val =  file->GetEntryString(gr,el);
00161                     i->second = irclean(val);
00162                   }
00163               }
00164           }
00165       }
00166   }
00167 
00168   //=====================================================================
00169   
00170 } // namespace creaImageIO
00171