]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Algorithms/RGBToSingleChannelFunctor.hxx
...
[cpPlugins.git] / lib / cpExtensions / Algorithms / RGBToSingleChannelFunctor.hxx
1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // -------------------------------------------------------------------------
4
5 #ifndef __cpExtensions__Algorithms__RGBToSingleChannelFunctor__hxx__
6 #define __cpExtensions__Algorithms__RGBToSingleChannelFunctor__hxx__
7
8 #include <cmath>
9 #include <limits>
10 #include <vnl/vnl_math.h>
11
12 // -------------------------------------------------------------------------
13 template< class _TScalar >
14 bool cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
15 IsChannelRed( ) const
16 {
17   return( this->m_Channel == Self::Red );
18 }
19
20 // -------------------------------------------------------------------------
21 template< class _TScalar >
22 bool cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
23 IsChannelGreen( ) const
24 {
25   return( this->m_Channel == Self::Green );
26 }
27
28 // -------------------------------------------------------------------------
29 template< class _TScalar >
30 bool cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
31 IsChannelBlue( ) const
32 {
33   return( this->m_Channel == Self::Blue );
34 }
35
36 // -------------------------------------------------------------------------
37 template< class _TScalar >
38 bool cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
39 IsChannelHue( ) const
40 {
41   return( this->m_Channel == Self::Hue );
42 }
43
44 // -------------------------------------------------------------------------
45 template< class _TScalar >
46 bool cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
47 IsChannelSaturation( ) const
48 {
49   return( this->m_Channel == Self::Saturation );
50 }
51
52 // -------------------------------------------------------------------------
53 template< class _TScalar >
54 bool cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
55 IsChannelValue( ) const
56 {
57   return( this->m_Channel == Self::Value );
58 }
59
60 // -------------------------------------------------------------------------
61 template< class _TScalar >
62 void cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
63 SetChannelToRed( )
64 {
65   this->SetChannel( Self::Red );
66 }
67
68 // -------------------------------------------------------------------------
69 template< class _TScalar >
70 void cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
71 SetChannelToGreen( )
72 {
73   this->SetChannel( Self::Green );
74 }
75
76 // -------------------------------------------------------------------------
77 template< class _TScalar >
78 void cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
79 SetChannelToBlue( )
80 {
81   this->SetChannel( Self::Blue );
82 }
83
84 // -------------------------------------------------------------------------
85 template< class _TScalar >
86 void cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
87 SetChannelToHue( )
88 {
89   this->SetChannel( Self::Hue );
90 }
91
92 // -------------------------------------------------------------------------
93 template< class _TScalar >
94 void cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
95 SetChannelToSaturation( )
96 {
97   this->SetChannel( Self::Saturation );
98 }
99
100 // -------------------------------------------------------------------------
101 template< class _TScalar >
102 void cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
103 SetChannelToValue( )
104 {
105   this->SetChannel( Self::Value );
106 }
107
108 // -------------------------------------------------------------------------
109 template< class _TScalar >
110 _TScalar cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
111 Evaluate( const TPixel& pixel ) const
112 {
113   _TScalar res;
114   switch( this->m_Channel )
115   {
116   case Self::Hue:
117   {
118     double R = double( pixel.GetRed( ) );
119     double G = double( pixel.GetRed( ) );
120     double B = double( pixel.GetBlue( ) );
121     double RG = R - G;
122     double RB = R - B;
123     double GB = G - B;
124     double A = std::sqrt( ( RG * RG ) + ( RB * GB ) );
125     if( A != _0 )
126       A = std::acos( ( RG + RB ) / ( double( 2 ) * A ) );
127     A /= double( 2 ) * double( vnl_math::pi );
128     res =
129       _TScalar(
130         double( std::numeric_limits< _TScalar >::max( ) ) *
131         ( ( G >= B )? A: double( 1 ) - A )
132         );
133   }
134   break;
135   case Self::Saturation:
136   {
137     double R = double( r );
138     double G = double( g );
139     double B = double( b );
140     double sRGB = R + G + B;
141     double RG = R - G;
142     double RB = R - B;
143     double GB = G - B;
144     if( sRGB != double( 0 ) )
145     {
146       double C = ( G < R )? G: R;
147       C        = ( B < C )? B: C;
148       res =
149         _TScalar(
150           double( std::numeric_limits< _TScalar >::max( ) ) *
151           ( double( 1 ) - ( ( double( 3 ) * C ) / sRGB ) )
152           );
153     }
154     else
155       res = _TScalar( 0 );
156   }
157   break;
158   case Self::Value:
159   {
160     res = ( pixel.GetRed( ) + pixel.GetGreen( ) + pixel.GetBlue( ) ) / 3;
161   }
162   break;
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;
166   } // hctiws
167   return( res );
168 }
169
170 // -------------------------------------------------------------------------
171 template< class _TScalar >
172 cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
173 RGBToSingleChannelFunctor( )
174   : Superclass( )
175 {
176   this->SetChannelToRed( );
177 }
178
179 // -------------------------------------------------------------------------
180 template< class _TScalar >
181 cpExtensions::Algorithms::RGBToSingleChannelFunctor< _TScalar >::
182 ~RGBToSingleChannelFunctor( )
183 {
184 }
185
186 #endif // __cpExtensions__Algorithms__RGBToSingleChannelFunctor__hxx__
187
188 // eof - $RCSfile$