$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
hsl.hh
1 // Copyright (C) 2008, 2009, 2012, 2013 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_VALUE_HSL_HH
28 # define MLN_VALUE_HSL_HH
29 
30 #include <mln/value/ops.hh>
31 
32 #include <mln/value/concept/vectorial.hh>
33 #include <mln/algebra/vec.hh>
34 
35 
36 namespace mln
37 {
38 
39  // Forward declarations.
40  namespace value {
41  namespace qt {
42  struct rgb32;
43  }
44  template <unsigned n> struct int_u;
45  template <typename H, typename S, typename L> class hsl_;
46  }
47 
48 } // end of namespace mln
49 
50 
51 namespace mln
52 {
53 
54  namespace trait
55  {
56 
57  template <typename H, typename S, typename L>
58  struct set_precise_binary_< op::plus, mln::value::hsl_<H,S,L>, mln::value::hsl_<H,S,L> >
59  {
61  };
62 
63  template <typename H, typename S, typename L>
64  struct set_precise_binary_< op::minus, mln::value::hsl_<H,S,L>, mln::value::hsl_<H,S,L> >
65  {
67  };
68 
69  template <typename H, typename S, typename L, typename S2>
70  struct set_precise_binary_< op::times, mln::value::hsl_<H,S,L>, mln::value::scalar_<S2> >
71  {
73  };
74 
75  template <typename H, typename S, typename L, typename S2>
76  struct set_precise_binary_< op::div, mln::value::hsl_<H,S,L>, mln::value::scalar_<S2> >
77  {
79  };
80 
81 
82  // FIXME : Is there any way more generic? a way to factor
83  // set_precise_binary_< op::div, mln::value::hsl_<H,S,L>, mln::value::scalar_<S> >
84  // and
85  // set_precise_binary_< op::div, mln::value::hsl_<H,S,L>, mln::value::int_u<m> >
86  // as for op::times.
87 
88  template <typename H, typename S, typename L, unsigned m>
89  struct set_precise_binary_< op::times, mln::value::hsl_<H,S,L>, mln::value::int_u<m> >
90  {
92  };
93 
94  template <typename H, typename S, typename L, unsigned m>
95  struct set_precise_binary_< op::div, mln::value::hsl_<H,S,L>, mln::value::int_u<m> >
96  {
98  };
99 
100  template <typename H, typename S, typename L>
101  struct value_< mln::value::hsl_<H,S,L> >
102  {
103  enum {
104  dim = 3,
105  nbits = (sizeof (H) + sizeof (S) + sizeof (L)) * 8,
106  card = mln_value_card_from_(nbits)
107  };
108 
109  typedef trait::value::nature::vectorial nature;
110  typedef trait::value::kind::color kind;
111  typedef mln_value_quant_from_(card) quant;
112 
113  typedef void comp;
114  typedef H comp_0;
115  typedef S comp_1;
116  typedef L comp_2;
117 
118  template <typename V> static comp_0 get_comp_0(const V& v) { return v.hue(); }
119  template <typename V> static comp_1 get_comp_1(const V& v) { return v.sat(); }
120  template <typename V> static comp_2 get_comp_2(const V& v) { return v.lum(); }
121 
122 // typedef algebra::vec<3, float> sum;
124  };
125 
126  } // end of namespace trait
127 
128 
129  namespace value
130  {
131 
132  template <typename E>
133  struct HSL : Object<E>
134  {
135  };
136 
137  template <typename H, typename S, typename L>
138  class hsl_ : public HSL< hsl_<H,S,L> >
139  {
140  public:
141 
142  typedef H h_type;
143  typedef S s_type;
144  typedef L l_type;
145 
148  {
149  }
150 
152  : hue_(0),
153  sat_(0),
154  lum_(0)
155  {
156  }
157 
159  hsl_(const H& hue, const S& sat, const L& lum)
160  : hue_(hue),
161  sat_(sat),
162  lum_(lum)
163  {
164  }
165 
167  const H& hue() const;
168  const S& sat() const;
169  const L& lum() const;
170 
172  H& hue();
173  S& sat();
174  L& lum();
175 
176  private:
177  //FIXME: Don't we want to store these values in a vector?
178  H hue_;
179  S sat_;
180  L lum_;
181  };
182 
183 
184  // FIXME: Use float01_8/float01_16 ?
193 
196 
205 
208 
209 
216  template <typename H, typename S, typename L>
217  std::ostream& operator<<(std::ostream& ostr, const hsl_<H,S,L>& c);
218 
219 
222  template <typename H, typename S, typename L>
224  operator+(const hsl_<H,S,L>& lhs, const hsl_<H,S,L>& rhs);
226 
229  template <typename H, typename S, typename L>
231  operator-(const hsl_<H,S,L>& lhs, const hsl_<H,S,L>& rhs);
233 
236  template <typename H, typename S, typename L, typename S2>
238  operator*(const hsl_<H,S,L>& lhs, const mln::value::scalar_<S2>& s);
240 
243  template <typename H, typename S, typename L, typename S2>
245  operator/(const hsl_<H,S,L>& lhs, const mln::value::scalar_<S2>& s);
247 
250  template <typename H, typename S, typename L>
251  bool
252  operator==(const hsl_<H,S,L>& lhs, const hsl_<H,S,L>& rhs);
254 
255 
257  // Conversions
258 
260  template <typename H, typename S, typename L>
261  void from_to_(const hsl_<H,S,L>& from, qt::rgb32& to);
262 
264  template <typename H, typename S, typename L>
265  void from_to_(const value::hsl_<H,S,L>&, value::rgb<8>& to);
267 
268  } // end of namespace mln::value
269 
270 
271 
272  // More forward declarations
273  namespace fun
274  {
275  namespace v2v
276  {
277 
278  template <typename T_rgb>
279  struct f_hsl_to_rgb_;
280 
281  typedef f_hsl_to_rgb_< value::rgb<8> > f_hsl_to_rgb_3x8_t;
282 // typedef f_hsl_to_rgb_< value::rgb<16> > f_hsl_to_rgb_3x16_t;
283 
285 // extern f_hsl_to_rgb_3x16_t f_hsl_to_rgb_3x16;
286 
287  }
288 
289  }
290 
291 } // end of namespace mln
292 
293 
294 // Required by mln::values::hsl_'s from_to_ routines.
295 # include <mln/value/rgb.hh>
296 # include <mln/value/qt/rgb32.hh>
297 # include <mln/fun/v2v/hsl_to_rgb.hh>
298 
299 
300 # ifndef MLN_INCLUDE_ONLY
301 
302 namespace mln
303 {
304 
305  namespace value
306  {
307 
308  template <typename H, typename S, typename L>
309  const H&
310  hsl_<H,S,L>::hue() const
311  {
312  return this->hue_;
313  }
314 
315  template <typename H, typename S, typename L>
316  const S&
317  hsl_<H,S,L>::sat() const
318  {
319  return this->sat_;
320  }
321 
322  template <typename H, typename S, typename L>
323  const L&
324  hsl_<H,S,L>::lum() const
325  {
326  return this->lum_;
327  }
328 
329  template <typename H, typename S, typename L>
330  H&
332  {
333  return this->hue_;
334  }
335 
336  template <typename H, typename S, typename L>
337  S&
339  {
340  return this->sat_;
341  }
342 
343  template <typename H, typename S, typename L>
344  L&
346  {
347  return this->lum_;
348  }
349 
350 
351  template <typename H, typename S, typename L>
352  inline
353  std::ostream& operator<<(std::ostream& ostr, const hsl_<H,S,L>& v)
354  {
355  return ostr << '(' << debug::format(v.hue())
356  << ',' << debug::format(v.sat())
357  << ',' << debug::format(v.lum())
358  << ')';
359  }
360 
361 
362  template <typename H, typename S, typename L>
363  hsl_<H,S,L>
364  operator+(const hsl_<H,S,L>& lhs, const hsl_<H,S,L>& rhs)
365  {
366  return hsl_<H,S,L>(lhs.hue() + rhs.hue(),
367  lhs.sat() + rhs.sat(),
368  lhs.lum() + rhs.lum());
369  }
370 
371 
372  template <typename H, typename S, typename L>
373  hsl_<H,S,L>
374  operator-(const hsl_<H,S,L>& lhs, const hsl_<H,S,L>& rhs)
375  {
376  return hsl_<H,S,L>(lhs.hue() - rhs.hue(),
377  lhs.sat() - rhs.sat(),
378  lhs.lum() - rhs.lum());
379  }
380 
381 
382  template <typename H, typename S, typename L, typename S2>
383  hsl_<H,S,L>
384  operator*(const hsl_<H,S,L>& lhs, const mln::value::scalar_<S2>& s)
385  {
386  return hsl_<H,S,L>(lhs.hue() * s,
387  lhs.sat() * s,
388  lhs.lum() * s);
389  }
390 
391 
392  template <typename H, typename S, typename L, typename S2>
393  hsl_<H,S,L>
394  operator/(const hsl_<H,S,L>& lhs, const mln::value::scalar_<S2>& s)
395  {
396  return hsl_<H,S,L>(lhs.hue() / s,
397  lhs.sat() / s,
398  lhs.lum() / s);
399  }
400 
401  template <typename H, typename S, typename L>
402  bool
403  operator==(const hsl_<H,S,L>& lhs, const hsl_<H,S,L>& rhs)
404  {
405  return lhs.hue() == rhs.hue()
406  && lhs.sat() == rhs.sat()
407  && lhs.lum() == rhs.lum();
408  }
409 
410 
411  // Conversions
412 
413  template <typename H, typename S, typename L>
414  void from_to_(const hsl_<H,S,L>& from, qt::rgb32& to)
415  {
416  rgb<8> v = fun::v2v::f_hsl_to_rgb_3x8(from);
417  to = v.to_equiv();
418  }
419 
420  template <typename H, typename S, typename L>
421  void
422  from_to_(const hsl_<H,S,L>& from, rgb<8>& to)
423  {
424  to = fun::v2v::f_hsl_to_rgb_3x8(from);
425  }
426 
427  } // end of namespace mln::value
428 
429 } // end of namespace mln
430 
431 # endif // ! MLN_INCLUDE_ONLY
432 
433 #endif // ! MLN_VALUE_HSL_HH