creaImageIODicomScanner.cpp

Go to the documentation of this file.
00001 #include <creaImageIODicomScanner.h>
00002 #include "creaImageIOSystem.h"
00003 #include <boost/filesystem.hpp>
00004 #include "boost/algorithm/string.hpp"
00005 
00006 
00007 
00008 #include <creaImageIOSystem.h>
00009 #include "boost/filesystem/path.hpp"
00010 
00011 #include <creaImageIOTreeAttributeDescriptor.h>
00012 #include <vtkStringArray.h>
00013 #include <creaImageIOGimmick.h>
00014 #include <gdcmDirectory.h>
00015 
00016 #include <fstream>
00017 
00018 
00019 #ifdef _DEBUG
00020 #define new DEBUG_NEW
00021 #endif
00022 namespace creaImageIO
00023 {
00024 
00025   //=====================================================================
00026   DicomImageScanner::DicomImageScanner()
00027   {
00028           mReader =  vtkGDCMImageReader::New();
00029         mscan.ClearTags();
00030         b_loaded = false;
00031   };
00032   //=====================================================================
00033   
00034   //=====================================================================
00035   DicomImageScanner::~DicomImageScanner()
00036   {
00037           mReader->Delete();
00038   }
00039   //=====================================================================
00040 
00041   //=====================================================================  
00042   bool DicomImageScanner::addDirectory(std::string& filename,  std::map<std::string,std::string>& attr)
00043   { 
00044                 if(!b_loaded)
00045         {
00046                 mscan.ClearTags();
00047                 std::map<std::string,std::string>::iterator i;
00048                 int j= 0;
00049                 for (i=attr.begin();i!=attr.end();++i, j++)
00050                   {
00051                         if ( i->first == "D0004_1500" ||  i->first == "FullFileName" || i->first == "FullFileDirectory" )
00052                         {
00053                         
00054                         }
00055                         else
00056                         {
00057                         uint16_t el;
00058                         uint16_t gr;
00059                         
00060                         tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
00061                         mscan.AddTag(gdcm::Tag(gr,el) );
00062                         
00063                         }
00064                 }
00065                 b_loaded = true;
00066         }
00067           gdcm::Directory d;
00068             
00069           boost::algorithm::replace_all(filename,"\\", "/");
00070           d.Load(filename.c_str(),true);
00071           mscan.Scan(d.GetFilenames());
00072           
00073       return  true;
00074          
00075   }
00076   //=====================================================================
00077 
00078   //=====================================================================
00079   vtkImageData* DicomImageScanner::ReadImage(const std::string& filename)
00080   {
00081     vtkImageData* im = 0;
00082     try
00083       {
00084         mReader->SetFileName(filename.c_str());
00085         mReader->Update();
00086         im = vtkImageData::New();
00087         im->ShallowCopy(mReader->GetOutput());
00088       }
00089     catch (...)
00090       {
00091         if (im!=0) im->Delete();
00092           im = 0;
00093       }
00094     return im;
00095   }
00096 
00097 
00098   //=====================================================================
00099   
00100   //========================================================================
00101   std::string DicomImageScanner::irclean(const std::string& str)
00102   {
00103           if(str.size() > 0)
00104           {
00105                 if (str == "GDCM::Unfound") 
00106                   {
00107                 return "";
00108                   }
00109                 if (str[str.size()-1]==' ')
00110                   {
00111                 return irclean(str.substr(0,str.size()-1));
00112                   }
00113                 if (str[str.size()-1]==0)
00114                   {
00115                 return irclean(str.substr(0,str.size()-1));
00116                   }
00117           }
00118     
00119     return str;
00120   }
00121   //========================================================================
00122  
00123   //=====================================================================
00124 
00125 void DicomImageScanner::ReadAttributes(const std::string& filename, 
00126                       std::map<std::string,std::string>& attr)
00127   {
00128           std::string name = "E:\\data-images\\dicoms\\CD1\\DICOM\\09112417\\37390000/31582235";
00129 
00130          bool b = mscan.IsKey(filename.c_str());
00131     if( b )      {
00132                   const gdcm::Scanner::TagToValue &mapping = mscan.GetMapping(filename.c_str());
00133                   gdcm::Scanner::TagToValue::const_iterator it = mapping.begin();
00134 
00135                   std::map<std::string, std::string>::iterator i;
00136                   
00137                   for (;it != mapping.end(); ++it)
00138                   {              char key[12] ;
00139                                   sprintf(key,"D%04x_%04x", it->first.GetGroup(), it->first.GetElement());
00140                                   attr[key] = irclean(it->second);
00141                   }
00142                   
00143                   
00144                  /* 
00145                   
00146                   
00147                   for (i=attr.begin();i!=attr.end();++i, j++)
00148                 {*/
00149                         if ( attr.find("D0004_1500") != attr.end())
00150                         {
00151                                 boost::filesystem::path full_path(filename);
00152                                 std::string f = full_path.leaf();
00153                                 attr["D0004_1500"] = f;
00154                         }
00155                         if ( attr.find("FullFileName")!= attr.end())
00156                         {
00157                                 attr["FullFileName"] = filename;
00158                         }
00159                         if ( attr.find("FullFileDirectory" )!= attr.end())
00160                         {
00161                                   std::string::size_type last_pos = filename.find_last_of("//");
00162                                   //find first separator
00163                                   attr["FullFileDirectory" ] = filename.substr(0, last_pos);
00164                         }
00165                 //      else
00166                 //      {
00167 
00168                 //              uint16_t el;
00169                 //              uint16_t gr;
00170 
00171                 //              tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
00172 
00173                 //      mscan.AddTag(gdcm::Tag(gr,el) );
00174 
00175                 //      //      const char *value = it->second;
00176                 //              i->second = irclean(it->second);
00177                 //              ++it;
00178                 //      }
00179                 //}
00180         }
00181 }
00182 
00183 
00184 
00185 
00186 
00187 
00188 
00189 
00190 
00191 
00192 
00193 
00194 
00195 
00196 
00197 
00198   const std::string DicomImageScanner::GetStringValueFromTag(const gdcm::DataElement& de)
00199 {
00200   static std::string buffer;
00201   buffer = "";  // cleanup previous call
00202 
00203 
00204     const gdcm::ByteValue *bv = de.GetByteValue();
00205     if( bv ) // Can be Type 2
00206       {
00207       buffer = std::string( bv->GetPointer(), bv->GetLength() );
00208       // Will be padded with at least one \0
00209       }
00210 
00211 
00212   // Since return is a const char* the very first \0 will be considered
00213   return buffer.c_str();
00214 }
00215   //=====================================================================
00216   
00217 } // namespace creaImageIO
00218