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