
https://svn.lrde.epita.fr/svn/oln/trunk Index: ChangeLog from Thierry Geraud <thierry.geraud@lrde.epita.fr> New sample code for Caroline (hsi and conversion). * milena/sandbox/vigouroux/color/my_hsi.hh: . * milena/sandbox/vigouroux/color/is_HSI.cc: New. * milena/sandbox/vigouroux/color/rgb_to_hsi.hh (fun::v2v::f_rgb_to_hsi_): New. is_HSI.cc | 20 ++++++++++++ my_hsi.hh | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++- rgb_to_hsi.hh | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 180 insertions(+), 13 deletions(-) Index: milena/sandbox/vigouroux/color/my_hsi.hh --- milena/sandbox/vigouroux/color/my_hsi.hh (revision 1756) +++ milena/sandbox/vigouroux/color/my_hsi.hh (working copy) @@ -4,13 +4,85 @@ #include <mln/value/int_u.hh> #include <mln/metal/vec.hh> +#include <mln/value/float01_8.hh> + #ifndef MLN_VALUE_HSI_HH # define MLN_VALUE_HSI_HH + namespace mln { + namespace value { + + template <typename E> + struct HSI + { + }; + + template <typename F> + class hsi_ : public HSI< hsi_<F> > + { + public: + + typedef F h_type; + typedef F s_type; + typedef F i_type; + + /// Constructor without argument. + hsi_() + { + } + + /// Constructor from component values. + hsi_(const F& h, const F& s, const F& i) + : h_(h), + s_(s), + i_(i) + { + } + + /// Read-only access to the h component. + const F& h() const + { + return this->h_; + } + const F& s() const + { + return this->s_; + } + const F& i() const + { + return this->i_; + } + + /// Read-write access to the h component. + F& h() + { + return this->h_; + } + F& s() + { + return this->s_; + } + F& i() + { + return this->i_; + } + + private: + F h_; + F s_; + F i_; + }; + + typedef hsi_<float01_8> hsi_3x8; + + typedef hsi_<double> hsi_d; + + + template <unsigned n> struct hsi { @@ -47,6 +119,9 @@ double i_; }; + +# ifndef MLN_INCLUDE_ONLY + template <unsigned n> inline hsi<n>::hsi() @@ -65,7 +140,11 @@ this->s_ = s; this->i_ = i; } - } -} + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::value + +} // end of namespace mln #endif // ! MLN_VALUE_HSI_HH Index: milena/sandbox/vigouroux/color/is_HSI.cc --- milena/sandbox/vigouroux/color/is_HSI.cc (revision 0) +++ milena/sandbox/vigouroux/color/is_HSI.cc (revision 0) @@ -0,0 +1,20 @@ +#include <iostream> + +#include <mln/metal/is_a.hh> +#include <mln/value/rgb8.hh> +#include "my_hsi.hh" +#include "rgb_to_hsi.hh" + + +int main() +{ + using namespace mln; + using namespace mln::value; + + typedef hsi_d C; + std::cout << mlc_is_a(C, HSI)::value << std::endl; + std::cout << mlc_is_a(rgb8, HSI)::value << std::endl; + + rgb8 c(255, 10, 1); + hsi_3x8 c_hsi = fun::v2v::f_rgb_to_hsi_3x8(c); +} Index: milena/sandbox/vigouroux/color/rgb_to_hsi.hh --- milena/sandbox/vigouroux/color/rgb_to_hsi.hh (revision 1756) +++ milena/sandbox/vigouroux/color/rgb_to_hsi.hh (working copy) @@ -1,3 +1,5 @@ +#include <cmath> + #include <mln/core/image_if_value.hh> #include <mln/core/inplace.hh> #include <mln/core/w_window2d_int.hh> @@ -8,12 +10,75 @@ #include "my_hsi.hh" -namespace mln { - namespace convert { + + +namespace mln +{ + + + namespace fun + { + + namespace v2v + { + + // NEW + + template <typename T_hsi> + struct f_rgb_to_hsi_ : public Function_v2v< f_rgb_to_hsi_<T_hsi> > + { + typedef T_hsi result; + + template <typename T_rgb> + T_hsi operator()(const T_rgb& rgb) const + { + // Locals. + static const double sqrt3_3 = std::sqrt(3) / 3; + static const double inv_sqrt6 = 1 / std::sqrt(6); + static const double inv_sqrt2 = 1 / std::sqrt(2); + + T_hsi hsi; + + double alpha = inv_sqrt2 * rgb.green() - inv_sqrt2 * rgb.blue(); + double beta = 2 * inv_sqrt6 * rgb.red() - inv_sqrt6 * rgb.green() - inv_sqrt6 * rgb.blue(); + + + float tmp = atan2(beta, alpha) / 3.1415 * 180.0; + if (tmp < 0 or tmp > 1) + { + std::cout << "FIXME: " << tmp << std::endl; + } + + hsi.h() = atan2(beta, alpha) / 3.1415 * 180.0; + if (hsi.h() < 0) + hsi.h() = hsi.h() + 360.0; + mln_invariant(hsi.h() >= 0); + hsi.s() = std::sqrt(alpha * alpha + beta * beta); + hsi.i() = sqrt3_3 * rgb.red() + sqrt3_3 * rgb.green() + sqrt3_3 * rgb.blue(); + + return hsi; + } + }; + + typedef f_rgb_to_hsi_<value::hsi_3x8> f_rgb_to_hsi_3x8_t; + + f_rgb_to_hsi_3x8_t f_rgb_to_hsi_3x8; + + // end of NEW + + } // end of namespace fun::v2v + + } // end of namespace fun + + + + + namespace convert + { + struct f_rgb_to_hsi { - struct value::hsi<8> - doit(const struct value::rgb<8> rgb) const + value::hsi<8> operator()(const value::rgb<8>& rgb) const { struct value::hsi<8> hsi; double sqrt3_3 = sqrt(3) / 3; @@ -30,14 +95,15 @@ hsi.s(sqrt(alpha * alpha + beta * beta)); hsi.i(sqrt3_3 * rgb.red() + sqrt3_3 * rgb.green() + sqrt3_3 * rgb.blue()); - return (hsi); + return hsi; } }; + struct f_hsi_to_rgb { - struct value::rgb<8> - doit(const struct value::hsi<8> hsi) const + + value::rgb<8> doit(const value::hsi<8>& hsi) const { int r; int g; @@ -55,10 +121,12 @@ g = int(sqrt3_3 * hsi.i() + inv_sqrt2 * alpha - inv_sqrt6 * beta); b = int(sqrt3_3 * hsi.i() - inv_sqrt2 * alpha - inv_sqrt6 * beta); - struct value::rgb<8> rgb(r, g, b); + value::rgb<8> rgb(r, g, b); - return (rgb); + return rgb; } }; - } -} + + } // end of FIXME + +} // end of FIXME