use std::num::NonZeroU32;
use mln_style::bitmap::{PremultipliedImage, UnassociatedImage};
#[must_use]
pub(crate) fn premultiply(src: UnassociatedImage) -> PremultipliedImage {
let (size, mut data) = src.into_parts();
let bytes = PremultipliedImage::CHANNELS * size.width as usize * size.height as usize;
for px in data.as_chunks_mut::<4>().0.iter_mut().take(bytes / 4) {
let a = u32::from(px[3]);
px[0] = ((u32::from(px[0]) * a + 127) / 255) as u8;
px[1] = ((u32::from(px[1]) * a + 127) / 255) as u8;
px[2] = ((u32::from(px[2]) * a + 127) / 255) as u8;
}
PremultipliedImage::from_vec(size, data)
}
#[must_use]
pub(crate) fn unpremultiply(src: PremultipliedImage) -> UnassociatedImage {
let (size, mut data) = src.into_parts();
let bytes = UnassociatedImage::CHANNELS * size.width as usize * size.height as usize;
for px in data.as_chunks_mut::<4>().0.iter_mut().take(bytes / 4) {
if let Some(a) = NonZeroU32::new(u32::from(px[3])) {
let half = a.get() / 2;
px[0] = ((255 * u32::from(px[0]) + half) / a) as u8;
px[1] = ((255 * u32::from(px[1]) + half) / a) as u8;
px[2] = ((255 * u32::from(px[2]) + half) / a) as u8;
}
}
UnassociatedImage::from_vec(size, data)
}
#[cfg(test)]
mod tests {
use mln_style::size::Size;
use super::*;
#[test]
fn premultiply_() {
let mut rgba = UnassociatedImage::new(Size::new(1, 1));
rgba.data.copy_from_slice(&[255, 254, 253, 128]);
let image = premultiply(std::mem::take(&mut rgba));
assert_eq!(image.data[0], 128);
assert_eq!(image.data[1], 127);
assert_eq!(image.data[2], 127);
assert_eq!(image.data[3], 128);
assert_eq!(image.size.width, 1);
assert_eq!(image.size.height, 1);
assert_eq!(rgba.size.width, 0);
assert_eq!(rgba.size.height, 0);
}
#[test]
fn unpremultiply_() {
let image =
PremultipliedImage::from_vec(Size::new(3, 1), vec![128, 127, 127, 128, 10, 20, 30, 0, 200, 0, 0, 100]);
let rgba = unpremultiply(image);
assert_eq!(&rgba.data[0..4], &[255, 253, 253, 128]);
assert_eq!(&rgba.data[4..8], &[10, 20, 30, 0]);
assert_eq!(&rgba.data[8..12], &[254, 0, 0, 100]);
}
#[test]
fn exhaustive_against_formula() {
let mut data = Vec::with_capacity(256 * 256 * 4);
for a in 0..=255u8 {
for c in 0..=255u8 {
data.extend_from_slice(&[c, c, c, a]);
}
}
let pre = premultiply(UnassociatedImage::from_vec(Size::new(256, 256), data));
for (i, px) in pre.data.chunks(4).enumerate() {
let (a, c) = ((i / 256) as i32, (i % 256) as i32);
assert_eq!(i32::from(px[0]), (c * a + 127) / 255);
}
let un = unpremultiply(pre.clone());
for (px, pre_px) in un.data.chunks(4).zip(pre.data.chunks(4)) {
let (a, c) = (i32::from(pre_px[3]), i32::from(pre_px[0]));
let expected = if a != 0 { ((255 * c + a / 2) / a) as u8 } else { c as u8 };
assert_eq!(px[0], expected);
}
}
}