summaryrefslogtreecommitdiff
path: root/v4l2-apps/lib/libv4l/libv4lconvert/rgbyuv.c
blob: 79c8ecb351b6ef2593f5a9f1405da99086772e7f (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*

# RGB <-> YUV conversion routines

#             (C) 2008 Hans de Goede <j.w.r.degoede@hhs.nl>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

#define RGB2YUV(r,g,b,y,u,v) \
    (y) = ((  8453*(r) + 16594*(g) +  3223*(b) +  524288) >> 15); \
    (u) = (( -4878*(r) -  9578*(g) + 14456*(b) + 4210688) >> 15); \
    (v) = (( 14456*(r) - 12105*(g) -  2351*(b) + 4210688) >> 15)

#define YUV2R(y, u, v) ({ \
  int r = (y) + ((((v)-128)*1436) >> 10); r > 255 ? 255 : r < 0 ? 0 : r; })
#define YUV2G(y, u, v) ({ \
  int g = (y) - ((((u)-128)*352 + ((v)-128)*731) >> 10); g > 255 ? 255 : g < 0 ? 0 : g; })
#define YUV2B(y, u, v) ({ \
  int b = (y) + ((((u)-128)*1814) >> 10); b > 255 ? 255 : b < 0 ? 0 : b; })

#define CLIP(color) (unsigned char)(((color)>0xFF)?0xff:(((color)<0)?0:(color)))

void v4lconvert_yuv420_to_bgr24(const unsigned char *src, unsigned char *dest,
  int width, int height)
{
  int i,j;

  const unsigned char *ysrc = src;
  const unsigned char *usrc = src + width * height;
  const unsigned char *vsrc = usrc + (width * height) / 4;

  for (i = 0; i < height; i++) {
    for (j = 0; j < width; j += 2) {
#if 1 /* fast slightly less accurate multiplication free code */
      int u1 = (((*usrc - 128) << 7) +  (*usrc - 128)) >> 6;
      int rg = (((*usrc - 128) << 1) +  (*usrc - 128) +
		((*vsrc - 128) << 2) + ((*vsrc - 128) << 1)) >> 3;
      int v1 = (((*vsrc - 128) << 1) +  (*vsrc - 128)) >> 1;

      *dest++ = CLIP(*ysrc + u1);
      *dest++ = CLIP(*ysrc - rg);
      *dest++ = CLIP(*ysrc + v1);
      ysrc++;

      *dest++ = CLIP(*ysrc + u1);
      *dest++ = CLIP(*ysrc - rg);
      *dest++ = CLIP(*ysrc + v1);
#else
      *dest++ = YUV2B(*ysrc, *usrc, *vsrc);
      *dest++ = YUV2G(*ysrc, *usrc, *vsrc);
      *dest++ = YUV2R(*ysrc, *usrc, *vsrc);
      ysrc++;

      *dest++ = YUV2B(*ysrc, *usrc, *vsrc);
      *dest++ = YUV2G(*ysrc, *usrc, *vsrc);
      *dest++ = YUV2R(*ysrc, *usrc, *vsrc);
#endif
      ysrc++;
      usrc++;
      vsrc++;
    }
    /* Rewind u and v for next line */
    if (i&1) {
      usrc -= width / 2;
      vsrc -= width / 2;
    }
  }
}