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
Unified · Split
| @@ -360,18 +360,35 @@ static void blend_slice(AVFilterContext *ctx, · 359 unmodified lines | |||
| 360 | const int dr = over->main_rgba_map[R]; | 360 | const int dr = over->main_rgba_map[R]; |
| 361 | const int dg = over->main_rgba_map[G]; | 361 | const int dg = over->main_rgba_map[G]; |
| 362 | const int db = over->main_rgba_map[B]; | 362 | const int db = over->main_rgba_map[B]; |
| 363 | const int da = over->main_rgba_map[A]; | ||
| 363 | const int dstep = over->main_pix_step[0]; | 364 | const int dstep = over->main_pix_step[0]; |
| 364 | const int sr = over->overlay_rgba_map[R]; | 365 | const int sr = over->overlay_rgba_map[R]; |
| 365 | const int sg = over->overlay_rgba_map[G]; | 366 | const int sg = over->overlay_rgba_map[G]; |
| 366 | const int sb = over->overlay_rgba_map[B]; | 367 | const int sb = over->overlay_rgba_map[B]; |
| 367 | const int sa = over->overlay_rgba_map[A]; | 368 | const int sa = over->overlay_rgba_map[A]; |
| 368 | const int sstep = over->overlay_pix_step[0]; | 369 | const int sstep = over->overlay_pix_step[0]; |
| 370 | const int main_has_alpha = over->main_has_alpha; | ||
| 369 | if (slice_y > y) | 371 | if (slice_y > y) |
| 370 | sp += (slice_y - y) * src->linesize[0]; | 372 | sp += (slice_y - y) * src->linesize[0]; |
| 371 | for (i = 0; i < height; i++) { | 373 | for (i = 0; i < height; i++) { |
| 372 | uint8_t *d = dp, *s = sp; | 374 | uint8_t *d = dp, *s = sp; |
| 373 | for (j = 0; j < width; j++) { | 375 | for (j = 0; j < width; j++) { |
| 374 | alpha = s[sa]; | 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 | switch (alpha) { | 392 | switch (alpha) { |
| 376 | case 0: | 393 | case 0: |
| 377 | break; | 394 | break; |
| @@ -387,6 +404,18 @@ static void blend_slice(AVFilterContext *ctx, · 9 unmodified lines | |||
| 387 | d[dg] = FAST_DIV255(d[dg] * (255 - alpha) + s[sg] * alpha); | 404 | d[dg] = FAST_DIV255(d[dg] * (255 - alpha) + s[sg] * alpha); |
| 388 | d[db] = FAST_DIV255(d[db] * (255 - alpha) + s[sb] * alpha); | 405 | d[db] = FAST_DIV255(d[db] * (255 - alpha) + s[sb] * alpha); |
| 389 | } | 406 | } |
| 407 | if (main_has_alpha) { | ||
| 408 | switch (alpha) { | ||
| 409 | case 0: | ||
| 410 | break; | ||
| 411 | case 255: | ||
| 412 | d[da] = s[sa]; | ||
| 413 | break; | ||
| 414 | default: | ||
| 415 | // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha | ||
| 416 | d[da] += FAST_DIV255((255 - d[da]) * s[sa]); | ||
| 417 | } | ||
| 418 | } | ||
| 390 | d += dstep; | 419 | d += dstep; |
| 391 | s += sstep; | 420 | s += sstep; |
| 392 | } | 421 | } |