creaImageIODicomImageReader2.cpp

Go to the documentation of this file.
00001 #include <creaImageIODicomImageReader2.h>
00002 #include "gdcmStringFilter.h"
00003 #include "vtkGDCMImageReader.h"
00004 
00005 #include <creaImageIOSystem.h>
00006 #include "boost/filesystem/path.hpp"
00007 
00008 #include <creaImageIOTreeAttributeDescriptor.h>
00009 #include <vtkStringArray.h>
00010 #include <creaImageIOGimmick.h>
00011 
00012 #include "gdcmUIDs.h"
00013 #ifdef _DEBUG
00014 #define new DEBUG_NEW
00015 #endif
00016 namespace creaImageIO
00017 {
00018 
00019   //=====================================================================
00020   DicomImageReader::DicomImageReader()
00021   {
00022       mReader =  vtkGDCMImageReader::New();
00023       SetName ( "Dicom" );
00024         
00025   };
00026   //=====================================================================
00027   
00028   //=====================================================================
00029   DicomImageReader::~DicomImageReader()
00030   {
00031       mReader->Delete();
00032   }
00033   //=====================================================================
00034 
00035   //=====================================================================  
00036   bool DicomImageReader::CanRead(const std::string& filename)
00037   { 
00038       gdcm::Reader reader;
00039       reader.SetFileName( filename.c_str() );
00040       return  reader.Read();
00041          
00042   }
00043   //=====================================================================
00044 
00045   //=====================================================================
00046   vtkImageData* DicomImageReader::ReadImage(const std::string& filename)
00047   {
00048     vtkImageData* im = 0;
00049     try
00050       {
00051         mReader->SetFileName(filename.c_str());
00052         mReader->Update();
00053         im = vtkImageData::New();
00054         im->ShallowCopy(mReader->GetOutput());
00055       }
00056     catch (...)
00057       {
00058         if (im!=0) im->Delete();
00059           im = 0;
00060       }
00061     return im;
00062     //gdcm::Tag grouplength(0x0000,0x0000);
00063     //gdcm::DataElement de(gdcm::Tag(0x0000, 0x0000));
00064     //de.SetByteValue("0",gdcm::VR::UL);
00065     //
00066     //
00067     //gdcm::UIDs::GetUIDString(gdcm::UIDS::uid_1_2_840_10008_1_1),gdcm::VR::UI);
00068 
00069     //std::ostream os;
00070     //de.Write(&os);
00071 
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   std::string irclean(const std::string& str)
00083   {
00084           if(str.size() > 0)
00085           {
00086                 if (str == "GDCM::Unfound") 
00087                   {
00088                 return "";
00089                   }
00090                 if (str[str.size()-1]==' ')
00091                   {
00092                 return irclean(str.substr(0,str.size()-1));
00093                   }
00094                 if (str[str.size()-1]==0)
00095                   {
00096                 return irclean(str.substr(0,str.size()-1));
00097                   }
00098           }
00099     
00100     return str;
00101   }
00102   //========================================================================
00103   //=====================================================================
00104   
00105 
00106         void DicomImageReader::getAttributes(const std::string filename, 
00107                 std::map <std::string , std::string> &infos, std::vector<std::string> i_attr)
00108         {
00109                 gdcm::Reader reader;
00110                 reader.SetFileName( filename.c_str() );
00111                 if (reader.Read())
00112                 {
00113                         std::vector<std::string>::iterator it = i_attr.begin();
00114                         for(;it != i_attr.end(); it++)
00115                         {
00116                                 unsigned short el;
00117                                 unsigned short gr;
00118                                 sscanf((*it).c_str(),"D%04hx_%04hx",&gr,&el);
00119                                 if ( ( gr!=0 ) && ( el!=0 ) )
00120                                 {
00121                                         infos[(*it)] =  ( GetStringValueFromTag(reader.GetFile().GetDataSet().GetDataElement(gdcm::Tag(gr,el))) );
00122                                 }
00123                         }
00124                 }
00125         }
00126 
00127   //=====================================================================
00128   void DicomImageReader::ReadAttributes(const std::string& filename, 
00129                       std::map<std::string,std::string>& attr)
00130   {
00131     GimmickMessage(2,"Reading attributes from DICOM file '"
00132                    <<filename<<"'"<<std::endl);
00133 
00134    
00135       gdcm::Reader reader;
00136       reader.SetFileName( filename.c_str() );
00137       if (reader.Read())
00138       {
00139                   gdcm::StringFilter sf;
00140                   sf.SetFile(reader.GetFile());
00141                   std::map<std::string,std::string>::iterator i;
00142                   for (i=attr.begin();i!=attr.end();++i)
00143               {
00144                           if ( i->first == "D0004_1500" )
00145                           {
00146                                   boost::filesystem::path full_path(filename);
00147                                   std::string f = full_path.leaf();
00148                                   i->second = f;
00149                           }
00150                           else if ( i->first == "FullFileName" )
00151                           {
00152                                 i->second = filename;
00153                           }
00154                           else if ( i->first == "FullFileDirectory" )
00155                           {
00156                                  std::string::size_type last_pos = filename.find_last_of("//");
00157                                  //find first separator
00158                                  i->second = filename.substr(0, last_pos);
00159                           }
00160                           else
00161                           {
00162                                   uint16_t el;
00163                                   uint16_t gr;
00164                       {
00165             gdcm::Attribute<  0x0008,0x103e> at;
00166             std::string sr;
00167             at.SetValue(sr);
00168             
00169         }
00170                                   tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
00171                                   //if ( ( gr!=0 ) && ( el!=0 ) )
00172                                   const gdcm::Tag tag(gr, el);
00173                                   if( reader.GetFile().GetDataSet().FindDataElement( tag ) )
00174                                   {
00175                                           i->second = irclean(sf.ToString(tag));
00176                                   }
00177                                   else
00178                                   {
00179                                           i->second = "";
00180                                   }
00181                           }
00182                   }
00183           }
00184   }
00185 
00186 void DicomImageReader::ReadAttributes2(const std::string& filename, 
00187                       std::map<std::string,std::string>& attr)
00188   {
00189         if(!b_loaded)
00190         {
00191                 std::map<std::string,std::string>::iterator i;
00192                 for (i=attr.begin();i!=attr.end();++i)
00193                   {
00194                         if ( i->first == "D0004_1500" ||  i->first == "FullFileName" || i->first == "FullFileDirectory" )
00195                         {
00196                         
00197                         }
00198                         else
00199                         {
00200                         uint16_t el;
00201                         uint16_t gr;
00202                         
00203                         tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
00204                         mscan.AddTag(gdcm::Tag(gr,el) );
00205                         }
00206                 }
00207                 b_loaded = true;
00208         }
00209          bool b = mscan.IsKey(filename.c_str());
00210      if( b )
00211       {
00212                   const gdcm::Scanner::TagToValue &mapping = mscan.GetMapping(filename.c_str());
00213                   gdcm::Scanner::TagToValue::const_iterator it = mapping.begin();
00214                   std::map<std::string, std::string>::iterator i;
00215                 for (i=attr.begin();i!=attr.end();++i, ++it)
00216                 {
00217                         if ( i->first == "D0004_1500" )
00218                         {
00219                                 boost::filesystem::path full_path(filename);
00220                                 std::string f = full_path.leaf();
00221                                 i->second = f;
00222                         }
00223                         else if ( i->first == "FullFileName" )
00224                         {
00225                                 i->second = filename;
00226                         }
00227                         else if ( i->first == "FullFileDirectory" )
00228                         {
00229                                   std::string::size_type last_pos = filename.find_last_of("//");
00230                                   //find first separator
00231                                   i->second = filename.substr(0, last_pos);
00232                         }
00233                         else
00234                         {
00235                                 const char *value = it->second;
00236                                 i->second = irclean(it->second);
00237                         }
00238                 }
00239          }
00240 }
00241 
00242 const std::string DicomImageReader::GetStringValueFromTag(const gdcm::DataElement& de)
00243 {
00244   static std::string buffer;
00245   buffer = "";  // cleanup previous call
00246 
00247 
00248     const gdcm::ByteValue *bv = de.GetByteValue();
00249     if( bv ) // Can be Type 2
00250       {
00251       buffer = std::string( bv->GetPointer(), bv->GetLength() );
00252       // Will be padded with at least one \0
00253       }
00254 
00255   // Since return is a const char* the very first \0 will be considered
00256   return buffer.c_str();
00257 }
00258   //=====================================================================
00259   
00260 } // namespace creaImageIO
00261