]> Creatis software - creaImageIO.git/blobdiff - src/creaImageIODicomImageReader2.cpp
move directory
[creaImageIO.git] / src / creaImageIODicomImageReader2.cpp
diff --git a/src/creaImageIODicomImageReader2.cpp b/src/creaImageIODicomImageReader2.cpp
new file mode 100644 (file)
index 0000000..3dd8120
--- /dev/null
@@ -0,0 +1,235 @@
+#include <creaImageIODicomImageReader2.h>
+
+
+
+#include <creaImageIOSystem.h>
+#include "boost/filesystem/path.hpp"
+
+#include <creaImageIOTreeAttributeDescriptor.h>
+#include <vtkStringArray.h>
+#include <creaImageIOGimmick.h>
+
+
+#ifdef _DEBUG
+#define new DEBUG_NEW
+#endif
+namespace creaImageIO
+{
+
+  //=====================================================================
+  DicomImageReader::DicomImageReader()
+  {
+         mReader =  vtkGDCMImageReader::New();
+    SetName ( "Dicom" );
+       
+  };
+  //=====================================================================
+  
+  //=====================================================================
+  DicomImageReader::~DicomImageReader()
+  {
+         mReader->Delete();
+  }
+  //=====================================================================
+
+  //=====================================================================  
+  bool DicomImageReader::CanRead(const std::string& filename)
+  { 
+         gdcm::Reader reader;
+      reader.SetFileName( filename.c_str() );
+      return  reader.Read();
+        
+  }
+  //=====================================================================
+
+  //=====================================================================
+  vtkImageData* DicomImageReader::ReadImage(const std::string& filename)
+  {
+    vtkImageData* im = 0;
+    try
+      {
+       mReader->SetFileName(filename.c_str());
+       mReader->Update();
+       im = vtkImageData::New();
+       im->ShallowCopy(mReader->GetOutput());
+      }
+    catch (...)
+      {
+       if (im!=0) im->Delete();
+         im = 0;
+      }
+    return im;
+  }
+
+  //=====================================================================
+  void DicomImageReader::PushBackExtensions(std::vector<std::string>& v)
+  {
+    v.push_back("dcm");
+    v.push_back("");
+  }
+  //=====================================================================
+  
+  //========================================================================
+  std::string irclean(const std::string& str)
+  {
+         if(str.size() > 0)
+         {
+               if (str == "GDCM::Unfound") 
+                 {
+               return "";
+                 }
+               if (str[str.size()-1]==' ')
+                 {
+               return irclean(str.substr(0,str.size()-1));
+                 }
+               if (str[str.size()-1]==0)
+                 {
+               return irclean(str.substr(0,str.size()-1));
+                 }
+         }
+    
+    return str;
+  }
+  //========================================================================
+  //=====================================================================
+  void DicomImageReader::ReadAttributes(const std::string& filename, 
+                     std::map<std::string,std::string>& attr)
+  {
+    GimmickMessage(2,"Reading attributes from DICOM file '"
+                  <<filename<<"'"<<std::endl);
+
+   
+        gdcm::Reader reader;
+      reader.SetFileName( filename.c_str() );
+         if (reader.Read())
+      {
+       std::map<std::string,std::string>::iterator i;
+       for (i=attr.begin();i!=attr.end();++i)
+         {
+           if ( i->first == "D0004_1500" )
+             {
+               boost::filesystem::path full_path(filename);
+               std::string f = full_path.leaf();
+               i->second = f;
+             }
+           else if ( i->first == "FullFileName" )
+             {
+               i->second = filename;
+             }
+                 else if ( i->first == "FullFileDirectory" )
+             {
+                         std::string::size_type last_pos = filename.find_last_of("//");
+                         //find first separator
+                         i->second = filename.substr(0, last_pos);
+             }
+           else
+             {
+               uint16_t el;
+               uint16_t gr;
+               
+               tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
+               if ( ( gr!=0 ) && ( el!=0 ) )
+                 {
+                          gdcm::DataElement de( gdcm::Tag(gr,el) );
+                          std::string val = GetStringValueFromTag(reader.GetFile().GetDataSet().GetDataElement(gdcm::Tag(gr,el)));
+                   i->second = irclean(val);
+                 }
+             }
+         }
+      }
+  }
+
+void DicomImageReader::ReadAttributes2(const std::string& filename, 
+                     std::map<std::string,std::string>& attr)
+  {
+      
+       if(!b_loaded)
+       {
+               std::map<std::string,std::string>::iterator i;
+               for (i=attr.begin();i!=attr.end();++i)
+                 {
+                       if ( i->first == "D0004_1500" ||  i->first == "FullFileName" || i->first == "FullFileDirectory" )
+                       {
+                       
+                       }
+                       else
+                       {
+                       uint16_t el;
+                       uint16_t gr;
+                       
+                       tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
+                       mscan.AddTag(gdcm::Tag(gr,el) );
+                       }
+               }
+               b_loaded = true;
+       }
+        bool b = mscan.IsKey(filename.c_str());
+     if( b )
+      {
+                 const gdcm::Scanner::TagToValue &mapping = mscan.GetMapping(filename.c_str());
+                 gdcm::Scanner::TagToValue::const_iterator it = mapping.begin();
+                 std::map<std::string, std::string>::iterator i;
+               for (i=attr.begin();i!=attr.end();++i, ++it)
+               {
+                       if ( i->first == "D0004_1500" )
+                       {
+                               boost::filesystem::path full_path(filename);
+                               std::string f = full_path.leaf();
+                               i->second = f;
+                       }
+                       else if ( i->first == "FullFileName" )
+                       {
+                               i->second = filename;
+                       }
+                       else if ( i->first == "FullFileDirectory" )
+                       {
+                                 std::string::size_type last_pos = filename.find_last_of("//");
+                                 //find first separator
+                                 i->second = filename.substr(0, last_pos);
+                       }
+                       else
+                       {
+                               const char *value = it->second;
+                               i->second = irclean(it->second);
+                       }
+               }
+        }
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+  const std::string DicomImageReader::GetStringValueFromTag(const gdcm::DataElement& de)
+{
+  static std::string buffer;
+  buffer = "";  // cleanup previous call
+
+
+    const gdcm::ByteValue *bv = de.GetByteValue();
+    if( bv ) // Can be Type 2
+      {
+      buffer = std::string( bv->GetPointer(), bv->GetLength() );
+      // Will be padded with at least one \0
+      }
+
+
+  // Since return is a const char* the very first \0 will be considered
+  return buffer.c_str();
+}
+  //=====================================================================
+  
+} // namespace creaImageIO
+