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

hsi_to_rgb.hh

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

Generated on Mon Jan 30 2012 17:35:58 for Milena (Olena) by  doxygen 1.7.1