]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Algorithms/RGBToYPbPrFunction.h
...
[cpPlugins.git] / lib / cpExtensions / Algorithms / RGBToYPbPrFunction.h
1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // -------------------------------------------------------------------------
4
5 #ifndef __CPEXTENSIONS__ALGORITHMS__RGBTOYPBPRFUNCTION__H__
6 #define __CPEXTENSIONS__ALGORITHMS__RGBTOYPBPRFUNCTION__H__
7
8 #include <cmath>
9 #include <limits>
10 #include <vnl/vnl_math.h>
11 #include <vnl/vnl_matrix.h>
12
13 #include <itkRGBPixel.h>
14 #include <itkMatrix.h>
15 #include <itkVector.h>
16
17 namespace cpExtensions
18   {
19     namespace Algorithms
20     {
21       /**
22        */
23       template< class O >
24       struct RGBToYPbPrFunction
25       {
26         typedef RGBToYPbPrFunction  Self;
27         typedef itk::Vector< O, 3 > TOutPixel;
28
29         template< class Tr, class Tg, class Tb >
30         TOutPixel operator()( const Tr& r, const Tg& g, const Tb& b ) const
31           {
32             static const O M[] =
33               {
34                 O(  0.2990 ), O(  0.5870 ), O(  0.1140 ),
35                 O( -0.1687 ), O( -0.3313 ), O(  0.5000 ),
36                 O(  0.5000 ), O( -0.4187 ), O( -0.0813 )
37               };
38             static const vnl_matrix< O > vM( M, 3, 3 );
39             static const itk::Matrix< O, 3, 3 > iM( vM );
40
41             TOutPixel rgb;
42             rgb[ 0 ] = O( r );
43             rgb[ 1 ] = O( g );
44             rgb[ 2 ] = O( b );
45             return( iM * rgb );
46           }
47
48         template< class C >
49         TOutPixel operator()( const itk::RGBPixel< C >& rgb ) const
50           {
51             return(
52               this->operator()(
53                 rgb.GetRed( ), rgb.GetGreen( ), rgb.GetBlue( )
54                 )
55               );
56           }
57       };
58
59     } // ecapseman
60
61 } // ecapseman
62
63 #endif // __CPEXTENSIONS__ALGORITHMS__RGBTOYPBPRFUNCTION__H__
64
65 // eof - $RCSfile$