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

rgb_to_hsi.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_RGB_TO_HSI_HH
00028 # define MLN_FUN_V2V_RGB_TO_HSI_HH
00029 
00030 #include <cmath>
00031 
00032 #include <mln/value/rgb8.hh>
00033 #include <mln/math/round.hh>
00034 
00035 #include <mln/value/hsi.hh>
00036 
00037 
00038 namespace mln
00039 {
00040 
00041 
00042   namespace fun
00043   {
00044 
00045     namespace v2v
00046     {
00047 
00048       template <typename T_hsi>
00049       struct f_rgb_to_hsi_ : public Function_v2v< f_rgb_to_hsi_<T_hsi> >
00050       {
00051         typedef T_hsi result;
00052 
00053         f_rgb_to_hsi_();
00054 
00055         template <typename T_rgb>
00056         T_hsi operator()(const T_rgb& rgb) const;
00057 
00058       };
00059 
00060       typedef f_rgb_to_hsi_<value::hsi_f> f_rgb_to_hsi_f_t;
00061 
00062       extern f_rgb_to_hsi_f_t f_rgb_to_hsi_f;
00063 
00064 
00065 # ifndef MLN_INCLUDE_ONLY
00066 
00067 #  ifndef MLN_WO_GLOBAL_VARS
00068 
00071       f_rgb_to_hsi_f_t f_rgb_to_hsi_f;
00073 
00074 #  endif // ! MLN_WO_GLOBAL_VARS
00075 
00076 
00077       template <typename T_hsi>
00078       f_rgb_to_hsi_<T_hsi>::f_rgb_to_hsi_()
00079       {
00080       }
00081 
00082       template <typename T_hsi>
00083       template <typename T_rgb>
00084       inline
00085       T_hsi
00086       f_rgb_to_hsi_<T_hsi>::operator()(const T_rgb& rgb) const
00087       {
00088         // Locals.
00089         static const double sqrt3_3 = std::sqrt(3) / 3;
00090         static const double inv_sqrt6 = 1 / std::sqrt(6);
00091         static const double inv_sqrt2 = 1 / std::sqrt(2);
00092 
00093         T_hsi hsi;
00094 
00095         double alpha = inv_sqrt2 * rgb.green() - inv_sqrt2 * rgb.blue();
00096         double beta =
00097           2 * inv_sqrt6 * rgb.red() -
00098           inv_sqrt6 * rgb.green() -
00099           inv_sqrt6 * rgb.blue();
00100 
00101 
00102         hsi.hue() = atan2(beta, alpha) / 3.1415 * 180.0;
00103         if (hsi.hue() < 0)
00104           hsi.hue() = hsi.hue() + 360.0;
00105         mln_invariant(hsi.hue() >= 0);
00106         hsi.sat() = std::sqrt(alpha * alpha + beta * beta);
00107         hsi.inty() =
00108           sqrt3_3 * rgb.red() +
00109           sqrt3_3 * rgb.green() +
00110           sqrt3_3 * rgb.blue();
00111 
00112         return hsi;
00113       }
00114 
00115 
00116 # endif // !MLN_INCLUDE_ONLY
00117 
00118     } // end of namespace fun::v2v
00119 
00120   } // end of namespace fun
00121 
00122 } // end of namespace mln
00123 
00124 #endif // ! MLN_FUN_V2V_RGB_TO_HSI_HH

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