• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

hsi_to_rgb.hh

00001 // Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE)
00002 //
00003 // This file is part of Olena.
00004 //
00005 // Olena is free software: you can redistribute it and/or modify it under
00006 // the terms of the GNU General Public License as published by the Free
00007 // Software Foundation, version 2 of the License.
00008 //
00009 // Olena is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free
00018 // software project without restriction.  Specifically, if other files
00019 // instantiate templates or use macros or inline functions from this
00020 // file, or you compile this file and link it with other files to produce
00021 // an executable, this file does not by itself cause the resulting
00022 // executable to be covered by the GNU General Public License.  This
00023 // exception does not however invalidate any other reasons why the
00024 // executable file might be covered by the GNU General Public License.
00025 
00026 #ifndef MLN_FUN_V2V_HSI_TO_RGB_HH
00027 # define MLN_FUN_V2V_HSI_TO_RGB_HH
00028 
00032 
00033 #include <cmath>
00034 
00035 #include <mln/value/rgb8.hh>
00036 #include <mln/math/round.hh>
00037 
00038 #include <mln/value/hsi.hh>
00039 
00040 
00041 namespace mln
00042 {
00043 
00044 
00045   namespace fun
00046   {
00047 
00048     namespace v2v
00049     {
00050 
00054       //
00055       template <typename T_rgb>
00056       struct f_hsi_to_rgb_ : public Function_v2v< f_hsi_to_rgb_<T_rgb> >
00057       {
00058         typedef T_rgb result;
00059 
00060         template <typename T_hsi>
00061         T_rgb operator()(const T_hsi& hsi) const;
00062 
00063       };
00064 
00065       typedef f_hsi_to_rgb_<value::rgb8> f_hsi_to_rgb_3x8_t;
00066 
00067       extern f_hsi_to_rgb_3x8_t f_hsi_to_rgb_3x8;
00068 
00069 
00070 # ifndef MLN_INCLUDE_ONLY
00071 
00074       f_hsi_to_rgb_3x8_t f_hsi_to_rgb_3x8;
00076 
00077       template <typename T_rgb>
00078       template <typename T_hsi>
00079       inline
00080       T_rgb
00081       f_hsi_to_rgb_<T_rgb>::operator()(const T_hsi& hsi) const
00082       {
00083         typedef typename T_rgb::red_t   red_t;
00084         typedef typename T_rgb::green_t green_t;
00085         typedef typename T_rgb::blue_t  blue_t;
00086 
00087         static math::round<red_t>   to_r;
00088         static math::round<green_t> to_g;
00089         static math::round<blue_t>  to_b;
00090 
00091         static const float
00092           sqrt3_3   = std::sqrt(3) / 3,
00093           inv_sqrt6 = 1 / std::sqrt(6),
00094           inv_sqrt2 = 1 / std::sqrt(2);
00095 
00096         float
00097           h     = hsi.hue() / 180.0 * 3.1415,
00098           alpha = hsi.sat() * std::cos(h),
00099           beta  = hsi.sat() * std::sin(h);
00100 
00101 
00102         red_t   r = to_r(sqrt3_3 * hsi.inty() + 2 * inv_sqrt6 * beta);
00103         green_t g =
00104           to_g(sqrt3_3 * hsi.inty() + inv_sqrt2 * alpha - inv_sqrt6 * beta);
00105         blue_t  b =
00106           to_b(sqrt3_3 * hsi.inty() - inv_sqrt2 * alpha - inv_sqrt6 * beta);
00107 
00108         T_rgb rgb(r, g, b);
00109 
00110         return rgb;
00111       }
00112 
00113 # endif // !MLN_INCLUDE_ONLY
00114 
00115     } // end of namespace fun::v2v
00116 
00117   } // end of namespace fun
00118 
00119 } // end of namespace mln
00120 
00121 #endif // ! MLN_FUN_V2V_HSI_TO_RGB_HH

Generated on Tue Oct 4 2011 15:23:55 for Milena (Olena) by  doxygen 1.7.1