Skip to content

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

fix(engine): retain Smart Filter coverage around blur edges

abc2cb1515cafae491
diff --git a/crates/engine/src/smart_cmds.rs b/crates/engine/src/smart_cmds.rs
index d80596a68..366011daf 100644
--- a/crates/engine/src/smart_cmds.rs
+++ b/crates/engine/src/smart_cmds.rs
@@ -291,10 +291,21 @@ fn blend_surfaces(base: &Surface, top: &Surface, mode: BlendMode, opacity: f32)
}
let n = fmt.channels();
let (b, t) = (base.read_region(area), top.read_region(area));
+ // Alpha-preserving filters edit the same coverage. A non-Normal filter that
+ // changes coverage (e.g. blur) must also retain input-only pixels and admit
+ // filtered-only pixels, as Photoshop's Smart Filter blend oracles require.
+ // Classify the whole result so unchanged pixels do not switch blend rules
+ // independently at the edge of a blur.
+ let composite_coverage = mode != BlendMode::Normal && fmt.alpha && b.chunks_exact(n).zip(t.chunks_exact(n)).any(|(bp, tp)| bp.last() != tp.last());
let mut o = vec![0.0f32; b.len()];
for ((bp, tp), op) in b.chunks_exact(n).zip(t.chunks_exact(n)).zip(o.chunks_exact_mut(n)) {
let b = photocraft_raster::to_rgba(&fmt, bp);
let t = photocraft_raster::to_rgba(&fmt, tp);
+ if composite_coverage {
+ let rgba = photocraft_color::blend::composite(mode, b, t, opacity);
+ photocraft_raster::from_rgba_into(&fmt, rgba, op);
+ continue;
+ }
let cb = [b[0], b[1], b[2]];
let ct = [t[0], t[1], t[2]];
let color = if fmt.sample == photocraft_color::SampleType::F32 {
@@ -304,7 +315,7 @@ fn blend_surfaces(base: &Surface, top: &Surface, mode: BlendMode, opacity: f32)
};
// A filter edits this surface, rather than adding a second source-over layer. Mix
// premultiplied results so alpha-preserving filters keep coverage at every opacity,
- // while filters that actually change coverage (e.g. blur) interpolate it too.
+ // while Normal-mode filters that change coverage interpolate it too.
let a = b[3] + (t[3] - b[3]) * opacity;
let mut rgba = [0.0, 0.0, 0.0, a];
if a > 0.0 {
diff --git a/crates/engine/src/smart_cmds/tests.rs b/crates/engine/src/smart_cmds/tests.rs
index b496e0451..55d5fa964 100644
--- a/crates/engine/src/smart_cmds/tests.rs
+++ b/crates/engine/src/smart_cmds/tests.rs
@@ -23,7 +23,7 @@ fn filter_strength_preserves_alpha_and_interpolates_changed_coverage() {
photocraft_color::SampleType::F32 => 1e-6,
};
for opacity in [0.0, 0.01, 0.5, 1.0] {
- for mode in [BlendMode::Normal, BlendMode::Multiply, BlendMode::Luminosity] {
+ for mode in [BlendMode::Normal, BlendMode::Multiply, BlendMode::Screen, BlendMode::Luminosity] {
let mixed = blend_surfaces(&base, &filtered, mode, opacity).pixel(0, 0);
assert!((mixed[3] - before[3]).abs() <= tolerance);
if mode == BlendMode::Normal {
@@ -42,6 +42,39 @@ fn filter_strength_preserves_alpha_and_interpolates_changed_coverage() {
}
}
+#[test]
+fn non_normal_coverage_changes_retain_source_over_semantics() {
+ for sample in photocraft_color::SampleType::ALL {
+ let fmt = PixelFormat { mode: photocraft_color::ColorMode::Rgb, sample, alpha: true };
+ let mut base = Surface::new(fmt);
+ let mut filtered = Surface::new(fmt);
+ // Input-only, output-only, opaque input with blurred output, and a
+ // translucent pixel whose alpha happens to be unchanged by this filter.
+ for (x, (ba, ta)) in [(0.5, 0.0), (0.0, 0.5), (1.0, 0.25), (0.5, 0.5)].into_iter().enumerate() {
+ let x = x as i32;
+ base.fill_rect(Rect::new(x, 0, 1, 1), &[0.2, 0.4, 0.6, ba]);
+ filtered.fill_rect(Rect::new(x, 0, 1, 1), &[0.8, 0.6, 0.4, ta]);
+ }
+ for mode in [BlendMode::Screen, BlendMode::Multiply, BlendMode::Luminosity] {
+ for opacity in [0.0, 0.01, 0.5, 1.0] {
+ let mixed = blend_surfaces(&base, &filtered, mode, opacity);
+ for x in 0..4 {
+ if opacity == 0.0 {
+ assert_eq!(mixed.pixel(x, 0), base.pixel(x, 0));
+ continue;
+ }
+ let b = photocraft_raster::to_rgba(&fmt, &base.pixel(x, 0));
+ let t = photocraft_raster::to_rgba(&fmt, &filtered.pixel(x, 0));
+ let expected = photocraft_color::blend::composite(mode, b, t, opacity);
+ let mut reference = Surface::new(fmt);
+ reference.fill_rect(Rect::new(x, 0, 1, 1), &expected);
+ assert_eq!(mixed.pixel(x, 0), reference.pixel(x, 0), "{sample:?} {mode:?} {opacity} x={x}");
+ }
+ }
+ }
+ }
+}
+
fn session(depth: u64) -> Session {
let mut s = Session::new();
s.execute("file.new", json!({"width": W, "height": H, "depth": depth})).unwrap();
diff --git a/crates/engine/tests/photoshop_oracles.rs b/crates/engine/tests/photoshop_oracles.rs
index 2edd13b2b..e07b451ef 100644
--- a/crates/engine/tests/photoshop_oracles.rs
+++ b/crates/engine/tests/photoshop_oracles.rs
@@ -80,6 +80,18 @@ fn rerender(doc: &mut Document) -> Result<usize, String> {
Ok(n)
}
+#[test]
+fn screen_motion_blur_retains_input_coverage() {
+ let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/photoshop/smart-filters/sf-blend-screen-100.psd");
+ let bytes = std::fs::read(&path).expect("Screen/Motion Blur oracle missing: run cargo xtask corpus --all");
+ let file = PsdFile::from_bytes(&bytes).expect("parse Screen/Motion Blur oracle");
+ let mut doc = photocraft_io::import("sf-blend-screen-100.psd", &bytes).expect("import Screen/Motion Blur oracle").document;
+ assert!(rerender(&mut doc).expect("re-render Screen/Motion Blur oracle") > 0);
+ let merged = photocraft_io::merged_composite(&file).expect("Photoshop merged composite");
+ let (error, bad) = max_diff(&photocraft_compose::flatten(&doc).px, &merged);
+ assert!(error <= PASS_TOL, "Screen/Motion Blur error {error}, {bad}% pixels exceed 2/255");
+}
+
#[test]
fn photoshop_oracles_rerendered() {
let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/photoshop");
diff --git a/docs/smart-filters.md b/docs/smart-filters.md
index 74c32ea46..278c98996 100644
--- a/docs/smart-filters.md
+++ b/docs/smart-filters.md
@@ -46,6 +46,10 @@ large-image performance budgets.
Filter opacity interpolates premultiplied input and filtered results, so
alpha-preserving filters retain translucent coverage at partial strength. This
-is scoped to Smart Filters; global layer compositing is unchanged. Dissolve
+also applies to Normal mode when a filter changes coverage. Non-Normal modes
+use source-over compositing when the filtered surface changes alpha, preserving
+input-only and filtered-only pixels around blur edges. The choice applies to
+the whole filtered surface. This is scoped to Smart Filters; global layer
+compositing is unchanged. Dissolve
currently follows the inherited Smart Filter color-blending path without
per-pixel coverage sampling; exact Dissolve parity remains a separate task.