]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Plugins/ImageWriter.cxx
Major refactoring: API-HCI bug corrected.
[cpPlugins.git] / lib / cpPlugins / Plugins / ImageWriter.cxx
1 #include <cpPlugins/Plugins/ImageWriter.h>
2
3 #undef ITK_MANUAL_INSTANTIATION
4 #include <itkImage.h>
5 #include <itkRGBPixel.h>
6 #include <itkImageFileWriter.h>
7
8 // -------------------------------------------------------------------------
9 #define cpPlugins_ImageWriter_Dimension( r, d, i, f )                \
10   if( dynamic_cast< itk::ImageBase< d >* >( i ) != NULL )            \
11     r = this->f< d >( i )
12
13 // -------------------------------------------------------------------------
14 #define cpPlugins_ImageWriter_Pixel( r, p, d, i, f )                    \
15   if( dynamic_cast< itk::Image< p, d >* >( i ) != NULL )                \
16     r = this->f< p, d >( i )
17
18 // -------------------------------------------------------------------------
19 #define cpPlugins_ImageWriter_RGB( r, p, d, i, f )                      \
20   if(                                                                   \
21     dynamic_cast< itk::Image< itk::RGBPixel< p >, d >* >( i ) != NULL   \
22     )                                                                   \
23     r = this->f< itk::RGBPixel< p >, d >( i )
24
25 // -------------------------------------------------------------------------
26 cpPlugins::Plugins::ImageWriter::
27 ImageWriter( )
28   : Superclass( )
29 {
30   this->m_ClassName = "cpPlugins::ImageWriter";
31   this->m_ClassCategory = "ImageWriter";
32   this->SetNumberOfInputs( 1 );
33
34   using namespace cpPlugins::Interface;
35   this->m_DefaultParameters.Configure( Parameters::String, "FileName" );
36   this->m_Parameters = this->m_DefaultParameters;
37 }
38
39 // -------------------------------------------------------------------------
40 cpPlugins::Plugins::ImageWriter::
41 ~ImageWriter( )
42 {
43 }
44
45 // -------------------------------------------------------------------------
46 std::string cpPlugins::Plugins::ImageWriter::
47 _GenerateData( )
48 {
49   itk::DataObject* i = this->m_Inputs[ 0 ]->GetITKDataObject( );
50
51   std::string r = "ImageWriter: Image dimension not supported.";
52   cpPlugins_ImageWriter_Dimension     ( r, 1, i, _GD0 );
53   else cpPlugins_ImageWriter_Dimension( r, 2, i, _GD0 );
54   else cpPlugins_ImageWriter_Dimension( r, 3, i, _GD0 );
55   return( r );
56 }
57
58 // -------------------------------------------------------------------------
59 template< unsigned int D >
60 std::string cpPlugins::Plugins::ImageWriter::
61 _GD0( itk::DataObject* i )
62 {
63   std::string r = "ImageWriter: Image pixel type not supported";
64   cpPlugins_ImageWriter_Pixel     ( r, char          , D, i, _GD1 );
65   else cpPlugins_ImageWriter_Pixel( r, short         , D, i, _GD1 );
66   else cpPlugins_ImageWriter_Pixel( r, int           , D, i, _GD1 );
67   else cpPlugins_ImageWriter_Pixel( r, long          , D, i, _GD1 );
68   else cpPlugins_ImageWriter_Pixel( r, unsigned char , D, i, _GD1 );
69   else cpPlugins_ImageWriter_Pixel( r, unsigned short, D, i, _GD1 );
70   else cpPlugins_ImageWriter_Pixel( r, unsigned int  , D, i, _GD1 );
71   else cpPlugins_ImageWriter_Pixel( r, unsigned long , D, i, _GD1 );
72   else cpPlugins_ImageWriter_Pixel( r, float         , D, i, _GD1 );
73   else cpPlugins_ImageWriter_Pixel( r, double        , D, i, _GD1 );
74   else cpPlugins_ImageWriter_RGB  ( r, char          , D, i, _GD1 );
75   else cpPlugins_ImageWriter_RGB  ( r, short         , D, i, _GD1 );
76   else cpPlugins_ImageWriter_RGB  ( r, int           , D, i, _GD1 );
77   else cpPlugins_ImageWriter_RGB  ( r, long          , D, i, _GD1 );
78   else cpPlugins_ImageWriter_RGB  ( r, unsigned char , D, i, _GD1 );
79   else cpPlugins_ImageWriter_RGB  ( r, unsigned short, D, i, _GD1 );
80   else cpPlugins_ImageWriter_RGB  ( r, unsigned int  , D, i, _GD1 );
81   else cpPlugins_ImageWriter_RGB  ( r, unsigned long , D, i, _GD1 );
82   else cpPlugins_ImageWriter_RGB  ( r, float         , D, i, _GD1 );
83   else cpPlugins_ImageWriter_RGB  ( r, double        , D, i, _GD1 );
84   return( r );
85 }
86
87 // -------------------------------------------------------------------------
88 template< class P, unsigned int D >
89 std::string cpPlugins::Plugins::ImageWriter::
90 _GD1( itk::DataObject* i )
91 {
92   typedef itk::Image< P, D > _I;
93   typedef itk::ImageFileWriter< _I > _W;
94
95   // Get filename
96   using namespace cpPlugins::Interface;
97   Parameters::TString fname =
98     this->m_Parameters.GetValueAsString( "FileName" );
99
100   _W* writer = dynamic_cast< _W* >( this->m_RealProcessObject.GetPointer( ) );
101   if( writer == NULL )
102   {
103     this->m_RealProcessObject = _W::New( );
104     writer = dynamic_cast< _W* >( this->m_RealProcessObject.GetPointer( ) );
105
106   } // fi
107   writer->SetFileName( fname );
108   writer->SetInput( dynamic_cast< _I* >( i ) );
109   try
110   {
111     writer->Update( );
112   }
113   catch( itk::ExceptionObject& err )
114   {
115     return( "ImageWriter: " + std::string( err.GetDescription( ) ) );
116
117   } // yrt
118   return( "" );
119 }
120
121 // eof - $RCSfile$