Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

perf(engine): read selection coverage per row when mixing edits back (#4424)

SckraT1e9f1ae48622
diff --git a/crates/engine/src/adjust_cmds.rs b/crates/engine/src/adjust_cmds.rs
index 48f6e5624..15c3af206 100644
--- a/crates/engine/src/adjust_cmds.rs
+++ b/crates/engine/src/adjust_cmds.rs
@@ -11,7 +11,7 @@
use photocraft_algo::tone::{self, HdrToning, MatchColor, ShadowsHighlights};
use photocraft_doc::{Adjustment, Document, Layer, LayerContent, Rect};
-use photocraft_raster::{Surface, from_rgba_into, to_rgba};
+use photocraft_raster::{Surface, to_rgba};
use serde_json::{Value, json};
use crate::commands::CommandSpec;
@@ -202,17 +202,8 @@ fn process_surface(surf: &mut Surface, selection: Option<&Surface>, f: &mut dyn
let raw = surf.read_region(r);
let mut px: Vec<[f32; 4]> = raw.chunks_exact(n).map(|q| to_rgba(&fmt, q)).collect();
let orig = px.clone();
- let w = r.width() as usize;
f(&mut px, r);
- let mut out = Vec::with_capacity(raw.len());
- for (i, (a, o)) in px.iter().zip(&orig).enumerate() {
- let k = selection.map_or(1.0, |sel| sel.sample_channel(r.x0 + (i % w) as i32, r.y0 + (i / w) as i32, 0));
- let mixed: [f32; 4] = std::array::from_fn(|c| o[c] + (a[c] - o[c]) * k);
- let mut enc = [0.0f32; 8];
- let m = from_rgba_into(&fmt, mixed, &mut enc);
- out.extend_from_slice(&enc[..m]);
- }
- surf.write_region(r, &out);
+ surf.write_region(r, &crate::pixels::mix_through_selection(&fmt, r, &orig, &px, selection));
}
/// One history step running `f` on the active pixel layer (or targeted channel or layer mask).
diff --git a/crates/engine/src/pixels.rs b/crates/engine/src/pixels.rs
index b19a1b270..f176315ca 100644
--- a/crates/engine/src/pixels.rs
+++ b/crates/engine/src/pixels.rs
@@ -99,14 +99,33 @@ fn adjusted(s: &Surface, r: Rect, adj: &Adjustment, selection: Option<&Surface>,
// integer depths keep the unrounded, clipped curves.
let depth = (fmt.sample == photocraft_color::SampleType::F32).then_some(fmt.sample);
adjust::apply_in(adj, &mut buf, adjust::Transfer::for_document(mode, fmt.sample), depth, mode);
- let w = r.width() as usize;
- let mut out = Vec::with_capacity(raw.len());
- for (i, (a, o)) in buf.px.iter().zip(&orig.px).enumerate() {
- let k = selection.map_or(1.0, |sel| sel.sample_channel(r.x0 + (i % w) as i32, r.y0 + (i / w) as i32, 0));
- let mixed: [f32; 4] = std::array::from_fn(|c| o[c] + (a[c] - o[c]) * k);
- let mut enc = [0.0f32; 8];
- let m = photocraft_raster::from_rgba_into(&fmt, mixed, &mut enc);
- out.extend_from_slice(&enc[..m]);
+ mix_through_selection(&fmt, r, &orig.px, &buf.px, selection)
+}
+
+/// `edited` mixed back over `orig` (straight RGBA over `r`, row-major) by the selection's
+/// coverage, encoded in `fmt`: what a destructive edit writes back through a selection.
+pub(crate) fn mix_through_selection(
+ fmt: &photocraft_color::PixelFormat,
+ r: Rect,
+ orig: &[[f32; 4]],
+ edited: &[[f32; 4]],
+ selection: Option<&Surface>,
+) -> Vec<f32> {
+ let w = (r.width() as usize).max(1);
+ let mut out = Vec::with_capacity(orig.len() * fmt.channels());
+ // The selection's coverage (channel 0) one row at a time, each of its tiles looked up once
+ // per row rather than once per pixel.
+ let mut coverage = vec![1.0f32; w];
+ for (j, (edited_row, orig_row)) in edited.chunks(w).zip(orig.chunks(w)).enumerate() {
+ if let Some(sel) = selection {
+ sel.sample_row_strided(r.y0.saturating_add(j as i32), r.x0, 1, 0, &mut coverage);
+ }
+ for ((a, o), k) in edited_row.iter().zip(orig_row).zip(&coverage) {
+ let mixed: [f32; 4] = std::array::from_fn(|c| o[c] + (a[c] - o[c]) * k);
+ let mut enc = [0.0f32; 8];
+ let m = photocraft_raster::from_rgba_into(fmt, mixed, &mut enc);
+ out.extend_from_slice(&enc[..m]);
+ }
}
out
}
diff --git a/crates/engine/src/pixels_tests.rs b/crates/engine/src/pixels_tests.rs
index c0dc368e4..4a9e577a3 100644
--- a/crates/engine/src/pixels_tests.rs
+++ b/crates/engine/src/pixels_tests.rs
@@ -259,3 +259,40 @@ fn invert_in_a_lab_document_mirrors_l_a_and_b() {
assert!(got.iter().zip([70.0, -13.0, 17.0]).all(|(g, w)| (g - w).abs() < 1.0), "{depth:?}: {got:?}");
}
}
+
+#[test]
+fn mixing_through_a_selection_matches_sampling_it_per_pixel() {
+ // A rectangle across tile edges (negative x included), a selection with fractional
+ // coverage, untouched tiles (default coverage) and a 16-bit as well as an 8-bit layer.
+ let r = Rect::new(-300, 200, 330, 290);
+ let gray32 = PixelFormat { mode: ColorMode::Grayscale, sample: SampleType::F32, alpha: false };
+ for sel_fmt in [PixelFormat::GRAY8, PixelFormat::GRAYA8, gray32] {
+ let mut sel = Surface::new(sel_fmt);
+ sel.fill_rect(Rect::new(-260, 210, 40, 280), &vec![0.6; sel_fmt.channels()]);
+ for x in 0..120 {
+ sel.write_pixel(x * 2, 230 + x % 40, &vec![x as f32 / 119.0; sel_fmt.channels()]);
+ }
+ for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16] {
+ let n = (r.width() * r.height()) as usize;
+ let orig: Vec<[f32; 4]> = (0..n).map(|i| [(i % 7) as f32 / 7.0, 0.5, (i % 13) as f32 / 13.0, 0.25 + (i % 3) as f32 / 4.0]).collect();
+ let edited: Vec<[f32; 4]> = orig.iter().map(|p| [1.0 - p[0], p[1] * 0.5, p[2], 1.0]).collect();
+ let w = r.width() as usize;
+ let mut expected = Vec::new();
+ for (i, (a, o)) in edited.iter().zip(&orig).enumerate() {
+ let k = sel.sample_channel(r.x0 + (i % w) as i32, r.y0 + (i / w) as i32, 0);
+ let mixed: [f32; 4] = std::array::from_fn(|c| o[c] + (a[c] - o[c]) * k);
+ let mut enc = [0.0f32; 8];
+ let m = photocraft_raster::from_rgba_into(&fmt, mixed, &mut enc);
+ expected.extend_from_slice(&enc[..m]);
+ }
+ assert_eq!(mix_through_selection(&fmt, r, &orig, &edited, Some(&sel)), expected, "{sel_fmt:?} over {fmt:?}");
+ }
+ }
+ // Without a selection the edit is taken whole; an empty rectangle mixes nothing.
+ let px = [[0.1, 0.2, 0.3, 1.0]];
+ let fmt = PixelFormat::RGBA8;
+ let mut enc = [0.0f32; 8];
+ let m = photocraft_raster::from_rgba_into(&fmt, px[0], &mut enc);
+ assert_eq!(mix_through_selection(&fmt, Rect::new(0, 0, 1, 1), &[[0.0; 4]], &px, None), enc[..m].to_vec());
+ assert!(mix_through_selection(&fmt, Rect::EMPTY, &[], &[], None).is_empty());
+}