Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
spoutn1k opened this issue Sep 21, 2024 · 0 comments
Closed

Comments

@spoutn1k
Copy link
Contributor

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

match bit_depth {
0..=8 => {
for i in samples..buf.len() {
buf[i] = buf[i].wrapping_add(buf[i - samples]);
}
}
9..=16 => {
for i in (samples * 2..buf.len()).step_by(2) {
let v = u16::from_ne_bytes(buf[i..][..2].try_into().unwrap());
let p = u16::from_ne_bytes(buf[i - samples..][..2].try_into().unwrap());
buf[i..][..2].copy_from_slice(&(v.wrapping_add(p)).to_ne_bytes());
}
}
17..=32 => {
for i in (samples * 4..buf.len()).step_by(4) {
let v = u32::from_ne_bytes(buf[i..][..4].try_into().unwrap());
let p = u32::from_ne_bytes(buf[i - samples..][..4].try_into().unwrap());
buf[i..][..4].copy_from_slice(&(v.wrapping_add(p)).to_ne_bytes());
}
}
33..=64 => {
for i in (samples * 8..buf.len()).step_by(8) {
let v = u64::from_ne_bytes(buf[i..][..8].try_into().unwrap());
let p = u64::from_ne_bytes(buf[i - samples..][..8].try_into().unwrap());
buf[i..][..8].copy_from_slice(&(v.wrapping_add(p)).to_ne_bytes());

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant