178 |
kaklik |
1 |
#include "colourspace.h" |
|
|
2 |
|
|
|
3 |
namespace mimas { |
|
|
4 |
|
|
|
5 |
#define Y_X_FACTOR 1.1644 |
|
|
6 |
#define V_R_FACTOR 1.5960 |
|
|
7 |
#define U_G_FACTOR -0.3918 |
|
|
8 |
#define V_G_FACTOR -0.8130 |
|
|
9 |
#define U_B_FACTOR 2.0172 |
|
|
10 |
|
|
|
11 |
|
|
|
12 |
#define V_R_FACTOR_P 1.402 |
|
|
13 |
#define U_G_FACTOR_P -0.344136 |
|
|
14 |
#define V_G_FACTOR_P -0.714136 |
|
|
15 |
#define U_B_FACTOR_P 1.772 |
|
|
16 |
|
|
|
17 |
unsigned char clip_colour_cell( int x ) { |
|
|
18 |
return x > 0xffffff ? 0xff : ( x <= 0xffff ? 0x0 : x >> 16 ); |
|
|
19 |
}; |
|
|
20 |
|
|
|
21 |
void rgb_to_rgba( const char *in, int width, int height, char *out ) |
|
|
22 |
{ |
|
|
23 |
// Direct access to image-memory! |
|
|
24 |
assert( out != NULL ); |
|
|
25 |
#ifndef NDEBUG |
|
|
26 |
std::cerr << "RGB to RGBA for " << width << "x" << height << "-image." |
|
|
27 |
<< std::endl; |
|
|
28 |
#endif |
|
|
29 |
int numpix = width * height; |
|
|
30 |
unsigned char *p = (unsigned char *)out; |
|
|
31 |
const unsigned char *q = (const unsigned char *)in; |
|
|
32 |
for ( int i=0; i<numpix; i++ ) { |
|
|
33 |
*p++ = *q++; |
|
|
34 |
*p++ = *q++; |
|
|
35 |
*p++ = *q++; |
|
|
36 |
*p++ = 0; |
|
|
37 |
}; |
|
|
38 |
} |
|
|
39 |
|
|
|
40 |
void yv12_to_rgba( const char *in, int width, int height, char *out ) |
|
|
41 |
{ |
|
|
42 |
// Direct access to image-memory! |
|
|
43 |
assert( out != NULL ); |
|
|
44 |
assert( width % 2 == 0 ); |
|
|
45 |
assert( height % 2 == 0 ); |
|
|
46 |
#ifndef NDEBUG |
|
|
47 |
std::cerr << "YV12 to RGBA for " << width << "x" << height << "-image." |
|
|
48 |
<< std::endl; |
|
|
49 |
#endif |
|
|
50 |
|
|
|
51 |
const unsigned char |
|
|
52 |
*src_y = (const unsigned char *)in; |
|
|
53 |
const signed char |
|
|
54 |
*src_u = (const signed char *)in + width * height, |
|
|
55 |
*src_v = src_u + ( width / 2 ) * ( height / 2 ); |
|
|
56 |
|
|
|
57 |
unsigned char |
|
|
58 |
*out1 = (unsigned char *)out, |
|
|
59 |
*out2 = (unsigned char *)out + ( width << 2 ); |
|
|
60 |
|
|
|
61 |
// R' = [ 1.1644 0 1.5960 ] ([ Y' ] [ 16 ]) |
|
|
62 |
// G' = [ 1.1644 -0.3918 -0.8130 ] * ([ Cb ] - [ 128 ]) |
|
|
63 |
// B' = [ 1.1644 2.0172 0 ] ([ Cr ] [ 128 ]) |
|
|
64 |
|
|
|
65 |
const int |
|
|
66 |
yxScale = (int)( Y_X_FACTOR * 65536 ), |
|
|
67 |
vrScale = (int)( V_R_FACTOR * 65536 ), |
|
|
68 |
ugScale = (int)( U_G_FACTOR * 65536 ), |
|
|
69 |
vgScale = (int)( V_G_FACTOR * 65536 ), |
|
|
70 |
ubScale = (int)( U_B_FACTOR * 65536 ); |
|
|
71 |
|
|
|
72 |
for ( int y=0; y<height; y+=2 ) { |
|
|
73 |
|
|
|
74 |
for ( int x=0; x<width; x+=2 ) { |
|
|
75 |
|
|
|
76 |
int |
|
|
77 |
ys0int = ( src_y[ 0 ] - 16 ) * yxScale, |
|
|
78 |
ys1int = ( src_y[ 1 ] - 16 ) * yxScale, |
|
|
79 |
ys2int = ( src_y[ width ] - 16 ) * yxScale, |
|
|
80 |
ys3int = ( src_y[ width + 1 ] - 16 ) * yxScale; |
|
|
81 |
|
|
|
82 |
signed char |
|
|
83 |
cb = *src_u ^ 0x80, |
|
|
84 |
cr = *src_v ^ 0x80; |
|
|
85 |
|
|
|
86 |
int |
|
|
87 |
rint = vrScale * cr, |
|
|
88 |
gint = ugScale * cb + vgScale * cr, |
|
|
89 |
bint = ubScale * cb; |
|
|
90 |
|
|
|
91 |
out1[ 0 ] = clip_colour_cell( ys0int + bint ); |
|
|
92 |
out1[ 1 ] = clip_colour_cell( ys0int + gint ); |
|
|
93 |
out1[ 2 ] = clip_colour_cell( ys0int + rint ); |
|
|
94 |
out1[ 3 ] = 0; |
|
|
95 |
out1[ 4 ] = clip_colour_cell( ys1int + bint ); |
|
|
96 |
out1[ 5 ] = clip_colour_cell( ys1int + gint ); |
|
|
97 |
out1[ 6 ] = clip_colour_cell( ys1int + rint ); |
|
|
98 |
out1[ 7 ] = 0; |
|
|
99 |
out2[ 0 ] = clip_colour_cell( ys2int + bint ); |
|
|
100 |
out2[ 1 ] = clip_colour_cell( ys2int + gint ); |
|
|
101 |
out2[ 2 ] = clip_colour_cell( ys2int + rint ); |
|
|
102 |
out2[ 3 ] = 0; |
|
|
103 |
out2[ 4 ] = clip_colour_cell( ys3int + bint ); |
|
|
104 |
out2[ 5 ] = clip_colour_cell( ys3int + gint ); |
|
|
105 |
out2[ 6 ] = clip_colour_cell( ys3int + rint ); |
|
|
106 |
out2[ 7 ] = 0; |
|
|
107 |
|
|
|
108 |
src_y += 2; |
|
|
109 |
src_u++; |
|
|
110 |
src_v++; |
|
|
111 |
|
|
|
112 |
out1 += 8; |
|
|
113 |
out2 += 8; |
|
|
114 |
|
|
|
115 |
} |
|
|
116 |
|
|
|
117 |
src_y += width; |
|
|
118 |
out1 += width << 2; |
|
|
119 |
out2 += width << 2; |
|
|
120 |
|
|
|
121 |
} |
|
|
122 |
|
|
|
123 |
} |
|
|
124 |
|
|
|
125 |
void rgba_to_yv12( const char *in, int width, int height, char *out ) |
|
|
126 |
{ |
|
|
127 |
// Direct access to image-memory! |
|
|
128 |
assert( out != NULL ); |
|
|
129 |
assert( width % 2 == 0 ); |
|
|
130 |
assert( height % 2 == 0 ); |
|
|
131 |
#ifndef NDEBUG |
|
|
132 |
std::cerr << "YV12 to RGBA for " << width << "x" << height << "-image." |
|
|
133 |
<< std::endl; |
|
|
134 |
#endif |
|
|
135 |
|
|
|
136 |
const unsigned char |
|
|
137 |
*in1 = (const unsigned char *)in, |
|
|
138 |
*in2 = (const unsigned char *)in + ( width << 2 ); |
|
|
139 |
|
|
|
140 |
unsigned char |
|
|
141 |
*out_y = (unsigned char *)out; |
|
|
142 |
signed char |
|
|
143 |
*out_u = (signed char *)out + width * height, |
|
|
144 |
*out_v = out_u + ( width / 2 ) * ( height / 2 ); |
|
|
145 |
|
|
|
146 |
// Y = = [ 0.257 0.504 0.098 ] [ R ] [ 16 ] |
|
|
147 |
// Cb = U = [ -0.148 -0.291 0.439 ] * [ G ] + [ 128 ] |
|
|
148 |
// Cr = V = [ 0.497 -0.368 -0.071 ] [ B ] [ 128 ] |
|
|
149 |
|
|
|
150 |
const int |
|
|
151 |
yrScale = (int)( 0.257 * 65536 ), |
|
|
152 |
ygScale = (int)( 0.504 * 65536 ), |
|
|
153 |
ybScale = (int)( 0.098 * 65536 ), |
|
|
154 |
brScale4 = (int)( -0.148 * 65536 / 4 ), |
|
|
155 |
bgScale4 = (int)( -0.291 * 65536 / 4 ), |
|
|
156 |
ubScale4 = (int)( 0.439 * 65536 / 4 ), |
|
|
157 |
vrScale4 = (int)( 0.497 * 65536 / 4 ), |
|
|
158 |
rgScale4 = (int)( -0.368 * 65536 / 4 ), |
|
|
159 |
rbScale4 = (int)( -0.071 * 65536 / 4 ), |
|
|
160 |
yOffset = 16 * 65536, |
|
|
161 |
rOffset = 128 * 65536, |
|
|
162 |
bOffset = 128 * 65536; |
|
|
163 |
|
|
|
164 |
for ( int y=0; y<height; y+=2 ) { |
|
|
165 |
|
|
|
166 |
for ( int x=0; x<width; x+=2 ) { |
|
|
167 |
|
|
|
168 |
out_y[ 0 ] = clip_colour_cell( in1[ 2 ] * yrScale + |
|
|
169 |
in1[ 1 ] * ygScale + |
|
|
170 |
in1[ 0 ] * ybScale + yOffset ); |
|
|
171 |
out_y[ 1 ] = clip_colour_cell( in1[ 6 ] * yrScale + |
|
|
172 |
in1[ 5 ] * ygScale + |
|
|
173 |
in1[ 4 ] * ybScale + yOffset ); |
|
|
174 |
out_y[ width ] = clip_colour_cell( in2[ 2 ] * yrScale + |
|
|
175 |
in2[ 1 ] * ygScale + |
|
|
176 |
in2[ 0 ] * ybScale + yOffset ), |
|
|
177 |
out_y[ width + 1 ] = clip_colour_cell( in2[ 6 ] * yrScale + |
|
|
178 |
in2[ 5 ] * ygScale + |
|
|
179 |
in2[ 4 ] * ybScale + yOffset ); |
|
|
180 |
int |
|
|
181 |
ravg4 = in1[ 2 ] + in1[ 6 ] + in2[ 2 ] + in2[ 6 ], |
|
|
182 |
gavg4 = in1[ 1 ] + in1[ 5 ] + in2[ 1 ] + in2[ 5 ], |
|
|
183 |
bavg4 = in1[ 0 ] + in1[ 4 ] + in2[ 0 ] + in2[ 4 ]; |
|
|
184 |
|
|
|
185 |
*out_u = (signed char)clip_colour_cell( ravg4 * brScale4 + |
|
|
186 |
gavg4 * bgScale4 + |
|
|
187 |
bavg4 * ubScale4 + |
|
|
188 |
rOffset ); |
|
|
189 |
*out_v = (signed char)clip_colour_cell( ravg4 * vrScale4 + |
|
|
190 |
gavg4 * rgScale4 + |
|
|
191 |
bavg4 * rbScale4 + |
|
|
192 |
bOffset ); |
|
|
193 |
|
|
|
194 |
out_y += 2; |
|
|
195 |
out_u++; |
|
|
196 |
out_v++; |
|
|
197 |
|
|
|
198 |
in1 += 8; |
|
|
199 |
in2 += 8; |
|
|
200 |
|
|
|
201 |
} |
|
|
202 |
|
|
|
203 |
out_y += width; |
|
|
204 |
in1 += width << 2; |
|
|
205 |
in2 += width << 2; |
|
|
206 |
|
|
|
207 |
} |
|
|
208 |
|
|
|
209 |
} |
|
|
210 |
|
|
|
211 |
void uyvy_to_rgba( const char *in, int width, int height, char *out ) |
|
|
212 |
{ |
|
|
213 |
// Direct access to image-memory! |
|
|
214 |
assert( out != NULL ); |
|
|
215 |
assert( width % 2 == 0 ); |
|
|
216 |
assert( height % 2 == 0 ); |
|
|
217 |
#ifndef NDEBUG |
|
|
218 |
std::cerr << "UYVY to RGBA for " << width << "x" << height << "-image." |
|
|
219 |
<< std::endl; |
|
|
220 |
#endif |
|
|
221 |
|
|
|
222 |
// R' = [ 1.1644 0 1.5960 ] ([ Y' ] [ 16 ]) |
|
|
223 |
// G' = [ 1.1644 -0.3918 -0.8130 ] * ([ Cb ] - [ 128 ]) |
|
|
224 |
// B' = [ 1.1644 2.0172 0 ] ([ Cr ] [ 128 ]) |
|
|
225 |
|
|
|
226 |
const int |
|
|
227 |
yxScale = (int)( Y_X_FACTOR * 65536 ), |
|
|
228 |
vrScale = (int)( V_R_FACTOR * 65536 ), |
|
|
229 |
ugScale = (int)( U_G_FACTOR * 65536 ), |
|
|
230 |
vgScale = (int)( V_G_FACTOR * 65536 ), |
|
|
231 |
ubScale = (int)( U_B_FACTOR * 65536 ); |
|
|
232 |
|
|
|
233 |
for ( int y=0; y<height; y++ ) { |
|
|
234 |
|
|
|
235 |
for ( int x=0; x<width; x+=2 ) { |
|
|
236 |
|
|
|
237 |
int |
|
|
238 |
ys0int = ( ((const unsigned char *)in)[ 1 ] - 16 ) * yxScale, |
|
|
239 |
ys1int = ( ((const unsigned char *)in)[ 3 ] - 16 ) * yxScale; |
|
|
240 |
|
|
|
241 |
signed char |
|
|
242 |
cb = ((const signed char *)in)[0] ^ 0x80, |
|
|
243 |
cr = ((const signed char *)in)[2] ^ 0x80; |
|
|
244 |
|
|
|
245 |
int |
|
|
246 |
rint = vrScale * cr, |
|
|
247 |
gint = ugScale * cb + vgScale * cr, |
|
|
248 |
bint = ubScale * cb; |
|
|
249 |
|
|
|
250 |
out[ 0 ] = clip_colour_cell( ys0int + bint ); |
|
|
251 |
out[ 1 ] = clip_colour_cell( ys0int + gint ); |
|
|
252 |
out[ 2 ] = clip_colour_cell( ys0int + rint ); |
|
|
253 |
out[ 3 ] = 0; |
|
|
254 |
out[ 4 ] = clip_colour_cell( ys1int + bint ); |
|
|
255 |
out[ 5 ] = clip_colour_cell( ys1int + gint ); |
|
|
256 |
out[ 6 ] = clip_colour_cell( ys1int + rint ); |
|
|
257 |
out[ 7 ] = 0; |
|
|
258 |
|
|
|
259 |
in += 4; |
|
|
260 |
out += 8; |
|
|
261 |
}; |
|
|
262 |
}; |
|
|
263 |
} |
|
|
264 |
|
|
|
265 |
void yuy2_to_rgba( const char *in, int width, int height, char *out ) |
|
|
266 |
{ |
|
|
267 |
// Direct access to image-memory! |
|
|
268 |
assert( out != NULL ); |
|
|
269 |
assert( width % 4 == 0 ); |
|
|
270 |
#ifndef NDEBUG |
|
|
271 |
std::cerr << "YUY2 to RGBA for " << width << "x" << height << "-image." |
|
|
272 |
<< std::endl; |
|
|
273 |
#endif |
|
|
274 |
|
|
|
275 |
// R' = [ 1.1644 0 1.5960 ] ([ Y' ] [ 16 ]) |
|
|
276 |
// G' = [ 1.1644 -0.3918 -0.8130 ] * ([ Cb ] - [ 128 ]) |
|
|
277 |
// B' = [ 1.1644 2.0172 0 ] ([ Cr ] [ 128 ]) |
|
|
278 |
|
|
|
279 |
const int |
|
|
280 |
yxScale = (int)( Y_X_FACTOR * 65536 ), |
|
|
281 |
vrScale = (int)( V_R_FACTOR * 65536 ), |
|
|
282 |
ugScale = (int)( U_G_FACTOR * 65536 ), |
|
|
283 |
vgScale = (int)( V_G_FACTOR * 65536 ), |
|
|
284 |
ubScale = (int)( U_B_FACTOR * 65536 ); |
|
|
285 |
|
|
|
286 |
for ( int y=0; y<height; y++ ) { |
|
|
287 |
|
|
|
288 |
for ( int x=0; x<width; x+=2 ) { |
|
|
289 |
|
|
|
290 |
int |
|
|
291 |
ys0int = ( ((const unsigned char *)in)[ 0 ] - 16 ) * yxScale, |
|
|
292 |
ys1int = ( ((const unsigned char *)in)[ 2 ] - 16 ) * yxScale; |
|
|
293 |
|
|
|
294 |
signed char |
|
|
295 |
cb = ((const signed char *)in)[ 1 ] ^ 0x80, |
|
|
296 |
cr = ((const signed char *)in)[ 3 ] ^ 0x80; |
|
|
297 |
|
|
|
298 |
int |
|
|
299 |
rint = vrScale * cr, |
|
|
300 |
gint = ugScale * cb + vgScale * cr, |
|
|
301 |
bint = ubScale * cb; |
|
|
302 |
|
|
|
303 |
out[ 0 ] = clip_colour_cell( ys0int + bint ); |
|
|
304 |
out[ 1 ] = clip_colour_cell( ys0int + gint ); |
|
|
305 |
out[ 2 ] = clip_colour_cell( ys0int + rint ); |
|
|
306 |
out[ 3 ] = 0; |
|
|
307 |
out[ 4 ] = clip_colour_cell( ys1int + bint ); |
|
|
308 |
out[ 5 ] = clip_colour_cell( ys1int + gint ); |
|
|
309 |
out[ 6 ] = clip_colour_cell( ys1int + rint ); |
|
|
310 |
out[ 7 ] = 0; |
|
|
311 |
|
|
|
312 |
in += 4; |
|
|
313 |
out += 8; |
|
|
314 |
} |
|
|
315 |
|
|
|
316 |
} |
|
|
317 |
|
|
|
318 |
} |
|
|
319 |
|
|
|
320 |
void yuv420p_to_rgba( const char *in, int width, int height, char *out ) |
|
|
321 |
{ |
|
|
322 |
// Direct access to image-memory! |
|
|
323 |
assert( out != NULL ); |
|
|
324 |
assert( width % 2 == 0 ); |
|
|
325 |
assert( height % 2 == 0 ); |
|
|
326 |
#ifndef NDEBUG |
|
|
327 |
std::cerr << "YV12 to RGBA for " << width << "x" << height << "-image." |
|
|
328 |
<< std::endl; |
|
|
329 |
#endif |
|
|
330 |
|
|
|
331 |
const unsigned char |
|
|
332 |
*src_y = (const unsigned char *)in; |
|
|
333 |
const signed char |
|
|
334 |
*src_u = (const signed char *)in + width * height, |
|
|
335 |
*src_v = src_u + ( width / 2 ) * ( height / 2 ); |
|
|
336 |
|
|
|
337 |
unsigned char |
|
|
338 |
*out1 = (unsigned char *)out, |
|
|
339 |
*out2 = (unsigned char *)out + ( width << 2 ); |
|
|
340 |
|
|
|
341 |
const int |
|
|
342 |
vrScale = (int)( V_R_FACTOR_P * 65536 ), |
|
|
343 |
ugScale = (int)( U_G_FACTOR_P * 65536 ), |
|
|
344 |
vgScale = (int)( V_G_FACTOR_P * 65536 ), |
|
|
345 |
ubScale = (int)( U_B_FACTOR_P * 65536 ); |
|
|
346 |
|
|
|
347 |
for ( int y=0; y<height; y+=2 ) { |
|
|
348 |
|
|
|
349 |
for ( int x=0; x<width; x+=2 ) { |
|
|
350 |
|
|
|
351 |
int |
|
|
352 |
ys0int = src_y[ 0 ] << 16, |
|
|
353 |
ys1int = src_y[ 1 ] << 16, |
|
|
354 |
ys2int = src_y[ width ] << 16, |
|
|
355 |
ys3int = src_y[ width + 1 ] << 16; |
|
|
356 |
|
|
|
357 |
signed char |
|
|
358 |
cb = *src_u ^ 0x80, |
|
|
359 |
cr = *src_v ^ 0x80; |
|
|
360 |
|
|
|
361 |
int |
|
|
362 |
rint = vrScale * cr, |
|
|
363 |
gint = ugScale * cb + vgScale * cr, |
|
|
364 |
bint = ubScale * cb; |
|
|
365 |
|
|
|
366 |
out1[ 0 ] = clip_colour_cell( ys0int + bint ); |
|
|
367 |
out1[ 1 ] = clip_colour_cell( ys0int + gint ); |
|
|
368 |
out1[ 2 ] = clip_colour_cell( ys0int + rint ); |
|
|
369 |
out1[ 3 ] = 0; |
|
|
370 |
out1[ 4 ] = clip_colour_cell( ys1int + bint ); |
|
|
371 |
out1[ 5 ] = clip_colour_cell( ys1int + gint ); |
|
|
372 |
out1[ 6 ] = clip_colour_cell( ys1int + rint ); |
|
|
373 |
out1[ 7 ] = 0; |
|
|
374 |
out2[ 0 ] = clip_colour_cell( ys2int + bint ); |
|
|
375 |
out2[ 1 ] = clip_colour_cell( ys2int + gint ); |
|
|
376 |
out2[ 2 ] = clip_colour_cell( ys2int + rint ); |
|
|
377 |
out2[ 3 ] = 0; |
|
|
378 |
out2[ 4 ] = clip_colour_cell( ys3int + bint ); |
|
|
379 |
out2[ 5 ] = clip_colour_cell( ys3int + gint ); |
|
|
380 |
out2[ 6 ] = clip_colour_cell( ys3int + rint ); |
|
|
381 |
out2[ 7 ] = 0; |
|
|
382 |
|
|
|
383 |
src_y += 2; |
|
|
384 |
src_u++; |
|
|
385 |
src_v++; |
|
|
386 |
|
|
|
387 |
out1 += 8; |
|
|
388 |
out2 += 8; |
|
|
389 |
|
|
|
390 |
} |
|
|
391 |
|
|
|
392 |
src_y += width; |
|
|
393 |
out1 += width << 2; |
|
|
394 |
out2 += width << 2; |
|
|
395 |
|
|
|
396 |
} |
|
|
397 |
|
|
|
398 |
} |
|
|
399 |
|
|
|
400 |
}; |