]> Creatis software - cpPlugins.git/commitdiff
Series reader added.
authorLeonardo Florez-Valencia <florez-l@javeriana.edu.co>
Wed, 10 Dec 2014 08:54:18 +0000 (09:54 +0100)
committerLeonardo Florez-Valencia <florez-l@javeriana.edu.co>
Wed, 10 Dec 2014 08:54:18 +0000 (09:54 +0100)
appli/examples/CMakeLists.txt
appli/examples/example_ReadImageSeriesWriteImage.cxx [new file with mode: 0644]
lib/cpPlugins/Interface/ProcessObject.cxx
lib/cpPlugins/Plugins/Host.cxx
lib/cpPlugins/Plugins/ImageReader.cxx
lib/cpPlugins/Plugins/ImageSeriesReader.cxx [new file with mode: 0644]
lib/cpPlugins/Plugins/ImageSeriesReader.h [new file with mode: 0644]
lib/cpPlugins/Plugins/ImageWriter.cxx

index 209a58f8fb56f8337a8c4d1af46dd744961873e4..248b052d11796c461364eb478779435d6acf1575 100644 (file)
@@ -7,6 +7,7 @@ SET(
   EXAMPLES_PROGRAMS
   example_LoadPlugins
   example_ReadWriteImage
+  example_ReadImageSeriesWriteImage
   )
 
 FOREACH(prog ${EXAMPLES_PROGRAMS}) 
diff --git a/appli/examples/example_ReadImageSeriesWriteImage.cxx b/appli/examples/example_ReadImageSeriesWriteImage.cxx
new file mode 100644 (file)
index 0000000..cab163c
--- /dev/null
@@ -0,0 +1,94 @@
+#include <cstdlib>
+#include <iostream>
+#include <sstream>
+#include <string>
+
+#include <cpPlugins/Interface/Interface.h>
+#include <cpPlugins/Interface/ProcessObject.h>
+
+int main( int argc, char* argv[] )
+{
+  if( argc < 7 )
+  {
+    std::cerr
+      << "Usage: " << argv[ 0 ]
+      << " plugins_file"
+      << " output_image"
+      << " dimensions pixel_type is_color input_image_files" << std::endl;
+    return( 1 );
+
+  } // fi
+  std::string plugins_file = argv[ 1 ];
+  std::string output_image_file = argv[ 2 ];
+  std::string dimensions = argv[ 3 ];
+  std::string pixel_type = argv[ 4 ];
+  bool is_color = ( std::atoi( argv[ 5 ] ) == 1 );
+
+  std::stringstream input_image_files;
+  for( int i = 6; i < argc; ++i )
+    input_image_files << argv[ i ] << ";";
+  
+  // Create interface
+  typedef cpPlugins::Interface::Interface TInterface;
+  typedef TInterface::TClasses            TClasses;
+
+  TInterface plugins;
+  plugins.Load( plugins_file );
+
+  // Create objects
+  typedef cpPlugins::Interface::ProcessObject TProcessObject;
+  typedef TProcessObject::TParameters         TParameters;
+  cpPlugins::Interface::ProcessObject* reader;
+  cpPlugins::Interface::ProcessObject* writer;
+
+  reader =
+    dynamic_cast< TProcessObject* >(
+      plugins.CreateObject( "cpPlugins::Plugins::ImageSeriesReader" )
+      );
+  if( reader == NULL )
+  {
+    std::cerr << "No suitable reader found in plugins." << std::endl;
+    return( 1 );
+
+  } // fi
+  writer =
+    dynamic_cast< TProcessObject* >(
+      plugins.CreateObject( "cpPlugins::Plugins::ImageWriter" )
+      );
+  if( writer == NULL )
+  {
+    delete reader;
+    std::cerr << "No suitable writer found in plugins." << std::endl;
+    return( 1 );
+
+  } // fi
+
+  // Configure reader
+  TParameters reader_params = reader->GetDefaultParameters( );
+  reader_params[ "FileNames" ].second = input_image_files.str( );
+  reader_params[ "PixelType" ].second = pixel_type;
+  reader_params[ "ImageDimension" ].second = dimensions;
+  reader_params[ "IsColorImage" ].second = ( is_color )? "1": "0";
+  reader->SetParameters( reader_params );
+
+  // Configure reader
+  TParameters writer_params = writer->GetDefaultParameters( );
+  writer_params[ "FileName" ].second = output_image_file;
+  writer->SetParameters( writer_params );
+
+  // Connect pipeline
+  writer->SetInput( 0, reader->GetOutput( 0 ) );
+
+  std::string msg = writer->Update( );
+
+  if( msg != "" )
+    std::cerr << "ERROR: " << msg << std::endl;
+
+  // Free memory
+  delete writer;
+  delete reader;
+
+  return( 0 );
+}
+
+// eof - $RCSfile$
index 96f6edea124e15f87c7cdea32ecfdd6dc4011f05..f2499c212f1e28cc05e067a3f451c3309e96f8c8 100644 (file)
@@ -94,15 +94,20 @@ std::string cpPlugins::Interface::ProcessObject::
 Update( )
 {
   // Force upstream updates
-  for( unsigned int idx = 0; idx < this->m_Inputs.size( ); ++idx )
-    this->m_Inputs[ idx ]->GetSource( )->Update( );
+  std::string r = "";
+  for( unsigned int i = 0; i < this->m_Inputs.size( ) && r == ""; ++i )
+    r = this->m_Inputs[ i ]->GetSource( )->Update( );
 
   // Current update
-  std::string ret = this->_GenerateData( );
-  this->m_OutputsDisconnected = false;
+  if( r == "" )
+  {
+    r = this->_GenerateData( );
+    this->m_OutputsDisconnected = false;
+
+  } // fi
 
   // Return error description, if any
-  return( ret );
+  return( r );
 }
 
 // -------------------------------------------------------------------------
