PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Image › Adjustments: apply per tile in parallel (#1800)
kayrockscreenprinting54ff207004f9
diff --git a/crates/engine/src/pixels.rs b/crates/engine/src/pixels.rsindex a73737131..1bf1face4 100644--- a/crates/engine/src/pixels.rs+++ b/crates/engine/src/pixels.rs@@ -8,15 +8,35 @@ use photocraft_raster::{Surface, from_rgba, to_rgba};/// Apply an adjustment destructively to a surface, weighted by an optional selection./// Applies `adj` to a surface (any colour model and depth, via straight RGBA) through the/// selection; `mode` is the document's, for the tone transfer (e.g. Exposure in Grayscale).+/// Works tile by tile in parallel, so a large layer needs one tile's buffers per thread rather+/// than four full-layer copies, and the result is the same as one pass over the whole region.pub fn adjust_surface(s: &mut Surface, adj: &Adjustment, selection: Option<&Surface>, mode: photocraft_color::ColorMode) {+ use rayon::prelude::*;let r = s.content_bounds();if r.is_empty() {return;}- let out = adjusted(s, r, adj, selection, mode);- s.write_region(r, &out);+ let fmt = s.format();+ let done: Vec<_> = s+ .take_tiles(r)+ .into_par_iter()+ .flat_map_iter(|(tc, tile)| {+ // A one-tile surface, so `adjusted` reads and writes this tile only.+ let mut one = Surface::new(fmt);+ one.put_tiles([(tc, tile)]);+ let tr = tc.rect().intersect(&r);+ let out = adjusted(&one, tr, adj, selection, mode);+ one.write_region(tr, &out);+ one.take_tiles(tc.rect())+ })+ .collect();+ s.put_tiles(done);}+#[cfg(test)]+#[path = "pixels_tests.rs"]+mod tests;+/// Applies `adj` to a layer mask through the selection. Untouched mask pixels count (they read as/// the mask's default value), and without a selection the default changes too, since a mask/// reaches past the canvas: Invert turns a reveal-all mask into a hide-all one (#780).diff --git a/crates/engine/src/pixels_tests.rs b/crates/engine/src/pixels_tests.rsnew file mode 100644index 000000000..a58bd4c1a--- /dev/null+++ b/crates/engine/src/pixels_tests.rs@@ -0,0 +1,88 @@+use photocraft_color::{ColorMode, PixelFormat, SampleType};+use serde_json::json;++use super::*;++/// The single pass over the content bounds that the tiled `adjust_surface` replaced (#1772).+fn one_pass(s: &Surface, adj: &Adjustment, selection: Option<&Surface>, mode: ColorMode) -> Surface {+ let mut s = s.clone();+ let r = s.content_bounds();+ if !r.is_empty() {+ let out = adjusted(&s, r, adj, selection, mode);+ s.write_region(r, &out);+ }+ s+}++/// Two patches over a 3×3 tile grid, not tile-aligned, with partial alpha; the bottom-left tiles+/// inside the content bounds are never allocated.+fn layer(fmt: PixelFormat) -> Surface {+ let mut s = Surface::new(fmt);+ for r in [Rect::new(200, 240, 300, 300), Rect::new(530, 250, 560, 530)] {+ let mut data = Vec::new();+ for y in r.y0..r.y1 {+ for x in r.x0..r.x1 {+ let a = if fmt.alpha { 0.3 + 0.7 * ((x + y) % 7) as f32 / 6.0 } else { 1.0 };+ data.extend(from_rgba(&fmt, [x as f32 / 600.0, y as f32 / 540.0, 0.5 + 0.4 * (x as f32 * 0.05).sin(), a]));+ }+ }+ s.write_region(r, &data);+ }+ assert!(!s.has_tiles_in(Rect::new(200, 512, 256, 530)), "a hole inside the content bounds");+ s+}++#[test]+fn tiled_adjustment_matches_one_pass_over_the_content() {+ let adjustments = [+ ("invert", json!({})),+ ("levels", json!({"inBlack": 20, "gamma": 1.3, "inWhite": 230})),+ ("curves", json!({"points": [[0, 10], [128, 160], [255, 240]]})),+ ("hueSaturation", json!({"hue": 40, "saturation": 20})),+ ("brightnessContrast", json!({"brightness": 30, "contrast": 20})),+ ("exposure", json!({"exposure": 0.8})),+ ("posterize", json!({"levels": 5})),+ ("threshold", json!({"level": 120})),+ ("colorBalance", json!({"midtones": [20, -10, 5]})),+ ("blackWhite", json!({"tint": true})),+ ("photoFilter", json!({"density": 50})),+ ("channelMixer", json!({"red": [80, 30, -10, 5]})),+ ("vibrance", json!({"vibrance": 40})),+ ("selectiveColor", json!({"reds": [10, -20, 30, 5]})),+ // Dithered kinds vary by position, so they show a tile's pixels kept their coordinates.+ ("gradientMap", json!({"stops": [[0, "#102030"], [1, "#f0e0a0"]], "dither": true})),+ ("colorLookup", json!({"lut": "warm", "dither": true})),+ ];+ let mut formats = Vec::new();+ for mode in [ColorMode::Rgb, ColorMode::Cmyk, ColorMode::Lab, ColorMode::Grayscale] {+ for sample in [SampleType::U8, SampleType::U16, SampleType::F32] {+ formats.push(PixelFormat { mode, sample, alpha: true });+ }+ }+ formats.push(PixelFormat { mode: ColorMode::Rgb, sample: SampleType::U8, alpha: false });+ formats.push(PixelFormat { mode: ColorMode::Grayscale, sample: SampleType::U16, alpha: false });+ // A soft selection that ends inside the content.+ let mut soft = Surface::new(PixelFormat::GRAY8);+ let sr = Rect::new(0, 0, 500, 400);+ soft.write_region(sr, &(0..sr.height()).flat_map(|_| (0..sr.width()).map(|x| x as f32 / 500.0)).collect::<Vec<_>>());+ for fmt in formats {+ let s = layer(fmt);+ for (kind, p) in &adjustments {+ let adj = crate::adjust_params::from_params(kind, p, None, fmt.mode).unwrap_or_else(|e| panic!("{kind}: {e}"));+ for sel in [None, Some(&soft)] {+ let want = one_pass(&s, &adj, sel, fmt.mode);+ let mut got = s.clone();+ adjust_surface(&mut got, &adj, sel, fmt.mode);+ assert!(want != s, "{kind} {fmt:?}: the adjustment changes the layer");+ assert!(got == want, "{kind} {fmt:?} selection {}: tiled result differs", sel.is_some());+ }+ }+ }+}++#[test]+fn an_empty_layer_is_left_alone() {+ let mut s = Surface::new(PixelFormat::RGBA8);+ adjust_surface(&mut s, &Adjustment::Invert, None, ColorMode::Rgb);+ assert_eq!(s.tile_count(), 0);+}diff --git a/crates/ui-egui/examples/large_image_bench.rs b/crates/ui-egui/examples/large_image_bench.rsindex 5ad50da16..5f03d8112 100644--- a/crates/ui-egui/examples/large_image_bench.rs+++ b/crates/ui-egui/examples/large_image_bench.rs@@ -175,6 +175,21 @@ fn main() {println!("Gaussian Blur r 10 {t1:>9.0} ms");println!(" refresh ({path:<8}) {:>9.0} ms", ms(t));}+ "invert" => {+ // Image › Adjustments on the whole layer (#1772), then on a half-transparent layer.+ let t = Instant::now();+ exec(&mut s, "image.adjustments.invert", json!({}));+ let t1 = ms(t);+ let t = Instant::now();+ let path = refresh(gpu, &s, false);+ println!("Invert {t1:>9.0} ms refresh ({path}) {:>6.0} ms", ms(t));+ exec(&mut s, "select.rect", json!({"x": 0, "y": 0, "width": w / 2, "height": h}));+ exec(&mut s, "layer.new.layerViaCopy", json!({}));+ let t = Instant::now();+ exec(&mut s, "image.adjustments.invert", json!({}));+ println!("Invert, half-transparent layer {:>8.0} ms", ms(t));+ json_rows.push(photocraft_testkit::perf::row("invert", &[t1], None, None));+ }"brush" => {exec(&mut s, "layer.new.layer", json!({"name": "paint"}));refresh(gpu, &s, true);