]> Creatis software - creaImageIO.git/blobdiff - src2/creaImageIOImageReader.cpp
*** empty log message ***
[creaImageIO.git] / src2 / creaImageIOImageReader.cpp
index aa8012bc7228abf5c435387a21e13c85760ff72d..a7937ccc8e7e017816a2f359c849ace037d0a514 100644 (file)
@@ -2,7 +2,9 @@
 #include <creaImageIOTreeAttributeDescriptor.h>
 #include <creaImageIOSystem.h>
 
-#include <vtkImageReader2.h>
+#include <creaImageIOVtkImageReader.h>
+#include <creaImageIODicomImageReader.h>
+
 #include <vtkPNGReader.h>
 #include <vtkTIFFReader.h>
 #include <vtkJPEGReader.h>
@@ -11,8 +13,6 @@
 #include <vtkMetaImageReader.h>
 //#include <vtkGESignalReader.h>
 
-#include <gdcmFile.h> 
-#include <vtkGdcmReader.h>
 
 
 #include "boost/filesystem/path.hpp"
 namespace creaImageIO
 {
   
-  //========================================================================
-  std::string irclean(const std::string& str)
-  {
-    if (str == "GDCM::Unfound") 
-      {
-       return "";
-      }
-    if (str[str.size()-1]==' ')
-      {
-       return str.substr(0,str.size()-1);
-      }
-    if (str[str.size()-1]==0)
-      {
-       return str.substr(0,str.size()-1);
-      }
-    
-    return str;
-  }
-  //========================================================================
-
-  void IRSplitString ( 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);
-      }
-    
-    }
-  
-
-  //=====================================================================
-  class SpecificImageReader 
-  {
-  public:
-    SpecificImageReader() {}
-    virtual ~SpecificImageReader() {}
-
-    void SetName(const std::string& s) { mName = s; }
-    const std::string& GetName() const { return mName; }
-
-    virtual void PushBackExtensions(std::vector<std::string>&) {}
-
-    virtual bool CanRead(const std::string& filename) { return false; }
-    virtual vtkImageData* ReadImage(const std::string& filename) { return 0; }
-    virtual void ReadAttributes(const std::string& filename, 
-                               std::map<std::string,std::string>& attr) {}
-
-  private:
-    std::string mName;
-  };
-  //=====================================================================
-  
-  //=====================================================================
-  class SpecificVtkReader : public SpecificImageReader         
-  {                                                                    
-  public:                                                              
-    //=====================================================================
-    SpecificVtkReader(vtkImageReader2* r, 
-                     const std::string& name = "",
-                     const std::string& extensions = "")
-      : mVTKReader(r), mExtensions(extensions)
-    {
-      if (name.size() == 0) 
-       {
-         SetName ( mVTKReader->GetDescriptiveName() );
-       }
-      else 
-       {
-         SetName ( name );
-       }
-    };
-    //=====================================================================
-    ~SpecificVtkReader()
-    {
-      mVTKReader->Delete();
-    }
-    //=====================================================================
-    bool CanRead(const std::string& filename)
-    { 
-      //      std::cout << "## Reader "<<GetName()
-      //<<" ::CanRead("<<filename<<")"
-      //               <<std::endl;
-      return (mVTKReader->CanReadFile(filename.c_str())!=0);
-    }
-    //=====================================================================
-
-    //=====================================================================
-    vtkImageData* ReadImage(const std::string& filename)
-    {
-      //      std::cout << "## Reader "<<GetName()
-      //<<" ::Read("<<filename<<")"
-      //               <<std::endl;
-      vtkImageData* im = 0;
-      try
-       {
-         mVTKReader->SetFileName(filename.c_str());
-         mVTKReader->Update();
-         im = vtkImageData::New();
-         im->ShallowCopy(mVTKReader->GetOutput());
-       }
-      catch (...)
-       {
-         if (im!=0) im->Delete();
-         im = 0;
-       }
-      return im;
-    }
-    //=====================================================================
-
-    //=====================================================================
-    void PushBackExtensions(std::vector<std::string>& v)
-    {
-      std::string ext = mExtensions;
-      if (ext.size()==0) ext = mVTKReader->GetFileExtensions ();
-      
-      IRSplitString(ext," ",v);
-    }
-    //=====================================================================
-    
-    //=====================================================================
-    void ReadAttributes(const std::string& filename, 
-                       std::map<std::string,std::string>& attr)
-    {
-      //      std::cout << "SpecificVtkReader::ReadDicomInfo '"<<filename<<"'"<<std::endl;
-      GimmickMessage(2,"Reading attributes from '"<<filename<<std::endl);
-      // Get image dimensions
-      // How to get the image info without loading it in vtk ?
-      mVTKReader->SetFileName(filename.c_str());
-      mVTKReader->Update(); //OpenFile();
-      int ext[6];
-      mVTKReader->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())
-       {
-         //      boost::filesystem::path full_path(filename);
-         // std::string f = full_path.leaf();
-         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;
-       }
 
-       GimmickMessage(2,"Attributes map:"<<std::endl<<attr<<std::endl);
-    }
-    //=====================================================================
-  private:
-    vtkImageReader2* mVTKReader;
-    std::string mExtensions;
-  };
-  //=====================================================================
   //===================================================================== 
   /*
   void IRFillFields(DicomNode* node, 
@@ -245,129 +55,6 @@ namespace creaImageIO
   */
   //=====================================================================
 
