kornelski / lodepng-rust

All-in-one PNG image encoder/decoder in pure Rust
https://lib.rs/lodepng
zlib License
100 stars 23 forks source link

Can't encode bitdepth lower than 8? #60

Closed lunasorcery closed 11 months ago

lunasorcery commented 11 months ago

Unless I've missed something, it seems that attempts to encode an image of 1/2/4-bitdepth will fail. buffer_for_type checks the data size as such:

let bytes_per_pixel = bitdepth as usize/8;
assert!(mem::size_of::<PixelType>() <= 4*bytes_per_pixel, "Implausibly large {}-byte pixel data type", mem::size_of::<PixelType>());

For an input bitdepth of less than 8, bytes_per_pixel will be 0, and then the assert cannot possibly pass unless PixelType is somehow a zero-size type.

kornelski commented 11 months ago

This assertion in particular checks that the pixel doesn't have more than 4 channels.

However, it's correct that you can't get less than 8 bits per pixel in RGB and RGBA color modes. Lower bits per pixel are possible only in palette modes.

lunasorcery commented 11 months ago

In this case I was trying to encode in a non-paletted Grey mode, which should support 1,2,4,8,16 bit.

https://github.com/kornelski/lodepng-rust/blob/8b0ef4828055a066d2dd6a8bb07d841313e144bb/src/ffi.rs#L23-L25

kornelski commented 11 months ago

Oh, you're right. I've reworked that check, and released a fix in 3.9.2.