1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // -------------------------------------------------------------------------
5 #ifndef __cpExtensions__Algorithms__RGBToSingleChannelFunctor__hxx__
6 #define __cpExtensions__Algorithms__RGBToSingleChannelFunctor__hxx__
10 #include <vnl/vnl_math.h>
12 // -------------------------------------------------------------------------
13 template< class _TScalar >
14 bool cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
17 return( this->m_Channel == Self::Red );
20 // -------------------------------------------------------------------------
21 template< class _TScalar >
22 bool cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
23 IsChannelGreen( ) const
25 return( this->m_Channel == Self::Green );
28 // -------------------------------------------------------------------------
29 template< class _TScalar >
30 bool cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
31 IsChannelBlue( ) const
33 return( this->m_Channel == Self::Blue );
36 // -------------------------------------------------------------------------
37 template< class _TScalar >
38 bool cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
41 return( this->m_Channel == Self::Hue );
44 // -------------------------------------------------------------------------
45 template< class _TScalar >
46 bool cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
47 IsChannelSaturation( ) const
49 return( this->m_Channel == Self::Saturation );
52 // -------------------------------------------------------------------------
53 template< class _TScalar >
54 bool cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
55 IsChannelValue( ) const
57 return( this->m_Channel == Self::Value );
60 // -------------------------------------------------------------------------
61 template< class _TScalar >
62 void cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
65 this->SetChannel( Self::Red );
68 // -------------------------------------------------------------------------
69 template< class _TScalar >
70 void cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
73 this->SetChannel( Self::Green );
76 // -------------------------------------------------------------------------
77 template< class _TScalar >
78 void cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
81 this->SetChannel( Self::Blue );
84 // -------------------------------------------------------------------------
85 template< class _TScalar >
86 void cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
89 this->SetChannel( Self::Hue );
92 // -------------------------------------------------------------------------
93 template< class _TScalar >
94 void cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
95 SetChannelToSaturation( )
97 this->SetChannel( Self::Saturation );
100 // -------------------------------------------------------------------------
101 template< class _TScalar >
102 void cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
105 this->SetChannel( Self::Value );
108 // -------------------------------------------------------------------------
109 template< class _TScalar >
110 _TScalar cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
111 Evaluate( const TPixel& pixel ) const
114 switch( this->m_Channel )
118 double R = double( pixel.GetRed( ) );
119 double G = double( pixel.GetRed( ) );
120 double B = double( pixel.GetBlue( ) );
124 double A = std::sqrt( ( RG * RG ) + ( RB * GB ) );
126 A = std::acos( ( RG + RB ) / ( double( 2 ) * A ) );
127 A /= double( 2 ) * double( vnl_math::pi );
130 double( std::numeric_limits< _TScalar >::max( ) ) *
131 ( ( G >= B )? A: double( 1 ) - A )
135 case Self::Saturation:
137 double R = double( r );
138 double G = double( g );
139 double B = double( b );
140 double sRGB = R + G + B;
144 if( sRGB != double( 0 ) )
146 double C = ( G < R )? G: R;
150 double( std::numeric_limits< _TScalar >::max( ) ) *
151 ( double( 1 ) - ( ( double( 3 ) * C ) / sRGB ) )
160 res = ( pixel.GetRed( ) + pixel.GetGreen( ) + pixel.GetBlue( ) ) / 3;
163 case Self::Green: { res = pixel.GetGreen( ); } break;
164 case Self::Blue: { res = pixel.GetBlue( ); } break;
165 case Self::Red: default: { res = pixel.GetRed( ); } break;
170 // -------------------------------------------------------------------------
171 template< class _TScalar >
172 cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
173 RGBToSingleChannelFunctor( )
176 this->SetChannelToRed( );
179 // -------------------------------------------------------------------------
180 template< class _TScalar >
181 cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
182 ~RGBToSingleChannelFunctor( )
186 #endif // __cpExtensions__Algorithms__RGBToSingleChannelFunctor__hxx__