image-rs / image

Encoding and decoding images in Rust
Apache License 2.0
4.76k stars 588 forks source link

Animated PNG is decoded incorrectly #2265

Open SludgePhD opened 1 week ago

SludgePhD commented 1 week ago

Expected

bonzi-book

This is how the animation should look (assuming your browser correctly implements APNG decoding; Firefox and Chromium-based browsers work fine in my tests).

Actual behaviour

Screencast_20240623_180709.webm

Using image to decode the animation and rendering the result to a window produces this flickering output instead. Other images work fine, so I don't think the bug is in my code.

Reproduction steps

The failing APNG is attached above.

ripytide commented 1 week ago

I can reproduce the issue:

use std::io::Cursor;

use image::codecs::png::PngDecoder;
use image::AnimationDecoder;

fn main() {
    let png_decoder = PngDecoder::new(Cursor::new(std::fs::read("monkey.png").unwrap())).unwrap();

    let apng_decoder = png_decoder.apng().unwrap();

    let frames = apng_decoder.into_frames().collect_frames().unwrap();

    for (i, frame) in frames.into_iter().enumerate() {
        frame
            .buffer()
            .save_with_format(format!("frame-{i}.png"), image::ImageFormat::Png)
            .unwrap();
    }
}

It seems frames 2, 12 and 13 have missing sections (possibly via alpha=0) and all other frames seem fine.