summaryrefslogtreecommitdiff
path: root/2003/i/buzz/src/vision/rgbyuv.h
blob: 104c309d3a618c7a54fe397553341efa248d51d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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