-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathyuv2rgb.pas
170 lines (144 loc) · 3.95 KB
/
yuv2rgb.pas
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
{
YUYV to BGRA conversion routines
Copyright (C) 2013 Yuriy Pilgun
This program is provided under the terms of the MIT License.
}
unit YUV2RGB;
{$mode objfpc}{$H+}
interface
{
Conversion coefficients kRV, kGU, kGV, kBU are guessed without deep investigation.
Values of YUV components assumed to extend to full range 0..255 / -128..127.
If you want faster conversion, see:
Etienne Dupuis. "Optimizing YUV-RGB Color Space Conversion Using Intel's SIMD Technology," August 2003.
Or adapt MMX code from FFmpeg/libswscale library.
}
procedure YUYV_to_BGRA(src: PLongWord; dest: PLongWord; size: integer);
procedure YUYV_to_BGRA_Slow(src: PLongWord; dest: PLongWord; size: integer);
procedure YUYV_to_Gray(src: PLongWord; dest: PLongWord; size: integer);
implementation
const
Alpha=$FF000000;
const
kRV=1.402;
kGU=-0.34414;
kGV=-0.71414;
kBU=1.772;
var
coeffRV: array [0..255] of SmallInt;
coeffGU: array [0..255] of SmallInt;
coeffGV: array [0..255] of SmallInt;
coeffBU: array [0..255] of SmallInt;
const
offsetC = 320;
var
coeffR: array[0..1023] of LongWord;
coeffG: array[0..1023] of LongWord;
coeffB: array[0..1023] of LongWord;
const
rv=round(kRV*65536);
gu=round(kGU*65536);
gv=round(kGV*65536);
bu=round(kBU*65536);
procedure InitializeTables;
var
i:integer;
begin
for i:=0 to 255 do begin
coeffRV[i]:=integer(rv)*(i-128) shr 16;
coeffGU[i]:=integer(gu)*(i-128) shr 16;
coeffGV[i]:=integer(gv)*(i-128) shr 16;
coeffBU[i]:=integer(bu)*(i-128) shr 16;
end;
for i:=0 to 1023 do begin
if i<offsetC then begin
coeffR[i]:=0 or Alpha;
coeffG[i]:=0 or Alpha;
coeffB[i]:=0 or Alpha;
end else
if i>offsetC+255 then begin
coeffR[i]:=$FF0000 or Alpha;
coeffG[i]:=$00FF00 or Alpha;
coeffB[i]:=$0000FF or Alpha;
end else begin
coeffR[i]:=(i-offsetC) shl 16 or Integer(Alpha);
coeffG[i]:=(i-offsetC) shl 8 or Integer(Alpha);
coeffB[i]:=(i-offsetC) or Integer(Alpha);
end;
end;
end;
procedure YUYV_to_BGRA(src: PLongWord; dest: PLongWord; size: integer);
var
i:integer;
a:integer;
Y1,U,Y2,V: integer;
r0,g0,b0: integer;
begin
for i := 0 to (size div 2) - 1 do begin
a:=src^;
Y1:=(a) and $FF;
U:=(a shr 8) and $FF;
Y2:=(a shr 16) and $FF;
V:=(a shr 24) and $FF;
r0:=coeffRV[V];
g0:=coeffGU[U]+coeffGV[V];
b0:=coeffBU[U];
dest^:=coeffR[Y1+r0+offsetC] or coeffG[Y1+g0+offsetC] or coeffB[Y1+b0+offsetC];
Inc(dest);
dest^:=coeffR[Y2+r0+offsetC] or coeffG[Y2+g0+offsetC] or coeffB[Y2+b0+offsetC];
Inc(dest);
Inc(src);
end;
end;
procedure YUYV_to_BGRA_Slow(src: PLongWord; dest: PLongWord; size: integer);
var
i:integer;
a:integer;
Y1,U,Y2,V: integer;
r0,g0,b0: integer;
R,G,B: integer;
begin
for i := 0 to (size div 2) - 1 do begin
a:=src^;
Y1:=(a) and $FF;
U:=(a shr 8) and $FF - 128;
Y2:=(a shr 16) and $FF;
V:=(a shr 24) and $FF - 128;
r0:=(rv*V shr 16);
g0:=(gu*U shr 16)+(gv*V shr 16);
b0:=(bu*U shr 16);
R:=Y1+r0;
G:=Y1+g0;
B:=Y1+b0;
if R<0 then R:=0 else if R>255 then R:=255;
if G<0 then G:=0 else if G>255 then G:=255;
if B<0 then B:=0 else if B>255 then B:=255;
dest^:=(R shl 16) or (G shl 8) or (B) or Integer(Alpha);
Inc(dest);
R:=Y2+r0;
G:=Y2+g0;
B:=Y2+b0;
if R<0 then R:=0 else if R>255 then R:=255;
if G<0 then G:=0 else if G>255 then G:=255;
if B<0 then B:=0 else if B>255 then B:=255;
dest^:=(R shl 16) or (G shl 8) or (B) or Integer(Alpha);
Inc(dest);
Inc(src);
end;
end;
procedure YUYV_to_Gray(src: PLongWord; dest: PLongWord; size: integer);
var i, b: integer;
begin
for i := 0 to (size div 2) - 1 do begin
b := src^ and $FF;
dest^ := (b shl 16) or (b shl 8) or (b) or Integer(Alpha);
Inc(dest);
b := (src^ shr 16) and $FF;
dest^ := (b shl 16) or (b shl 8) or (b) or Integer(Alpha);
Inc(dest);
Inc(src);
end;
end;
initialization
InitializeTables;
end.