AldaronLau / png_pong

A pure Rust PNG image decoder and encoder based on lodepng.

Home Page:https://docs.rs/png_pong

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Thread 'main' panicked at 'called `Option::unwrap()` on a `None` value'

joshstoik1 opened this issue · comments

commented

Describe the bug
The attached image seems to crash while attempting to decode with the following error (referencing this line)

thread 'main' panicked at 'called `Option::unwrap()` on a `None` value'

To Reproduce

// "raw" is an &[u8] of the raw file.
let png_pong::Step { raster, .. } = png_pong::Decoder::new(raw)
	.unwrap()
	.into_steps()
	.last();

Screenshots
mountains-on-mars

Desktop (please complete the following information):

  • OS: Debian Buster
  • Rust: stable-x86_64-unknown-linux-gnu (default) / rustc 1.52.1 (9bc8c42bb 2021-05-09)

@joshstoik1 Thanks! I'll look into fixing this.

@joshstoik1 Should be fixed in 0.8.1 (just released on crates.io)

commented

Thanks @AldaronLau!

Version 0.8.1 fixes the panic for me, but when I try the following, the PngRaster's palette and trailing vec are both empty:

// Loading from a slice, works A-OK now.
let png_pong::Step { raster, .. } = png_pong::Decoder::new(raw)
	.map_err(|_| Error::Source)?
	.into_steps()
	.last()
	.ok_or(tError::Source)
	.map_err(|_| Error::Source)?
	.map_err(|_| Error::Source)?;

// Trying to extract the inner raster bits...
let raster = match raster {
	PngRaster::Palette(x, y, z) => {
		println!("{:?} {:?}", y.len(), z.len()); // 0 0
	},
	...
}

If I use into_chunks() instead of into_steps(), there is a Palette object in there with the correct number of colors for this image. So basically the data just doesn't make it into the step's PngRaster::Palette(_, here, _).

png_pong::Decoder::new(raw)
	.map_err(|_| Error::Source)?
	.into_chunks()
	.for_each(|x| {
		match x {
			Ok(Chunk::Palette(Palette { palette })) => {
				println!("{:?}", palette.len()); // 221
			},
			_ => {}
		}
	});

If I remap the grey indexes from steps to the above palette, all's right with the world. 😉

(If you'd like me to open a new issue, please let me know. Thanks!)

@joshstoik1 Thanks for the extra information! I just tried your code, and with your provided PNG everything works fine (the palette makes it into PngRaster). The alpha vec should be empty since there's no alpha channel in the image. Full test code:

fn main() -> png_pong::decode::Result<()> {
    let raw = std::fs::read("test.png").unwrap();
    let raw = raw.as_slice();

    let png_pong::Step { raster, .. } = png_pong::Decoder::new(raw)?
	    .into_steps()
	    .last()
	    .unwrap()
	    .unwrap();

    let raster = match raster {
	    png_pong::PngRaster::Palette(x, y, z) => {
		    println!("{:?} {:?}", y.len(), z.len()); // 221 0
	    },
	    _ => {},
    };
    
    Ok(())
}

I'm not sure what you're doing different, any ideas?

@joshstoik1 Just figured out why that's happenning, I'll release 0.8.2 in a moment.

@joshstoik1 Just released 0.8.2, should be fixed now!