ImageMagick6 icon indicating copy to clipboard operation
ImageMagick6 copied to clipboard

Transparency in PNG is ignored in some cases

Open melonmouse opened this issue 3 years ago • 5 comments

ImageMagick version

6.9.12-50 Q16 x86_64 17254 libpng 1.6.37 GIF version 87a

Operating system

Linux

Operating system, version and so on

Fedora 35, version 5.17.13-200.fc35.x86_64

Description

Using convert in.png out.gif should preserve any transparency in the png (this is especially relevant when dispose=1). This works properly in most cases, but depending on the input png, the background is sometimes incorrectly replaced with (0,0,0). I found a somewhat stable neighborhood of failing inputs, and some very similar inputs that are working properly. Surprisingly, the only difference between the input PNGs that cause imagemagic to fail is pixel data. This points to some failing heuristic or a bug that depends on the input.

Example input PNG + output gif, where imagemagic does not preserve transparency: overlay_bad15 a

Example input PNG + output gif, where imagemagic does preserve transparency: overlay_good6 b

Note that I encountered this bug in the wild several times before filing this bug. The above images were simplified to get closer to a minimal example, but it should be noted that this bug is not just fuzzer territory.

P.S. Imagemagic is amazing, thanks so much for working on it! Let me know if I can provide any additional information.

Steps to Reproduce

For each of the files in "example_images.zip" below: convert overlay_bad###.png test.gif yields a gif without proper transparency. convert overlay_good###.png test.gif yields a gif with proper transparency.

Images

Zip with 17 failing images (overlay_bad###.png) and 6 working images (overlay_good###.png):

example_images.zip

melonmouse avatar Jun 16 '22 14:06 melonmouse

Potentially, https://github.com/ImageMagick/ImageMagick/issues/3599 is related.

melonmouse avatar Jun 16 '22 14:06 melonmouse

I was unable to reproduce this bug when using ImageMagick 7.1.0-37 Q16-HDRI x86_64 b65d0d9af:20220605 magick in.png out.gif.

melonmouse avatar Jun 16 '22 15:06 melonmouse

It looks like this has to do with color reduction and that works different between IM6 and IM7. I am getting consistent results with the following command: convert input.png -type palettebilevelalpha -colors 256 output.gif

dlemstra avatar Jun 26 '22 16:06 dlemstra

Why is it different and how?

fmw42 avatar Jun 26 '22 18:06 fmw42

PNG has 8-bit transparency. GIF is only binary transparency. So when converting a PNG to GIF the transparency is effectively thresholded to black and white and the partial transparency is lost.

fmw42 avatar Jun 26 '22 18:06 fmw42