]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Plugins/ImageWriter.cxx
41fdb301f0f67dc4375ef714d805e5794ab38bdf
[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 cpPlugins::Plugins::ImageWriter::
28 ImageWriter( )
29   : Superclass( )
30 {
31   this->SetNumberOfInputs( 1 );
32
33   this->m_DefaultParameters[ "FileName" ] =
34     TParameter( "string", "no_file_name" );
35 }
36
37 // -------------------------------------------------------------------------
38 cpPlugins::Plugins::ImageWriter::
39 ~ImageWriter( )
40 {
41 }
42
43 // -------------------------------------------------------------------------
44 std::string cpPlugins::Plugins::ImageWriter::
45 GetClassName( ) const
46 {
47   return( "cpPlugins::Plugins::ImageWriter" );
48 }
49
50 // -------------------------------------------------------------------------
51 std::string cpPlugins::Plugins::ImageWriter::
52 _GenerateData( )
53 {
54   itk::DataObject* o = this->_GetInput( 0 );
55
56   std::string r = "cpPlugins::Plugins::ImageWriter: itk::Image dimension not supported.";
57   cpPlugins_ImageWriter_Dimension( r, 1, o, _GD0 );
58   else cpPlugins_ImageWriter_Dimension( r, 2, o, _GD0 );
59   else cpPlugins_ImageWriter_Dimension( r, 3, o, _GD0 );
60   else cpPlugins_ImageWriter_Dimension( r, 4, o, _GD0 );
61   return( r );
62 }
63
64 // -------------------------------------------------------------------------
65 template< unsigned int D >
66 std::string cpPlugins::Plugins::ImageWriter::
67 _GD0( )
68 {
69   itk::ImageBase< D >* i =
70     dynamic_cast< itk::ImageBase< D >* >( this->_GetInput( 0 ) );
71
72   std::string r = "cpPlugins::Plugins::ImageWriter: itk::Image pixel type not supported";
73   cpPlugins_ImageWriter_Pixel( r, char, D, i, _GD1 );
74   else cpPlugins_ImageWriter_Pixel( r, short, D, i, _GD1 );
75   else cpPlugins_ImageWriter_Pixel( r, int, D, i, _GD1 );
76   else cpPlugins_ImageWriter_Pixel( r, long, D, i, _GD1 );
77   else cpPlugins_ImageWriter_Pixel( r, unsigned char, D, i, _GD1 );
78   else cpPlugins_ImageWriter_Pixel( r, unsigned short, D, i, _GD1 );
79   else cpPlugins_ImageWriter_Pixel( r, unsigned int, D, i, _GD1 );
80   else cpPlugins_ImageWriter_Pixel( r, unsigned long, D, i, _GD1 );
81   else cpPlugins_ImageWriter_Pixel( r, float, D, i, _GD1 );
82   else cpPlugins_ImageWriter_Pixel( r, double, D, i, _GD1 );
83   else cpPlugins_ImageWriter_RGB( r, char, D, i, _GD1 );
84   else cpPlugins_ImageWriter_RGB( r, short, D, i, _GD1 );
85   else cpPlugins_ImageWriter_RGB( r, int, D, i, _GD1 );
86   else cpPlugins_ImageWriter_RGB( r, long, D, i, _GD1 );
87   else cpPlugins_ImageWriter_RGB( r, unsigned char, D, i, _GD1 );
88   else cpPlugins_ImageWriter_RGB( r, unsigned short, D, i, _GD1 );
89   else cpPlugins_ImageWriter_RGB( r, unsigned int, D, i, _GD1 );
90   else cpPlugins_ImageWriter_RGB( r, unsigned long, D, i, _GD1 );
91   else cpPlugins_ImageWriter_RGB( r, float, D, i, _GD1 );
92   else cpPlugins_ImageWriter_RGB( r, double, D, i, _GD1 );
93   return( r );
94 }
95
96 // -------------------------------------------------------------------------
97 template< class P, unsigned int D >
98 std::string cpPlugins::Plugins::ImageWriter::
99 _GD1( )
100 {
101   typedef itk::Image< P, D > _TImage;
102   typedef itk::ImageFileWriter< _TImage > _TImageWriter;
103
104   TParameters::const_iterator fIt;
105
106   // Get image pixelType
107   fIt = this->m_Parameters.find( "FileName" );
108   if( fIt == this->m_Parameters.end( ) )
109     fIt = this->m_DefaultParameters.find( "FileName" );
110
111   _TImageWriter* writer =
112     dynamic_cast< _TImageWriter* >( this->m_Writer.GetPointer( ) );
113   if( writer == NULL )
114   {
115     this->m_Writer = _TImageWriter::New( );
116     writer = dynamic_cast< _TImageWriter* >( this->m_Writer.GetPointer( ) );
117
118   } // fi
119   writer->SetFileName( fIt->second.second );
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$