forked from twam/v4l2grab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yuv.c
172 lines (146 loc) · 5.45 KB
/
yuv.c
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/***************************************************************************
* Copyright (C) 2012 by Tobias Müller *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 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 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. *
***************************************************************************/
// use ITU-R float conversion for YUV420toRGB888 by default
#if !defined(ITU_R_FLOAT) && !defined(ITU_R_INT) && !defined(NTSC)
#define ITU_R_FLOAT
#endif
#if ((defined(ITU_R_FLOAT)) && (defined(ITU_R_INT)) && (defined(NTSC))) || ((defined(ITU_R_FLOAT)) && (defined(ITU_R_INT))) || ((defined(ITU_R_FLOAT)) && (defined(NTSC))) || ((defined(ITU_R_INT)) && (defined(NTSC)))
#error Only one conversion for YUV420toRGB888 is allowed!
#endif
#define CLIP(x) ( (x)>=0xFF ? 0xFF : ( (x) <= 0x00 ? 0x00 : (x) ) )
/**
Convert from YUV format to RGB. Formulae are described on http://en.wikipedia.org/wiki/YUV
**/
static void YUYtoRGB(unsigned char* y, unsigned char* u, unsigned char* v, unsigned char* r, unsigned char* g, unsigned char* b)
{
#ifdef ITU_R_FLOAT
// ITU-R float
*r = CLIP((double)*y + 1.402*((double)*v-128.0));
*g = CLIP((double)*y - 0.344*((double)*u-128.0) - 0.714*((double)*v-128.0));
*b = CLIP((double)*y + 1.772*((double)*u-128.0));
#endif
#ifdef ITU_R_INT
// ITU-R integer
*r = CLIP( *y + (*v-128) + ((*v-128) >> 2) + ((*v-128) >> 3) + ((*v-128) >> 5) );
*g = CLIP( *y - (((*u-128) >> 2) + ((*u-128) >> 4) + ((*u-128) >> 5)) - (((*v-128) >> 1) + ((*v-128) >> 3) + ((*v-128) >> 4) + ((*v-128) >> 5)) ); // 52 58
*b = CLIP( *y + (*u-128) + ((*u-128) >> 1) + ((*u-128) >> 2) + ((*u-128) >> 6) );
#endif
#ifdef NTSC
// NTSC integer
*r = CLIP( (298*(*y-16) + 409*(*v-128) + 128) >> 8 );
*g = CLIP( (298*(*y-16) - 100*(*u-128) - 208*(*v-128) + 128) >> 8 );
*b = CLIP( (298*(*y-16) + 516*(*u-128) + 128) >> 8 );
#endif
}
/**
Convert from YUV422 format to RGB888.
\param width width of image
\param height height of image
\param src source
\param dst destination
*/
void YUV422toRGB888(int width, int height, unsigned char *src, unsigned char *dst)
{
int line, column;
unsigned char *py, *pu, *pv;
unsigned char r, g, b;
unsigned char *tmp = dst;
// This format is planar. The three components are three seperate sub-images
// First the Y image with full resolution, than Cb and Cr with half resolution
py = src;
pu = src + 1;
pv = src + 3;
for (line = 0; line < height; ++line) {
for (column = 0; column < width; ++column) {
// convert YUV to RGB
YUYtoRGB(py, pu, pv, &r, &g, &b);
*tmp++ = r;
*tmp++ = g;
*tmp++ = b;
// increase py every time
py += 2;
// increase pu,pv every second time
if ((column & 1)==1) {
pu += 4;
pv += 4;
}
}
}
}
/**
Convert from YUV420 format to RGB888.
\param width width of image
\param height height of image
\param src source
\param dst destination
*/
void YUV420toRGB888(int width, int height, unsigned char *src, unsigned char *dst)
{
int line, column;
unsigned char *py, *pu, *pv;
unsigned char r, g, b;
unsigned char *tmp = dst;
// In this format each four bytes is two pixels. Each four bytes is two Y's, a Cb and a Cr.
// Each Y goes to one of the pixels, and the Cb and Cr belong to both pixels.
unsigned char *base_py = src;
unsigned char *base_pu = src+(height*width);
unsigned char *base_pv = src+(height*width)+(height*width)/4;
for (line = 0; line < height; ++line) {
for (column = 0; column < width; ++column) {
py = base_py+(line*width)+column;
pu = base_pu+(line/2*width/2)+column/2;
pv = base_pv+(line/2*width/2)+column/2;
// convert YUV to RGB
YUYtoRGB(py, pu, pv, &r, &g, &b);
*tmp++ = r;
*tmp++ = g;
*tmp++ = b;
}
}
}
/**
Convert from YUV420 format to YUV444.
\param width width of image
\param height height of image
\param src source
\param dst destination
*/
void YUV422toYUV444(int width, int height, unsigned char* src, unsigned char* dst) {
int line, column;
unsigned char *py, *pu, *pv;
unsigned char *tmp = dst;
py = src;
pu = src + 1;
pv = src + 3;
for (line = 0; line<height; line++) {
for (column = 0; column<width; column++) {
*tmp++ = *py;
*tmp++ = *pu;
*tmp++ = *pv;
// increase py every time
py += 2;
// increase pu,pv every second time
if ((column & 1)==1) {
pu += 4;
pv += 4;
}
}
}
}