pub fn forward(packed: &mut [u8], stored_row_bytes: usize, spp: usize) {
for row in packed.chunks_mut(stored_row_bytes) {
for i in (spp..row.len()).rev() {
row[i] = row[i].wrapping_sub(row[i - spp]);
}
}
}
pub fn reverse(packed: &mut [u8], stored_row_bytes: usize, spp: usize) {
for row in packed.chunks_mut(stored_row_bytes) {
for i in spp..row.len() {
row[i] = row[i].wrapping_add(row[i - spp]);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn forward_then_reverse_is_identity() {
for spp in [1usize, 3] {
let row_bytes = 7 * spp;
let original: Vec<u8> = (0..(row_bytes * 4) as u32)
.map(|i| (i * 37) as u8)
.collect();
let mut buf = original.clone();
forward(&mut buf, row_bytes, spp);
assert_ne!(buf, original, "differencing should change the data");
reverse(&mut buf, row_bytes, spp);
assert_eq!(buf, original);
}
}
}