PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Smart Objects: Curves preview no longer zooms in on a reduced proxy (#5766) (#5779)
Michael Higgins7e94c507e933
diff --git a/crates/compose/src/proxy.rs b/crates/compose/src/proxy.rsindex 84c934802..f48f10d83 100644--- a/crates/compose/src/proxy.rs+++ b/crates/compose/src/proxy.rs@@ -4,8 +4,8 @@//! channel thumbnails, histograms, file previews), where a full-resolution composite of a 200 MP//! document would cost seconds and gigabytes.-use photocraft_doc::{Document, Layer, LayerContent, Size};-use photocraft_geom::Rect;+use photocraft_doc::{Document, Layer, LayerContent, Size, SmartObject};+use photocraft_geom::{Affine, Rect};use photocraft_raster::Surface;/// Nearest-neighbour downsample of a surface by integer factor `k` (document coordinates / k).@@ -60,11 +60,30 @@ fn shrink_layer(l: &mut Layer, k: u32) {}LayerContent::Text(t) => t.cache = t.cache.as_ref().map(|s| downsample(s, k)),LayerContent::Shape(sh) => sh.cache = sh.cache.as_ref().map(|s| downsample(s, k)),- LayerContent::Smart(so) => so.cache = so.cache.as_ref().map(|s| downsample(s, k)),+ LayerContent::Smart(so) => shrink_smart(so, k),LayerContent::Adjustment(_) | LayerContent::Fill(_) => {}}}+/// Scales a smart object with its proxy: the cache and filter mask like any surface, and the+/// placement too. A preview that adds a smart filter re-renders the source through the placement;+/// left in full-size document coordinates, that render came out `k` times too large on the proxy+/// canvas, so the Curves preview on a smart object zoomed in (#5766).+fn shrink_smart(so: &mut SmartObject, k: u32) {+ so.cache = so.cache.as_ref().map(|s| downsample(s, k));+ if let Some(m) = &mut so.filter_mask {+ m.surface = downsample(&m.surface, k);+ }+ let s = 1.0 / f64::from(k);+ so.transform = Affine::scale(s).mul(&so.transform);+ // Row-major source → document map: scaling the document side scales its first two rows.+ if let Some(h) = &mut so.perspective {+ for v in h.iter_mut().take(6) {+ *v *= s;+ }+ }+}+/// A copy of `doc` scaled down by `k` (same layer ids, so adjustments can be swapped in). The/// selection scales with it, so a filter previewed on the proxy stays inside the (feathered)/// selection exactly as the full-size result does.@@ -166,6 +185,33 @@ mod tests {assert!(proxy_faithful(&doc));}+ /// #5766: a smart object re-rendered on the proxy (a smart filter preview) goes through its+ /// placement, which must shrink with the canvas, cache and filter mask.+ #[test]+ fn proxy_scales_smart_object_placement() {+ use photocraft_doc::SmartSource;+ let mut doc = Document::new("smart", Size::new(64, 64), ColorMode::Rgb, SampleType::U8);+ let mut cache = Surface::new(doc.pixel_format());+ cache.fill_rect(Rect::new(16, 16, 32, 48), &[0.0, 0.0, 0.0, 1.0]);+ let source = SmartSource::Embedded { file_name: "s.png".into(), bytes: std::sync::Arc::new(Vec::new()) };+ let mut sm = SmartObject::new(source, Affine::translate(16.0, 16.0).mul(&Affine::scale(2.0)), Some(cache));+ let mut mask = LayerMask::hide_all();+ mask.surface.fill_rect(Rect::new(16, 16, 32, 32), &[1.0]);+ sm.filter_mask = Some(mask);+ sm.perspective = Some([2.0, 0.0, 16.0, 0.0, 2.0, 16.0, 0.0, 0.0, 1.0]);+ doc.layers.push(Layer::new("smart", LayerContent::Smart(sm)));+ let p = proxy_document(&doc, 4);+ let LayerContent::Smart(sm) = &p.layers[0].content else { panic!("a smart object") };+ assert_eq!(sm.transform, Affine { m: [0.5, 0.0, 0.0, 0.5, 4.0, 4.0] });+ assert_eq!(sm.perspective, Some([0.5, 0.0, 4.0, 0.0, 0.5, 4.0, 0.0, 0.0, 1.0]));+ assert_eq!(sm.cache.as_ref().unwrap().content_bounds(), Rect::new(4, 4, 8, 12));+ let mask = &sm.filter_mask.as_ref().unwrap().surface;+ assert_eq!((mask.pixel(4, 4)[0], mask.pixel(8, 8)[0]), (1.0, 0.0));+ // The document keeps its full-size placement.+ let LayerContent::Smart(orig) = &doc.layers[0].content else { panic!("a smart object") };+ assert_eq!(orig.transform.m[4], 16.0);+ }+#[test]fn proxy_scales_the_selection() {let mut doc = Document::with_background("d", Size::new(64, 64), ColorMode::Rgb, SampleType::U8, Color::WHITE);diff --git a/crates/ui-egui/src/filter_dialog.rs b/crates/ui-egui/src/filter_dialog.rsindex 53885a45a..5d86cc16b 100644--- a/crates/ui-egui/src/filter_dialog.rs+++ b/crates/ui-egui/src/filter_dialog.rs@@ -1830,6 +1830,38 @@ mod tests {assert_eq!(photocraft_compose::flatten(&shown).px, photocraft_compose::flatten(&full).px);}+ /// #5766: Curves on a smart object adds a smart filter, which re-renders the source through+ /// the object's placement. On a reduced proxy that placement stayed in full-size document+ /// coordinates, so the preview showed the object `k` times too large until OK.+ #[test]+ fn smart_object_curves_preview_keeps_its_size_on_a_reduced_proxy() {+ let mut s = photocraft_engine::Session::new();+ s.execute("file.new", json!({"width": 64, "height": 64})).unwrap();+ s.execute("layer.new.layer", json!({})).unwrap();+ s.execute("select.rect", json!({"x": 16, "y": 16, "width": 16, "height": 16})).unwrap();+ s.execute("edit.fill", json!({"contents": "black"})).unwrap();+ s.execute("select.deselect", json!({})).unwrap();+ s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap();+ let st = s.active().unwrap();+ let (doc, layer) = ((*st.doc).clone(), st.active_layer.unwrap());+ let alpha = |d: &Document, x: i32, y: i32| match &d.layer(layer).unwrap().content {+ photocraft_doc::LayerContent::Smart(sm) => sm.cache.as_ref().unwrap().pixel(x, y).last().copied().unwrap(),+ _ => panic!("a smart object"),+ };+ let k = 4;+ let shown = preview_document(&doc, Some(layer), "image.adjustments.curves", &json!({"points": [[0, 20], [90, 150], [255, 230]]}), k).unwrap();+ let photocraft_doc::LayerContent::Smart(sm) = &shown.layer(layer).unwrap().content else { panic!("a smart object") };+ assert_eq!(sm.smart_filters.len(), 1, "the preview adds the Curves smart filter");+ assert_eq!(shown.size, crate::proxy::proxy_document(&doc, k).size);+ // The 16 px square at (16, 16) is a 4 px square at (4, 4) on the proxy, not 16 px at (16, 16).+ for (x, y) in [(4, 4), (5, 6), (7, 7)] {+ assert!(alpha(&shown, x, y) > 0.99, "({x}, {y}) is inside the reduced square");+ }+ for (x, y) in [(2, 2), (9, 9), (12, 12), (15, 15)] {+ assert!(alpha(&shown, x, y) < 0.01, "({x}, {y}) is outside the reduced square");+ }+ }+/// #2063: a pixel-sized parameter divided by the proxy factor was clamped to the command's/// minimum (Pointillize's 5 px cells became 3 px proxy cells = 12 px), so the preview factor/// drops until every pixel-sized value fits its range.