PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Quick Mask: an empty start and Select All stay apart (#2743) (#2922)
Fabio Lussi9441484d168a
diff --git a/crates/doc/src/lib.rs b/crates/doc/src/lib.rsindex c695b028f..608cc46f4 100644--- a/crates/doc/src/lib.rs+++ b/crates/doc/src/lib.rs@@ -810,6 +810,9 @@ pub struct Document {/// Select › Edit in Quick Mask Mode: the temporary "Quick Mask" channel while the mode is on./// Part of the document so entering and leaving are history steps (as in Photoshop).pub quick_mask: Option<AlphaChannel>,+ /// Quick Mask was entered with no selection, so an untouched white mask (Color Indicates+ /// Masked Areas) leaves no selection rather than Select All.+ pub quick_mask_unselected: bool,/// Patterns stored with the document (PSD `Patt`/`Pat2`/`Pat3`): those its layers use, plus/// any imported with it.pub patterns: Vec<Pattern>,@@ -861,6 +864,7 @@ impl Document {work_path: None,clipping_path: None,quick_mask: None,+ quick_mask_unselected: false,patterns: Vec::new(),color_table: None,duotone: None,diff --git a/crates/engine/src/channel_cmds.rs b/crates/engine/src/channel_cmds.rsindex 2aca23e53..9be74a4a6 100644--- a/crates/engine/src/channel_cmds.rs+++ b/crates/engine/src/channel_cmds.rs@@ -872,27 +872,40 @@ fn load_selection(s: &mut Session, p: &Value) -> Result<Value> {// ---------------------------------------------------------------------------------------------// Quick Mask-/// Quick Mask channel for the current selection: white where selected; no selection = an empty-/// mask (nothing masked), as in Photoshop.+/// Quick Mask channel for the current selection: white where selected. No selection is an empty+/// mask that shows no colour: white (nothing masked, as in Photoshop) when the colour indicates+/// masked areas, black (nothing selected) when it indicates selected areas. White and "no+/// selection" are told apart by [`Document::quick_mask_unselected`].fn quick_mask_from(doc: &Document, o: &QuickMaskOptions) -> AlphaChannel {let area = doc.bounds();let fmt = channel_format(doc);let surface = match &doc.selection {Some(s) => plane_surface(&sel::mask_from_surface(Some(s), area), area, fmt),- None => {- let mut s = Surface::new(fmt);- s.fill_rect(area, &[1.0]);- s- }+ None => unselected_surface(area, fmt, o.indicates),};AlphaChannel { color: o.color, opacity: o.opacity, indicates: o.indicates, ..AlphaChannel::new("Quick Mask", surface) }}-/// The selection a Quick Mask channel turns back into (an untouched or all-white mask = none).+/// The colourless, nothing-selected Quick Mask for `indicates`.+fn unselected_surface(area: Rect, fmt: PixelFormat, indicates: ColorIndicates) -> Surface {+ let mut s = Surface::new(fmt);+ if indicates != ColorIndicates::SelectedAreas {+ s.fill_rect(area, &[1.0]);+ }+ s+}++/// Every pixel of `vals` reads as `target` on the 8-bit grid.+fn uniform(vals: &[f32], target: f32) -> bool {+ vals.iter().all(|v| (v - target).abs() <= 0.5 / 255.0)+}++/// The selection a Quick Mask channel turns back into. All white is Select All, unless Quick+/// Mask was entered with no selection and the white is its untouched masked-areas start.fn selection_from(doc: &Document, ch: &AlphaChannel) -> Option<Surface> {let area = doc.bounds();let vals = read_plane(&ch.surface, area);- if vals.iter().all(|v| *v >= 1.0 - 0.5 / 255.0) {+ if doc.quick_mask_unselected && ch.indicates != ColorIndicates::SelectedAreas && uniform(&vals, 1.0) {return None;}sel::combine(None, &vals, area, SelectionMode::Replace)@@ -908,6 +921,7 @@ fn quick_mask(s: &mut Session, p: &Value) -> Result<Value> {if want {s.edit("Edit in Quick Mask", |doc, _| {doc.quick_mask = Some(quick_mask_from(doc, &opts));+ doc.quick_mask_unselected = doc.selection.is_none();doc.selection = None;Ok(())})?;@@ -1315,7 +1329,18 @@ fn options(s: &mut Session, p: &Value) -> Result<Value> {if s.active().is_some_and(|d| d.doc.quick_mask.is_some()) {let o = s.quick_mask_options;s.edit("Quick Mask Options", |doc, _| {+ let area = doc.bounds();+ let fmt = channel_format(doc);+ let unselected = doc.quick_mask_unselected;if let Some(q) = &mut doc.quick_mask {+ // An untouched no-selection mask stays colourless (nothing selected)+ // when the colour switches between masked and selected areas.+ if unselected && q.indicates != o.indicates {+ let empty = if q.indicates == ColorIndicates::SelectedAreas { 0.0 } else { 1.0 };+ if uniform(&read_plane(&q.surface, area), empty) {+ q.surface = unselected_surface(area, fmt, o.indicates);+ }+ }q.color = o.color;q.opacity = o.opacity;q.indicates = o.indicates;diff --git a/crates/engine/src/channel_cmds/tests.rs b/crates/engine/src/channel_cmds/tests.rsindex 93005859e..16b2b4294 100644--- a/crates/engine/src/channel_cmds/tests.rs+++ b/crates/engine/src/channel_cmds/tests.rs@@ -337,6 +337,89 @@ fn quick_mask_without_selection_and_options() {assert!(sel_at(&s, 10, 10) < 0.05 && sel_at(&s, 30, 10) > 0.99, "{} {}", sel_at(&s, 10, 10), sel_at(&s, 30, 10));}+/// Overlay coverage of the live Quick Mask at (x, y): how much colour the canvas shows there.+fn qm_overlay(s: &Session, x: i32, y: i32) -> f32 {+ let q = doc(s).quick_mask.as_ref().unwrap();+ q.overlay_coverage(q.surface.sample_channel(x, y, 0))+}++#[test]+fn quick_mask_without_selection_starts_empty_in_both_modes() {+ // #2743 case 1: nothing selected → nothing selected in the mask, whichever way the colour+ // reads; painting white adds to the selection that comes out.+ for depth in [8, 16, 32] {+ // Selected Areas set before entering.+ let mut s = session_depth(depth);+ s.execute("channel.options", json!({"channel": "quickMask", "indicates": "selected"})).unwrap();+ s.execute("select.editInQuickMaskMode", json!({"on": true})).unwrap();+ assert_eq!((qm_overlay(&s, 5, 5), qm_overlay(&s, 35, 15)), (0.0, 0.0), "depth {depth}: no overlay");+ s.execute("select.editInQuickMaskMode", json!({"on": false})).unwrap();+ assert!(doc(&s).selection.is_none(), "depth {depth}: untouched → no selection");+ s.execute("select.editInQuickMaskMode", json!({"on": true})).unwrap();+ s.execute("paint.stroke", json!({"points": [[5, 5]], "size": 4, "hardness": 1.0, "color": "#ffffff"})).unwrap();+ assert!(qm_overlay(&s, 5, 5) > 0.99 && qm_overlay(&s, 30, 10) == 0.0, "depth {depth}");+ s.execute("select.editInQuickMaskMode", json!({"on": false})).unwrap();+ assert!(sel_at(&s, 5, 5) > 0.99 && sel_at(&s, 30, 10) == 0.0, "depth {depth}: painted area is the selection");++ // Selected Areas switched on while in Quick Mask (the order in the report).+ let mut s = session_depth(depth);+ s.execute("select.editInQuickMaskMode", json!({"on": true})).unwrap();+ assert_eq!(qm_overlay(&s, 5, 5), 0.0, "depth {depth}: Masked Areas, nothing masked yet");+ s.execute("channel.options", json!({"channel": "quickMask", "indicates": "selected"})).unwrap();+ assert_eq!((qm_overlay(&s, 5, 5), qm_overlay(&s, 35, 15)), (0.0, 0.0), "depth {depth}: still no overlay");+ // …and back again: still the untouched, nothing-selected mask.+ s.execute("channel.options", json!({"channel": "quickMask", "indicates": "masked"})).unwrap();+ assert_eq!(qm_overlay(&s, 5, 5), 0.0, "depth {depth}");+ s.execute("select.editInQuickMaskMode", json!({"on": false})).unwrap();+ assert!(doc(&s).selection.is_none(), "depth {depth}");+ }+}++#[test]+fn quick_mask_round_trip_keeps_select_all() {+ // #2743 case 2: a full selection comes back full, in either mode.+ for depth in [8, 16] {+ for indicates in ["masked", "selected"] {+ let mut s = session_depth(depth);+ s.execute("channel.options", json!({"channel": "quickMask", "indicates": indicates})).unwrap();+ s.execute("select.all", json!({})).unwrap();+ s.execute("select.editInQuickMaskMode", json!({})).unwrap();+ s.execute("select.editInQuickMaskMode", json!({})).unwrap();+ assert!(doc(&s).selection.is_some(), "depth {depth} {indicates}: Select All survives");+ assert_eq!((sel_at(&s, 0, 0), sel_at(&s, 39, 19)), (1.0, 1.0), "depth {depth} {indicates}");+ // Switching the mode in between doesn't change what is selected.+ s.execute("select.editInQuickMaskMode", json!({})).unwrap();+ let other = if indicates == "masked" { "selected" } else { "masked" };+ s.execute("channel.options", json!({"channel": "quickMask", "indicates": other})).unwrap();+ s.execute("select.editInQuickMaskMode", json!({})).unwrap();+ assert_eq!((sel_at(&s, 0, 0), sel_at(&s, 39, 19)), (1.0, 1.0), "depth {depth} {indicates}→{other}");+ }+ }+ // A no-selection entry followed by Select All entry: the second one isn't "no selection".+ let mut s = session();+ s.execute("select.editInQuickMaskMode", json!({})).unwrap();+ s.execute("select.editInQuickMaskMode", json!({})).unwrap();+ s.execute("select.all", json!({})).unwrap();+ s.execute("select.editInQuickMaskMode", json!({})).unwrap();+ s.execute("select.editInQuickMaskMode", json!({})).unwrap();+ assert_eq!(sel_at(&s, 20, 10), 1.0);+}++#[test]+fn quick_mask_odd_states_dont_panic() {+ let mut s = Session::new();+ assert!(s.execute("select.editInQuickMaskMode", json!({})).is_err(), "no document");+ let _ = s.execute("channel.options", json!({"channel": "quickMask", "indicates": "selected"}));+ let mut s = session();+ s.execute("select.editInQuickMaskMode", json!({})).unwrap();+ s.execute("select.all", json!({})).unwrap();+ s.execute("select.deselect", json!({})).unwrap();+ s.execute("channel.options", json!({"channel": "quickMask", "indicates": "selected"})).unwrap();+ s.undo();+ s.undo();+ s.execute("select.editInQuickMaskMode", json!({})).unwrap();+}+#[test]fn channel_options_and_spot_channels() {let mut s = session();diff --git a/crates/engine/src/layer_multi_cmds.rs b/crates/engine/src/layer_multi_cmds.rsindex 27424e41b..384f2deea 100644--- a/crates/engine/src/layer_multi_cmds.rs+++ b/crates/engine/src/layer_multi_cmds.rs@@ -584,6 +584,7 @@ fn doc_pixels_equal(a: &Document, b: &Document) -> bool {work_path: _,clipping_path: _,quick_mask,+ quick_mask_unselected: _,patterns,color_table,duotone,diff --git a/crates/format/src/convert.rs b/crates/format/src/convert.rsindex c81faadef..4ee79f36b 100644--- a/crates/format/src/convert.rs+++ b/crates/format/src/convert.rs@@ -243,6 +243,7 @@ pub(crate) fn doc_m(d: &Document, sink: &mut dyn Sink) -> DocM {work_path: d.work_path.clone(),clipping_path: d.clipping_path.clone(),quick_mask: d.quick_mask.as_ref().map(|c| channel_m(c, sink)),+ quick_mask_unselected: d.quick_mask_unselected,patterns: d.patterns.iter()@@ -607,6 +608,7 @@ impl Loader<'_> {work_path: m.work_path.clone(),clipping_path: m.clipping_path.clone(),quick_mask,+ quick_mask_unselected: m.quick_mask_unselected,patterns,color_table: m.color_table.clone(),duotone: m.duotone.clone(),diff --git a/crates/format/src/manifest.rs b/crates/format/src/manifest.rsindex 3bc97baf9..6524bfdfc 100644--- a/crates/format/src/manifest.rs+++ b/crates/format/src/manifest.rs@@ -59,6 +59,9 @@ pub struct DocM {/// The temporary Quick Mask channel (saved while Quick Mask mode is on).#[serde(default)]pub quick_mask: Option<ChannelM>,+ /// Quick Mask was entered with no selection.+ #[serde(default, skip_serializing_if = "std::ops::Not::not")]+ pub quick_mask_unselected: bool,/// Patterns stored with the document.#[serde(default)]pub patterns: Vec<PatternM>,diff --git a/crates/format/tests/common/mod.rs b/crates/format/tests/common/mod.rsindex 4adf8ccb3..b4940ecd1 100644--- a/crates/format/tests/common/mod.rs+++ b/crates/format/tests/common/mod.rs@@ -219,6 +219,7 @@ pub fn rich_doc(mode: ColorMode, depth: SampleType) -> Document {d.channels.push(alpha);d.channels.push(AlphaChannel { spot: Some((Color::rgb(1.0, 0.0, 0.5), 0.7)), ..AlphaChannel::new("Spot", ch.clone()) });d.quick_mask = Some(AlphaChannel::new("Quick Mask", ch));+ d.quick_mask_unselected = true;let mut pat = Surface::new(PixelFormat::new(photocraft_color::ColorMode::Rgb, photocraft_color::SampleType::U16, true));scribble(&mut pat, 21, false);d.patterns.push(photocraft_doc::Pattern::new("$$$/Patterns/Test=Scribble", pat, 32, 24));diff --git a/crates/ui-egui/src/channel_view.rs b/crates/ui-egui/src/channel_view.rsindex 783a6313b..e4642d946 100644--- a/crates/ui-egui/src/channel_view.rs+++ b/crates/ui-egui/src/channel_view.rs@@ -315,6 +315,35 @@ mod tests {assert_eq!(px[6], Color32::from_rgba_premultiplied(128, 0, 0, 128), "masked: 50% red");}+ #[test]+ fn quick_mask_without_selection_shows_no_overlay_in_either_mode() {+ // #2743: nothing selected is a colourless mask, whichever way the colour reads.+ for (before, after) in [(None, Some("selected")), (Some("selected"), None), (Some("selected"), Some("masked"))] {+ let mut s = session();+ if let Some(i) = before {+ s.execute("channel.options", json!({"channel": "quickMask", "indicates": i})).unwrap();+ }+ s.execute("select.editInQuickMaskMode", json!({})).unwrap();+ let st = s.active().unwrap();+ let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false, st.active_layer).unwrap();+ assert!(px.iter().all(|p| *p == Color32::TRANSPARENT), "{before:?}");+ if let Some(i) = after {+ s.execute("channel.options", json!({"channel": "quickMask", "indicates": i})).unwrap();+ let st = s.active().unwrap();+ let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false, st.active_layer).unwrap();+ assert!(px.iter().all(|p| *p == Color32::TRANSPARENT), "{before:?} → {i}");+ }+ }+ // Selected Areas with Select All: the whole canvas is coloured.+ let mut s = session();+ s.execute("channel.options", json!({"channel": "quickMask", "indicates": "selected"})).unwrap();+ s.execute("select.all", json!({})).unwrap();+ s.execute("select.editInQuickMaskMode", json!({})).unwrap();+ let st = s.active().unwrap();+ let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false, st.active_layer).unwrap();+ assert!(px.iter().all(|p| *p == Color32::from_rgba_premultiplied(128, 0, 0, 128)));+ }+#[test]fn layer_mask_views_and_damage_on_the_downsampled_grid() {let mut s = Session::new();