JayXon / Leanify

lightweight lossless file minifier/optimizer
MIT License
831 stars 75 forks source link

APNG corruption #63

Open f2d opened 3 years ago

f2d commented 3 years ago

Hi.

As APNG is explicitly supported here, I tried leanify -v on some of my files. It appears to work and does not report anything suspicious:

Processing: 4_frames_good_from_gif2apng.png
PNG detected.
tEXt chunk removed, 36 bytes.
Filter strategy zero: 2409 bytes
4.57 KB -> 4.37 KB      Leanified: 203 B (4.34%)
Processing: 4_frames_bad_edges_left.png
PNG detected.
Filter strategy zero: 2407 bytes
24.08 KB -> 20.15 KB    Leanified: 3.93 KB (16.31%)
Processing: 4_frames_bad_edges_right.png
PNG detected.
Filter strategy zero: 2409 bytes
24.12 KB -> 20.17 KB    Leanified: 3.95 KB (16.37%)

But result either shows areas of garbled pixels after the first frame, or is completely not animated, depending on specific file. XnView says that unplayable file is "multipage" instead of simply playing animation, and selecting its pages does not show any change.

Identical results with Leanify build 244, 243, and the last v0.4.3 release from the GitHub page. Edit: Also identical results with build from latest source on a Linux server.

All files from gif2apng v1.9 (this was the best result I got back then, when I tried various ways to create APNG) become garbled. Other files are probably from paint.net with plugins. They become unplayable.

I'm attaching the original and result files here. apng_corruption.zip

I'm thinking to try creating APNG files with more recent software sometime later.

JayXon commented 3 years ago

This is because even though leanify will keep all the apng chunks, zopflipng and lodepng doesn't have native apng support https://github.com/google/zopfli/issues/118 https://github.com/lvandeve/lodepng/issues/98 And in some cases an optimization will change the color type based on only the first frame, and that might break the rest of the frames. It looks like setting keep_colortype will stop that from happening, but at the expense of missing many potential optimizations, but I think it's better than breaking the image, we can detect if it is apng based on existence of the apng chunks and set keep_colortype accordingly.

Liam2349 commented 1 year ago

Hi, thanks for this tool. I just noticed this issue today in that Leanify is corrupting my PNGs that use alpha channels, or at least some of them. I just checked the latest Nightly build and it seems to have the same issue. Are you planning to implement the above fix?

Not pushing you, just wondering - thanks!

JayXon commented 1 year ago

@Liam2349 If your PNG isn't an APNG (Animated PNG), then it's a different issue. By default Leanify will modify fully transparent pixels to make it more compressible, because those values don't affect how the image is displayed at all, but if you don't want this behavior, you can pass --png-lossless-transparent to the nightly build.