rgbhsi.hh

Go to the documentation of this file.
00001 // Copyright (C) 2001, 2002, 2003, 2004  EPITA Research and Development Laboratory
00002 //
00003 // This file is part of the Olena Library.  This library is free
00004 // software; you can redistribute it and/or modify it under the terms
00005 // of the GNU General Public License version 2 as published by the
00006 // Free Software Foundation.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this library; see the file COPYING.  If not, write to
00015 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
00016 // MA 02111-1307, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software library without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to
00022 // produce an executable, this file does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public
00024 // License.  This exception does not however invalidate any other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
00027 
00028 
00029 #ifndef OLENA_CONVERT_RGBHSI_HH
00030 # define OLENA_CONVERT_RGBHSI_HH
00031 
00032 # include <oln/convert/abstract/colorconv.hh>
00033 
00034 # include <ntg/basics.hh>
00035 # include <ntg/color/rgb.hh>
00036 # include <ntg/color/hsi.hh>
00037 
00038 # include <sstream>
00039 
00046 namespace oln {
00047 
00048   using namespace ntg;
00049 
00050   namespace convert {
00051 
00052     static const float sqrt3_3   = sqrt(3) / 3;
00053     static const float inv_sqrt6 = 1 / sqrt(6);
00054     static const float inv_sqrt2 = 1 / sqrt(2);
00055 
00060     template<unsigned inbits, unsigned outbits>
00061     struct f_rgb_to_hsi
00062       : public abstract::color_conversion<3, inbits, rgb_traits,
00063                                           3, outbits, hsi_traits, f_rgb_to_hsi<inbits, outbits> >
00064     {
00065       color<3, inbits, hsi_traits>
00066       doit(const color<3, outbits, rgb_traits>& v) const
00067       {
00068         vec<3, float> in = v.to_float();
00069         vec<3, float> out;
00070         out[hsi_I] =
00071           sqrt3_3 * in[rgb_R] + sqrt3_3 * in[rgb_G] + sqrt3_3 * in[rgb_B];
00072         const float v1 = inv_sqrt2 * in[rgb_G] - inv_sqrt2 * in[rgb_B];
00073         const float v2 = 2 * inv_sqrt6 * in[rgb_R] - inv_sqrt6 * in[rgb_G]
00074           - inv_sqrt6 * in[rgb_B];
00075         out[hsi_H] = atan2(v2, v1) / M_PI * 180.0;
00076         if (out[hsi_H] < 0)
00077           out[hsi_H] += 360.0;
00078         assert(out[hsi_H] >= 0);
00079         out[hsi_S] = sqrt(v1 * v1 + v2 * v2);
00080         return out;
00081       }
00082 
00083       static std::string
00084       name()
00085       {
00086         std::ostringstream s;
00087         s << "f_rgb_to_hsi<" << inbits << ", " << outbits << '>';
00088         s.str();
00089       }
00090     };
00091 
00096     template <unsigned inbits, unsigned outbits>
00097     color<3, outbits, hsi_traits>
00098     rgb_to_hsi(const color<3, inbits, rgb_traits>& v)
00099     {
00100       f_rgb_to_hsi<inbits, outbits> f;
00101 
00102       return f(v);
00103     }
00104 
00109     template<unsigned inbits, unsigned outbits>
00110     struct f_hsi_to_rgb
00111       : public abstract::color_conversion<3, inbits, hsi_traits,
00112                                           3, outbits, rgb_traits, f_hsi_to_rgb<inbits, outbits> >
00113     {
00114       color<3, outbits, rgb_traits>
00115       doit(const color<3, inbits, hsi_traits>& v) const
00116       {
00117         vec<3, float> in = v.to_float();
00118         vec<3, float> out;
00119         const float h = in[hsi_H] / 180.0 * M_PI;
00120         const float v1 = in[hsi_S] * cos(h);
00121         const float v2 = in[hsi_S] * sin(h);
00122         out[rgb_R] = sqrt3_3 * in[hsi_I] + 2 * inv_sqrt6 * v2;
00123         out[rgb_G] = sqrt3_3 * in[hsi_I] + inv_sqrt2 * v1 - inv_sqrt6 * v2;
00124         out[rgb_B] = sqrt3_3 * in[hsi_I] - inv_sqrt2 * v1 - inv_sqrt6 * v2;
00125         return out;
00126       }
00127 
00128       static std::string
00129       name()
00130         {
00131         std::ostringstream s;
00132         s << "f_hsi_to_rgb<" << inbits << ", " << outbits << '>';
00133         s.str();
00134       }
00135     };
00136 
00141     template <unsigned inbits, unsigned outbits>
00142     color<3, outbits, rgb_traits>
00143     hsi_to_rgb (const color<3, inbits, hsi_traits>& v)
00144     {
00145       f_hsi_to_rgb<inbits, outbits> f;
00146 
00147       return f(v);
00148     }
00149 
00150   } // convert
00151 } // oln
00152 
00153 #endif // OLENA_CONVERT_RGBHSI_HH

Generated on Thu Apr 15 20:13:14 2004 for Olena by doxygen 1.3.6-20040222