#[must_use]
pub fn clip_pixel8(x: i32) -> u8 {
x.clamp(0, 255) as u8
}
#[must_use]
pub fn clip_pixel(x: i32, bit_depth: u32) -> u16 {
let max = (1i32 << bit_depth) - 1;
x.clamp(0, max) as u16
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn clip_pixel8_saturates() {
assert_eq!(clip_pixel8(-1), 0);
assert_eq!(clip_pixel8(-1000), 0);
assert_eq!(clip_pixel8(0), 0);
assert_eq!(clip_pixel8(128), 128);
assert_eq!(clip_pixel8(255), 255);
assert_eq!(clip_pixel8(1000), 255);
}
#[test]
fn clip_pixel_saturates_per_bit_depth() {
for &(x, expect8) in &[(-5, 0u16), (0, 0), (200, 200), (255, 255), (5000, 255)] {
assert_eq!(clip_pixel(x, 8), expect8);
assert_eq!(clip_pixel(x, 8), u16::from(clip_pixel8(x)));
}
assert_eq!(clip_pixel(1023, 10), 1023);
assert_eq!(clip_pixel(2000, 10), 1023); assert_eq!(clip_pixel(-1, 10), 0);
assert_eq!(clip_pixel(4095, 12), 4095);
assert_eq!(clip_pixel(9000, 12), 4095); }
}