]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Plugins/ImageReader.cxx
Major refactoring: API-HCI bug corrected.
[cpPlugins.git] / lib / cpPlugins / Plugins / ImageReader.cxx
index da7b949485bcb72e7bb9947b3a63df4dd88f1ea7..2d1ae0493887d5cc8976be55a595eea88b118420 100644 (file)
@@ -1,36 +1,27 @@
 #include <cpPlugins/Plugins/ImageReader.h>
 #include <cpPlugins/Interface/Image.h>
 
-#include <itkImageFileReader.h>
-#include <itkImageSeriesReader.h>
+#include <set>
 
-#define ITK_MANUAL_INSTANTIATION
+#undef ITK_MANUAL_INSTANTIATION
 #include <itkImage.h>
 #include <itkRGBPixel.h>
-
-// -------------------------------------------------------------------------
-std::string cpPlugins::Plugins::ImageReader::
-GetClassName( ) const
-{
-  return( "cpPlugins::Plugins::ImageReader" );
-}
+#include <itkImageFileReader.h>
+#include <itkImageSeriesReader.h>
 
 // -------------------------------------------------------------------------
 cpPlugins::Plugins::ImageReader::
 ImageReader( )
   : Superclass( )
 {
+  this->m_ClassName = "cpPlugins::ImageReader";
+  this->m_ClassCategory = "ImageReader";
+
   this->SetNumberOfOutputs( 1 );
   this->_MakeOutput< cpPlugins::Interface::Image >( 0 );
 
   using namespace cpPlugins::Interface;
   this->m_DefaultParameters.Configure( Parameters::StringList, "FileNames" );
-  this->m_DefaultParameters.Configure( Parameters::String, "PixelType" );
-  this->m_DefaultParameters.Configure( Parameters::Uint, "Dimension" );
-  this->m_DefaultParameters.Configure( Parameters::Uint, "IsColorImage" );
-  this->m_DefaultParameters.SetValueAsString( "PixelType", "uchar" );
-  this->m_DefaultParameters.SetValueAsUint( "Dimension", 3 );
-  this->m_DefaultParameters.SetValueAsUint( "IsColorImage", 0 );
   this->m_Parameters = this->m_DefaultParameters;
 }
 
@@ -44,157 +35,225 @@ cpPlugins::Plugins::ImageReader::
 std::string cpPlugins::Plugins::ImageReader::
 _GenerateData( )
 {
-  using namespace cpPlugins::Interface;
-  Parameters::TUint dim = this->m_Parameters.GetValueAsUint( "Dimension" );
-  std::string r = "cpPlugins::Plugins::ImageReader: itk::Image dimension not supported.";
-  if     ( dim == 1 ) r = this->_GD0< 1 >( );
-  else if( dim == 2 ) r = this->_GD0< 2 >( );
-  else if( dim == 3 ) r = this->_GD0< 3 >( );
-  else if( dim == 4 ) r = this->_GD0< 4 >( );
+  // Get filenames
+  TStringList names;
+  this->m_Parameters.GetValueAsStringList( names, "FileNames" );
 
+  std::string r = "";
+  if( names.size( ) >= 1 )
+  {
+    // Guess image properties
+    itk::ImageIOBase::Pointer io =
+      itk::ImageIOFactory::CreateImageIO(
+        names[ 0 ].c_str( ),
+        itk::ImageIOFactory::ReadMode
+        );
+    if( io.IsNotNull( ) )
+    {
+      io->SetFileName( names[ 0 ] );
+      io->ReadImageInformation( );
+      if( names.size( ) == 1 )
+      {
+        switch( io->GetNumberOfDimensions( ) )
+        {
+        case 1: r = this->_GD0< 1 >( io, names ); break;
+        case 2: r = this->_GD0< 2 >( io, names ); break;
+        case 3: r = this->_GD0< 3 >( io, names ); break;
+        default:
+          r = "ImageReader: Image dimension not supported.";
+          break;
+        } // hctiws
+      }
+      else if( names.size( ) > 1 )
+      {
+        switch( io->GetNumberOfDimensions( ) )
+        {
+        case 1: r = this->_GD0< 2 >( io, names ); break;
+        case 2: r = this->_GD0< 3 >( io, names ); break;
+        default:
+          r = "ImageReader: Image dimension not supported.";
+          break;
+        } // hctiws
+
+      } // fi
+    }
+    else
+      r = "ImageReader: Could not CreateImageIO for \"" + names[ 0 ] + "\"";
+  }
+  else
+    r = "No image files given";
   return( r );
 }
 
 // -------------------------------------------------------------------------
 template< unsigned int D >
 std::string cpPlugins::Plugins::ImageReader::
