]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Algorithms/RGBToYPbPrFunction.h
yet another refactoring
[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 P >
24     struct RGBToYPbPrFunction
25     {
26       typedef RGBToYPbPrFunction    Self;
27       typedef P                     TOutPixel;
28       typedef typename P::ValueType TValue;
29
30       template< class Tr, class Tg, class Tb >
31       P operator()( const Tr& r, const Tg& g, const Tb& b ) const
32         {
33           static const double M[] =
34             {
35               double(  0.2990 ), double(  0.5870 ), double(  0.1140 ),
36               double( -0.1687 ), double( -0.3313 ), double(  0.5000 ),
37               double(  0.5000 ), double( -0.4187 ), double( -0.0813 )
38             };
39           static const vnl_matrix< double > vM( M, 3, 3 );
40           static const itk::Matrix< double, 3, 3 > iM( vM );
41
42           itk::Vector< double, 3 > rgb;
43           rgb[ 0 ] = double( r );
44           rgb[ 1 ] = double( b );
45           rgb[ 2 ] = double( g );
46           rgb = iM * rgb;
47
48           P out;
49           out[ 0 ] = TValue( rgb[ 0 ] );
50           out[ 1 ] = TValue( rgb[ 1 ] );
51           out[ 2 ] = TValue( rgb[ 2 ] );
52           return( out );
53         }
54
55       template< class C >
56       TOutPixel operator()( const itk::RGBPixel< C >& rgb ) const
57         {
58           return(
59             this->operator()(
60               rgb.GetRed( ), rgb.GetGreen( ), rgb.GetBlue( )
61               )
62             );
63         }
64     };
65
66   } // ecapseman
67
68 } // ecapseman
69
70 #endif // __CPEXTENSIONS__ALGORITHMS__RGBTOYPBPRFUNCTION__H__
71
72 // eof - $RCSfile$