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