History

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

Stefano Sabatini committed 15 years ago

Commit 2f7c8ae1 parent 3013bfa1 file · +29 −0Browse files

Unified · Split

libavfilter/vf_overlay.c+29−0
@@ -360,18 +360,35 @@ static void blend_slice(AVFilterContext *ctx, · 359 unmodified lines
360360 const int dr = over->main_rgba_map[R];
361361 const int dg = over->main_rgba_map[G];
362362 const int db = over->main_rgba_map[B];
363+ const int da = over->main_rgba_map[A];
363364 const int dstep = over->main_pix_step[0];
364365 const int sr = over->overlay_rgba_map[R];
365366 const int sg = over->overlay_rgba_map[G];
366367 const int sb = over->overlay_rgba_map[B];
367368 const int sa = over->overlay_rgba_map[A];
368369 const int sstep = over->overlay_pix_step[0];
370+ const int main_has_alpha = over->main_has_alpha;
369371 if (slice_y > y)
370372 sp += (slice_y - y) * src->linesize[0];
371373 for (i = 0; i < height; i++) {
372374 uint8_t *d = dp, *s = sp;
373375 for (j = 0; j < width; j++) {
374376 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+
375392 switch (alpha) {
376393 case 0:
377394 break;
@@ -387,6 +404,18 @@ static void blend_slice(AVFilterContext *ctx, · 9 unmodified lines
387404 d[dg] = FAST_DIV255(d[dg] * (255 - alpha) + s[sg] * alpha);
388405 d[db] = FAST_DIV255(d[db] * (255 - alpha) + s[sb] * alpha);
389406 }
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+ }
390419 d += dstep;
391420 s += sstep;
392421 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t