26 #ifndef MLN_VALUE_GRAYLEVEL_HH
27 # define MLN_VALUE_GRAYLEVEL_HH
35 # include <mln/value/ops.hh>
37 # include <mln/core/contract.hh>
38 # include <mln/metal/math/pow.hh>
39 # include <mln/metal/math/max.hh>
40 # include <mln/metal/bexpr.hh>
41 # include <mln/literal/ops.hh>
43 # include <mln/value/graylevel_f.hh>
44 # include <mln/value/int_u.hh>
45 # include <mln/trait/value_.hh>
65 template <
unsigned n>
class gray_;
67 template <
unsigned n_src,
unsigned n_dest>
70 template <
unsigned n>
struct graylevel;
80 template <
unsigned n,
unsigned m >
83 typedef mln::value::internal::gray_< mlc_max_int(n, m) > ret;
86 template <
unsigned n,
unsigned m >
89 typedef mln::value::internal::gray_< mlc_max_int(m, n) > ret;
92 template <
unsigned n,
unsigned m >
95 typedef mln::value::internal::gray_<mlc_max_int(m, n)> ret;
98 template <
unsigned n,
unsigned m >
101 typedef mln::value::internal::gray_f ret;
104 template <
unsigned n,
typename I >
105 struct set_binary_< op::
times,
109 typedef mln::value::internal::gray_<n> ret;
112 template <
typename I,
unsigned n >
113 struct set_binary_< op::
times,
117 typedef mln::value::internal::gray_<n> ret;
121 template <
unsigned n,
typename F >
122 struct set_binary_< op::
times,
124 mln::value::Floating, F >
126 typedef mln::value::internal::gray_f ret;
129 template <
typename F,
unsigned n >
130 struct set_binary_< op::
times,
131 mln::value::Floating, F,
134 typedef mln::value::internal::gray_f ret;
138 template <
unsigned n,
typename S >
141 typedef mln_value_equiv(S) E;
142 typedef mln::metal::or_< mlc_equal(E,
float), mlc_equal(E,
double) > is_float;
143 typedef mlc_if(is_float, mln::value::internal::gray_f, mln::value::internal::gray_<n>) ret;
146 template <
unsigned n, typename S >
147 struct set_precise_binary_< op::div, mln::value::graylevel<n>, mln::value::scalar_<S> >
149 typedef mln::value::internal::gray_f ret;
154 template <
unsigned n>
155 struct value_< mln::value::graylevel<n> >
164 card = mln_value_card_from_(n)
167 typedef trait::value::nature::integer nature;
168 typedef trait::value::kind::gray kind;
169 typedef mln_value_quant_from_(card) quant;
171 static const self_ min() {
return 0; }
172 static const self_
max() {
return card - 1; }
173 static const self_ epsilon() {
return 0; }
256 template <
unsigned n>
259 public Integer< graylevel<n> >,
261 public internal::value_like_< int_u<n>,
280 template <
unsigned m>
283 template <
unsigned m>
289 graylevel(
const mln::literal::medium_gray_t&);
297 graylevel<n>& operator=(
const mln::literal::medium_gray_t&);
303 unsigned value()
const;
306 float to_float()
const;
314 template <
typename T>
struct convert_;
317 template <
unsigned n>
328 template <
unsigned n>
329 std::ostream& operator<<(std::ostream& ostr, const graylevel<n>& g);
332 template <
unsigned n,
unsigned m>
333 mln_trait_op_plus(graylevel<n>, graylevel<m>)
334 operator+(const graylevel<n>& lhs, const graylevel<m>& rhs);
337 template <
unsigned n, typename I>
339 operator+(const graylevel<n>& lhs, const I& i);
342 template <
unsigned n, typename I>
344 operator+(const I& i, const graylevel<n>& rhs);
347 template <
unsigned n,
unsigned m>
348 mln_trait_op_minus(graylevel<n>, graylevel<m>)
349 operator-(const graylevel<n>& lhs, const graylevel<m>& rhs);
352 template <
unsigned n, typename I>
354 operator-(const graylevel<n>& lhs, const I& i);
357 template <
unsigned n, typename I>
359 operator-(const I& i, const graylevel<n>& rhs);
362 template <
unsigned n,
unsigned m>
363 mln_trait_op_times(graylevel<n>, graylevel<m>)
364 operator*(const graylevel<n>& lhs, const graylevel<m>& rhs);
369 template <
unsigned n, typename T>
370 mln_trait_op_times(graylevel<n>, T)
371 operator*(const graylevel<n>& lhs, const T& rhs);
374 template <
unsigned n, typename T>
375 mln_trait_op_times(graylevel<n>, T)
376 operator*(const T& lhs, const graylevel<n>& rhs);
379 template <
unsigned n, typename T>
382 operator/(const graylevel<n>& lhs, const T& rhs);
387 template <
unsigned n, typename I>
388 mln_trait_op_times(graylevel<n>, I)
389 operator*(const graylevel<n>& lhs, const Integer<I>& rhs);
392 template <typename I,
unsigned n>
393 mln_trait_op_times(I, graylevel<n>)
394 operator*(const Integer<I>& lhs, const graylevel<n>& rhs);
397 template <
unsigned n, typename I>
398 mln_trait_op_div(graylevel<n>, I)
399 operator/(const graylevel<n>& lhs, const Integer<I>& rhs);
402 template <typename I,
unsigned n>
403 mln_trait_op_div(I, graylevel<n>)
404 operator/(const Integer<I>& lhs, const graylevel<n>& rhs);
409 template <
unsigned n, typename F>
410 mln_trait_op_times(graylevel<n>, F)
411 operator*(const graylevel<n>& lhs, const Floating<F>& rhs);
414 template <typename F,
unsigned n>
415 mln_trait_op_times(F, graylevel<n>)
416 operator*(const Floating<F>& lhs, const graylevel<n>& rhs);
420 template <
unsigned n, typename F>
421 mln_trait_op_div(graylevel<n>, F)
422 operator/(const graylevel<n>& lhs, const Floating<F>& rhs);
425 template <typename F,
unsigned n>
426 mln_trait_op_div(F, graylevel<n>)
427 operator/(const Floating<F>& lhs, const graylevel<n>& rhs);
429 # ifndef MLN_INCLUDE_ONLY
434 template <
unsigned n>
441 template <
unsigned n>
445 mln_precondition(val >= 0);
446 mln_precondition(
unsigned(val) <= mln_max(mln_enc(
int_u<n>)));
450 template <
unsigned n>
455 mln_precondition(val >= 0);
456 mln_precondition(
unsigned(val) <= mln_max(mln_enc(
int_u<n>)));
461 template <
unsigned n>
469 template <
unsigned n>
478 template <
unsigned n>
479 template <
unsigned m>
483 this->v_ = internal::convert<m, n>(rhs.
value());
486 template <
unsigned n>
487 template <
unsigned m>
492 this->v_ = internal::convert<m, n>(rhs.
value());
497 template <
unsigned n>
504 template <
unsigned n>
513 template <
unsigned n>
517 this->v_ = metal::math::pow_int<2, n - 1>::value;
520 template <
unsigned n>
525 this->v_ = metal::math::pow_int<2, n - 1>::value;
530 template <
unsigned n>
534 this->v_ = mln_max(mln_enc(int_u<n>));
537 template <
unsigned n>
542 this->v_ = mln_max(mln_enc(int_u<n>));
546 template <
unsigned n>
554 template <
unsigned n>
559 static const float denom = float(metal::math::pow_int<2, n>::value) - 1.f;
560 return float(this->v_) / denom;
568 template <
unsigned n>
571 convert_< graylevel<n> >::value_at_index(
unsigned i)
577 template <
unsigned n>
580 convert_< graylevel<n> >::index_of_value(
graylevel<n> v)
589 template <
unsigned n>
591 std::ostream& operator<<(std::ostream& ostr, const graylevel<n>& g)
593 return ostr << g.value() <<
"/gl" << n;
598 # endif // ! MLN_INCLUDE_ONLY
605 #include <mln/value/internal/gray_f.hh>
606 #include <mln/value/internal/gray_.hh>
608 #endif // ! MLN_VALUE_GRAYLEVEL_HH