PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Speed up Mosaic with portable row reads (#1524)
optimalbyteed651be1393b7
diff --git a/crates/algo/src/stylize.rs b/crates/algo/src/stylize.rsindex 5a287710b..2a88c6ed6 100644--- a/crates/algo/src/stylize.rs+++ b/crates/algo/src/stylize.rs@@ -73,14 +73,21 @@ pub(crate) fn mosaic(src: &Image, out: Rect, ctx: &Ctx, cell: f32) -> Vec<f32> {let r = cell_rect.intersect(&b);acc.fill(0.0);let mut count = 0.0;- for yy in r.y0..r.y1 {- for xx in r.x0..r.x1 {- let a = if ctx.alpha { src.get(xx, yy, n - 1) } else { 1.0 };- for (c, v) in acc.iter_mut().enumerate() {- let s = src.get(xx, yy, c);- *v += if ctx.alpha && c < n - 1 { s * a } else { s };+ if n == 4+ && let Some((sum, samples)) = mosaic_sum4(src, r, ctx.alpha)+ {+ acc.copy_from_slice(&sum);+ count = samples;+ } else {+ for yy in r.y0..r.y1 {+ for xx in r.x0..r.x1 {+ let a = if ctx.alpha { src.get(xx, yy, n - 1) } else { 1.0 };+ for (c, v) in acc.iter_mut().enumerate() {+ let s = src.get(xx, yy, c);+ *v += if ctx.alpha && c < n - 1 { s * a } else { s };+ }+ count += 1.0;}- count += 1.0;}}if count > 0.0 {@@ -110,6 +117,32 @@ pub(crate) fn mosaic(src: &Image, out: Rect, ctx: &Ctx, cell: f32) -> Vec<f32> {res}+/// Read each four-channel pixel once, keeping independent channel sums in registers.+/// Preserve the original row/pixel addition order and alpha arithmetic.+fn mosaic_sum4(src: &Image, r: Rect, alpha: bool) -> Option<([f32; 4], f32)> {+ if r.intersect(&src.rect) != r {+ return None;+ }+ let stride = (src.rect.width() as usize).checked_mul(4)?;+ let offset = (r.x0.abs_diff(src.rect.x0) as usize).checked_mul(4)?;+ let width = (r.width() as usize).checked_mul(4)?;+ let mut sum = [0.0; 4];+ let mut count = 0.0;+ for y in r.y0..r.y1 {+ let start = (y.abs_diff(src.rect.y0) as usize).checked_mul(stride)?.checked_add(offset)?;+ let row = src.data.get(start..start.checked_add(width)?)?;+ for pixel in row.as_chunks::<4>().0 {+ let a = if alpha { pixel[3] } else { 1.0 };+ for c in 0..3 {+ sum[c] += if alpha { pixel[c] * a } else { pixel[c] };+ }+ sum[3] += pixel[3];+ count += 1.0;+ }+ }+ Some((sum, count))+}+/// Emboss: gray relief from luminance differences along the light angle.pub(crate) fn emboss(src: &Image, out: Rect, ctx: &Ctx, angle: f32, height: f32, amount: f32) -> Vec<f32> {let n = src.ch;diff --git a/crates/algo/src/tests_mosaic.rs b/crates/algo/src/tests_mosaic.rsindex a739d59c3..24c964752 100644--- a/crates/algo/src/tests_mosaic.rs+++ b/crates/algo/src/tests_mosaic.rs@@ -101,7 +101,7 @@ fn mosaic_preserves_depth_color_alpha_and_tile_independence() {input.write_region(b, &pattern.data);let src = Image::read(&input, b);let ctx = Ctx { bounds: b, mode, alpha };- for cell_size in [6.0, 50.0, 180.0] {+ for cell_size in [6.0, 13.0, 50.0, 180.0] {let params = FilterParams::Mosaic { cell_size };let mut expected = Surface::new(fmt);expected.write_region(b, &legacy_mosaic(&src, b, &ctx, cell_size));@@ -132,6 +132,29 @@ fn mosaic_handles_zero_channels() {}}+#[test]+fn mosaic_row_reads_preserve_hdr_alpha_and_partial_source_bits() {+ let bounds = Rect::new(-7, 3, 30, 26);+ for rect in [bounds, Rect::new(-3, 7, 23, 21)] {+ let mut src = image_pattern(rect, 4);+ for (i, pixel) in src.data.as_chunks_mut::<4>().0.iter_mut().enumerate() {+ pixel[0] = pixel[0] * 8.0 - 4.0;+ pixel[1] = if i.is_multiple_of(3) { -0.0 } else { pixel[1] * 16.0 };+ pixel[2] = if i.is_multiple_of(7) { f32::MIN_POSITIVE } else { -pixel[2] };+ pixel[3] = if i.is_multiple_of(5) { 0.0 } else { pixel[3] };+ }+ for alpha in [false, true] {+ let ctx = Ctx { bounds, mode: ColorMode::Rgb, alpha };+ for cell in [2.0, 4.0, 10.0, 50.0, 180.0] {+ let expected = legacy_mosaic(&src, bounds, &ctx, cell);+ let got = stylize::mosaic(&src, bounds, &ctx, cell);+ assert_eq!(got.len(), expected.len());+ assert!(got.iter().zip(&expected).all(|(a, b)| a.to_bits() == b.to_bits()));+ }+ }+ }+}+#[test]#[ignore = "24 MP release performance comparison; run explicitly with --release --ignored --nocapture"]fn mosaic_24mp_release_comparison() {