Opened 3 years ago

Closed 3 years ago

#9626 closed defect (invalid)

alphamerge and transparency in main

Reported by: Jozef Chutka Owned by:
Priority: normal Component: undetermined
Version: git-master Keywords:
Cc: Blocked By:
Blocking: Reproduced by developer: no
Analyzed by developer: no

Description (last modified by Jozef Chutka)

Summary of the bug:

Using ffmpeg version 2022-01-24-git-0a83ecbf48-full_build-www.gyan.dev

When alphamerge alpha has white pixels where in has transparent, those are expected to result transparent on out, however ffmpeg renders it with some(?) color.

How to reproduce:

% ffmpeg -f lavfi -i color=color=#ff0000:size=100x100:rate=25:duration=4 -i main.png -i mask.png -filter_complex [1][2]alphamerge[v];[0][v]overlay -frames:v 1 output/bug.jpg -y

In order to fix, one can "mask" the alpha by the shape of in before alphamerge is applied. Now the new alpha has no white pixels outside of in shape.

% ffmpeg -f lavfi -i color=color=#ff0000:size=100x100:rate=25:duration=4 -i main.png -i mask.png -filter_complex color=color=#000000@0:size=100x100:rate=25,split[b1][b2];[b1][1]overlay[v];[v]split[v][s];[2][s]alphamerge[m];[b2][m]overlay[m];[v][m]alphamerge[v];[0][v]overlay -frames:v 1 output/fix.jpg -y

In this case filter graph ends up with 8 total filters instead of 2.

I think ffmpeg should respect transparent pixels in in hence marking it as defect.

Attachments (4)

main.png (1.3 KB ) - added by Jozef Chutka 3 years ago.
mask.png (1.1 KB ) - added by Jozef Chutka 3 years ago.
bug.jpg (2.0 KB ) - added by Jozef Chutka 3 years ago.
fix.jpg (2.7 KB ) - added by Jozef Chutka 3 years ago.

Download all attachments as: .zip

Change History (11)

by Jozef Chutka, 3 years ago

Attachment: main.png added

by Jozef Chutka, 3 years ago

Attachment: mask.png added

by Jozef Chutka, 3 years ago

Attachment: bug.jpg added

by Jozef Chutka, 3 years ago

Attachment: fix.jpg added

comment:1 by Jozef Chutka, 3 years ago

Description: modified (diff)

comment:2 by Michael Koch, 3 years ago

When alphamerge alpha has white pixels where in has transparent, those are expected to result transparent on out, however ffmpeg renders it with some(?) color.

It's the other way round. Black pixels become alpha=0 and that's transparent. White pixels become alpha=255 and that's opaque.
In my opinion the output of the alphamerge filter is correct, and the output of the overlay filter is also correct.

Michael

comment:3 by Jozef Chutka, 3 years ago

Hi Michael,
please check the attachments I included. I am trying to mask semi transparent image containing "X" by mask in "O" shape. I would really expect output to respects my transparent pixels in the original X. However, instead, some very unexpected green pixels appear, where are these coming from?

comment:4 by pdr0, 3 years ago

It's working as expected

alphamerge
https://ffmpeg.org/ffmpeg-filters.html#alphamerge
"Add or replace the alpha component of the primary input with the grayscale value of a second input. "

The RGB greyscale of mask.png is a circle shape. That replaces the "x" character alpha channel in the original main.png

The RGB channels of "main.png" is green , with some white at the right edge, so the expected output is a green circle overlaid on #ff0000

in reply to:  3 comment:5 by Michael Koch, 3 years ago

Replying to Jozef Chutka:

Hi Michael,
please check the attachments I included. I am trying to mask semi transparent image containing "X" by mask in "O" shape.

Your image main.png (which contains a green X over a black background) can't be semi transparent because it has no alpha channel. The pixel format is rgb24.

Michael

comment:6 by Jozef Chutka, 3 years ago

Thanks both for looking into it. I can understand alphamerge a bit better. I eventually ended up using blend instead.

ffmpeg -f lavfi -i color=color=#ff0000:size=100x100 -i main.png -i mask.png -filter_complex [2]split,alphamerge,[1]blend=c3_mode=and,[0]overlay -frames:v 1 output/fix4.jpg -y

Feel free to close the ticket, blend is a working alternative.

comment:7 by Michael Koch, 3 years ago

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.