image-rs / image-tiff

TIFF decoding and encoding library in pure Rust
MIT License
121 stars 79 forks source link

Decoding horizontally-predicted images of more than 8bit per channel fails #247

Closed spoutn1k closed 2 months ago

spoutn1k commented 2 months ago

The code responsible for rebuilding a predicted row is wrong for images using more that 8 bits per channel:

https://github.com/image-rs/image-tiff/blob/de6d9cf131850662b418bc2a21ca0823ba23b68a/src/decoder/mod.rs#L259-L283

The assignment to the variable p, which is the previous value in the chain, does not take into account the size of each sample. As we are on a u8 buffer, the previous value should be offset by sample byte count * sample size yet we only jump by sample size.

- let p = u16::from_ne_bytes(buf[i - samples..][..2].try_into().unwrap());
+ let p = u16::from_ne_bytes(buf[i - 2 * samples..][..2].try_into().unwrap());

Found this while working on #240, and pushed a fix on there.