-_GD0( )
+_GD0( itk::ImageIOBase* io, const TStringList& names )
 {
-  using namespace cpPlugins::Interface;
-  Parameters::TString pt = this->m_Parameters.GetValueAsString( "PixelType" );
-  Parameters::TUint ci = this->m_Parameters.GetValueAsUint( "IsColorImage" );
-
-  std::string r = "cpPlugins::Plugins::ImageReader: itk::Image pixel type not supported";
-  if( ci == 0 )
-  {
-    if( pt == "char" )
-      r = this->_GD1< char, D >( );
-    else if( pt == "short" )
-      r = this->_GD1< short, D >( );
-    else if( pt == "int" )
-      r = this->_GD1< int, D >( );
-    else if( pt == "long" )
-      r = this->_GD1< long, D >( );
-    else if( pt == "uchar" )
-      r = this->_GD1< unsigned char, D >( );
-    else if( pt == "ushort" )
-      r = this->_GD1< unsigned short, D >( );
-    else if( pt == "uint" )
-      r = this->_GD1< unsigned int, D >( );
-    else if( pt == "ulong" )
-      r = this->_GD1< unsigned long, D >( );
-    else if( pt == "float" )
-      r = this->_GD1< float, D >( );
-    else if( pt == "double" )
-      r = this->_GD1< double, D >( );
-  }
-  else
+  std::string r = "";
+  switch( io->GetComponentType( ) )
   {
-    if( pt == "char" )
-      r = this->_GD1< itk::RGBPixel< char >, D >( );
-    else if( pt == "short" )
-      r = this->_GD1< itk::RGBPixel< short >, D >( );
-    else if( pt == "int" )
-      r = this->_GD1< itk::RGBPixel< int >, D >( );
-    else if( pt == "long" )
-      r = this->_GD1< itk::RGBPixel< long >, D >( );
-    else if( pt == "uchar" )
-      r = this->_GD1< itk::RGBPixel< unsigned char >, D >( );
-    else if( pt == "ushort" )
-      r = this->_GD1< itk::RGBPixel< unsigned short >, D >( );
-    else if( pt == "uint" )
-      r = this->_GD1< itk::RGBPixel< unsigned int >, D >( );
-    else if( pt == "ulong" )
-      r = this->_GD1< itk::RGBPixel< unsigned long >, D >( );
-    else if( pt == "float" )
-      r = this->_GD1< itk::RGBPixel< float >, D >( );
-    else if( pt == "double" )
-      r = this->_GD1< itk::RGBPixel< double >, D >( );
-  } // fi
+  case itk::ImageIOBase::UCHAR:
+    r = this->_GD1< unsigned char, D >( io, names );
+    break;
+  case itk::ImageIOBase::CHAR:
+    r = this->_GD1< char, D >( io, names );
+    break;
+  case itk::ImageIOBase::USHORT:
+    r = this->_GD1< unsigned short, D >( io, names );
+    break;
+  case itk::ImageIOBase::SHORT:
+    r = this->_GD1< short, D >( io, names );
+    break;
+  case itk::ImageIOBase::UINT:
+    r = this->_GD1< unsigned int, D >( io, names );
+    break;
+  case itk::ImageIOBase::INT:
+    r = this->_GD1< int, D >( io, names );
+    break;
+  case itk::ImageIOBase::ULONG:
+    r = this->_GD1< unsigned long, D >( io, names );
+    break;
+  case itk::ImageIOBase::LONG:
+    r = this->_GD1< long, D >( io, names );
+    break;
+  case itk::ImageIOBase::FLOAT:
+    r = this->_GD1< float, D >( io, names );
+    break;
+  case itk::ImageIOBase::DOUBLE:
+    r = this->_GD1< double, D >( io, names );
+    break;
+  default:
+    r = "ImageReader: Atomic pixel type not supported.";
+    break;
+  } // hctiws
   return( r );
 }
 
 // -------------------------------------------------------------------------
 template< class P, unsigned int D >
 std::string cpPlugins::Plugins::ImageReader::
-_GD1( )
+_GD1( itk::ImageIOBase* io, const TStringList& names )
 {
-  typedef itk::Image< P, D > _TImage;
+  std::string r = "";
+  switch( io->GetPixelType( ) )
+  {
+  case itk::ImageIOBase::SCALAR:
+    r = this->_GD2< P, D >( names );
+    break;
+  case itk::ImageIOBase::RGB:
+    r = this->_GD2< itk::RGBPixel< P >, D >( names );
+    break;
+  case itk::ImageIOBase::RGBA:
+    r = this->_GD2< itk::RGBAPixel< P >, D >( names );
+    break;
+    /* TODO
+       case itk::ImageIOBase::OFFSET:
+       r = this->_GD2< itk::Offset< P >, D >( names );
+       break;
+       case itk::ImageIOBase::VECTOR:
+       r = this->_GD2< P, D >( names );
+       break;
+       case itk::ImageIOBase::POINT:
+       r = this->_GD2< P, D >( names );
+       break;
+       case itk::ImageIOBase::COVARIANTVECTOR:
+       r = this->_GD2< P, D >( names );
+       break;
+       case itk::ImageIOBase::SYMMETRICSECONDRANKTENSOR:
+       r = this->_GD2< P, D >( names );
+       break;
+       case itk::ImageIOBase::DIFFUSIONTENSOR3D:
+       r = this->_GD2< P, D >( names );
+       break;
+    */
+    /* TODO
+       case itk::ImageIOBase::COMPLEX:
+       r = this->_GD2< std::complex< P >, D >( names );
+       break;
+       case itk::ImageIOBase::FIXEDARRAY:
+       r = this->_GD2< P, D >( names );
+       break;
+       case itk::ImageIOBase::MATRIX:
+       r = this->_GD2< P, D >( names );
+       break;
+    */
+  default:
+    r = "ImageReader: Pixel type not supported.";
+    break;
+  } // hctiws
+  return( r );
+}
 
