]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Plugins/ImageWriter.cxx
Some syntax simplifations
[cpPlugins.git] / lib / cpPlugins / Plugins / ImageWriter.cxx
index 3dd319afef43ec75ef6d6476aa57ce70db4c23fc..aea4a21e7d42914c01d9f7d33d5cf05abf8eae98 100644 (file)
@@ -1,10 +1,44 @@
 #include <cpPlugins/Plugins/ImageWriter.h>
 
+#include <itkImageFileWriter.h>
+
+#define ITK_MANUAL_INSTANTIATION
+#include <itkImage.h>
+#include <itkRGBPixel.h>
+
+// -------------------------------------------------------------------------
+#define cpPlugins_ImageWriter_Dimension( r, d, o, f )                \
+  if( dynamic_cast< itk::ImageBase< d >* >( o ) != NULL )            \
+    r = this->f< d >( )
+
+// -------------------------------------------------------------------------
+#define cpPlugins_ImageWriter_Pixel( r, p, d, o, f )                    \
+  if( dynamic_cast< itk::Image< p, d >* >( o ) != NULL )                \
+    r = this->f< p, d >( )
+
+// -------------------------------------------------------------------------
+#define cpPlugins_ImageWriter_RGB( r, p, d, o, f )                      \
+  if(                                                                   \
+    dynamic_cast< itk::Image< itk::RGBPixel< p >, d >* >( o ) != NULL   \
+    )                                                                   \
+    r = this->f< itk::RGBPixel< p >, d >( )
+
+// -------------------------------------------------------------------------
+std::string cpPlugins::Plugins::ImageWriter::
+GetClassName( ) const
+{
+  return( "cpPlugins::Plugins::ImageWriter" );
+}
+
 // -------------------------------------------------------------------------
 cpPlugins::Plugins::ImageWriter::
 ImageWriter( )
   : Superclass( )
 {
+  this->SetNumberOfInputs( 1 );
+
+  this->m_DefaultParameters[ "FileName" ] =
+    TParameter( "string", "no_file_name" );
 }
 
 // -------------------------------------------------------------------------
@@ -15,16 +49,86 @@ cpPlugins::Plugins::ImageWriter::
 
 // -------------------------------------------------------------------------
 std::string cpPlugins::Plugins::ImageWriter::
-GetClassName( ) const
+_GenerateData( )
 {
-  return( "cpPlugins::Plugins::ImageWriter" );
+  itk::DataObject* o = this->_GetInput( 0 );
+
+  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 );
+  else cpPlugins_ImageWriter_Dimension( r, 4, o, _GD0 );
+  return( r );
 }
 
 // -------------------------------------------------------------------------
-bool cpPlugins::Plugins::ImageWriter::
-Update( )
+template< unsigned int D >
+std::string cpPlugins::Plugins::ImageWriter::
+_GD0( )
 {
-  return( false );
+  itk::ImageBase< D >* i =
+    dynamic_cast< itk::ImageBase< D >* >( this->_GetInput( 0 ) );
+
+  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 );
+  else cpPlugins_ImageWriter_Pixel( r, long, D, i, _GD1 );
+  else cpPlugins_ImageWriter_Pixel( r, unsigned char, D, i, _GD1 );
+  else cpPlugins_ImageWriter_Pixel( r, unsigned short, D, i, _GD1 );
+  else cpPlugins_ImageWriter_Pixel( r, unsigned int, D, i, _GD1 );
+  else cpPlugins_ImageWriter_Pixel( r, unsigned long, D, i, _GD1 );
+  else cpPlugins_ImageWriter_Pixel( r, float, D, i, _GD1 );
+  else cpPlugins_ImageWriter_Pixel( r, double, D, i, _GD1 );
+  else cpPlugins_ImageWriter_RGB( r, char, D, i, _GD1 );
+  else cpPlugins_ImageWriter_RGB( r, short, D, i, _GD1 );
+  else cpPlugins_ImageWriter_RGB( r, int, D, i, _GD1 );
+  else cpPlugins_ImageWriter_RGB( r, long, D, i, _GD1 );
+  else cpPlugins_ImageWriter_RGB( r, unsigned char, D, i, _GD1 );
+  else cpPlugins_ImageWriter_RGB( r, unsigned short, D, i, _GD1 );
+  else cpPlugins_ImageWriter_RGB( r, unsigned int, D, i, _GD1 );
+  else cpPlugins_ImageWriter_RGB( r, unsigned long, D, i, _GD1 );
+  else cpPlugins_ImageWriter_RGB( r, float, D, i, _GD1 );
+  else cpPlugins_ImageWriter_RGB( r, double, D, i, _GD1 );
+  return( r );
+}
+
+// -------------------------------------------------------------------------
+template< class P, unsigned int D >
+std::string cpPlugins::Plugins::ImageWriter::
+_GD1( )
+{
+  typedef itk::Image< P, D > _TImage;
+  typedef itk::ImageFileWriter< _TImage > _TWriter;
+
+  TParameters::const_iterator fIt;
+
+  // Get image pixelType
+  fIt = this->m_Parameters.find( "FileName" );
+  if( fIt == this->m_Parameters.end( ) )
+    fIt = this->m_DefaultParameters.find( "FileName" );
+
+  _TWriter* writer =
+    dynamic_cast< _TWriter* >( this->m_RealProcessObject.GetPointer( ) );
+  if( writer == NULL )
+  {
+    this->m_RealProcessObject = _TWriter::New( );
+    writer =
+      dynamic_cast< _TWriter* >( this->m_RealProcessObject.GetPointer( ) );
+
+  } // fi
+  writer->SetFileName( fIt->second.second );
+  writer->SetInput( dynamic_cast< _TImage* >( this->_GetInput( 0 ) ) );
+  try
+  {
+    writer->Update( );
+  }
+  catch( itk::ExceptionObject& err )
+  {
+    return( err.GetDescription( ) );
+
+  } // yrt
+  return( "" );
 }
 
 // eof - $RCSfile$