Skip to content

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

Plug-ins: running a filter plug-in respects layer locks (#6770)

David Mann8c6412bbb21c
diff --git a/crates/engine/src/filters.rs b/crates/engine/src/filters.rs
index e40be7b6a..221709080 100644
--- a/crates/engine/src/filters.rs
+++ b/crates/engine/src/filters.rs
@@ -313,7 +313,7 @@ pub(crate) fn run_filter(s: &mut Session, id: &str, p: &Value) -> Result<Value>
}
/// Lock transparency: put the layer's alpha back after a filter, in the tiles it changed.
-fn keep_alpha(old: &photocraft_raster::Surface, new: &mut photocraft_raster::Surface) {
+pub(crate) fn keep_alpha(old: &photocraft_raster::Surface, new: &mut photocraft_raster::Surface) {
if old.format() != new.format() || !new.format().alpha {
return;
}
diff --git a/crates/engine/src/plugin_cmds.rs b/crates/engine/src/plugin_cmds.rs
index 5c7d2dd71..a2044f5e8 100644
--- a/crates/engine/src/plugin_cmds.rs
+++ b/crates/engine/src/plugin_cmds.rs
@@ -77,10 +77,17 @@ fn run(s: &mut Session, p: &Value) -> Result<Value> {
*surf = plugin.apply(surf, canvas, selection.as_ref(), &resolved).map_err(err)?;
return Ok(());
}
+ crate::commands::check_pixels_unlocked(doc, layer)?;
+ let keep_alpha = doc.effective_locks(layer).transparency;
let l = doc.layer_mut(layer).ok_or(EngineError::NoLayer(layer))?;
match &mut l.content {
LayerContent::Raster(surf) => {
+ // Lock Transparency: the plug-in may change colour but not alpha (as built-in filters).
+ let before = keep_alpha.then(|| surf.clone());
*surf = plugin.apply(surf, canvas, selection.as_ref(), &resolved).map_err(err)?;
+ if let Some(before) = &before {
+ crate::filters::keep_alpha(before, surf);
+ }
Ok(())
}
LayerContent::Smart(_) => {
diff --git a/crates/engine/src/plugin_cmds/tests.rs b/crates/engine/src/plugin_cmds/tests.rs
index 3d178fada..39bb1acc7 100644
--- a/crates/engine/src/plugin_cmds/tests.rs
+++ b/crates/engine/src/plugin_cmds/tests.rs
@@ -155,6 +155,56 @@ fn params_are_validated_and_passed() {
Session::new().execute("plugin.remove", json!({"id": "test.cmds.grey"})).unwrap();
}
+#[test]
+fn locked_layers_refuse_plugin_filters() {
+ // Like built-in filters: locked pixels (or Lock All) refuse the run with no undo step.
+ let id = "test.cmds.locks";
+ Session::new().execute("plugin.install", json!({"data": b64(&wat_plugin(id, "{}", GREY))})).unwrap();
+ for lock_all in [false, true] {
+ let mut s = doc(16);
+ let layer = s.active().unwrap().active_layer.unwrap();
+ s.edit("lock", |doc, _| {
+ let l = doc.layer_mut(layer).unwrap();
+ if lock_all {
+ l.locks.all = true;
+ } else {
+ l.locks.pixels = true;
+ }
+ Ok(())
+ })
+ .unwrap();
+ let before = pixels(&s);
+ let r = s.execute("plugin.run", json!({"id": id}));
+ assert!(r.as_ref().is_err_and(|e| e.to_string().contains("is locked")), "lock_all {lock_all}: {r:?}");
+ assert_eq!(pixels(&s), before, "lock_all {lock_all}");
+ s.execute("edit.undo", json!({})).unwrap();
+ let l = s.active().unwrap().doc.layer(layer).unwrap();
+ assert!(!l.locks.pixels && !l.locks.all, "the refused run left no undo step");
+ // Unlocked, the plug-in runs.
+ s.execute("plugin.run", json!({"id": id})).unwrap();
+ assert_ne!(pixels(&s), before);
+ }
+ // Lock Transparency: the run changes colour but keeps every pixel's alpha (GREY sets alpha too).
+ let mut s = doc(16);
+ let layer = s.active().unwrap().active_layer.unwrap();
+ s.edit("lock", |doc, _| {
+ doc.layer_mut(layer).unwrap().locks.transparency = true;
+ Ok(())
+ })
+ .unwrap();
+ let before = pixels(&s);
+ s.execute("plugin.run", json!({"id": id})).unwrap();
+ let after = pixels(&s);
+ assert_ne!(after, before);
+ for (a, b) in after.as_chunks::<4>().0.iter().zip(before.as_chunks::<4>().0) {
+ assert_eq!(a[3], b[3], "alpha kept");
+ if b[3] > 0.0 {
+ assert!((a[0] - 0.5).abs() < 1e-3, "colour changed: {a:?}");
+ }
+ }
+ Session::new().execute("plugin.remove", json!({"id": id})).unwrap();
+}
+
#[test]
fn hostile_plugins_fail_without_touching_the_document() {
for (id, body) in [