-  // Get filenames
-  using namespace cpPlugins::Interface;
-  std::vector< Parameters::TString > unordered_names;
-  this->m_Parameters.GetValueAsStringList( unordered_names, "FileNames" );
+// -------------------------------------------------------------------------
+template< class P, unsigned int D >
+std::string cpPlugins::Plugins::ImageReader::
+_GD2( const TStringList& names )
+{
+  typedef itk::Image< P, D > _I;
 
-  if( unordered_names.size( ) == 1 )
+  std::string r = "";
+  if( names.size( ) == 1 )
   {
     // Read single image
-    typedef itk::ImageFileReader< _TImage > _TSingleReader;
-
-    _TSingleReader* singleReader =
-      dynamic_cast< _TSingleReader* >(
-        this->m_RealProcessObject.GetPointer( )
-        );
-    if( singleReader == NULL )
+    typedef itk::ImageFileReader< _I > _SR;
+    _SR* reader =
+      dynamic_cast< _SR* >( this->m_RealProcessObject.GetPointer( ) );
+    if( reader == NULL )
     {
-      this->m_RealProcessObject = _TSingleReader::New( );
-      singleReader =
-        dynamic_cast< _TSingleReader* >(
-          this->m_RealProcessObject.GetPointer( )
-          );
+      this->m_RealProcessObject = _SR::New( );
+      reader =
+        dynamic_cast< _SR* >( this->m_RealProcessObject.GetPointer( ) );
 
     } // fi
-    singleReader->SetFileName( unordered_names.front( ) );
+    reader->SetFileName( names[ 0 ] );
     try
     {
-      singleReader->Update( );
+      reader->Update( );
+      this->m_Outputs[ 0 ]->SetITKDataObject( reader->GetOutput( ) );
     }
     catch( itk::ExceptionObject& err )
     {
-      return( err.GetDescription( ) );
+      r = "ImageReader: " + std::string( err.GetDescription( ) );
+      this->m_Outputs[ 0 ]->SetITKDataObject( NULL );
 
     } // yrt
-    this->_SetOutput( 0, singleReader->GetOutput( ) );
-    return( "" );
   }
-  else if( unordered_names.size( ) > 1 )
+  else if( names.size( ) > 1 )
   {
     // Read image series
     std::set< std::string > ordered_names;
-    for( unsigned int i = 0; i < unordered_names.size( ); ++i )
-      ordered_names.insert( unordered_names[ i ] );
+    for( unsigned int i = 0; i < names.size( ); ++i )
+      ordered_names.insert( names[ i ] );
 
-    typedef itk::ImageSeriesReader< _TImage > _TMultiReader;
-    _TMultiReader* multiReader =
-      dynamic_cast< _TMultiReader* >(
-        this->m_RealProcessObject.GetPointer( )
-        );
-    if( multiReader == NULL )
+    typedef itk::ImageSeriesReader< _I > _MR;
+    _MR* reader =
+      dynamic_cast< _MR* >( this->m_RealProcessObject.GetPointer( ) );
+    if( reader == NULL )
     {
-      this->m_RealProcessObject = _TMultiReader::New( );
-      multiReader =
-        dynamic_cast< _TMultiReader* >(
-          this->m_RealProcessObject.GetPointer( )
-          );
+      this->m_RealProcessObject = _MR::New( );
+      reader =
+        dynamic_cast< _MR* >( this->m_RealProcessObject.GetPointer( ) );
 
     } // fi
     std::set< std::string >::const_iterator fnIt = ordered_names.begin( );
     for( ; fnIt != ordered_names.end( ); ++fnIt )
-      multiReader->AddFileName( *fnIt );
+      reader->AddFileName( *fnIt );
     try
     {
-      multiReader->Update( );
+      reader->Update( );
+      this->m_Outputs[ 0 ]->SetITKDataObject( reader->GetOutput( ) );
     }
     catch( itk::ExceptionObject& err )
     {
-      return( err.GetDescription( ) );
+      r = "ImageReader: " + std::string( err.GetDescription( ) );
+      this->m_Outputs[ 0 ]->SetITKDataObject( NULL );
 
     } // yrt
-    this->_SetOutput( 0, multiReader->GetOutput( ) );
-    return( "" );
   }
   else
-    return( "No image filename(s) given." );
+    r = "ImageReader: No image files given";
+  return( r );
 }
 
 // eof - $RCSfile$