]> Creatis software - cpPlugins.git/blobdiff - plugins/ITKIO/ImageReader.cxx
yet another refactoring
[cpPlugins.git] / plugins / ITKIO / ImageReader.cxx
diff --git a/plugins/ITKIO/ImageReader.cxx b/plugins/ITKIO/ImageReader.cxx
new file mode 100644 (file)
index 0000000..90b032f
--- /dev/null
@@ -0,0 +1,269 @@
+#include <ITKIO/ImageReader.h>
+#include <cpInstances/DataObjects/Image.h>
+#include <cpPlugins/QT/OpenFileDialog.h>
+
+#define ITKIOImageBase_HIDDEN
+#include <itkImageFileReader.h>
+#include <itkImageSeriesReader.h>
+#include <itkImageIOFactory.h>
+
+#ifdef cpPlugins_QT4
+#  include <QApplication>
+#endif // cpPlugins_QT4
+
+// -------------------------------------------------------------------------
+QDialog* cpPluginsITKIO::ImageReader::
+CreateQDialog( )
+{
+#ifdef cpPlugins_QT4
+  cpPlugins::QT::OpenFileDialog* dlg = NULL;
+  if( QApplication::instance( ) != NULL )
+  {
+    dlg = new cpPlugins::QT::OpenFileDialog( );
+    dlg->SetParameters( &( this->m_Parameters ), "FileNames" );
+
+  } // fi
+  return( dlg );
+#else // cpPlugins_QT4
+  return( NULL );
+#endif // cpPlugins_QT4
+}
+
+// -------------------------------------------------------------------------
+cpPluginsITKIO::ImageReader::
+ImageReader( )
+  : Superclass( )
+{
+  this->_ConfigureOutput< cpInstances::DataObjects::Image >( "Output" );
+  this->m_Parameters.Clear( );
+  this->m_Parameters.ConfigureAsOpenFileNameList( "FileNames" );
+  this->m_Parameters.SetAcceptedFileExtensions(
+    "FileNames",
+    "Image files (*.bmp *.png *.jpg *.jpeg *.dcm *.mhd *.nhdr *.nrrd *.tiff)"
+    );
+}
+
+// -------------------------------------------------------------------------
+cpPluginsITKIO::ImageReader::
+~ImageReader( )
+{
+}
+
+// -------------------------------------------------------------------------
+void cpPluginsITKIO::ImageReader::
+_GenerateData( )
+{
+  // Get filenames
+  auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
+  std::string fname = "";
+  if( fnames.size( ) > 1 )
+  {
+    std::stringstream fname_str;
+    fname_str << fnames[ 0 ] << cpPlugins_PATH_SEPARATOR << fnames[ 1 ];
+    fname = fname_str.str( );
+  }
+  else if( fnames.size( ) == 1 )
+    fname = fnames[ 0 ];
+
+  if( fname != "" )
+  {
+    // Guess image properties
+    itk::ImageIOBase::Pointer io =
+      itk::ImageIOFactory::CreateImageIO(
+        fname.c_str( ), itk::ImageIOFactory::ReadMode
+        );
+    if( io.IsNotNull( ) )
+    {
+      io->SetFileName( fname );
+      io->ReadImageInformation( );
+      bool success = false;
+      unsigned int dim = io->GetNumberOfDimensions( );
+#ifdef cpPlugins_PROCESS_DIMS_1
+      if( dim == 1 ) success = this->_GD0< 1 >( io );
+#endif // cpPlugins_PROCESS_DIMS_1
+#ifdef cpPlugins_PROCESS_DIMS_2
+      if( dim == 2 ) success = this->_GD0< 2 >( io );
+#endif // cpPlugins_PROCESS_DIMS_2
+#ifdef cpPlugins_PROCESS_DIMS_3
+      if( dim == 3 ) success = this->_GD0< 3 >( io );
+#endif // cpPlugins_PROCESS_DIMS_3
+#ifdef cpPlugins_PROCESS_DIMS_4
+      if( dim == 4 ) success = this->_GD0< 4 >( io );
+#endif // cpPlugins_PROCESS_DIMS_4
+      if( !success )
+        this->_Error( "Image dimension not supported." );
+    }
+    else
+      this->_Error(
+        std::string( "Could not create an ImageIO for \"" ) +
+        fnames[ 0 ] +
+        std::string( "\"" )
+        );
+  }
+  else
+    this->_Error( "No image(s) given" );
+}
+
+// -------------------------------------------------------------------------
+template< unsigned int _Dim >
+bool cpPluginsITKIO::ImageReader::
+_GD0( itk::ImageIOBase* io )
+{
+  typedef unsigned char  uchar;
+  typedef unsigned short ushort;
+  typedef unsigned int   uint;
+  typedef unsigned long  ulong;
+
+  itk::ImageIOBase::IOComponentType ct = io->GetComponentType( );
+  itk::ImageIOBase::IOPixelType pt = io->GetPixelType( );
+
+  bool success = false;
+  if( pt == itk::ImageIOBase::SCALAR )
+  {
+    if( ct == itk::ImageIOBase::CHAR ) success = this->_GD1< char, _Dim >( io );
+    if( ct == itk::ImageIOBase::UCHAR ) success = this->_GD1< uchar, _Dim >( io );
+    if( ct == itk::ImageIOBase::SHORT ) success = this->_GD1< short, _Dim >( io );
+    if( ct == itk::ImageIOBase::USHORT ) success = this->_GD1< ushort, _Dim >( io );
+    if( ct == itk::ImageIOBase::INT ) success = this->_GD1< int, _Dim >( io );
+    if( ct == itk::ImageIOBase::UINT ) success = this->_GD1< uint, _Dim >( io );
+    if( ct == itk::ImageIOBase::LONG ) success = this->_GD1< long, _Dim >( io );
+    if( ct == itk::ImageIOBase::ULONG ) success = this->_GD1< ulong, _Dim >( io );
+    if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< float, _Dim >( io );
+    if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< double, _Dim >( io );
+  }
+  else if( pt == itk::ImageIOBase::RGB )
+  {
+    if( ct == itk::ImageIOBase::CHAR ) success = this->_GD1< itk::RGBPixel< char >, _Dim >( io );
+    if( ct == itk::ImageIOBase::UCHAR ) success = this->_GD1< itk::RGBPixel< uchar >, _Dim >( io );
+    if( ct == itk::ImageIOBase::SHORT ) success = this->_GD1< itk::RGBPixel< short >, _Dim >( io );
+    if( ct == itk::ImageIOBase::USHORT ) success = this->_GD1< itk::RGBPixel< ushort >, _Dim >( io );
+    if( ct == itk::ImageIOBase::INT ) success = this->_GD1< itk::RGBPixel< int >, _Dim >( io );
+    if( ct == itk::ImageIOBase::UINT ) success = this->_GD1< itk::RGBPixel< uint >, _Dim >( io );
+    if( ct == itk::ImageIOBase::LONG ) success = this->_GD1< itk::RGBPixel< long >, _Dim >( io );
+    if( ct == itk::ImageIOBase::ULONG ) success = this->_GD1< itk::RGBPixel< ulong >, _Dim >( io );
+    if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::RGBPixel< float >, _Dim >( io );
+    if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::RGBPixel< double >, _Dim >( io );
+  }
+  else if( pt == itk::ImageIOBase::RGBA )
+  {
+    if( ct == itk::ImageIOBase::CHAR ) success = this->_GD1< itk::RGBAPixel< char >, _Dim >( io );
+    if( ct == itk::ImageIOBase::UCHAR ) success = this->_GD1< itk::RGBAPixel< uchar >, _Dim >( io );
+    if( ct == itk::ImageIOBase::SHORT ) success = this->_GD1< itk::RGBAPixel< short >, _Dim >( io );
+    if( ct == itk::ImageIOBase::USHORT ) success = this->_GD1< itk::RGBAPixel< ushort >, _Dim >( io );
+    if( ct == itk::ImageIOBase::INT ) success = this->_GD1< itk::RGBAPixel< int >, _Dim >( io );
+    if( ct == itk::ImageIOBase::UINT ) success = this->_GD1< itk::RGBAPixel< uint >, _Dim >( io );
+    if( ct == itk::ImageIOBase::LONG ) success = this->_GD1< itk::RGBAPixel< long >, _Dim >( io );
+    if( ct == itk::ImageIOBase::ULONG ) success = this->_GD1< itk::RGBAPixel< ulong >, _Dim >( io );
+    if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::RGBAPixel< float >, _Dim >( io );
+    if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::RGBAPixel< double >, _Dim >( io );
+  }
+  else if( pt == itk::ImageIOBase::COMPLEX )
+  {
+    if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< std::complex< float >, _Dim >( io );
+    if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< std::complex< double >, _Dim >( io );
+  }
+  else if( pt == itk::ImageIOBase::COVARIANTVECTOR )
+  {
+    if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::CovariantVector< float, _Dim >, _Dim >( io );
+    if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::CovariantVector< double, _Dim >, _Dim >( io );
+  }
+  else if( pt == itk::ImageIOBase::POINT )
+  {
+    if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::Point< float, _Dim >, _Dim >( io );
+    if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::Point< double, _Dim >, _Dim >( io );
+  }
+  else if( pt == itk::ImageIOBase::VECTOR )
+  {
+    if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::Vector< float, _Dim >, _Dim >( io );
+    if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::Vector< double, _Dim >, _Dim >( io );
+  }
+  else if( pt == itk::ImageIOBase::SYMMETRICSECONDRANKTENSOR )
+  {
+    if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::SymmetricSecondRankTensor< float, _Dim >, _Dim >( io );
+    if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::SymmetricSecondRankTensor< double, _Dim >, _Dim >( io );
+  }
+  else if( pt == itk::ImageIOBase::DIFFUSIONTENSOR3D )
+  {
+    if( _Dim == 3 )
+    {
+      if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::DiffusionTensor3D< float >, _Dim >( io );
+      if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::DiffusionTensor3D< double >, _Dim >( io );
+    }
+    else
+      this->_Error( "DiffusionTensor3D dimension not supported." );
+  }
+  else if( pt == itk::ImageIOBase::MATRIX )
+  {
+    if( ct == itk::ImageIOBase::FLOAT ) success = this->_GD1< itk::Matrix< float, _Dim, _Dim >, _Dim >( io );
+    if( ct == itk::ImageIOBase::DOUBLE ) success = this->_GD1< itk::Matrix< double, _Dim, _Dim >, _Dim >( io );
+  }
+  else if( pt == itk::ImageIOBase::OFFSET )
+  {
+    this->_GD1< itk::Offset< _Dim >, _Dim >( io );
+  }
+  else if( pt == itk::ImageIOBase::FIXEDARRAY )
+  {
+  } // fi
+  return( success );
+}
+
+// -------------------------------------------------------------------------
+template< class _TPixel, unsigned int _Dim >
+bool cpPluginsITKIO::ImageReader::
+_GD1( itk::ImageIOBase* io )
+{
+  typedef itk::Image< _TPixel, _Dim > _TImage;
+
+  // Get filenames
+  auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
+  if( fnames.size( ) == 1 || fnames.size( ) == 2 )
+  {
+    std::stringstream fname_str;
+    if( fnames.size( ) == 1 )
+      fname_str << fnames[ 0 ];
+    else
+      fname_str << fnames[ 0 ] << cpPlugins_PATH_SEPARATOR << fnames[ 1 ];
+
+    auto f = this->_CreateITK< itk::ImageFileReader< _TImage > >( );
+    f->SetFileName( fname_str.str( ) );
+    f->SetImageIO( io );
+    try
+    {
+      f->Update( );
+      this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
+      return( true );
+    }
+    catch( itk::ExceptionObject& err )
+    {
+      this->_Error( err.GetDescription( ) );
+      return( false );
+
+    } // yrt
+  }
+  else // if( fnames.size( ) > 1 )
+  {
+    auto f = this->_CreateITK< itk::ImageSeriesReader< _TImage > >( );
+    auto i = fnames.begin( );
+    std::stringstream dir;
+    dir << *i << cpPlugins_PATH_SEPARATOR;
+    i++;
+    for( ; i != fnames.end( ); ++i )
+      f->AddFileName( dir.str( ) + *i );
+    f->SetImageIO( io );
+    try
+    {
+      f->Update( );
+      this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
+      return( true );
+    }
+    catch( itk::ExceptionObject& err )
+    {
+      this->_Error( err.GetDescription( ) );
+      return( false );
+
+    } // yrt
+
+  } // fi
+}
+
+// eof - $RCSfile$