Skip to content

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

fix(history): read pixels from locked history states (#3076) (#3186)

Sir Starch40d7e6a1d8a9
diff --git a/crates/engine/src/channel_cmds.rs b/crates/engine/src/channel_cmds.rs
index 9be74a4a6..ab2c48008 100644
--- a/crates/engine/src/channel_cmds.rs
+++ b/crates/engine/src/channel_cmds.rs
@@ -540,6 +540,24 @@ pub(crate) fn target_surface<'a>(doc: &'a mut Document, layer: Option<LayerId>,
}
}
+pub(crate) fn read_target_surface<'a>(doc: &'a Document, layer: Option<LayerId>, p: &Value) -> Result<&'a Surface> {
+ match target_of(p) {
+ Target::Alpha(i) => {
+ let n = doc.channels.len();
+ doc.channels.get(i).map(|c| &c.surface).ok_or_else(|| EngineError::Other(format!("no alpha channel {i} (document has {n})")))
+ }
+ Target::QuickMask => doc.quick_mask.as_ref().map(|c| &c.surface).ok_or_else(|| EngineError::Other("not in Quick Mask mode".into())),
+ Target::Pixels => {
+ let id = layer.ok_or_else(|| EngineError::Other("no active layer".into()))?;
+ doc.layer(id).and_then(Layer::surface).ok_or(EngineError::NoLayer(id))
+ }
+ Target::Mask => {
+ let id = layer.ok_or_else(|| EngineError::Other("no active layer".into()))?;
+ doc.layer(id).and_then(|l| l.mask.as_ref()).map(|m| &m.surface).ok_or(EngineError::NoLayer(id))
+ }
+ }
+}
+
/// The grayscale surface filters and adjustments edit instead of the layer's pixels (made explicit
/// over the canvas), if the params target one: an alpha channel, the Quick Mask, or the mask of
/// `layer`.
diff --git a/crates/engine/src/fill_cmds.rs b/crates/engine/src/fill_cmds.rs
index 78a28b048..1c2048464 100644
--- a/crates/engine/src/fill_cmds.rs
+++ b/crates/engine/src/fill_cmds.rs
@@ -124,8 +124,7 @@ pub fn fill(s: &mut Session, p: &Value) -> Result<Value> {
// The History Brush's default source: the oldest state still held.
None => st.history.state(0).unwrap_or_else(|| st.doc.clone()),
};
- let mut source_doc = (*source_doc).clone();
- let (src, _) = crate::channel_cmds::target_surface(&mut source_doc, id, p)
+ let src = crate::channel_cmds::read_target_surface(source_doc.as_ref(), id, p)
.map_err(|_| EngineError::Other("the target did not exist (as pixels, a mask or a channel) in that history state".into()))?;
Source::Pixels(area, read_rgba(src, area, "reading Fill's history source")?)
}
@@ -489,6 +488,39 @@ mod tests {
assert!(close(px(&s, 17, 12), [0x33 as f32 / 255.0, 0x66 as f32 / 255.0, 0x99 as f32 / 255.0, 1.0]), "{:?}", px(&s, 17, 12));
}
+ #[test]
+ fn history_fill_reads_a_source_when_its_pixels_were_locked() {
+ // Given a white layer locked in history state 1, then unlocked and filled red.
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 16, "height": 16, "background": "white"})).unwrap();
+ s.execute("layer.lockLayers", json!({"pixels": true})).unwrap();
+ s.execute("layer.lockLayers", json!({"pixels": false})).unwrap();
+ s.execute(CMD, json!({"color": "#ff0000"})).unwrap();
+
+ // When Fill reads the locked historical pixels.
+ let result = s.execute(CMD, json!({"contents": "history", "state": 1}));
+
+ // Then it restores white; the historical lock limits edits, not reads.
+ assert!(result.is_ok(), "{result:?}");
+ assert!(close(px(&s, 8, 8), [1.0; 4]), "{:?}", px(&s, 8, 8));
+ }
+
+ #[test]
+ fn history_fill_refuses_a_locked_destination_when_source_is_unlocked() {
+ // Given an unlocked white source and a red, currently locked destination.
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 16, "height": 16, "background": "white"})).unwrap();
+ s.execute(CMD, json!({"color": "#ff0000"})).unwrap();
+ s.execute("layer.lockLayers", json!({"pixels": true})).unwrap();
+
+ // When Fill reads the unlocked source and attempts to write to the locked destination.
+ let result = s.execute(CMD, json!({"contents": "history", "state": 0}));
+
+ // Then the destination lock still rejects the edit and preserves red pixels.
+ assert!(result.is_err());
+ assert!(close(px(&s, 8, 8), [1.0, 0.0, 0.0, 1.0]), "{:?}", px(&s, 8, 8));
+ }
+
/// `cargo test -p photocraft-engine --release --lib fill_cmds::tests::timing_24mp -- --ignored --nocapture`
#[test]
#[ignore]
diff --git a/crates/engine/src/retouch_cmds.rs b/crates/engine/src/retouch_cmds.rs
index 0da474c22..f93a4a7f3 100644
--- a/crates/engine/src/retouch_cmds.rs
+++ b/crates/engine/src/retouch_cmds.rs
@@ -510,9 +510,8 @@ fn history_source(s: &Session, id: Option<LayerId>, p: &Value, cmd: &str) -> Res
Some(i) => st.history.state(i).ok_or_else(|| bad(cmd, format!("no history state {i} (0..={})", st.history.past_len())))?,
None => st.history.state(0).unwrap_or_else(|| st.doc.clone()),
};
- let mut source_doc = (*source_doc).clone();
- crate::channel_cmds::target_surface(&mut source_doc, id, p)
- .map(|(surf, _)| surf.clone())
+ crate::channel_cmds::read_target_surface(source_doc.as_ref(), id, p)
+ .cloned()
.map_err(|_| EngineError::Other("the target did not exist (as pixels, a mask or a channel) in that history state".into()))
}
diff --git a/crates/engine/src/retouch_cmds/tests.rs b/crates/engine/src/retouch_cmds/tests.rs
index 6ceb14c10..ca258e2e5 100644
--- a/crates/engine/src/retouch_cmds/tests.rs
+++ b/crates/engine/src/retouch_cmds/tests.rs
@@ -379,6 +379,40 @@ fn history_brush_restores_earlier_pixels() {
}
}
+#[test]
+fn history_brush_reads_a_source_when_its_pixels_were_locked() {
+ // Given a white layer locked in history state 1, then unlocked and filled red.
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 16, "height": 16, "depth": 8, "mode": "rgb", "background": "white"})).unwrap();
+ s.execute("layer.lockLayers", json!({"pixels": true})).unwrap();
+ s.execute("layer.lockLayers", json!({"pixels": false})).unwrap();
+ s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap();
+
+ // When History Brush samples the locked historical pixels.
+ let result = s.execute("paint.historyBrush", json!({"points": [[8, 8]], "size": 4, "hardness": 100, "state": 1}));
+
+ // Then it paints white; the historical lock limits edits, not reads.
+ assert!(result.is_ok(), "{result:?}");
+ assert!(rgba(&s, 8, 8).iter().all(|v| *v > 0.99), "{:?}", rgba(&s, 8, 8));
+}
+
+#[test]
+fn history_brush_refuses_a_locked_destination_when_source_is_unlocked() {
+ // Given an unlocked white source and a red, currently locked destination.
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 16, "height": 16, "depth": 8, "mode": "rgb", "background": "white"})).unwrap();
+ s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap();
+ s.execute("layer.lockLayers", json!({"pixels": true})).unwrap();
+
+ // When History Brush samples the unlocked source and attempts to paint the locked destination.
+ let result = s.execute("paint.historyBrush", json!({"points": [[8, 8]], "size": 4, "hardness": 100, "state": 0}));
+
+ // Then the destination lock still rejects the edit and preserves red pixels.
+ assert!(result.is_err());
+ let pixel = rgba(&s, 8, 8);
+ assert!(pixel[0] > 0.99 && pixel[1] < 0.01, "{pixel:?}");
+}
+
#[test]
fn history_brush_errors_when_layer_is_new() {
let mut s = session(20, 20, 8, "rgb");