]> Creatis software - creaImageIO.git/blobdiff - src/creaImageIOVtkImageReader.cpp
move directory
[creaImageIO.git] / src / creaImageIOVtkImageReader.cpp
diff --git a/src/creaImageIOVtkImageReader.cpp b/src/creaImageIOVtkImageReader.cpp
new file mode 100644 (file)
index 0000000..0d3780a
--- /dev/null
@@ -0,0 +1,167 @@
+#include <creaImageIOVtkImageReader.h>
+#include <vtkImageReader2.h>
+#include <creaImageIOSystem.h>
+#include "boost/filesystem/path.hpp"
+
+namespace creaImageIO{
+  //=====================================================================
+  VtkImageReader::VtkImageReader(vtkImageReader2* r, 
+                                      const std::string& name,
+                                      const std::string& extensions)
+    : mReader(r), mExtensions(extensions)
+  {
+    if (name.size() == 0) 
+      {
+                 const char *test =mReader->GetDescriptiveName();
+                 if(test != "")
+                 {
+                       SetName ( "toto");// mReader->GetDescriptiveName());
+                 }
+               
+      }
+    else 
+      {
+       SetName ( name );
+      }
+    GimmickDebugMessage(5,"Constructing vtkImageReader : "<<GetName()
+                       <<std::endl);
+      
+  }
+  //=====================================================================
+  
+  //=====================================================================
+  VtkImageReader::~VtkImageReader()
+  {
+
+    mReader->Delete();
+  }
+  //=====================================================================
+
+  //=====================================================================
+  bool VtkImageReader::CanRead(const std::string& filename)
+  { 
+
+    return (mReader->CanReadFile(filename.c_str())!=0);
+/*       if(filename != "")
+         {
+               return (mReader->CanReadFile(filename.c_str())!=0);
+         }
+         else
+         {
+                 return false;
+         }*/
+  }
+  //=====================================================================
+  
+  //=====================================================================
+  vtkImageData* VtkImageReader::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 SplitExtensionsString ( const std::string& str, 
+                              const std::string& delimiters, 
+                              std::vector<std::string>& tokens)
+  {
+    // Skip delimiters at beginning.
+    std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
+    // Find first delimiter.
+    std::string::size_type pos     = str.find_first_of(delimiters, lastPos);
+    
+    while (std::string::npos != pos || std::string::npos != lastPos)
+      {
+       // Found a token, add it to the vector.
+       // SPECIFIC : REMOVE INITIAL DOT (lastPos + 1)
+       tokens.push_back(str.substr(lastPos+1, pos - lastPos));
+       // Skip delimiters.  Note the "not_of"
+       lastPos = str.find_first_not_of(delimiters, pos);
+       // Find next delimiter
+       pos = str.find_first_of(delimiters, lastPos);
+      }
+    
+    }
+  //=====================================================================
+  
+  //=====================================================================
+  void VtkImageReader::PushBackExtensions(std::vector<std::string>& v)
+  {
+    std::string ext = mExtensions;
+    if (ext.size()==0) ext = mReader->GetFileExtensions ();
+    
+    SplitExtensionsString(ext," ",v);
+  }
+  //=====================================================================
+
+
+  //=====================================================================
+  void VtkImageReader::ReadAttributes(const std::string& filename, 
+                                     std::map<std::string,std::string>& attr)
+  {
+    GimmickMessage(2,"Reading attributes from '"<<filename<<std::endl);
+    // Get image dimensions
+    // How to get the image info without loading it in vtk ?
+    mReader->SetFileName(filename.c_str());
+    mReader->Update(); //OpenFile();
+    int ext[6];
+    mReader->GetDataExtent(ext);
+    // Columns
+    char cols[128];
+    sprintf(cols,"%i",ext[1]-ext[0]);
+    // Rows
+    char rows[128];
+    sprintf(rows,"%i",ext[3]-ext[2]);
+    // Planes 
+    char planes[128];
+    sprintf(planes,"%i",ext[5]-ext[4]);
+   
+       std::map<std::string,std::string>::iterator i;
+    if ( (i = attr.find("FullFileName")) != attr.end())
+      {
+       i->second = filename;
+      }
+    if ( (i = attr.find("D0004_1500")) != attr.end())
+      {
+       boost::filesystem::path full_path(filename);
+       std::string f = full_path.leaf();
+       i->second = f;
+      }
+    if ( (i = attr.find("D0028_0010")) != attr.end())
+      {
+       i->second = rows;
+      }
+    if ( (i = attr.find("D0028_0011")) != attr.end())
+      {
+       i->second = cols;
+      }
+    
+    if ( (i = attr.find("D0028_0012")) != attr.end())
+      {
+               i->second = planes;
+      }
+         if ( (i = attr.find("FullFileDirectory")) != attr.end())
+      {
+        std::string::size_type last_pos = filename.find_last_of("//");
+                i->second = filename.substr(0, last_pos);
+         }
+
+    GimmickMessage(2,"Attributes map:"<<std::endl<<attr<<std::endl);
+  }
+  //=====================================================================
+
+} // namespace creaImageIO