-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathyuv.c
87 lines (72 loc) · 1.52 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
/*
* EffecTV - Realtime Digital Video Effector
* Copyright (C) 2001-2006 FUKUCHI Kentaro
*
* yuv.c: YUV(YCbCr) color system utilities
*
*/
#include "EffecTV.h"
#include "utils.h"
/*
* conversion from YUV to RGB
* r = 1.164*(y-16) + 1.596*(v-128);
* g = 1.164*(y-16) - 0.813*(v-128) - 0.391*(u-128);
* b = 1.164*(y-16) + 2.018*(u-128);
* conversion from RGB to YUV
* y = 0.257*r + 0.504*g + 0.098*b + 16
* u = -0.148*r - 0.291*g + 0.439*b + 128
* v = 0.439*r - 0.368*g - 0.071*b + 128
*/
int YtoRGB[256];
int VtoR[256], VtoG[256];
int UtoG[256], UtoB[256];
int RtoY[256], RtoU[256], RtoV[256];
int GtoY[256], GtoU[256], GtoV[256];
int BtoY[256], BtoV[256];
int yuv_init(void)
{
int i;
for(i=0; i<256; i++) {
YtoRGB[i] = 1.164*(i-16);
VtoR[i] = 1.596*(i-128);
VtoG[i] = -0.813*(i-128);
UtoG[i] = -0.391*(i-128);
UtoB[i] = 2.018*(i-128);
RtoY[i] = 0.257*i;
RtoU[i] = -0.148*i;
RtoV[i] = 0.439*i;
GtoY[i] = 0.504*i;
GtoU[i] = -0.291*i;
GtoV[i] = -0.368*i;
BtoY[i] = 0.098*i;
BtoV[i] = -0.071*i;
}
return 0;
}
unsigned char yuv_RGBtoY(int rgb)
{
int i;
i = RtoY[(rgb>>16)&0xff]
+ GtoY[(rgb>>8)&0xff]
+ BtoY[rgb&0xff]
+ 16;
return i;
}
unsigned char yuv_RGBtoU(int rgb)
{
int i;
i = RtoU[(rgb>>16)&0xff]
+ GtoU[(rgb>>8)&0xff]
+ RtoV[rgb&0xff] /* BtoU == RtoV */
+ 128;
return i;
}
unsigned char yuv_RGBtoV(int rgb)
{
int i;
i = RtoV[(rgb>>16)&0xff]
+ GtoV[(rgb>>8)&0xff]
+ BtoV[rgb&0xff]
+ 128;
return i;
}