27 #ifndef MLN_VALUE_INTERNAL_GRAY__HH
28 # define MLN_VALUE_INTERNAL_GRAY__HH
37 # include <mln/metal/math/max.hh>
38 # include <mln/metal/math/pow.hh>
40 # include <mln/value/concept/integer.hh>
41 # include <mln/value/internal/value_like.hh>
43 # include <mln/value/graylevel_f.hh>
61 template <
unsigned N>
struct graylevel;
65 template <
unsigned n>
class gray_;
75 template <
template <
class,
class>
class Name,
unsigned n,
unsigned m>
81 template <
unsigned n,
unsigned m>
87 template <
unsigned n,
unsigned m>
96 struct value_< mln::value::internal::gray_<n> >
107 typedef trait::value::nature::integer
nature;
108 typedef trait::value::kind::gray
kind;
109 typedef trait::value::quant::high
quant;
113 static self_ epsilon();
130 template <
unsigned n>
161 template <
unsigned m>
163 template <
unsigned m>
171 template <
unsigned m>
175 template <
unsigned m>
186 template <
unsigned n>
187 std::ostream& operator<<(std::ostream& ostr, const gray_<n>& g);
189 template <
unsigned n,
unsigned m>
191 template <
unsigned n,
unsigned m>
192 bool operator<(const gray_<n>& lhs,
const gray_<m>&
rhs);
194 template <
unsigned n,
unsigned m>
198 template <
unsigned n,
unsigned m>
200 operator-(const
gray_<n>& lhs, const
gray_<m>& rhs);
202 template <
unsigned n>
203 gray_<n> operator*(
int s, const
gray_<n>& rhs);
204 template <
unsigned n>
205 gray_<n> operator*(const
gray_<n>& lhs,
int s);
206 template <
unsigned n>
207 gray_<n> operator/(const
gray_<n>& lhs,
int s);
216 # include <mln/value/internal/gray_f.hh>
222 # ifndef MLN_INCLUDE_ONLY
229 template <
unsigned n_src,
unsigned n_dest>
233 using mln::metal::int_;
234 typedef mlc_max(int_<n_dest - n_src> , int_<n_src - n_dest>) m;
240 return val * mlc_pow_int(2, m::
value);
242 return val / mlc_pow_int(2, m::value);
247 template <
unsigned n>
253 template <
unsigned n>
254 template <
unsigned m>
257 this->v_ = convert<m, n>(g.
value());
260 template <
unsigned n>
261 template <
unsigned m>
266 this->v_ = convert<m, n>(g.
value());
270 template <
unsigned n>
272 gray_<n>::gray_(
const literal::white_t&)
274 this->v_ = mlc_pow_int(2, n) - 1;
277 template <
unsigned n>
279 gray_<n>::gray_(
const literal::black_t&)
284 template <
unsigned n>
286 gray_<n>::gray_(
int val)
291 template <
unsigned n>
299 template <
unsigned n>
300 template <
unsigned m>
302 gray_<n>::operator gray_<m>()
const
304 return gray_<m>(convert<n, m>(this->v_));
308 template <
unsigned n>
309 template <
unsigned m>
311 gray_<n>::operator graylevel<m>()
const
313 graylevel<m> tmp(convert<n, m>(this->v_));
314 mln_assertion(tmp.value() < std::pow(2.f,
int(m)));
319 template <
unsigned n>
321 gray_<n>::operator graylevel_f()
const
324 return graylevel_f(
float(this->v_) / denom);
329 template <
unsigned n>
331 std::ostream& operator<<(std::ostream& ostr, const gray_<n>& g)
333 return ostr << g.
value() <<
"g/" << n <<
"bits";
336 template <
unsigned n,
unsigned m>
338 bool operator==(
const gray_<n>& lhs,
const gray_<m>& rhs)
341 return internal::gray_<mlc_max_int(n, m)>(lhs).
value() ==
342 internal::gray_<mlc_max_int(n, m)>(
rhs).
value();
346 gray_<n>
up(down.value() +
348 return (lhs.value() >= down.value() && lhs.value() <
up.value());
353 gray_<m>
up(down.value() +
355 return (rhs.value() >= down.value() && rhs.value() <
up.value());
359 template <
unsigned n,
unsigned m>
361 bool operator<(const gray_<n>& lhs,
const gray_<m>&
rhs)
364 return lhs.value() < rhs.value();
366 return lhs.value() < gray_<n>(
rhs).
value();
368 return gray_<m>(lhs).
value() < rhs.value();
371 template <
unsigned n,
unsigned m>
373 mln_trait_op_plus(gray_<n>, gray_<m>)
374 operator+(const gray_<n>& lhs, const gray_<m>& rhs)
376 typedef mln_trait_op_plus(gray_<n>, gray_<m>)
ret;
389 template <
unsigned n,
unsigned m>
391 mln_trait_op_minus(gray_<n>, gray_<m>)
392 operator-(const gray_<n>& lhs, const gray_<m>& rhs)
394 typedef mln_trait_op_minus(gray_<n>, gray_<m>) ret;
404 mln_assertion(lhs.value() >= l.value());
405 ret tmp(lhs.value() - l.value());
411 mln_assertion(l.value() >= rhs.value());
412 ret tmp(l.value() - rhs.value());
417 template <
unsigned n,
unsigned m>
419 mln_trait_op_times(gray_<n>, gray_<m>)
420 operator*(const gray_<n>& lhs, const gray_<m>& rhs)
422 typedef mln_trait_op_times(gray_<n>, gray_<m>) ret;
424 return ret(ret(lhs).value() * ret(rhs).value());
427 template <
unsigned n>
429 gray_<n> operator*(
int s, const gray_<n>& rhs)
431 gray_<n> tmp(rhs.value() * s);
435 template <
unsigned n>
437 gray_<n>
operator*(
const gray_<n>& lhs,
int s)
439 gray_<n> tmp(lhs.value() * s);
443 template <
unsigned n>
445 gray_<n>
operator/(
const gray_<n>& lhs,
int s)
447 mln_precondition(s != 0);
448 gray_<n> tmp(lhs.value() / s);
458 template <
typename I,
unsigned n>
461 operator==(
const Integer<I>& lhs,
const graylevel<n>& rhs)
463 return rhs.value() ==
exact(lhs);
468 template <
unsigned n,
unsigned m>
471 operator==(
const graylevel<n>& lhs,
const graylevel<m>& rhs)
473 return internal::gray_<n>(
rhs) == internal::gray_<m>(lhs);
478 template <
unsigned n,
unsigned m>
480 mln_trait_op_plus(graylevel<n>, graylevel<m>)
481 operator+(const graylevel<n>& lhs, const graylevel<m>& rhs)
483 return internal::gray_<n>(lhs) + internal::gray_<m>(rhs);
487 template <
unsigned n,
typename I>
490 operator+(
const graylevel<n>& lhs,
const I& i)
494 typename graylevel<n>::wrong_use_of_graylevel___Please_use_the__to_enc__method a;
499 template <
unsigned n,
typename I>
502 operator+(
const I& i,
const graylevel<n>& rhs)
506 typename graylevel<n>::wrong_use_of_graylevel___Please_use_the__to_enc__method a;
510 template <
unsigned n,
unsigned m>
512 mln_trait_op_minus(graylevel<n>, graylevel<m>)
513 operator-(const graylevel<n>& lhs, const graylevel<m>& rhs)
515 return internal::gray_<n>(lhs) - internal::gray_<m>(rhs);
519 template <
unsigned n,
typename I>
522 operator-(
const graylevel<n>& lhs,
const I& i)
526 typename graylevel<n>::wrong_use_of_graylevel___Please_use_the__to_enc__method a;
531 template <
unsigned n,
typename I>
534 operator-(
const I& i,
const graylevel<n>& rhs)
538 typename graylevel<n>::wrong_use_of_graylevel___Please_use_the__to_enc__method a;
542 template <
unsigned n,
unsigned m>
544 mln_trait_op_times(graylevel<n>, graylevel<m>)
545 operator*(const graylevel<n>& lhs, const graylevel<m>& rhs)
547 return internal::gray_<n>(lhs) * internal::gray_<m>(rhs);
551 template <
unsigned n,
typename I>
554 operator*(
const graylevel<n>& lhs,
const Integer<I>& rhs)
556 return internal::gray_<n>(lhs) *
int(
exact(rhs));
559 template <
typename I,
unsigned n>
561 mln_trait_op_times(I, graylevel<n>)
562 operator*(const Integer<I>& lhs, const graylevel<n>& rhs)
564 return internal::gray_<n>(
rhs) *
int(
exact(lhs));
568 template <
unsigned n,
typename F>
570 mln_trait_op_times(graylevel<n>, F)
571 operator*(const graylevel<n>& lhs, const Floating<F>& rhs)
576 template <
typename F,
unsigned n>
578 mln_trait_op_times(F, graylevel<n>)
579 operator*(const Floating<F>& lhs, const graylevel<n>& rhs)
585 template <
unsigned n,
typename T>
586 mln_trait_op_times(graylevel<n>,
T)
587 operator*(const graylevel<n>& lhs, const
T& rhs)
589 return lhs * scalar_<T>(
rhs);
592 template <
unsigned n,
typename T>
593 mln_trait_op_times(graylevel<n>,
T)
594 operator*(const
T& lhs, const graylevel<n>& rhs)
596 return rhs * scalar_<T>(lhs);
608 template <
unsigned n,
typename T>
610 operator/(
const graylevel<n>& lhs,
const T& rhs)
612 return lhs / scalar_<T>(
rhs);
620 template <
typename ret>
621 struct helper_gray__op_;
623 template <
unsigned n>
624 struct helper_gray__op_< gray_<n> >
626 template <
unsigned m,
typename S>
628 mln_trait_op_times(graylevel<m>, scalar_<S>)
629 times(const graylevel<m>& lhs, const scalar_<S>& rhs)
631 typedef mln_trait_op_times(graylevel<m>, scalar_<S>) ret;
632 ret tmp(typename ret::equiv
633 (lhs.value() * typename ret::equiv(rhs.to_equiv())));
637 template <
unsigned m, typename S>
639 mln_trait_op_times(graylevel<m>, scalar_<S>)
640 div(const graylevel<m>& lhs, const scalar_<S>& rhs)
642 typedef mln_trait_op_times(graylevel<m>, scalar_<S>) ret;
643 ret tmp(typename ret::equiv
644 (lhs.value() / typename ret::equiv(rhs.to_equiv())));
650 struct helper_gray__op_< gray_f >
652 template <
unsigned n,
typename S>
654 mln_trait_op_times(graylevel<n>, scalar_<S>)
655 times(const graylevel<n>& lhs, const scalar_<S>& rhs)
657 typedef mln_trait_op_times(graylevel<n>, scalar_<S>) ret;
658 ret tmp(lhs.to_float() * typename ret::equiv(rhs.to_equiv()));
661 template <
unsigned n, typename S>
663 mln_trait_op_div(graylevel<n>, scalar_<S>)
664 div(const graylevel<n>& lhs, const scalar_<S>& rhs)
666 typedef mln_trait_op_div(graylevel<n>, scalar_<S>) ret;
667 ret tmp(typename ret::equiv
668 (lhs.to_float() / typename ret::equiv(rhs.to_equiv())));
676 template <
unsigned n, typename S>
678 mln_trait_op_times(graylevel<n>, scalar_<S>)
679 operator*(const graylevel<n>& lhs, const scalar_<S>& rhs)
681 typedef mln_trait_op_times(graylevel<n>, scalar_<S>) ret;
682 return internal::helper_gray__op_<ret>::times(lhs, rhs);
686 template <
unsigned n, typename S>
688 mln_trait_op_div(graylevel<n>, scalar_<S>)
689 operator/(const graylevel<n>& lhs, const scalar_<S>& rhs)
691 mln_precondition(rhs.to_equiv() != S(0));
692 typedef mln_trait_op_div(graylevel<n>, scalar_<S>) ret;
693 return internal::helper_gray__op_<ret>::div(lhs, rhs);
705 template <
unsigned n>
708 value_< mln::value::internal::gray_<n> >
::min()
713 template <
unsigned n>
716 value_< mln::value::internal::gray_<n> >
::max()
721 template <
unsigned n>
724 value_< mln::value::internal::gray_<n> >::epsilon()
732 # endif // ! MLN_INCLUDE_ONLY
736 #endif // ! MLN_VALUE_INTERNAL_GRAY__HH