image-rs / image-png

PNG decoding and encoding library in pure Rust
https://docs.rs/png
Apache License 2.0
347 stars 139 forks source link

No longer able to decode certain palletized PNGs #448

Closed RandomInsano closed 6 months ago

RandomInsano commented 6 months ago

I've been using the png crate for about four years on a personal project. In the beginning (some years ago) palletized PNG files worked fine. About a year or so ago, it began to fail to load at all and resulted in a fatal error I don't quite recall.

Here are the two images. First, the one that fails: Bad image

And the one that succeeds: Good image

I'm using pngquant 2.18.0 (January 2023) at the moment, but as mentioned before it's been an issue for some time. When decoding, I get the following (teal is transparent)

image

Instead of this:

image
RandomInsano commented 6 months ago

Results of pngcheck on "master" branch:

     Running `target/release/examples/pngcheck /Users/me/Downloads/font.png`
OK: /Users/me/Downloads/font.png (256x256, 1-bit palette, non-interlaced, 94.9%)
$ cargo run --release --example pngcheck -- -v ~/Downloads/font.png
    Finished release [optimized] target(s) in 0.07s
     Running `target/release/examples/pngcheck -v /Users/me/Downloads/font.png`
File: /Users/me/Downloads/font.png (10240) bytes
  chunk ChunkType { type: IHDR, critical: true, private: false, reserved: false, safecopy: false } at offset 0x0000c, length 13
    256 x 256 image, 1-bit palette, non-interlaced
  chunk ChunkType { type: gAMA, critical: false, private: false, reserved: false, safecopy: false } at offset 0x00025, length 4
  chunk ChunkType { type: sRGB, critical: false, private: false, reserved: false, safecopy: false } at offset 0x00035, length 1
  chunk ChunkType { type: pHYs, critical: false, private: false, reserved: false, safecopy: true } at offset 0x00042, length 9
  chunk ChunkType { type: PLTE, critical: true, private: false, reserved: false, safecopy: false } at offset 0x00057, length 6
  chunk ChunkType { type: IDAT, critical: true, private: false, reserved: false, safecopy: false } at offset 0x00069, length 1256
  chunk ChunkType { type: IEND, critical: true, private: false, reserved: false, safecopy: false } at offset 0x0055d, length 0
No errors detected in /Users/me/Downloads/font.png (7 chunks, 94.9% compression)
RandomInsano commented 6 months ago

Hmm. Using the show example, it does load properly.

My code to load the image looks like this:

        let decoder = Decoder::new(file);
        let mut reader = match decoder.read_info() {
            Ok(x) => x,
            Err(x) => match x {
                DecodingError::IoError(x) => return Err(x),
                _ => panic!("Unable to decode image header")
            }
        };

        // Load the image
        let mut pixels = vec![0; reader.output_buffer_size()];
        match reader.next_frame(&mut pixels) {
            Ok(_) => {},
            Err(x) => match x {
                DecodingError::IoError(x) => return Err(x),
                _ => panic!("Unable to decode image file")
            }
        };

        let image_info = reader.info();
        self.width = image_info.width as usize;
        self.height = image_info.height as usize;

     ...

        unsafe {
            gl::TexImage2D(gl::TEXTURE_2D, 
                0,
                gl::RGBA8 as i32,
                self.width as GLsizei,
                self.height as GLsizei,
                0,
                gl::RGBA,
                gl::UNSIGNED_BYTE,
                pixels.as_ptr() as *const GLvoid);
       }

I'm guessing that at some point, the in-memory representation of the image changes from RGBA? This seems like user error at this point so I'll focus on trying to fix my work, but let me know if it's worth creating a test case.

RandomInsano commented 6 months ago

Fixed by setting transformations on the decoder:

        decoder.set_transformations(Transformations::ALPHA);

Sorry for the noise all! I've been pretty happy with this crate so far, keep up the great work!