Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
graylevel_f.hh
1 // Copyright (C) 2006, 2007, 2008, 2009, 2011 EPITA Research and
2 // Development 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_GRAYLEVEL_F_HH
28 # define MLN_VALUE_GRAYLEVEL_F_HH
29 
33 
34 # include <iostream>
35 
36 # include <mln/value/ops.hh>
37 
38 # include <mln/core/contract.hh>
39 # include <mln/metal/math/pow.hh>
40 # include <mln/metal/bexpr.hh>
41 # include <mln/literal/ops.hh>
42 
43 # include <mln/value/float01_f.hh>
44 # include <mln/trait/value_.hh>
45 
46 
47 namespace mln
48 {
49 
50  namespace literal
51  {
53  struct black_t;
54  struct medium_gray_t;
55  struct white_t;
57  }
58  namespace value
59  {
61  namespace internal { struct gray_f; }
62  struct graylevel_f;
63  template <unsigned n> struct graylevel;
64  struct float01_f;
66  }
67 
68 
69 
70  namespace trait
71  {
72 
73  template <>
74  struct set_precise_binary_< op::plus, mln::value::graylevel_f, mln::value::graylevel_f >
75  {
76  typedef mln::value::internal::gray_f ret;
77  };
78 
79  template <unsigned n>
80  struct set_precise_binary_< op::plus, mln::value::graylevel_f, mln::value::graylevel<n> >
81  {
82  typedef mln::value::internal::gray_f ret;
83  };
84  template <unsigned n>
85  struct set_precise_binary_< op::plus, mln::value::graylevel<n>, mln::value::graylevel_f >
86  {
87  typedef mln::value::internal::gray_f ret;
88  };
89 
90  template <>
91  struct set_precise_binary_< op::minus, mln::value::graylevel_f, mln::value::graylevel_f >
92  {
93  typedef mln::value::internal::gray_f ret;
94  };
95 
96  template <>
97  struct set_precise_binary_< op::times, mln::value::graylevel_f, mln::value::graylevel_f >
98  {
99  typedef mln::value::internal::gray_f ret;
100  };
101 
102  template <>
103  struct set_precise_binary_< op::div, mln::value::graylevel_f, mln::value::graylevel_f >
104  {
105  typedef mln::value::internal::gray_f ret;
106  };
107 
108  template < typename I >
109  struct set_binary_< op::times,
112  {
113  typedef mln::value::internal::gray_f ret;
114  };
115 
116  template < typename I >
117  struct set_binary_< op::times,
120  {
121  typedef mln::value::internal::gray_f ret;
122  };
123 
124 
125  template < typename F >
126  struct set_binary_< op::times,
128  mln::value::Floating, F >
129  {
130  typedef mln::value::internal::gray_f ret;
131  };
132 
133  template < typename F >
134  struct set_binary_< op::times,
135  mln::value::Floating, F,
137  {
138  typedef mln::value::internal::gray_f ret;
139  };
140 
141 
142  template < typename S >
143  struct set_precise_binary_< op::times, mln::value::graylevel_f, mln::value::scalar_<S> >
144  {
145  typedef mln::value::internal::gray_f ret;
146  };
147 
148  template < typename S >
149  struct set_precise_binary_< op::div, mln::value::graylevel_f, mln::value::scalar_<S> >
150  {
151  typedef mln::value::internal::gray_f ret;
152  };
153 
154 
156  template <typename T> struct value_;
157 
158  // 'graylevel_f' as a value.
159  template <>
160  struct value_<mln::value::graylevel_f>
161  {
162  private:
163  typedef mln::value::graylevel_f self_;
164  typedef mln::value::float01_f equiv_;
165 
166  public:
167 
168  enum {
169  dim = 1,
170  nbits = mln_nbits(equiv_),
171  card = 0
172  };
173 
174  typedef trait::value::nature::floating nature;
175  typedef trait::value::kind::gray kind;
176  typedef mln_trait_value_quant_(equiv_) quant;
177 
178  static const equiv_ min() { return 0; }
179  static const equiv_ max() { return 1; }
180  static const equiv_ epsilon() { return mln_epsilon(equiv_); }
181 
182  typedef float comp;
183 
184  typedef float sum;
185  };
186 
187  } // end of namespace mln::trait
188 
189 
190  namespace value
191  {
192 
194  struct graylevel_f
195  :
196  public Floating< graylevel_f >,
197 
198  public internal::value_like_< float01_f, // Equivalent.
199  mln_enc_(float01_f),// Encoding.
200  internal::gray_f, // Interoperation.
201  graylevel_f > // Exact.
202  {
204  graylevel_f();
206  graylevel_f(const graylevel_f& rhs);
208  graylevel_f& operator=(const graylevel_f& rhs);
209 
210 
212  graylevel_f(float val);
214  graylevel_f& operator=(float val);
215 
217  template <unsigned n>
218  graylevel_f(const graylevel<n>& rhs);
220  template <unsigned n>
221  graylevel_f& operator=(const graylevel<n>& rhs);
222 
226  graylevel_f(const mln::literal::medium_gray_t&);
229 
232  graylevel_f& operator=(const mln::literal::black_t&);
233  graylevel_f& operator=(const mln::literal::medium_gray_t&);
234  graylevel_f& operator=(const mln::literal::white_t&);
236 
238  template <unsigned n>
239  operator graylevel<n>() const;
240 
242  float value() const;
243  };
244 
245 
246  // Operators.
247 
249  std::ostream& operator<<(std::ostream& ostr, const graylevel_f& g);
250 
251  // graylevel_f + graylevel_f.
252  mln_trait_op_plus_(graylevel_f, graylevel_f)
253  operator+(const graylevel_f& lhs, const graylevel_f& rhs);
254 
255 
256  // graylevel_f + graylevel<n>.
257  template <unsigned n>
258  mln_trait_op_plus(graylevel_f, graylevel<n>)
259  operator+(const graylevel_f& lhs, const graylevel<n>& rhs);
260  // graylevel<n> + graylevel_f.
261  template <unsigned n>
262  mln_trait_op_plus(graylevel_f, graylevel<n>)
263  operator+(const graylevel<n>& lhs, const graylevel_f& rhs);
264 
265  // graylevel_f - graylevel_f.
266  mln_trait_op_minus_(graylevel_f, graylevel_f)
267  operator-(const graylevel_f& lhs, const graylevel_f& rhs);
268 
269  // graylevel_f * graylevel_f.
270  mln_trait_op_times_(graylevel_f, graylevel_f)
271  operator*(const graylevel_f& lhs, const graylevel_f& rhs);
272 
273 
274  // With Builtins.
275 
276  // graylevel_f * T.
277  template <unsigned n, typename T>
278  mln_trait_op_times(graylevel_f, T)
279  operator*(const graylevel_f& lhs, const T& rhs);
280 
281  // T * graylevel_f.
282  template <unsigned n, typename T>
283  mln_trait_op_times(graylevel_f, T)
284  operator*(const T& lhs, const graylevel_f& rhs);
285 
286  // graylevel_f / T.
287  template <unsigned n, typename T>
288  internal::gray_f
289  operator/(const graylevel_f& lhs, const T& rhs);
290 
291  // With Integer.
292 
293  // graylevel_f * Integer<I>.
294  template <typename I>
295  mln_trait_op_times(graylevel_f, I)
296  operator*(const graylevel_f& lhs, const Integer<I>& rhs);
297 
298  // Integer<I> * graylevel_f.
299  template <typename I>
300  mln_trait_op_times(I, graylevel_f)
301  operator*(const Integer<I>& lhs, const graylevel_f& rhs);
302 
303  // graylevel_f / Integer<I>.
304  template <typename I>
305  mln_trait_op_div(graylevel_f, I)
306  operator/(const graylevel_f& lhs, const Integer<I>& rhs);
307 
308  // Integer<I> / graylevel_f.
309  template <typename I>
310  mln_trait_op_div(I, graylevel_f)
311  operator/(const Integer<I>& lhs, const graylevel_f& rhs);
312 
313  // With Floating.
314 
315  // graylevel_f * Floating<F>.
316  template <typename F>
317  mln_trait_op_times(graylevel_f, F)
318  operator*(const graylevel_f& lhs, const Floating<F>& rhs);
319 
320  // Floating<F>, graylevel_f.
321  template <typename F>
322  mln_trait_op_times(F, graylevel_f)
323  operator*(const Floating<F>& lhs, const graylevel_f& rhs);
324 
325 
326  // graylevel_f / Floating<F>.
327  template <typename F>
328  mln_trait_op_div(graylevel_f, F)
329  operator/(const graylevel_f& lhs, const Floating<F>& rhs);
330 
331  // Floating<F> / graylevel_f.
332  template <typename F>
333  mln_trait_op_div(F, graylevel_f)
334  operator/(const Floating<F>& lhs, const graylevel_f& rhs);
335 
336 
337 # ifndef MLN_INCLUDE_ONLY
338 
339  // graylevel_f.
340 
341  inline
343  {
344  }
345 
346 
347  inline
349  {
350  mln_precondition(val >= 0);
351  mln_precondition(val <= 1);
352  this->v_ = val;
353  }
354 
355  inline
356  graylevel_f&
358  {
359  mln_precondition(val >= 0);
360  mln_precondition(val <= 1);
361  this->v_ = val;
362  return *this;
363  }
364 
365  template <unsigned n>
367  {
368  mln_precondition(rhs.to_float() >= 0);
369  mln_precondition(rhs.to_float() <= 1);
370  this->v_ = rhs.to_float();
371  }
372 
373  template <unsigned n>
374  graylevel_f&
376  {
377  mln_precondition(rhs.to_float() >= 0);
378  mln_precondition(rhs.to_float() <= 1);
379  this->v_ = rhs.to_float();
380  return *this;
381  }
382 
383  inline
384  graylevel_f::graylevel_f(const graylevel_f& rhs)
385  : Floating<graylevel_f>()
386  {
387  this->v_ = rhs.v_;
388  }
389 
390  inline
391  graylevel_f&
392  graylevel_f::operator=(const graylevel_f& rhs)
393  {
394  mln_precondition(rhs.v_ >= 0);
395  mln_precondition(rhs.v_ <= 1);
396  this->v_ = rhs.v_;
397  return *this;
398  }
399 
400  inline
402  {
403  this->v_ = 0.0f;
404  }
405 
406  inline
407  graylevel_f&
409  {
410  this->v_ = 0.0f;
411  return *this;
412  }
413 
414  inline
415  graylevel_f::graylevel_f(const mln::literal::medium_gray_t&)
416  {
417  this->v_ = 0.5f;
418  }
419 
420  inline
421  graylevel_f&
422  graylevel_f::operator=(const mln::literal::medium_gray_t&)
423  {
424  this->v_ = 0.5f;
425  return *this;
426  }
427 
428  inline
430  {
431  this->v_ = 1.0f;
432  }
433 
434  inline
435  graylevel_f&
437  {
438  this->v_ = 1.0f;
439  return *this;
440  }
441 
442  template <unsigned n>
443  inline
444  graylevel_f::operator graylevel<n>() const
445  {
446  return graylevel<n>(internal::gray_f(*this));
447  }
448 
449  inline
450  float
452  {
453  return this->v_;
454  }
455 
456  // Operators.
457 
458  inline
459  std::ostream& operator<<(std::ostream& ostr, const graylevel_f& g)
460  {
461  return ostr << g.value() << "/gl_f"; // FIXME: Be more explicit!.
462  }
463 
464 # endif // ! MLN_INCLUDE_ONLY
465 
466  } // end of namespace mln::value
467 
468 } // end of namespace mln
469 
470 
471 //# include <mln/value/internal/gray_f.hh>
473 
474 #endif // ! MLN_VALUE_GRAYLEVEL_F_HH