]> Creatis software - cpPlugins.git/blobdiff - lib/cpExtensions/Algorithms/RGBToYPbPrFunction.h
yet another refactoring
[cpPlugins.git] / lib / cpExtensions / Algorithms / RGBToYPbPrFunction.h
diff --git a/lib/cpExtensions/Algorithms/RGBToYPbPrFunction.h b/lib/cpExtensions/Algorithms/RGBToYPbPrFunction.h
new file mode 100644 (file)
index 0000000..83085fe
--- /dev/null
@@ -0,0 +1,72 @@
+// -------------------------------------------------------------------------
+// @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
+// -------------------------------------------------------------------------
+
+#ifndef __CPEXTENSIONS__ALGORITHMS__RGBTOYPBPRFUNCTION__H__
+#define __CPEXTENSIONS__ALGORITHMS__RGBTOYPBPRFUNCTION__H__
+
+#include <cmath>
+#include <limits>
+#include <vnl/vnl_math.h>
+#include <vnl/vnl_matrix.h>
+
+#include <itkRGBPixel.h>
+#include <itkMatrix.h>
+#include <itkVector.h>
+
+namespace cpExtensions
+{
+  namespace Algorithms
+  {
+    /**
+     */
+    template< class P >
+    struct RGBToYPbPrFunction
+    {
+      typedef RGBToYPbPrFunction    Self;
+      typedef P                     TOutPixel;
+      typedef typename P::ValueType TValue;
+
+      template< class Tr, class Tg, class Tb >
+      P operator()( const Tr& r, const Tg& g, const Tb& b ) const
+        {
+          static const double M[] =
+            {
+              double(  0.2990 ), double(  0.5870 ), double(  0.1140 ),
+              double( -0.1687 ), double( -0.3313 ), double(  0.5000 ),
+              double(  0.5000 ), double( -0.4187 ), double( -0.0813 )
+            };
+          static const vnl_matrix< double > vM( M, 3, 3 );
+          static const itk::Matrix< double, 3, 3 > iM( vM );
+
+          itk::Vector< double, 3 > rgb;
+          rgb[ 0 ] = double( r );
+          rgb[ 1 ] = double( b );
+          rgb[ 2 ] = double( g );
+          rgb = iM * rgb;
+
+          P out;
+          out[ 0 ] = TValue( rgb[ 0 ] );
+          out[ 1 ] = TValue( rgb[ 1 ] );
+          out[ 2 ] = TValue( rgb[ 2 ] );
+          return( out );
+        }
+
+      template< class C >
+      TOutPixel operator()( const itk::RGBPixel< C >& rgb ) const
+        {
+          return(
+            this->operator()(
+              rgb.GetRed( ), rgb.GetGreen( ), rgb.GetBlue( )
+              )
+            );
+        }
+    };
+
+  } // ecapseman
+
+} // ecapseman
+
+#endif // __CPEXTENSIONS__ALGORITHMS__RGBTOYPBPRFUNCTION__H__
+
+// eof - $RCSfile$