Skip to content

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

Merge origin/main (up to a1e19a0b) into feat/psd-render-smart-filters

Matthias Schlichc6fff8a3a82e
diff --cc crates/engine/src/smart_cmds.rs
index 4f9c8c148,6ffc89e67..e9a2d3754
--- a/crates/engine/src/smart_cmds.rs
+++ b/crates/engine/src/smart_cmds.rs
@@@ -307,34 -309,24 +309,39 @@@ pub fn apply_smart_filters(placed: &Sur
// Edge pixels repeat at the canvas ∪ placed-content edge, as for layer filters.
let extent = canvas.union(&placed.content_bounds());
for f in sm.smart_filters.iter().filter(|f| f.visible) {
- cur = apply_smart_filter(f, &cur, filter_bounds(f, placed, canvas), extent);
- cur = apply_smart_filter(f, &cur, canvas, extent)?;
++ cur = apply_smart_filter(f, &cur, filter_bounds(f, placed, canvas), extent)?;
}
- match sm.filter_mask.as_ref().filter(|m| m.enabled) {
+ Ok(match sm.filter_mask.as_ref().filter(|m| m.enabled) {
Some(m) => photocraft_compose::filter_mask::mix(placed, &cur, m),
None => cur,
- }
+ })
}
+/// The reference bounds of smart filter `f`. Render filters work within the smart object, as
+/// Photoshop's do: Lens Flare's centre is a fraction of the object, and Clouds and Fibers fill it
+/// and size their pattern by it. The object is its placed content's bounds (which leaves out
+/// fully transparent edges of the source); the other filters use the canvas, as on a layer.
+fn filter_bounds(f: &SmartFilter, placed: &Surface, canvas: Rect) -> Rect {
+ match placed.content_bounds() {
+ b if renders_in_object(f) && !b.is_empty() => b,
+ _ => canvas,
+ }
+}
+
+fn renders_in_object(f: &SmartFilter) -> bool {
+ matches!(f.command.as_str(), "filter.render.clouds" | "filter.render.differenceClouds" | "filter.render.fibers" | "filter.render.lensFlare")
+}
+
- /// One visible smart filter over `cur`, blended with its blend mode and opacity.
- fn apply_smart_filter(f: &SmartFilter, cur: &Surface, canvas: Rect, extent: Rect) -> Surface {
- // Unknown ids (e.g. Photoshop filters we don't implement) leave the pixels alone.
- let Some(out) = crate::filters::apply_filter_to_surface(&f.command, &f.params, cur, canvas, None, extent) else { return cur.clone() };
- if f.blend == BlendMode::Normal && f.opacity >= 1.0 { out } else { blend_surfaces(cur, &out, f.blend, f.opacity.clamp(0.0, 1.0)) }
+ /// One visible smart filter over `cur`, blended with its blend mode and opacity. `Err` when the
+ /// requesting session's authority denies the filter's params: callers surface the error instead of
+ /// rendering (and caching) unfiltered pixels.
+ fn apply_smart_filter(f: &SmartFilter, cur: &Surface, canvas: Rect, extent: Rect) -> Result<Surface> {
+ let out = match crate::filters::apply_filter_to_surface(&f.command, &f.params, cur, canvas, None, extent)? {
+ // Unknown ids (e.g. Photoshop filters we don't implement) leave the pixels alone.
+ Some(out) => out,
+ None => cur.clone(),
+ };
+ if f.blend == BlendMode::Normal && f.opacity >= 1.0 { Ok(out) } else { Ok(blend_surfaces(cur, &out, f.blend, f.opacity.clamp(0.0, 1.0))) }
}
/// The source placed in document pixels, before any smart filter.
@@@ -425,8 -422,7 +437,8 @@@ fn render_in(doc: &Document, sm: &Smart
for (i, (f, key)) in filters.iter().zip(&keys).enumerate().skip(done) {
ctx.check()?;
ctx.progress(i as f32 / n, "");
- cur = Stage { surface: apply_smart_filter(f, &cur.surface, canvas, cur.extent)?, extent: cur.extent };
+ let bounds = if renders_in_object(f) { filter_bounds(f, &placed()?.surface, canvas) } else { canvas };
- cur = Stage { surface: apply_smart_filter(f, &cur.surface, bounds, cur.extent), extent: cur.extent };
++ cur = Stage { surface: apply_smart_filter(f, &cur.surface, bounds, cur.extent)?, extent: cur.extent };
stack_cache::put(*key, &cur);
}
ctx.progress(1.0, "");