summaryrefslogtreecommitdiff
path: root/2003/i/buzz/src/vision/rgbyuv.h
diff options
context:
space:
mode:
Diffstat (limited to '2003/i/buzz/src/vision/rgbyuv.h')
-rw-r--r--2003/i/buzz/src/vision/rgbyuv.h57
1 files changed, 57 insertions, 0 deletions
diff --git a/2003/i/buzz/src/vision/rgbyuv.h b/2003/i/buzz/src/vision/rgbyuv.h
new file mode 100644
index 0000000..104c309
--- /dev/null
+++ b/2003/i/buzz/src/vision/rgbyuv.h
@@ -0,0 +1,57 @@
+#ifndef rgbyuv_h
+#define rgbyuv_h
+// rgbyuv.h - Convertions RGB <-> YUV
+// buzz - Programme du robot Efrei Robotique I1-I2 2003
+// Copyright (C) 2003 Nicolas Schodet
+
+// TODO: Implementer une version plus rapide avec des tableaux ???
+// Pas pour le momment, on utilise pas YUV.
+
+class RgbYuv
+{
+ public:
+ // Convertie un pixel RGB en YUV.
+ static void rgbToYuv (unsigned char r, unsigned char g, unsigned char b,
+ unsigned char &y, unsigned char &u, unsigned char
+ &v);
+ // Convertie un pixel YUV en RGB.
+ static void yuvToRgb (unsigned char y, unsigned char u, unsigned char v,
+ unsigned char &r, unsigned char &g, unsigned char
+ &b);
+};
+
+// Convertie un pixel RGB en YUV.
+inline void RgbYuv::rgbToYuv (unsigned char r, unsigned char g, unsigned char
+ b, unsigned char &y, unsigned char &u, unsigned
+ char &v)
+{
+ y = (( 66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
+ u = (( -38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
+ v = (( 112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
+}
+
+// Convertie un pixel YUV en RGB.
+inline void RgbYuv::yuvToRgb (unsigned char y, unsigned char u, unsigned char
+ v, unsigned char &r, unsigned char &g, unsigned
+ char &b)
+{
+ int c, d, e;
+ int _r, _g, _b;
+ c = (int) (y & 0xff) - 16;
+ d = (int) (u & 0xff) - 128;
+ e = (int) (v & 0xff) - 128;
+ _r = (298 * c + 409 * e + 128) >> 8;
+ _g = (298 * c - 100 * d - 208 * e + 128) >> 8;
+ _b = (298 * c + 516 * d + 128) >> 8;
+ if (_r > 255) _r = 255;
+ if (_g > 255) _g = 255;
+ if (_b > 255) _b = 255;
+ if (_r < 0) _r = 0;
+ if (_g < 0) _g = 0;
+ if (_b < 0) _b = 0;
+ r = _r;
+ g = _g;
+ b = _b;
+}
+
+#endif // rgbyuv_h