core/vil1/vil1_colour_space.cxx

Go to the documentation of this file.
00001 // This is core/vil1/vil1_colour_space.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author fsm
00008 
00009 #include "vil1_colour_space.h"
00010 #include <vcl_cstdlib.h>
00011 #include <vcl_algorithm.h>
00012 #include <vcl_cmath.h>
00013 
00014 template <class T>
00015 void vil1_colour_space_RGB_to_YIQ(T const in[3], T out[3])
00016 {
00017   out[0] = T(0.299) * in[0] + T(0.587) * in[1] + T(0.114) * in[2];
00018   out[1] = T(0.596) * in[0] - T(0.275) * in[1] - T(0.321) * in[2];
00019   out[2] = T(0.212) * in[0] - T(0.523) * in[1] + T(0.311) * in[2];
00020 }
00021 
00022 //:
00023 // \verbatim
00024 //     green --- yellow     //
00025 //    /    \     /     \    //
00026 // cyan --- white ---- red  //
00027 //    \    /     \     /    //
00028 //     blue  --- magenta    //
00029 //                          //
00030 // \endverbatim
00031 
00032 template <class T>
00033 void vil1_colour_space_RGB_to_HSV(T r, T g, T b, T *h, T *s, T *v)
00034 {
00035   T max = vcl_max(r, vcl_max(g, b));
00036   T min = vcl_min(r, vcl_min(g, b));
00037 
00038   // The value v is just the maximum.
00039   *v = max;
00040 
00041   // Next, saturation.
00042   if (max > 0)
00043     *s = (max - min)/max;
00044   else
00045     *s = 0;
00046 
00047   // Lastly, the hue:
00048   if (*s == 0)
00049     *h = T(); // The hue is undefined in the achromatic case.
00050   else {
00051     T delta = max - min;
00052     if      (r == max)
00053       *h = (g - b)/delta;
00054     else if (g == max)
00055       *h = 2 + (b - r)/delta;
00056     else if (b == max)
00057       *h = 4 + (r - g)/delta;
00058     else
00059       vcl_abort();
00060 
00061     *h *= 60;
00062     if (*h < 0)
00063       *h += 360;
00064   }
00065 }
00066 
00067 template <class T>
00068 void vil1_colour_space_HSV_to_RGB(T h, T s, T v, T *r, T *g, T *b)
00069 {
00070   T p1, p2, p3, f, nr=0, ng=0, nb=0;
00071   T xh;
00072   int i;
00073 
00074   v = v/255;
00075 
00076 #if 0
00077   extern float hue,  s,  v;  // hue (0.0 to 360.0, is circular, 0=360)
00078                              // s and v are from 0.0 to 1.0
00079   extern long  r2,  g2,  b2; // values from 0 to 63
00080 #endif
00081 
00082   if (h == 360)
00083   h = 0;           /* (THIS LOOKS BACKWARDS)       */
00084 
00085   xh = h / 60;                   // convert hue to be in [0,6)
00086   i = (int)vcl_floor((double)xh);// i = greatest integer <= xh
00087   f = xh - i;                    // f = fractional part of xh
00088   p1 = v * (1 - s);
00089   p2 = v * (1 - (s * f));
00090   p3 = v * (1 - (s * (1 - f)));
00091 
00092   switch (i)
00093   {
00094     case 0:
00095             nr = v;
00096             ng = p3;
00097             nb = p1;
00098             break;
00099     case 1:
00100             nr = p2;
00101             ng = v;
00102             nb = p1;
00103             break;
00104     case 2:
00105             nr = p1;
00106             ng = v;
00107             nb = p3;
00108             break;
00109     case 3:
00110             nr = p1;
00111             ng = p2;
00112             nb = v;
00113             break;
00114     case 4:
00115             nr = p3;
00116             ng = p1;
00117             nb = v;
00118             break;
00119     case 5:
00120             nr = v;
00121             ng = p1;
00122             nb = p2;
00123             break;
00124   }
00125 
00126   *r = nr * 255; /* Normalize the values to 63 */
00127   *g = ng * 255;
00128   *b = nb * 255;
00129   return;
00130 }
00131 
00132 
00133 //----------------------------------------------------------------------
00134 
00135 #define inst(T) \
00136 template void vil1_colour_space_RGB_to_YIQ(T const [3], T [3]); \
00137 template void vil1_colour_space_RGB_to_HSV(T, T, T, T *, T *, T *); \
00138 template void vil1_colour_space_HSV_to_RGB(T, T, T, T *, T *, T *)
00139 
00140 inst(double);

Generated on Sun Sep 7 05:08:27 2008 for core/vil1 by  doxygen 1.5.1