]> Creatis software - cpPlugins.git/blobdiff - plugins/IO/ImageReader.cxx
Architecture updated.
[cpPlugins.git] / plugins / IO / ImageReader.cxx
diff --git a/plugins/IO/ImageReader.cxx b/plugins/IO/ImageReader.cxx
new file mode 100644 (file)
index 0000000..0402e9e
--- /dev/null
@@ -0,0 +1,364 @@
+#include <plugins/IO/ImageReader.h>
+#include <plugins/IO/ImageReaderQDialog.h>
+#include <cpPlugins/DataObjects/Image.h>
+
+#include <itkImageFileReader.h>
+#include <itkImageSeriesReader.h>
+
+#include <itkImageFileReader.hxx>
+#include <itkImageSeriesReader.hxx>
+#include <itkConvertPixelBuffer.hxx>
+#include <itkImageAlgorithm.hxx>
+#include <itkSimpleDataObjectDecorator.hxx>
+
+#ifdef cpPlugins_QT4
+
+#include <QApplication>
+#include <QFileInfo>
+
+// -------------------------------------------------------------------------
+cpPluginsIO::ImageReaderQDialog::
+ImageReaderQDialog( QWidget* parent )
+  : QFileDialog( parent ),
+    m_ProcessObject( NULL )
+{
+  this->connect(
+    this, SIGNAL( accepted( ) ), this, SLOT( _dlg_Accepted( ) )
+    );
+  this->setWindowTitle( "Open an(some) image(s)" );
+}
+
+// -------------------------------------------------------------------------
+cpPluginsIO::ImageReaderQDialog::
+~ImageReaderQDialog( )
+{
+}
+
+// -------------------------------------------------------------------------
+void cpPluginsIO::ImageReaderQDialog::
+setProcessObject( cpPlugins::BaseObjects::ProcessObject* obj )
+{
+  if( obj == NULL )
+    return;
+  this->m_ProcessObject = obj;
+  auto param = this->m_ProcessObject->GetParameters( );
+  auto extensions = param->GetAcceptedFileExtensions( "FileNames" );
+  auto files = param->GetOpenFileNameList( "FileNames" );
+
+  QStringList filters;
+  if( extensions != "" )
+    filters << extensions.c_str( );
+  filters << "Any file (*)";
+  this->setFileMode( QFileDialog::ExistingFiles );
+  this->setNameFilters( filters );
+  this->setAcceptMode( QFileDialog::AcceptOpen );
+  if( files.size( ) > 0 )
+  {
+    QFileInfo info( files[ 0 ].c_str( ) );
+    this->setDirectory( info.canonicalPath( ) );
+
+  } // fi
+}
+
+// -------------------------------------------------------------------------
+void cpPluginsIO::ImageReaderQDialog::
+_dlg_Accepted( )
+{
+  if( this->m_ProcessObject != NULL )
+  {
+    auto param = this->m_ProcessObject->GetParameters( );
+    auto files = this->selectedFiles( );
+    param->ClearOpenFileNameList( "FileNames" );
+    for( auto fIt = files.begin( ); fIt != files.end( ); ++fIt )
+      param->AddToOpenFileNameList( "FileNames", fIt->toStdString( ) );
+
+  } // fi
+}
+
+#endif // cpPlugins_QT4
+
+// -------------------------------------------------------------------------
+QDialog* cpPluginsIO::ImageReader::
+CreateQDialog( )
+{
+#ifdef cpPlugins_QT4
+  ImageReaderQDialog* dlg = NULL;
+  if( QApplication::instance( ) != NULL )
+  {
+    dlg = new ImageReaderQDialog( );
+    dlg->setProcessObject( this );
+
+  } // fi
+  return( dlg );
+#else // cpPlugins_QT4
+  return( NULL );
+#endif // cpPlugins_QT4
+}
+
+// -------------------------------------------------------------------------
+cpPluginsIO::ImageReader::
+ImageReader( )
+  : Superclass( )
+{
+  this->_ConfigureOutput< cpPlugins::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)"
+    );
+}
+
+// -------------------------------------------------------------------------
+cpPluginsIO::ImageReader::
+~ImageReader( )
+{
+}
+
+// -------------------------------------------------------------------------
+void cpPluginsIO::ImageReader::
+_GenerateData( )
+{
+  // Get filenames
+  auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
+  if( fnames.size( ) >= 1 )
+  {
+    // Guess image properties
+    itk::ImageIOBase::Pointer io =
+      itk::ImageIOFactory::CreateImageIO(
+        fnames[ 0 ].c_str( ),
+        itk::ImageIOFactory::ReadMode
+        );
+    if( io.IsNotNull( ) )
+    {
+      io->SetFileName( fnames[ 0 ] );
+      io->ReadImageInformation( );
+      if( fnames.size( ) >= 1 )
+      {
+        switch( io->GetNumberOfDimensions( ) )
+        {
+        case 1: this->_GD0< 1 >( io ); break;
+        case 2: this->_GD0< 2 >( io ); break;
+        case 3: this->_GD0< 3 >( io ); break;
+        case 4: this->_GD0< 4 >( io ); break;
+        default:
+          this->_Error( "Image dimension not supported." );
+          break;
+        } // hctiws
+
+      } // fi
+    }
+    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 >
+void cpPluginsIO::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( );
+
+  if( pt == itk::ImageIOBase::SCALAR )
+  {
+    switch( ct )
+    {
+    case itk::ImageIOBase::CHAR   : this->_GD1<   char, _Dim >( io ); break;
+    case itk::ImageIOBase::SHORT  : this->_GD1<  short, _Dim >( io ); break;
+    case itk::ImageIOBase::INT    : this->_GD1<    int, _Dim >( io ); break;
+    case itk::ImageIOBase::LONG   : this->_GD1<   long, _Dim >( io ); break;
+    case itk::ImageIOBase::FLOAT  : this->_GD1<  float, _Dim >( io ); break;
+    case itk::ImageIOBase::DOUBLE : this->_GD1< double, _Dim >( io ); break;
+    case itk::ImageIOBase::UCHAR  : this->_GD1<  uchar, _Dim >( io ); break;
+    case itk::ImageIOBase::USHORT : this->_GD1< ushort, _Dim >( io ); break;
+    case itk::ImageIOBase::UINT   : this->_GD1<   uint, _Dim >( io ); break;
+    case itk::ImageIOBase::ULONG  : this->_GD1<  ulong, _Dim >( io ); break;
+    default: this->_Error( "Scalar pixel type not supported." ); break;
+    } // hctiws
+  }
+  else if( pt == itk::ImageIOBase::RGB )
+  {
+    switch( ct )
+    {
+    case itk::ImageIOBase::CHAR   : this->_GD1< itk::RGBPixel<   char >, _Dim >( io ); break;
+    case itk::ImageIOBase::SHORT  : this->_GD1< itk::RGBPixel<  short >, _Dim >( io ); break;
+    case itk::ImageIOBase::INT    : this->_GD1< itk::RGBPixel<    int >, _Dim >( io ); break;
+    case itk::ImageIOBase::LONG   : this->_GD1< itk::RGBPixel<   long >, _Dim >( io ); break;
+    case itk::ImageIOBase::FLOAT  : this->_GD1< itk::RGBPixel<  float >, _Dim >( io ); break;
+    case itk::ImageIOBase::DOUBLE : this->_GD1< itk::RGBPixel< double >, _Dim >( io ); break;
+    case itk::ImageIOBase::UCHAR  : this->_GD1< itk::RGBPixel<  uchar >, _Dim >( io ); break;
+    case itk::ImageIOBase::USHORT : this->_GD1< itk::RGBPixel< ushort >, _Dim >( io ); break;
+    case itk::ImageIOBase::UINT   : this->_GD1< itk::RGBPixel<   uint >, _Dim >( io ); break;
+    case itk::ImageIOBase::ULONG  : this->_GD1< itk::RGBPixel<  ulong >, _Dim >( io ); break;
+    default: this->_Error( "RGB pixel type not supported." ); break;
+    } // hctiws
+  }
+  else if( pt == itk::ImageIOBase::RGBA )
+  {
+    switch( ct )
+    {
+    case itk::ImageIOBase::CHAR   : this->_GD1< itk::RGBAPixel<   char >, _Dim >( io ); break;
+    case itk::ImageIOBase::SHORT  : this->_GD1< itk::RGBAPixel<  short >, _Dim >( io ); break;
+    case itk::ImageIOBase::INT    : this->_GD1< itk::RGBAPixel<    int >, _Dim >( io ); break;
+    case itk::ImageIOBase::LONG   : this->_GD1< itk::RGBAPixel<   long >, _Dim >( io ); break;
+    case itk::ImageIOBase::FLOAT  : this->_GD1< itk::RGBAPixel<  float >, _Dim >( io ); break;
+    case itk::ImageIOBase::DOUBLE : this->_GD1< itk::RGBAPixel< double >, _Dim >( io ); break;
+    case itk::ImageIOBase::UCHAR  : this->_GD1< itk::RGBAPixel<  uchar >, _Dim >( io ); break;
+    case itk::ImageIOBase::USHORT : this->_GD1< itk::RGBAPixel< ushort >, _Dim >( io ); break;
+    case itk::ImageIOBase::UINT   : this->_GD1< itk::RGBAPixel<   uint >, _Dim >( io ); break;
+    case itk::ImageIOBase::ULONG  : this->_GD1< itk::RGBAPixel<  ulong >, _Dim >( io ); break;
+    default: this->_Error( "RGBA pixel type not supported." ); break;
+    } // hctiws
+  }
+  else if( pt == itk::ImageIOBase::COMPLEX )
+  {
+    switch( ct )
+    {
+    case itk::ImageIOBase::FLOAT  : this->_GD1< std::complex< float >, _Dim >( io ); break;
+    case itk::ImageIOBase::DOUBLE : this->_GD1< std::complex< double >, _Dim >( io ); break;
+    default: this->_Error( "Complex pixel type not supported." ); break;
+    } // hctiws
+  }
+  else if( pt == itk::ImageIOBase::COVARIANTVECTOR )
+  {
+    switch( ct )
+    {
+    case itk::ImageIOBase::FLOAT  : this->_GD1< itk::CovariantVector< float, _Dim >, _Dim >( io ); break;
+    case itk::ImageIOBase::DOUBLE : this->_GD1< itk::CovariantVector< double, _Dim >, _Dim >( io ); break;
+    default: this->_Error( "CovariantVector pixel type not supported." ); break;
+    } // hctiws
+  }
+  else if( pt == itk::ImageIOBase::POINT )
+  {
+    switch( ct )
+    {
+    case itk::ImageIOBase::FLOAT  : this->_GD1< itk::Point< float, _Dim >, _Dim >( io ); break;
+    case itk::ImageIOBase::DOUBLE : this->_GD1< itk::Point< double, _Dim >, _Dim >( io ); break;
+    default: this->_Error( "Point pixel type not supported." ); break;
+    } // hctiws
+  }
+  else if( pt == itk::ImageIOBase::VECTOR )
+  {
+    switch( ct )
+    {
+    case itk::ImageIOBase::FLOAT  : this->_GD1< itk::Vector< float, _Dim >, _Dim >( io ); break;
+    case itk::ImageIOBase::DOUBLE : this->_GD1< itk::Vector< double, _Dim >, _Dim >( io ); break;
+    default: this->_Error( "Vector pixel type not supported." ); break;
+    } // hctiws
+  }
+  else if( pt == itk::ImageIOBase::SYMMETRICSECONDRANKTENSOR )
+  {
+    switch( ct )
+    {
+    case itk::ImageIOBase::FLOAT  : this->_GD1< itk::SymmetricSecondRankTensor< float, _Dim >, _Dim >( io ); break;
+    case itk::ImageIOBase::DOUBLE : this->_GD1< itk::SymmetricSecondRankTensor< double, _Dim >, _Dim >( io ); break;
+    default: this->_Error( "SymmetricSecondRankTensor pixel type not supported." ); break;
+    } // hctiws
+  }
+  else if( pt == itk::ImageIOBase::DIFFUSIONTENSOR3D )
+  {
+    if( _Dim == 3 )
+    {
+      switch( ct )
+      {
+      case itk::ImageIOBase::FLOAT  : this->_GD1< itk::DiffusionTensor3D< float >, 3 >( io ); break;
+      case itk::ImageIOBase::DOUBLE : this->_GD1< itk::DiffusionTensor3D< double >, 3 >( io ); break;
+      default: this->_Error( "DiffusionTensor3D pixel type not supported." ); break;
+      } // hctiws
+    }
+    else
+      this->_Error( "DiffusionTensor3D dimension not supported." );
+  }
+  else if( pt == itk::ImageIOBase::MATRIX )
+  {
+    switch( ct )
+    {
+    case itk::ImageIOBase::FLOAT  : this->_GD1< itk::Matrix< float, _Dim, _Dim >, _Dim >( io ); break;
+    case itk::ImageIOBase::DOUBLE : this->_GD1< itk::Matrix< double, _Dim, _Dim >, _Dim >( io ); break;
+    default: this->_Error( "Matrix pixel type not supported." ); break;
+    } // hctiws
+  }
+  else if( pt == itk::ImageIOBase::OFFSET )
+  {
+    this->_GD1< itk::Offset< _Dim >, _Dim >( io );
+  }
+  else if( pt == itk::ImageIOBase::FIXEDARRAY )
+  {
+    switch( ct )
+    {
+    case itk::ImageIOBase::CHAR   : this->_GD1< itk::FixedArray<   char, _Dim >, _Dim >( io ); break;
+    case itk::ImageIOBase::SHORT  : this->_GD1< itk::FixedArray<  short, _Dim >, _Dim >( io ); break;
+    case itk::ImageIOBase::INT    : this->_GD1< itk::FixedArray<    int, _Dim >, _Dim >( io ); break;
+    case itk::ImageIOBase::LONG   : this->_GD1< itk::FixedArray<   long, _Dim >, _Dim >( io ); break;
+    case itk::ImageIOBase::FLOAT  : this->_GD1< itk::FixedArray<  float, _Dim >, _Dim >( io ); break;
+    case itk::ImageIOBase::DOUBLE : this->_GD1< itk::FixedArray< double, _Dim >, _Dim >( io ); break;
+    case itk::ImageIOBase::UCHAR  : this->_GD1< itk::FixedArray<  uchar, _Dim >, _Dim >( io ); break;
+    case itk::ImageIOBase::USHORT : this->_GD1< itk::FixedArray< ushort, _Dim >, _Dim >( io ); break;
+    case itk::ImageIOBase::UINT   : this->_GD1< itk::FixedArray<   uint, _Dim >, _Dim >( io ); break;
+    case itk::ImageIOBase::ULONG  : this->_GD1< itk::FixedArray<  ulong, _Dim >, _Dim >( io ); break;
+    default: this->_Error( "Scalar pixel type not supported." ); break;
+    } // hctiws
+  }
+  else
+    this->_Error( "Image pixel type not yet supported." );
+}
+
+// -------------------------------------------------------------------------
+template< class _TPixel, unsigned int _Dim >
+void cpPluginsIO::ImageReader::
+_GD1( itk::ImageIOBase* io )
+{
+  typedef itk::Image< _TPixel, _Dim > _TImage;
+
+  // Get filenames
+  auto fnames = this->m_Parameters.GetOpenFileNameList( "FileNames" );
+  if( fnames.size( ) == 1 )
+  {
+    auto f = this->_CreateITK< itk::ImageFileReader< _TImage > >( );
+    f->SetFileName( fnames[ 0 ] );
+    f->SetImageIO( io );
+    try
+    {
+      f->Update( );
+      this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
+    }
+    catch( itk::ExceptionObject& err )
+    {
+      this->_Error( err.GetDescription( ) );
+
+    } // yrt
+  }
+  else // if( fnames.size( ) > 1 )
+  {
+    auto f = this->_CreateITK< itk::ImageSeriesReader< _TImage > >( );
+    for( auto i = fnames.begin( ); i != fnames.end( ); ++i )
+      f->AddFileName( *i );
+    f->SetImageIO( io );
+    try
+    {
+      f->Update( );
+      this->GetOutput( "Output" )->SetITK( f->GetOutput( ) );
+    }
+    catch( itk::ExceptionObject& err )
+    {
+      this->_Error( err.GetDescription( ) );
+
+    } // yrt
+
+  } // fi
+}
+
+// eof - $RCSfile$