diff options
author | Stefano Sabatini <stefasab@gmail.com> | 2011-10-29 00:29:25 +0200 |
---|---|---|
committer | Stefano Sabatini <stefasab@gmail.com> | 2011-10-31 11:06:31 +0100 |
commit | 2f7c8aefa82ce4cb929f7c6ce9ff072abfa8826b (patch) | |
tree | 6a03da4827c56d21199632760a7fe5af0d11f1cd | |
parent | 3013bfa81ef17153a07970a8a5961e350992fdf0 (diff) | |
download | ffmpeg-2f7c8aefa82ce4cb929f7c6ce9ff072abfa8826b.tar.gz |
vf_overlay: add support to alpha pre-multiplication in the RGBA path
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
-rw-r--r-- | libavfilter/vf_overlay.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/libavfilter/vf_overlay.c b/libavfilter/vf_overlay.c index db5398551e..06967c2db5 100644 --- a/libavfilter/vf_overlay.c +++ b/libavfilter/vf_overlay.c @@ -360,18 +360,35 @@ static void blend_slice(AVFilterContext *ctx, const int dr = over->main_rgba_map[R]; const int dg = over->main_rgba_map[G]; const int db = over->main_rgba_map[B]; + const int da = over->main_rgba_map[A]; const int dstep = over->main_pix_step[0]; const int sr = over->overlay_rgba_map[R]; const int sg = over->overlay_rgba_map[G]; const int sb = over->overlay_rgba_map[B]; const int sa = over->overlay_rgba_map[A]; const int sstep = over->overlay_pix_step[0]; + const int main_has_alpha = over->main_has_alpha; if (slice_y > y) sp += (slice_y - y) * src->linesize[0]; for (i = 0; i < height; i++) { uint8_t *d = dp, *s = sp; for (j = 0; j < width; j++) { alpha = s[sa]; + + // if the main channel has an alpha channel, alpha has to be calculated + // to create an un-premultiplied (straight) alpha value + if (main_has_alpha && alpha != 0 && alpha != 255) { + // apply the general equation: + // alpha = alpha_overlay / ( (alpha_main + alpha_overlay) - (alpha_main * alpha_overlay) ) + alpha = + // the next line is a faster version of: 255 * 255 * alpha + ( (alpha << 16) - (alpha << 9) + alpha ) + / + // the next line is a faster version of: 255 * (alpha + d[da]) + ( ((alpha + d[da]) << 8 ) - (alpha + d[da]) + - d[da] * alpha ); + } + switch (alpha) { case 0: break; @@ -387,6 +404,18 @@ static void blend_slice(AVFilterContext *ctx, d[dg] = FAST_DIV255(d[dg] * (255 - alpha) + s[sg] * alpha); d[db] = FAST_DIV255(d[db] * (255 - alpha) + s[sb] * alpha); } + if (main_has_alpha) { + switch (alpha) { + case 0: + break; + case 255: + d[da] = s[sa]; + break; + default: + // apply alpha compositing: main_alpha += (1-main_alpha) * overlay_alpha + d[da] += FAST_DIV255((255 - d[da]) * s[sa]); + } + } d += dstep; s += sstep; } |