Skip to content

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

keep the rendering of smart filters that cannot be reproduced

Omar Saidahmed6822125f7916
diff --git a/crates/engine/src/smart_cmds.rs b/crates/engine/src/smart_cmds.rs
index 6ffc89e67..1c1f2a787 100644
--- a/crates/engine/src/smart_cmds.rs
+++ b/crates/engine/src/smart_cmds.rs
@@ -453,11 +453,22 @@ pub fn render_below_filter(doc: &Document, sm: &SmartObject, index: usize) -> Re
}
/// Re-renders smart layer `l` (which lives in `doc`) in place. Returns false if its source is
-/// unavailable (the cache is then left alone).
+/// unavailable or a visible filter can't be applied (the cache is then left alone).
pub fn refresh_layer(doc: &Document, l: &mut Layer) -> Result<bool> {
+ if matches!(&l.content, LayerContent::Smart(sm) if !unapplied_filters(sm).is_empty()) {
+ return Ok(false);
+ }
refresh_layer_in(doc, l, &JobCtx::new())
}
+fn unapplied_filters(sm: &SmartObject) -> Vec<String> {
+ sm.smart_filters
+ .iter()
+ .filter(|f| sm.filters_enabled && f.visible && crate::commands::find(&f.command).is_none())
+ .map(|f| f.params.get("name").and_then(Value::as_str).unwrap_or(&f.command).to_string())
+ .collect()
+}
+
fn refresh_layer_in(doc: &Document, l: &mut Layer, ctx: &JobCtx) -> Result<bool> {
let LayerContent::Smart(sm) = &mut l.content else { return Ok(false) };
match render_in(doc, sm, ctx)? {
@@ -805,24 +816,27 @@ fn set_source(s: &mut Session, p: &Value, label: &str, keep_psd: bool, make: imp
// Keep the shared-instance transaction out of the per-command source-builder instantiations.
fn set_shared_source(s: &mut Session, id: LayerId, label: &str, keep_psd: bool, new: SmartSource) -> Result<Value> {
- let (parent, contents_id, changed) = s.edit(label, |doc, _| {
+ let (parent, contents_id, changed, warnings) = s.edit(label, |doc, _| {
let contents_id = smart(doc, id)?.contents_id;
let changed = instances(doc, contents_id);
+ let mut warnings = std::collections::BTreeSet::new();
for &instance in &changed {
smart_mut(doc, instance)?.source = new.clone();
if !keep_psd {
detach_psd(doc.layer_mut(instance).ok_or(EngineError::NoLayer(instance))?);
}
+ warnings
+ .extend(unapplied_filters(smart(doc, instance)?).into_iter().map(|n| format!("the {n} smart filter is not supported, so it was not applied")));
refresh_or_fail(doc, instance)?;
}
- Ok((doc.id, contents_id, changed))
+ Ok((doc.id, contents_id, changed, warnings))
})?;
// An old editor must not overwrite newly replaced/relinked contents on close. Keep its
// document open, but the next Edit Contents opens the replacement source.
s.smart_links.retain(|l| l.parent != parent || l.contents_id != contents_id);
// Every instance changed, not only `layer`: list them, so a caller doesn't repeat the
// command once per copy (#4075).
- Ok(json!({"layer": id.0, "instances": changed.iter().map(|l| l.0).collect::<Vec<_>>()}))
+ Ok(json!({"layer": id.0, "instances": changed.iter().map(|l| l.0).collect::<Vec<_>>(), "warnings": warnings}))
}
fn replace_contents(s: &mut Session, p: &Value) -> Result<Value> {
diff --git a/crates/engine/tests/unsupported_smart_filters.rs b/crates/engine/tests/unsupported_smart_filters.rs
new file mode 100644
index 000000000..8861fea4d
--- /dev/null
+++ b/crates/engine/tests/unsupported_smart_filters.rs
@@ -0,0 +1,85 @@
+use photocraft_doc::{LayerContent, LayerId, SmartFilter};
+use photocraft_engine::{Session, smart_cmds};
+use serde_json::json;
+
+const STORED: [f32; 4] = [0.0, 0.0, 1.0, 1.0];
+
+fn mockup() -> (Session, LayerId) {
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 40, "height": 30})).unwrap();
+ s.execute("layer.new.layer", json!({})).unwrap();
+ s.execute("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap();
+ s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap();
+ let id = s.active().unwrap().active_layer.unwrap();
+ s.edit("imported filter", |doc, _| {
+ let LayerContent::Smart(sm) = &mut doc.layer_mut(id).unwrap().content else { panic!("not a smart object") };
+ sm.smart_filters.push(SmartFilter {
+ command: "psd.unsupportedFilter".into(),
+ params: json!({"name": "Sharpen", "filterID": 1399353968, "psd": ""}),
+ blend: photocraft_color::BlendMode::Normal,
+ opacity: 1.0,
+ visible: true,
+ });
+ let c = sm.cache.as_mut().unwrap();
+ let r = c.content_bounds();
+ c.fill_rect(r, &STORED);
+ Ok(())
+ })
+ .unwrap();
+ (s, id)
+}
+
+fn cache(s: &Session, id: LayerId) -> photocraft_raster::Surface {
+ let LayerContent::Smart(sm) = &s.active().unwrap().doc.layer(id).unwrap().content else { panic!("not a smart object") };
+ sm.cache.clone().unwrap()
+}
+
+fn png() -> Vec<u8> {
+ let image = photocraft_codecs::Image::from_u8(8, 8, photocraft_codecs::ChannelLayout::Rgb, vec![0; 8 * 8 * 3]).unwrap();
+ photocraft_codecs::encode(&image, photocraft_codecs::Format::Png, &Default::default()).unwrap()
+}
+
+#[test]
+fn geometry_edits_keep_the_stored_render_of_an_unsupported_filter() {
+ for (cmd, params) in [("edit.transform", json!({"scaleX": 50, "scaleY": 50})), ("image.imageSize", json!({"width": 20, "height": 15}))] {
+ let (mut s, id) = mockup();
+ let before = cache(&s, id).content_bounds();
+ s.execute(cmd, params).unwrap();
+ let after = cache(&s, id);
+ let r = after.content_bounds();
+ assert!(r.width() < before.width(), "{cmd} resized the stored render");
+ assert_eq!(after.rgba((r.x0 + r.x1) / 2, (r.y0 + r.y1) / 2), STORED, "{cmd} kept the stored render");
+ }
+}
+
+#[test]
+fn replacing_contents_reports_the_filters_it_cannot_apply() {
+ let (mut s, id) = mockup();
+ let r = smart_cmds::replace_contents_bytes(&mut s, id, "new.png", png()).unwrap();
+ let warnings = r["warnings"].as_array().unwrap();
+ assert_eq!(warnings.len(), 1, "{r}");
+ assert!(warnings[0].as_str().unwrap().contains("Sharpen"), "{r}");
+}
+
+#[test]
+fn supported_filters_still_re_render_after_a_transform() {
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 40, "height": 30})).unwrap();
+ s.execute("layer.new.layer", json!({})).unwrap();
+ s.execute("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap();
+ s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap();
+ s.execute("filter.blur.gaussianBlur", json!({"radius": 2})).unwrap();
+ let id = s.active().unwrap().active_layer.unwrap();
+ s.edit("stale cache", |doc, _| {
+ let LayerContent::Smart(sm) = &mut doc.layer_mut(id).unwrap().content else { panic!("not a smart object") };
+ let c = sm.cache.as_mut().unwrap();
+ let r = c.content_bounds();
+ c.fill_rect(r, &STORED);
+ Ok(())
+ })
+ .unwrap();
+ s.execute("edit.transform", json!({"scaleX": 50, "scaleY": 50})).unwrap();
+ let c = cache(&s, id);
+ let r = c.content_bounds();
+ assert_ne!(c.rgba((r.x0 + r.x1) / 2, (r.y0 + r.y1) / 2), STORED, "a supported filter re-renders from the source");
+}