index b4cd940d05c07a49e45299eb825c4d90afdac4f8..4431db64d8d669ef45c40be8b4a34cea72b397fb 100644 (file)
@@ -1,5 +1,6 @@
 #include <Pluma/Connector.hpp>
 #include <cpPlugins/Plugins/ImageReader.h>
+#include <cpPlugins/Plugins/ImageSeriesReader.h>
 #include <cpPlugins/Plugins/ImageWriter.h>
 
 /// TODO: doc
@@ -7,6 +8,7 @@ PLUMA_CONNECTOR
 bool connect( pluma::Host& host )
 {
   host.add( new cpPlugins::Plugins::ImageReaderProvider( ) );
+  host.add( new cpPlugins::Plugins::ImageSeriesReaderProvider( ) );
   host.add( new cpPlugins::Plugins::ImageWriterProvider( ) );
   return( true );
 }
index 8838bf469f12a8f3f80bfefaaa4d4a1486f2c034..df0f1e6cf2a7a9e15d93483e82cce468882b88a3 100644 (file)
@@ -46,7 +46,7 @@ _GenerateData( )
   if( dIt == this->m_Parameters.end( ) )
     dIt = this->m_DefaultParameters.find( "ImageDimension" );
 
-  std::string r = "itk::Image dimension not supported.";
+  std::string r = "cpPlugins::Plugins::ImageReader: itk::Image dimension not supported.";
   if     ( dIt->second.second == "1" ) r = this->_GD0< 1 >( );
   else if( dIt->second.second == "2" ) r = this->_GD0< 2 >( );
   else if( dIt->second.second == "3" ) r = this->_GD0< 3 >( );
@@ -70,7 +70,7 @@ _GD0( )
   if( cIt == this->m_Parameters.end( ) )
     cIt = this->m_DefaultParameters.find( "IsColorImage" );
 
