Based on the work of Mark Himsley <mark@mdsh.com>.
See thread:
Subject: [FFmpeg-devel] libavfilter: extending overlay filter
Date: Sun, 13 Mar 2011 14:18:42 +0000
... | ... |
@@ -360,18 +360,35 @@ static void blend_slice(AVFilterContext *ctx, |
360 | 360 |
const int dr = over->main_rgba_map[R]; |
361 | 361 |
const int dg = over->main_rgba_map[G]; |
362 | 362 |
const int db = over->main_rgba_map[B]; |
363 |
+ const int da = over->main_rgba_map[A]; |
|
363 | 364 |
const int dstep = over->main_pix_step[0]; |
364 | 365 |
const int sr = over->overlay_rgba_map[R]; |
365 | 366 |
const int sg = over->overlay_rgba_map[G]; |
366 | 367 |
const int sb = over->overlay_rgba_map[B]; |
367 | 368 |
const int sa = over->overlay_rgba_map[A]; |
368 | 369 |
const int sstep = over->overlay_pix_step[0]; |
370 |
+ const int main_has_alpha = over->main_has_alpha; |
|
369 | 371 |
if (slice_y > y) |
370 | 372 |
sp += (slice_y - y) * src->linesize[0]; |
371 | 373 |
for (i = 0; i < height; i++) { |
372 | 374 |
uint8_t *d = dp, *s = sp; |
373 | 375 |
for (j = 0; j < width; j++) { |
374 | 376 |
alpha = s[sa]; |
377 |
+ |
|
378 |
+ // if the main channel has an alpha channel, alpha has to be calculated |
|
379 |
+ // to create an un-premultiplied (straight) alpha value |
|
380 |
+ if (main_has_alpha && alpha != 0 && alpha != 255) { |
|
381 |
+ // apply the general equation: |
|
382 |
+ // alpha = alpha_overlay / ( (alpha_main + alpha_overlay) - (alpha_main * alpha_overlay) ) |
|
383 |
+ alpha = |
|
384 |
+ // the next line is a faster version of: 255 * 255 * alpha |
|
385 |
+ ( (alpha << 16) - (alpha << 9) + alpha ) |
|
386 |
+ / |
|
387 |
+ // the next line is a faster version of: 255 * (alpha + d[da]) |
|
388 |
+ ( ((alpha + d[da]) << 8 ) - (alpha + d[da]) |
|
389 |
+ - d[da] * alpha ); |
|
390 |
+ } |
|
391 |
+ |
|
375 | 392 |
switch (alpha) { |
376 | 393 |
case 0: |
377 | 394 |
break; |
... | ... |
@@ -387,6 +404,18 @@ static void blend_slice(AVFilterContext *ctx, |
387 | 387 |
d[dg] = FAST_DIV255(d[dg] * (255 - alpha) + s[sg] * alpha); |
388 | 388 |
d[db] = FAST_DIV255(d[db] * (255 - alpha) + s[sb] * alpha); |
389 | 389 |
} |
390 |
+ if (main_has_alpha) { |
|
391 |
+ switch (alpha) { |
|
392 |
+ case 0: |
|
393 |
+ break; |
|
394 |
+ case 255: |
|
395 |
+ d[da] = s[sa]; |
|
396 |
+ break; |
|
397 |
+ default: |
|
398 |
+ // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha |
|
399 |
+ d[da] += FAST_DIV255((255 - d[da]) * s[sa]); |
|
400 |
+ } |
|
401 |
+ } |
|
390 | 402 |
d += dstep; |
391 | 403 |
s += sstep; |
392 | 404 |
} |