URL:
https://svn.lrde.epita.fr/svn/oln/trunk/milena
ChangeLog:
2008-02-19 Caroline Vigouroux <vigour_c(a)epita.fr>
Corrections.
* rgb_to_hsi.hh: Correction.
* rgb_to_hsl.hh: New.
* rgb_to_hsv.hh: New.
* rgb_to_xyz.hh: New.
* rgb_to_yiq.hh: New.
---
rgb_to_hsi.hh | 40 ++++++++++++++++++++++++-----------
rgb_to_hsl.hh | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
rgb_to_hsv.hh | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++
rgb_to_xyz.hh | 53 +++++++++++++++++++++++++++++++++++++++++++++++
rgb_to_yiq.hh | 48 ++++++++++++++++++++++++++++++++++++++++++
5 files changed, 254 insertions(+), 12 deletions(-)
Index: trunk/milena/sandbox/vigouroux/color/rgb_to_hsl.hh
===================================================================
--- trunk/milena/sandbox/vigouroux/color/rgb_to_hsl.hh (revision 0)
+++ trunk/milena/sandbox/vigouroux/color/rgb_to_hsl.hh (revision 1738)
@@ -0,0 +1,65 @@
+#ifndef OLENA_CONVERT_NRGBHSL_HH
+# define OLENA_CONVERT_NRGBHSL_HH
+
+# include <mln/core/image_if_value.hh>
+# include <mln/core/inplace.hh>
+# include <mln/core/w_window2d_int.hh>
+# include <mln/display/show.hh>
+# include <mln/io/ppm/save.hh>
+# include <mln/display/save_and_show.hh>
+# include <mln/level/fill.hh>
+
+# include "my_hsl.hh"
+
+namespace mln {
+ namespace convert {
+ struct f_rgb_to_hsl
+ {
+ struct value::hsl<8>
+ doit(const struct value::rgb<8> rgb) const
+ {
+ struct value::hsl<8> hsl;
+
+ double max_in = std::max(rgb.red(), std::max(rgb.blue(), rgb.green()));
+ double min_in = std::min(rgb.red(), std::min(rgb.blue(), rgb.green()));
+ double diff = max_in - min_in;
+
+ hsl.l((max_in + min_in) / 2);
+ hsl.h();
+ hsl.s();
+ hsl.l();
+
+ return (hsl);
+ }
+ };
+
+ struct f_hsl_to_rgb
+ {
+ struct value::rgb<8>
+ doit(const struct value::hsl<8> hsl) const
+ {
+ struct value::rgb<8> rgb;
+ int r;
+ int g;
+ int b;
+
+ double sqrt3_3 = sqrt(3) / 3;
+ double inv_sqrt6 = 1 / sqrt(6);
+ double inv_sqrt2 = 1 / sqrt(2);
+
+ 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();
+
+ r = int(sqrt3_3 * hsl.i() + 2 * inv_sqrt6 * beta);
+ g = int(sqrt3_3 * hsl.i() + inv_sqrt2 * alpha - inv_sqrt6 * beta);
+ b = int(sqrt3_3 * hsl.i() - inv_sqrt2 * alpha - inv_sqrt6 * beta);
+
+ struct value::rgb<8> rgb(r, g, b);
+
+ return (rgb);
+ }
+ };
+ }
+}
+
+#endif // OLENA_CONVERT_RGBHSL_HH
Index: trunk/milena/sandbox/vigouroux/color/rgb_to_xyz.hh
===================================================================
--- trunk/milena/sandbox/vigouroux/color/rgb_to_xyz.hh (revision 0)
+++ trunk/milena/sandbox/vigouroux/color/rgb_to_xyz.hh (revision 1738)
@@ -0,0 +1,53 @@
+#ifndef OLENA_CONVERT_RGBXYZ_HH
+# define OLENA_CONVERT_RGBXYZ_HH
+
+# include <mln/core/image_if_value.hh>
+# include <mln/core/inplace.hh>
+# include <mln/core/w_window2d_int.hh>
+# include <mln/display/show.hh>
+# include <mln/io/ppm/save.hh>
+# include <mln/display/save_and_show.hh>
+# include <mln/level/fill.hh>
+
+# include "my_xyz.hh"
+
+namespace mln {
+ namespace convert {
+ struct f_rgb_to_xyz
+ {
+ struct value::xyz<8>
+ doit(const struct value::rgb<8> rgb) const
+ {
+ struct value::xyz<8> xyz;
+
+ xyz.x(0.490 * rgb.red() + 0.310 * rgb.green() + 0.200 * rgb.blue());
+ xyz.y(0.177 * rgb.red() + 0.812 * rgb.green() + 0.011 * rgb.blue());
+ xyz.z(0.010 * rgb.green() + 0.990 * rgb.blue());
+
+ return (xyz);
+_ }
+ };
+
+ struct f_xyz_to_rgb
+ {
+ struct value::rgb<8>
+ doit(const struct value::xyz<8> xyz) const
+ {
+ struct value::rgb<8> rgb;
+ int r;
+ int g;
+ int b;
+
+ r = int(2.365 * xyz.x() - 0.896 * xyz.y() - 0.468 * xyz.z());
+ g = int(-0.515 * xyz.x() + 1.425 * xyz.y() + 0.089 * xyz.z());
+ b = int(0.005 * xyz.x() - 0.014 * xyz.y() + 1.01 * xyz.z());
+
+ struct value::rgb<8> rgb(r, g, b);
+
+ return (rgb);
+ }
+ };
+ }
+}
+
+#endif // OLENA_CONVERT_RGBXYZ_HH
Index: trunk/milena/sandbox/vigouroux/color/rgb_to_hsv.hh
===================================================================
--- trunk/milena/sandbox/vigouroux/color/rgb_to_hsv.hh (revision 0)
+++ trunk/milena/sandbox/vigouroux/color/rgb_to_hsv.hh (revision 1738)
@@ -0,0 +1,60 @@
+#ifndef OLENA_CONVERT_NRGBHSV_HH
+# define OLENA_CONVERT_NRGBHSV_HH
+
+# include <mln/core/image_if_value.hh>
+# include <mln/core/inplace.hh>
+# include <mln/core/w_window2d_int.hh>
+# include <mln/display/show.hh>
+# include <mln/io/ppm/save.hh>
+# include <mln/display/save_and_show.hh>
+# include <mln/level/fill.hh>
+
+# include "my_hsv.hh"
+
+namespace mln {
+ namespace convert {
+ struct f_rgb_to_hsv
+ {
+ struct value::hsv<8>
+ doit(const struct value::rgb<8> rgb) const
+ {
+ struct value::hsv<8> hsv;
+
+ hsv.h();
+ hsv.s();
+ hsv.v();
+
+ return (hsv);
+ }
+ };
+
+ struct f_hsv_to_rgb
+ {
+ struct value::rgb<8>
+ doit(const struct value::hsv<8> hsv) const
+ {
+ struct value::rgb<8> rgb;
+ int r;
+ int g;
+ int b;
+
+ double sqrt3_3 = sqrt(3) / 3;
+ double inv_sqrt6 = 1 / sqrt(6);
+ double inv_sqrt2 = 1 / sqrt(2);
+
+ 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();
+
+ r = int(sqrt3_3 * hsv.i() + 2 * inv_sqrt6 * beta);
+ g = int(sqrt3_3 * hsv.i() + inv_sqrt2 * alpha - inv_sqrt6 * beta);
+ b = int(sqrt3_3 * hsv.i() - inv_sqrt2 * alpha - inv_sqrt6 * beta);
+
+ struct value::rgb<8> rgb(r, g, b);
+
+ return (rgb);
+ }
+ };
+ }
+}
+
+#endif // OLENA_CONVERT_RGBHSV_HH
Index: trunk/milena/sandbox/vigouroux/color/rgb_to_yiq.hh
===================================================================
--- trunk/milena/sandbox/vigouroux/color/rgb_to_yiq.hh (revision 0)
+++ trunk/milena/sandbox/vigouroux/color/rgb_to_yiq.hh (revision 1738)
@@ -0,0 +1,48 @@
+#include <mln/core/image_if_value.hh>
+#include <mln/core/inplace.hh>
+#include <mln/core/w_window2d_int.hh>
+#include <mln/display/show.hh>
+#include <mln/io/ppm/save.hh>
+#include <mln/display/save_and_show.hh>
+#include <mln/level/fill.hh>
+#include <mln/value/int_u.hh>
+
+#include "my_yiq.hh"
+
+namespace mln {
+ namespace convert {
+ struct f_rgb_to_yiq
+ {
+ struct mln::value::yiq<8>
+ doit(const struct value::rgb<8> rgb) const
+ {
+ struct value::yiq<8> yiq;
+
+ yiq.y(0.1768 * rgb.red() + 0.8130 * rgb.green() + 0.0101 * rgb.blue());
+ yiq.i(0.5346 * rgb.red() - 0.2461 * rgb.green() - 0.1791 * rgb.blue());
+ yiq.q(0.2474 * rgb.red() - 0.6783 * rgb.green() + 0.4053 * rgb.blue());
+
+ return (yiq);
+ }
+ };
+
+ struct f_yiq_to_rgb
+ {
+ struct value::rgb<8>
+ doit(const struct value::yiq<8> yiq) const
+ {
+ int r;
+ int g;
+ int b;
+
+ r = int(0.87 * yiq.y() + 1.3223 * yiq.i() + 0.5628 * yiq.q());
+ g = int(1.026 * yiq.y() - 0.2718 * yiq.i() - 0.1458 * yiq.q());
+ b = int(1.186 * yiq.y() - 1.2620 * yiq.i() + 1.8795 * yiq.q());
+
+ struct value::rgb<8> rgb(r, g, b);
+
+ return (rgb);
+ }
+ };
+ }
+}
Index: trunk/milena/sandbox/vigouroux/color/rgb_to_hsi.hh
===================================================================
--- trunk/milena/sandbox/vigouroux/color/rgb_to_hsi.hh (revision 1737)
+++ trunk/milena/sandbox/vigouroux/color/rgb_to_hsi.hh (revision 1738)
@@ -16,15 +16,19 @@
doit(const struct value::rgb<8> rgb) const
{
struct value::hsi<8> hsi;
- double alpha;
- double beta;
-
- alpha = rgb.red() - 1 / 2 * (rgb.green() + rgb.blue());
- beta = sqrt(3) / 2 * (rgb.green() - rgb.blue());
-
- hsi.h(atan2(beta / alpha));
+ double sqrt3_3 = sqrt(3) / 3;
+ double inv_sqrt6 = 1 / sqrt(6);
+ double inv_sqrt2 = 1 / sqrt(2);
+
+ 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();
+
+ hsi.h(atan2(beta, alpha) / 3.1415 * 180.0);
+ if (hsi.h() < 0)
+ hsi.h(hsi.h() + 360.0);
+ mln_precondition(hsi.h() >= 0);
hsi.s(sqrt(alpha * alpha + beta * beta));
- hsi.i((rgb.red() + rgb.green() + rgb.blue()) / 3);
+ hsi.i(sqrt3_3 * rgb.red() + sqrt3_3 * rgb.green() + sqrt3_3 * rgb.blue());
return (hsi);
}
@@ -35,11 +39,23 @@
struct value::rgb<8>
doit(const struct value::hsi<8> hsi) const
{
- struct value::rgb<8> rgb;
+ int r;
+ int g;
+ int b;
+
+ double sqrt3_3 = sqrt(3) / 3;
+ double inv_sqrt6 = 1 / sqrt(6);
+ double inv_sqrt2 = 1 / sqrt(2);
+
+ double h = hsi.h() / 180.0 * 3.1415;
+ double alpha = hsi.s() * cos(h);
+ double beta = hsi.s() * sin(h);
+
+ r = int(sqrt3_3 * hsi.i() + 2 * inv_sqrt6 * beta);
+ g = int(sqrt3_3 * hsi.i() + inv_sqrt2 * alpha - inv_sqrt6 * beta);
+ b = int(sqrt3_3 * hsi.i() - inv_sqrt2 * alpha - inv_sqrt6 * beta);
- rgb.red() = 0; // not yet implemented
- rgb.green() = 0; // not yet implemented
- rgb.blue() = 0; // not yet implemented
+ struct value::rgb<8> rgb(r, g, b);
return (rgb);
}