PhotoCraft: An open-source, photo editor written by scratch in pure Rust
perf(psd): 8-bit merged composite unmattes through a table, not a division per sample (#5694) (#6029)
xsvmf2ae78ed93d1
diff --git a/crates/psd/src/pixels.rs b/crates/psd/src/pixels.rsindex d4948457f..5af4903c5 100644--- a/crates/psd/src/pixels.rs+++ b/crates/psd/src/pixels.rs@@ -180,6 +180,20 @@ fn color_channel_count(mode: ColorMode) -> Result<usize> {}}+/// One 8-bit sample matted against white at alpha `a`, back to straight colour (rounded, at most+/// 255). At `a == 0` there is no colour to recover and the sample is kept.+fn unmatte_u8(c: u8, a: u8) -> u8 {+ let a = u32::from(a);+ ((u32::from(c) + a).saturating_sub(255) * 255 + a / 2).checked_div(a).map_or(c, |straight| straight.min(255) as u8)+}++/// [`unmatte_u8`] of every sample, one row per alpha: `table[a][c]`. Built per call (64 KiB, a+/// division per entry) rather than per pixel, and not as a static, which would add 64 KiB to+/// every binary that links this crate.+fn unmatte_u8_table() -> Vec<[u8; 256]> {+ (0..=255).map(|a| std::array::from_fn(|c| unmatte_u8(c as u8, a))).collect()+}+fn unmatte_to_u8(color: &[u8], alpha: &[u8], depth: u16) -> Result<Vec<u8>> {let convert = |c: f32, a: f32| {let straight = if a > 0.0 { (c + a - 1.0) / a } else { c };@@ -341,12 +355,11 @@ impl PsdFile {let alpha = if self.merged_has_alpha() { Some(get(cc)?) } else { None };let mut data = interleave(h.color_mode, &color, alpha.as_deref(), &self.color_mode_data, n)?;if h.color_mode == ColorMode::Rgb && h.depth == 8 && alpha.is_some() {+ let table = unmatte_u8_table();for pixel in data.as_chunks_mut::<4>().0 {- let a = u32::from(pixel[3]);+ let row = &table[usize::from(pixel[3])];for c in pixel.iter_mut().take(3) {- if let Some(straight) = ((u32::from(*c) + a).saturating_sub(255) * 255 + a / 2).checked_div(a) {- *c = straight.min(255) as u8;- }+ *c = row[usize::from(*c)];}}}@@ -368,6 +381,29 @@ mod tests {assert_eq!(file.decode_merged().unwrap(), native);}+ /// #5694: the 8-bit RGB composite unmattes through a table; every (sample, alpha) pair comes+ /// out exactly as the per-pixel division it replaced (kept here as the reference).+ #[test]+ fn rgba8_composite_unmattes_every_sample_and_alpha_as_the_division_did() {+ let mut file = crate::testgen::merged_only(Version::Psd, ColorMode::Rgb, 8, crate::Compression::Raw, 256, 256);+ file.header.channels = 4;+ // Pixel (x, y): red x, green 255 - x, blue x ^ y, alpha y.+ let (x, y) = (|i: usize| (i % 256) as u8, |i: usize| (i / 256) as u8);+ let plane = |f: &dyn Fn(usize) -> u8| (0..256 * 256).map(f).collect::<Vec<u8>>();+ let planes = [plane(&|i| x(i)), plane(&|i| 255 - x(i)), plane(&|i| x(i) ^ y(i)), plane(&|i| y(i))];+ file.image_data = crate::ImageData::encode(crate::Compression::Raw, &planes.concat(), &file.header).unwrap();+ let mut expected: Vec<u8> = (0..256 * 256).flat_map(|i| planes.each_ref().map(|p| p[i])).collect();+ for pixel in expected.as_chunks_mut::<4>().0 {+ let a = u32::from(pixel[3]);+ for c in pixel.iter_mut().take(3) {+ if let Some(straight) = ((u32::from(*c) + a).saturating_sub(255) * 255 + a / 2).checked_div(a) {+ *c = straight.min(255) as u8;+ }+ }+ }+ assert_eq!(file.composite_rgba8().unwrap().data, expected);+ }+#[test]fn oversized_layer_preview_rejects_missing_channels_before_allocating() {let mut file = crate::testgen::small(Version::Psb, crate::Compression::Raw);