]> Creatis software - cpPlugins.git/blobdiff - lib/cpExtensions/Algorithms/RGBToHSVFunction.h
...
[cpPlugins.git] / lib / cpExtensions / Algorithms / RGBToHSVFunction.h
index 8536b4aa8df1a876d88a0e193b391d2acb8d29cb..b10f2954976a85e6727fa862ef8edd425d000257 100644 (file)
 #include <vnl/vnl_math.h>
 
 #include <itkRGBPixel.h>
-#include <itkVector.h>
 
 namespace cpExtensions
+{
+  namespace Algorithms
   {
-    namespace Algorithms
+    /**
+     */
+    template< class P >
+    struct RGBToHSVFunction
     {
-      /**
-       */
-      template< class O >
-      struct RGBToHSVFunction
-      {
-        typedef RGBToHSVFunction    Self;
-        typedef itk::Vector< O, 3 > TOutPixel;
+      typedef RGBToHSVFunction      Self;
+      typedef P                     TOutPixel;
+      typedef typename P::ValueType TValue;
 
-        template< class Tr, class Tg, class Tb >
-        TOutPixel operator()( const Tr& r, const Tg& g, const Tb& b ) const
-          {
-            static const double mVal =
-              double( std::numeric_limits< O >::max( ) );
-            static const double _0 = double( 0 );
-            static const double _1 = double( 1 );
-            static const double _2 = double( 2 );
-            static const double _3 = double( 3 );
-            static const double _2pi = _2 * double( vnl_math::pi );
-
-            TOutPixel hsv;
+      template< class Tr, class Tg, class Tb >
+      P operator()( const Tr& r, const Tg& g, const Tb& b ) const
+        {
+          static const double mVal =
+            double( std::numeric_limits< TValue >::max( ) );
+          static const double _0 = double( 0 );
+          static const double _1 = double( 1 );
+          static const double _2 = double( 2 );
+          static const double _3 = double( 3 );
+          static const double _2pi = _2 * double( vnl_math::pi );
 
-            double R = double( r );
-            double G = double( g );
-            double B = double( b );
-            double sRGB = R + G + B;
-            double RG = R - G;
-            double RB = R - B;
-            double GB = G - B;
+          P hsv;
 
-            // Hue
-            double A = std::sqrt( ( RG * RG ) + ( RB * GB ) );
-            if( A != _0 )
-              A = std::acos( ( RG + RB ) / ( _2 * A ) );
-            A /= _2pi;
-            hsv[ 0 ] = O( mVal * ( ( G >= B )? A: _1 - A ) );
+          double R = double( r );
+          double G = double( g );
+          double B = double( b );
+          double sRGB = R + G + B;
+          double RG = R - G;
+          double RB = R - B;
+          double GB = G - B;
 
-            // Saturation
-            if( sRGB != _0 )
-            {
-              double C = ( G < R )? G: R;
-              C        = ( B < C )? B: C;
-              hsv[ 1 ] = O( mVal * ( _1 - ( ( _3 * C ) / sRGB ) ) );
-            }
-            else
-              hsv[ 1 ] = O( 0 );
-  
-            // Value
-            hsv[ 2 ] = O( sRGB / _3 );
-            return( hsv );
-          }
+          // Hue
+          double A = std::sqrt( ( RG * RG ) + ( RB * GB ) );
+          if( A != _0 )
+            A = std::acos( ( RG + RB ) / ( _2 * A ) );
+          A /= _2pi;
+          hsv[ 0 ] = TValue( mVal * ( ( G >= B )? A: _1 - A ) );
 
-        template< class C >
-        TOutPixel operator()( const itk::RGBPixel< C >& rgb ) const
+          // Saturation
+          if( sRGB != _0 )
           {
-            return(
-              this->operator()(
-                rgb.GetRed( ), rgb.GetGreen( ), rgb.GetBlue( )
-                )
-              );
+            double C = ( G < R )? G: R;
+            C        = ( B < C )? B: C;
+            hsv[ 1 ] = TValue( mVal * ( _1 - ( ( _3 * C ) / sRGB ) ) );
           }
-      };
+          else
+            hsv[ 1 ] = TValue( 0 );
+
+          // Value
+          hsv[ 2 ] = TValue( sRGB / _3 );
+          return( hsv );
+        }
+
+      template< class C >
+      TOutPixel operator()( const itk::RGBPixel< C >& rgb ) const
+        {
+          return(
+            this->operator()(
+              rgb.GetRed( ), rgb.GetGreen( ), rgb.GetBlue( )
+              )
+            );
+        }
+    };
 
   } // ecapseman