-  //=====================================================================
-  class DicomReader : public SpecificImageReader               
-  {                                                                    
-  public:                                                              
-    //=====================================================================
-    DicomReader()
-    {
-      mReader = vtkGdcmReader::New();
-      SetName ( "Dicom" );
-    };
-    //=====================================================================
-    ~DicomReader()
-    {
-      mReader->Delete();
-    }
-    //=====================================================================
-    bool CanRead(const std::string& filename)
-    { 
-      //      std::cout << "## Reader "<<GetName()
-      //<<" ::CanRead("<<filename<<")"
-      //               <<std::endl;
-      //      return true;
-
-      
-      //      GDCM_NAME_SPACE
-      //  std::cout << "GDCM_NAME_SPACE = '" << STRINGIFY_SYMBOL(GDCM_NAME_SPACE)
-      // << "'" 
-      // <<std::endl;
-      
-      GDCM_NAME_SPACE::File* file = GDCM_NAME_SPACE::File::New();
-      file->SetLoadMode( GDCM_NAME_SPACE::LD_ALL);
-      file->SetFileName(filename.c_str());
-      file->Load();
-      bool ok = file->IsReadable();
-      file->Delete();
-      return ok;
-    }
-    //=====================================================================
-    vtkImageData* ReadImage(const std::string& filename)
-    {
-      //      std::cout << "## Reader "<<GetName()
-      //<<" ::Read("<<filename<<")"
-      //               <<std::endl;
-
-      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 PushBackExtensions(std::vector<std::string>& v)
-    {
-      v.push_back("dcm");
-      v.push_back("");
-    }
-    //=====================================================================
-
-    //=====================================================================
-   //=====================================================================
-    void ReadAttributes(const std::string& filename, 
-                       std::map<std::string,std::string>& attr)
-    {
-      //    std::cout << "DicomReader::ReadDicomInfo '"<<filename<<"'"<<std::endl;
-      GimmickMessage(2,"Reading attributes from DICOM file '"
-                    <<filename<<"'"<<std::endl);
-
-      GDCM_NAME_SPACE::File* file = GDCM_NAME_SPACE::File::New();
-      file->SetLoadMode( GDCM_NAME_SPACE::LD_ALL);
-      file->SetFileName(filename.c_str());
-      file->Load();
-      if (file->IsReadable())
-       {
-         
-         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
-               {
-                 uint16_t gr;
-                 uint16_t el;
-                 tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
-                 //              GimmickMessage(2,"Key '"<<i->first<<"' : "<<gr<<"|"<<el
-                 //                             <<std::endl);
-                 if ( ( gr!=0 ) && ( el!=0 ) )
-                   {
-                     std::string val = file->GetEntryString(gr,el);
-                     i->second = irclean(val);
-                     //                      GimmickMessage(2,"Key '"<<i->first<<"' : "<<gr<<"|"<<el
-                     //                                     <<"="<<i->second<<std::endl);
-                   }
-               }
-           }
-       }
-      file->Delete();
-    }
-  
-
-  private:
-    vtkGdcmReader* mReader;
-  };
-  //=====================================================================
-
 
 
 
@@ -382,14 +69,14 @@ namespace creaImageIO
     //    std::cout << "#### ImageReader::ImageReader()"<<std::endl;
     if (mUnreadableImage!=0) return;
 
-    Register(new SpecificVtkReader(vtkPNGReader::New()));
-    Register(new SpecificVtkReader(vtkTIFFReader::New()));
-    Register(new SpecificVtkReader(vtkJPEGReader::New()));
-    Register(new SpecificVtkReader(vtkBMPReader::New()));
-    Register(new SpecificVtkReader(vtkSLCReader::New()));
-    Register(new SpecificVtkReader(vtkMetaImageReader::New(),"MHD",".mhd"));
-    //   Register(new SpecificVtkReader(vtkGESignalReader::New()));
-    Register(new DicomReader);
+    Register(new VtkImageReader(vtkPNGReader::New()));
+    Register(new VtkImageReader(vtkTIFFReader::New()));
+    Register(new VtkImageReader(vtkJPEGReader::New()));
+    Register(new VtkImageReader(vtkBMPReader::New()));
+    Register(new VtkImageReader(vtkSLCReader::New()));
+    Register(new VtkImageReader(vtkMetaImageReader::New(),"MHD",".mhd"));
+    //   Register(new VtkImageReader(vtkGESignalReader::New()));
+    Register(new DicomImageReader);
 
     /*
     std::cout << "## Registered file extensions : "<<std::endl;