-  std::string r = "itk::Image pixel type not supported";
+  std::string r = "cpPlugins::Plugins::ImageReader: itk::Image pixel type not supported";
   if( cIt->second.second == "0" )
   {
     if( tIt->second.second == "char" )
@@ -133,14 +133,14 @@ _GD1( )
     fIt = this->m_DefaultParameters.find( "FileName" );
 
   typedef itk::Image< P, D > _TImage;
-  typedef itk::ImageFileReader< _TImage > _TImageReader;
+  typedef itk::ImageFileReader< _TImage > _TReader;
 
-  _TImageReader* reader =
-    dynamic_cast< _TImageReader* >( this->m_Reader.GetPointer( ) );
+  _TReader* reader =
+    dynamic_cast< _TReader* >( this->m_Reader.GetPointer( ) );
   if( reader == NULL )
   {
-    this->m_Reader = _TImageReader::New( );
-    reader = dynamic_cast< _TImageReader* >( this->m_Reader.GetPointer( ) );
+    this->m_Reader = _TReader::New( );
+    reader = dynamic_cast< _TReader* >( this->m_Reader.GetPointer( ) );
 
   } // fi
   reader->SetFileName( fIt->second.second );
diff --git a/lib/cpPlugins/Plugins/ImageSeriesReader.cxx b/lib/cpPlugins/Plugins/ImageSeriesReader.cxx
new file mode 100644 (file)
index 0000000..416352a
--- /dev/null
@@ -0,0 +1,170 @@
+#include <cpPlugins/Plugins/ImageSeriesReader.h>
+#include <cpPlugins/Interface/Image.h>
+
+#include <set>
+#include <sstream>
+
+#include <itkImageSeriesReader.h>
+
+#define ITK_MANUAL_INSTANTIATION
+#include <itkImage.h>
+#include <itkRGBPixel.h>
+
+// -------------------------------------------------------------------------
+cpPlugins::Plugins::ImageSeriesReader::
+ImageSeriesReader( )
+  : Superclass( )
+{
+  this->SetNumberOfOutputs( 1 );
+  this->_MakeOutput< cpPlugins::Interface::Image >( 0 );
+
+  this->m_DefaultParameters[ "FileNames" ] =
+    TParameter( "string", "file_name1;file_name2;file_name3;..." );
+  this->m_DefaultParameters[ "PixelType" ] = TParameter( "type", "uchar" );
+  this->m_DefaultParameters[ "ImageDimension" ] = TParameter( "int", "3" );
+  this->m_DefaultParameters[ "IsColorImage" ] = TParameter( "bool", "0" );
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::Plugins::ImageSeriesReader::
+~ImageSeriesReader( )
+{
+}
+
+// -------------------------------------------------------------------------
+std::string cpPlugins::Plugins::ImageSeriesReader::
+GetClassName( ) const
+{
+  return( "cpPlugins::Plugins::ImageSeriesReader" );
+}
+
+// -------------------------------------------------------------------------
+std::string cpPlugins::Plugins::ImageSeriesReader::
+_GenerateData( )
+{
+  TParameters::const_iterator dIt;
+
+  // Get image dimension
+  dIt = this->m_Parameters.find( "ImageDimension" );
+  if( dIt == this->m_Parameters.end( ) )
+    dIt = this->m_DefaultParameters.find( "ImageDimension" );
+
+  std::string r = "cpPlugins::Plugins::ImageSeriesReader: itk::Image dimension not supported.";
+  if( dIt->second.second == "2" ) r = this->_GD0< 2 >( );
+  else if( dIt->second.second == "3" ) r = this->_GD0< 3 >( );
+  else if( dIt->second.second == "4" ) r = this->_GD0< 4 >( );
+
+  return( r );
+}
+
+// -------------------------------------------------------------------------
+template< unsigned int D >
+std::string cpPlugins::Plugins::ImageSeriesReader::
+_GD0( )
+{
+  TParameters::const_iterator tIt, cIt;
+
+  // Get image pixel type
+  tIt = this->m_Parameters.find( "PixelType" );
+  if( tIt == this->m_Parameters.end( ) )
+    tIt = this->m_DefaultParameters.find( "PixelType" );
+  cIt = this->m_Parameters.find( "IsColorImage" );
+  if( cIt == this->m_Parameters.end( ) )
+    cIt = this->m_DefaultParameters.find( "IsColorImage" );
+
+  std::string r = "cpPlugins::Plugins::ImageSeriesReader: itk::Image pixel type not supported";
+  if( cIt->second.second == "0" )
+  {
+    if( tIt->second.second == "char" )
+      r = this->_GD1< char, D >( );
+    else if( tIt->second.second == "short" )
+      r = this->_GD1< short, D >( );
+    else if( tIt->second.second == "int" )
+      r = this->_GD1< int, D >( );
+    else if( tIt->second.second == "long" )
+      r = this->_GD1< long, D >( );
+    else if( tIt->second.second == "uchar" )
+      r = this->_GD1< unsigned char, D >( );
+    else if( tIt->second.second == "ushort" )
+      r = this->_GD1< unsigned short, D >( );
+    else if( tIt->second.second == "uint" )
+      r = this->_GD1< unsigned int, D >( );
+    else if( tIt->second.second == "ulong" )
+      r = this->_GD1< unsigned long, D >( );
+    else if( tIt->second.second == "float" )
+      r = this->_GD1< float, D >( );
+    else if( tIt->second.second == "double" )
+      r = this->_GD1< double, D >( );
+  }
+  else if( cIt->second.second == "1" )
+  {
+    if( tIt->second.second == "char" )
+      r = this->_GD1< itk::RGBPixel< char >, D >( );
+    else if( tIt->second.second == "short" )
+      r = this->_GD1< itk::RGBPixel< short >, D >( );
+    else if( tIt->second.second == "int" )
+      r = this->_GD1< itk::RGBPixel< int >, D >( );
+    else if( tIt->second.second == "long" )
+      r = this->_GD1< itk::RGBPixel< long >, D >( );
+    else if( tIt->second.second == "uchar" )
+      r = this->_GD1< itk::RGBPixel< unsigned char >, D >( );
+    else if( tIt->second.second == "ushort" )
+      r = this->_GD1< itk::RGBPixel< unsigned short >, D >( );
+    else if( tIt->second.second == "uint" )
+      r = this->_GD1< itk::RGBPixel< unsigned int >, D >( );
+    else if( tIt->second.second == "ulong" )
+      r = this->_GD1< itk::RGBPixel< unsigned long >, D >( );
+    else if( tIt->second.second == "float" )
+      r = this->_GD1< itk::RGBPixel< float >, D >( );
+    else if( tIt->second.second == "double" )
+      r = this->_GD1< itk::RGBPixel< double >, D >( );
+  } // fi
+  return( r );
+}
+
+// -------------------------------------------------------------------------
+template< class P, unsigned int D >
+std::string cpPlugins::Plugins::ImageSeriesReader::
+_GD1( )
+{
+  TParameters::const_iterator fIt;
+
+  // Get filenames
+  std::set< std::string > filenames;
+  fIt = this->m_Parameters.find( "FileNames" );
+  if( fIt == this->m_Parameters.end( ) )
+    fIt = this->m_DefaultParameters.find( "FileName" );
+  std::istringstream filenames_stream( fIt->second.second );
+  std::string filename;
+  while( std::getline( filenames_stream, filename, ';' ) )
+    filenames.insert( filename );
+
+  // Reader
+  typedef itk::Image< P, D > _TImage;
+  typedef itk::ImageSeriesReader< _TImage > _TReader;
+
+  _TReader* reader =
+    dynamic_cast< _TReader* >( this->m_Reader.GetPointer( ) );
+  if( reader == NULL )
+  {
+    this->m_Reader = _TReader::New( );
+    reader = dynamic_cast< _TReader* >( this->m_Reader.GetPointer( ) );
+
+  } // fi
+  std::set< std::string >::const_iterator fnIt = filenames.begin( );
+  for( ; fnIt != filenames.end( ); ++fnIt )
+    reader->AddFileName( *fnIt );
+  try
+  {
+    reader->Update( );
+  }
+  catch( itk::ExceptionObject& err )
+  {
+    return( err.GetDescription( ) );
+
+  } // yrt
+  this->_SetOutput( 0, reader->GetOutput( ) );
+  return( "" );
+}
+
+// eof - $RCSfile$
diff --git a/lib/cpPlugins/Plugins/ImageSeriesReader.h b/lib/cpPlugins/Plugins/ImageSeriesReader.h
new file mode 100644 (file)
index 0000000..6ddf039
--- /dev/null
@@ -0,0 +1,53 @@
+#ifndef __CPPLUGINS__PLUGINS__IMAGESERIESREADER__H__
+#define __CPPLUGINS__PLUGINS__IMAGESERIESREADER__H__
+
+#include <cpPlugins/Plugins/cpPlugins_Export.h>
+#include <cpPlugins/Interface/SourceObject.h>
+#include <itkProcessObject.h>
+
+namespace cpPlugins
+{
+  namespace Plugins
+  {
+    /**
+     */
+    class cpPlugins_EXPORT ImageSeriesReader
+      : public cpPlugins::Interface::SourceObject
+    {
+    public:
+      typedef ImageSeriesReader                        Self;
+      typedef cpPlugins::Interface::SourceObject Superclass;
+
+      typedef Superclass::TParameter  TParameter;
+      typedef Superclass::TParameters TParameters;
+
+    public:
+      ImageSeriesReader( );
+      virtual ~ImageSeriesReader( );
+
+      virtual std::string GetClassName( ) const;
+
+    protected:
+
+      virtual std::string _GenerateData( );
+
+      template< unsigned int D >
+      std::string _GD0( );
+
+      template< class P, unsigned int D >
+      std::string _GD1( );
+
+    protected:
+      itk::ProcessObject::Pointer m_Reader;
+    };
+
+    // ---------------------------------------------------------------------
+    PLUMA_INHERIT_PROVIDER( ImageSeriesReader, cpPlugins::Interface::Object );
+
+  } // ecapseman
+
+} // ecapseman
+
+#endif // __CPPLUGINS__PLUGINS__IMAGESERIESREADER__H__
+
+// eof - $RCSfile$
index afd5b30858b5b9b72b4f8f1c3af2bf8d1e0d0d86..41fdb301f0f67dc4375ef714d805e5794ab38bdf 100644 (file)
@@ -53,7 +53,7 @@ _GenerateData( )
 {
   itk::DataObject* o = this->_GetInput( 0 );
 
-  std::string r = "itk::Image dimension not supported.";
+  std::string r = "cpPlugins::Plugins::ImageWriter: itk::Image dimension not supported.";
   cpPlugins_ImageWriter_Dimension( r, 1, o, _GD0 );
   else cpPlugins_ImageWriter_Dimension( r, 2, o, _GD0 );
   else cpPlugins_ImageWriter_Dimension( r, 3, o, _GD0 );
@@ -69,7 +69,7 @@ _GD0( )
   itk::ImageBase< D >* i =
     dynamic_cast< itk::ImageBase< D >* >( this->_GetInput( 0 ) );
 
-  std::string r = "itk::Image pixel type not supported";
+  std::string r = "cpPlugins::Plugins::ImageWriter: itk::Image pixel type not supported";
   cpPlugins_ImageWriter_Pixel( r, char, D, i, _GD1 );
   else cpPlugins_ImageWriter_Pixel( r, short, D, i, _GD1 );
   else cpPlugins_ImageWriter_Pixel( r, int, D, i, _GD1 );