PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(engine): apply document-wide image and colour conversions to stored video frames (#4956) (#5078)
MarekSpodBiedry1a03735cc675
diff --git a/crates/engine/src/canvas_geom.rs b/crates/engine/src/canvas_geom.rsindex 99f7e4cc8..8de32f432 100644--- a/crates/engine/src/canvas_geom.rs+++ b/crates/engine/src/canvas_geom.rs@@ -253,7 +253,8 @@ impl Turn {}/// Every pixel surface a canvas turn moves: content, rendered caches (type, shape, smart object,-/// fill), masks (layer, smart filter), alpha channels, Quick Mask and the selection.+/// fill), stored video frames (current and original), masks (layer, smart filter), alpha+/// channels, Quick Mask and the selection.fn remap_pixels(doc: &mut Document, map: &dyn Fn(i32, i32) -> (i32, i32)) {fn rec(layers: &mut [Layer], map: &dyn Fn(i32, i32) -> (i32, i32)) {let re = |s: &mut Surface| *s = remap_surface(s, map);@@ -264,6 +265,10 @@ fn remap_pixels(doc: &mut Document, map: &dyn Fn(i32, i32) -> (i32, i32)) {if let Some(fc) = &mut l.fill_cache {re(&mut fc.surface);}+ // Frames away from the playhead come back on scrubbing; they turn with the canvas (#4956).+ if let Some(v) = &mut l.video {+ v.frames.iter_mut().chain(v.original_frames.iter_mut().flatten()).for_each(re);+ }match &mut l.content {LayerContent::Raster(s) => re(s),LayerContent::Text(t) => t.cache.iter_mut().for_each(re),diff --git a/crates/engine/src/canvas_geom/tests.rs b/crates/engine/src/canvas_geom/tests.rsindex 0dbf03d2e..2ba0dfbd6 100644--- a/crates/engine/src/canvas_geom/tests.rs+++ b/crates/engine/src/canvas_geom/tests.rs@@ -325,6 +325,30 @@ fn arbitrary_rotation_moves_marks_and_unlinked_vector_masks() {assert!(mean < 3.0 / 255.0 && big < 0.02, "mean {mean} big {big}");}+/// A canvas turn moves every stored video frame, not just the displayed one (#4956).+#[test]+fn turns_move_stored_video_frames() {+ let mut s = Session::new();+ s.execute("file.new", json!({"width": W, "height": H})).unwrap();+ s.execute("layer.new.layer", json!({})).unwrap();+ paint(&mut s, |x, y| if (x, y) == (1, 2) { [1.0, 0.0, 0.0, 1.0] } else { [0.0; 4] });+ s.edit("video", |doc, active| {+ let l = doc.layer_mut(active.unwrap()).unwrap();+ let frame = l.surface().unwrap().clone();+ let mut v = photocraft_doc::VideoData::new(vec![frame.clone(), frame.clone()], 24.0);+ v.original_frames = Some(vec![frame.clone(), frame]);+ l.video = Some(v);+ Ok(())+ })+ .unwrap();+ s.execute(command(Turn::FlipHorizontal), json!({})).unwrap();+ let v = doc(&s).layers[1].video.as_ref().unwrap();+ for frame in v.frames.iter().chain(v.original_frames.iter().flatten()) {+ assert_eq!(frame.pixel(W as i32 - 2, 2), vec![1.0, 0.0, 0.0, 1.0]);+ assert_eq!(frame.pixel(1, 2)[3], 0.0);+ }+}+#[test]fn turns_on_empty_and_tiny_documents_never_panic() {for (w, h) in [(1, 1), (1, 7), (5, 1)] {diff --git a/crates/engine/src/image_cmds.rs b/crates/engine/src/image_cmds.rsindex 805fbde55..54422f32c 100644--- a/crates/engine/src/image_cmds.rs+++ b/crates/engine/src/image_cmds.rs@@ -16,7 +16,8 @@ fn has_doc(s: &Session) -> std::result::Result<(), String> {}/// Applies `f` to every pixel surface of a layer tree: content (raster or-/// cached text/shape/smart pixels), fill caches, and (if `masks`) masks.+/// cached text/shape/smart pixels), fill caches, stored video frames (current and original),+/// and (if `masks`) masks.pub(crate) fn for_each_surface(layers: &mut [Layer], masks: bool, f: &mut dyn FnMut(&mut Surface, bool)) {for l in layers {if masks && let Some(m) = &mut l.mask {@@ -25,6 +26,13 @@ pub(crate) fn for_each_surface(layers: &mut [Layer], masks: bool, f: &mut dyn Fnif let Some(fc) = &mut l.fill_cache {f(&mut fc.surface, false);}+ // The displayed content is only a copy of the frame at the playhead; the other frames+ // would keep the old geometry and depth and come back stale when scrubbing (#4956).+ if let Some(v) = &mut l.video {+ for frame in v.frames.iter_mut().chain(v.original_frames.iter_mut().flatten()) {+ f(frame, false);+ }+ }match &mut l.content {LayerContent::Raster(s) => f(s, false),LayerContent::Text(t) => {@@ -938,6 +946,28 @@ mod tests {&s.active().unwrap().doc}+ /// Document-wide conversions must move every stored video frame, not just the displayed one (#4956).+ #[test]+ fn crop_moves_stored_video_frames() {+ let mut s = session();+ s.edit("video", |doc, active| {+ let l = doc.layer_mut(active.unwrap()).unwrap();+ let frame = l.surface().unwrap().clone();+ let mut v = photocraft_doc::VideoData::new(vec![frame.clone(), frame.clone()], 24.0);+ v.original_frames = Some(vec![frame.clone(), frame]);+ l.video = Some(v);+ Ok(())+ })+ .unwrap();+ s.execute("image.crop", json!({"x": -10, "y": -5, "width": 60, "height": 30})).unwrap();+ let v = doc(&s).layers[1].video.as_ref().unwrap();+ let red = vec![1.0, 0.0, 0.0, 1.0];+ for frame in v.frames.iter().chain(v.original_frames.iter().flatten()) {+ assert_eq!(frame.pixel(20, 10), red);+ assert_eq!(frame.pixel(5, 5)[3], 0.0);+ }+ }+/// Resample on with only a new resolution keeps the print size, as Photoshop's Image Size/// does (an action's `imageSize` with `Rslt` alone); without `keepPrintSize` only the/// resolution changes.