Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
rgb_to_hsi.hh
1 // Copyright (C) 2008, 2009, 2011 EPITA Research and Development
2 // Laboratory (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef MLN_FUN_V2V_RGB_TO_HSI_HH
28 # define MLN_FUN_V2V_RGB_TO_HSI_HH
29 
30 #include <cmath>
31 
32 #include <mln/value/rgb8.hh>
33 #include <mln/math/round.hh>
34 
35 #include <mln/value/hsi.hh>
36 
37 
38 namespace mln
39 {
40 
41 
42  namespace fun
43  {
44 
45  namespace v2v
46  {
47 
48  template <typename T_hsi>
49  struct f_rgb_to_hsi_ : public Function_v2v< f_rgb_to_hsi_<T_hsi> >
50  {
51  typedef T_hsi result;
52 
53  f_rgb_to_hsi_();
54 
55  template <typename T_rgb>
56  T_hsi operator()(const T_rgb& rgb) const;
57 
58  };
59 
60  typedef f_rgb_to_hsi_<value::hsi_f> f_rgb_to_hsi_f_t;
61 
62  extern f_rgb_to_hsi_f_t f_rgb_to_hsi_f;
63 
64 
65 # ifndef MLN_INCLUDE_ONLY
66 
67 # ifndef MLN_WO_GLOBAL_VARS
68 
71  f_rgb_to_hsi_f_t f_rgb_to_hsi_f;
73 
74 # endif // ! MLN_WO_GLOBAL_VARS
75 
76 
77  template <typename T_hsi>
78  f_rgb_to_hsi_<T_hsi>::f_rgb_to_hsi_()
79  {
80  }
81 
82  template <typename T_hsi>
83  template <typename T_rgb>
84  inline
85  T_hsi
86  f_rgb_to_hsi_<T_hsi>::operator()(const T_rgb& rgb) const
87  {
88  // Locals.
89  static const double sqrt3_3 = std::sqrt(3) / 3;
90  static const double inv_sqrt6 = 1 / std::sqrt(6);
91  static const double inv_sqrt2 = 1 / std::sqrt(2);
92 
93  T_hsi hsi;
94 
95  double alpha = inv_sqrt2 * rgb.green() - inv_sqrt2 * rgb.blue();
96  double beta =
97  2 * inv_sqrt6 * rgb.red() -
98  inv_sqrt6 * rgb.green() -
99  inv_sqrt6 * rgb.blue();
100 
101 
102  hsi.hue() = atan2(beta, alpha) / 3.1415 * 180.0;
103  if (hsi.hue() < 0)
104  hsi.hue() = hsi.hue() + 360.0;
105  mln_invariant(hsi.hue() >= 0);
106  hsi.sat() = std::sqrt(alpha * alpha + beta * beta);
107  hsi.inty() =
108  sqrt3_3 * rgb.red() +
109  sqrt3_3 * rgb.green() +
110  sqrt3_3 * rgb.blue();
111 
112  return hsi;
113  }
114 
115 
116 # endif // !MLN_INCLUDE_ONLY
117 
118  } // end of namespace fun::v2v
119 
120  } // end of namespace fun
121 
122 } // end of namespace mln
123 
124 #endif // ! MLN_FUN_V2V_RGB_TO_HSI_HH