diff --git a/crates/engine/src/cmd/retouch_cmds/heal.rs b/crates/engine/src/cmd/retouch_cmds/heal.rs index ca7a44328e3..b0d33ca5e93 100644 --- a/crates/engine/src/cmd/retouch_cmds/heal.rs +++ b/crates/engine/src/cmd/retouch_cmds/heal.rs @@ -44,8 +44,15 @@ pub(super) fn heal_region(fmt: &PixelFormat, src: &Region, dst: &Region, mask: & out } +/// The reference's alert for the Healing Brush in an Indexed Color document (#12112). Bitmap, +/// Duotone and Multichannel weren't measured and heal as before. +const INDEXED_REFUSAL: &str = "Could not set the clone source because it does not work with Indexed Color images (convert image to RGB Color mode to edit)."; + pub(super) fn healing_brush(s: &mut Session, p: &Value) -> Result { const CMD: &str = "paint.healingBrush"; + if s.active().is_some_and(|st| st.doc.mode == photocraft_color::ColorMode::Indexed) { + return Err(EngineError::Other(INDEXED_REFUSAL.into())); + } let (stroke, id) = parse_brush(s, p, CMD)?; reject_huge_stroke(CMD, &stroke)?; let which = sample_layers(p, CMD)?; @@ -291,3 +298,7 @@ pub(super) fn spot_healing(s: &mut Session, p: &Value) -> Result { let dmg = run_stroke(s, "Spot Healing Brush", id, p, |pre, surf, sel, lock| spot_heal_surface(surf, pre, &stroke, kind, all, sel, lock))?; Ok(json!({ "damage": damage_json(dmg) })) } + +#[cfg(test)] +#[path = "heal_indexed_tests.rs"] +mod heal_indexed_tests; diff --git a/crates/engine/src/cmd/retouch_cmds/heal_indexed_tests.rs b/crates/engine/src/cmd/retouch_cmds/heal_indexed_tests.rs new file mode 100644 index 00000000000..48a3afb5e24 --- /dev/null +++ b/crates/engine/src/cmd/retouch_cmds/heal_indexed_tests.rs @@ -0,0 +1,65 @@ +//! #12112: the Healing Brush refuses an Indexed Color document with the reference application's +//! message, painting nothing and adding no history. RGB, Grayscale, CMYK and Lab at every depth +//! keep healing; Bitmap, Duotone and Multichannel (not measured) don't get the refusal. + +use photocraft_color::{Color, ColorMode, SampleType}; +use photocraft_doc::Size; + +use super::*; + +const MESSAGE: &str = "Could not set the clone source because it does not work with Indexed Color images (convert image to RGB Color mode to edit)."; + +/// A 64 × 64 `mode` document of `depth`, mid grey with its left half black: the edge is the +/// texture the stroke heals onto the flat grey. +fn edged(mode: ColorMode, depth: SampleType) -> Session { + let mut s = Session::new(); + s.add_document(Document::with_background("t", Size::new(64, 64), mode, depth, Color::rgb(0.5, 0.5, 0.5)), None); + s.execute("select.rect", json!({"x": 0, "y": 0, "width": 32, "height": 64})).unwrap(); + s.execute("edit.fill", json!({"contents": "color", "color": "#000000"})).unwrap(); + s.execute("select.deselect", json!({})).unwrap(); + s +} + +fn stroke() -> Value { + json!({"points": [[48, 32]], "source": [32, 32], "size": 20, "hardness": 100}) +} + +/// The Background's native samples and the History length. +fn state(s: &Session) -> (Vec, usize) { + let st = s.active().unwrap(); + let surf = st.doc.layers[0].surface().unwrap(); + ((0..64).flat_map(|y| (0..64).flat_map(move |x| surf.pixel(x, y))).collect(), st.history.past_len()) +} + +#[test] +fn an_indexed_document_refuses_with_the_reference_words() { + let mut s = edged(ColorMode::Indexed, SampleType::U8); + let before = state(&s); + let err = s.execute("paint.healingBrush", stroke()).unwrap_err().to_string(); + assert_eq!(err, MESSAGE); + assert!(state(&s) == before, "nothing painted, no history state"); +} + +#[test] +fn other_modes_keep_healing_at_every_depth() { + let depths = [SampleType::U8, SampleType::U16, SampleType::F32]; + for mode in [ColorMode::Rgb, ColorMode::Grayscale, ColorMode::Cmyk, ColorMode::Lab] { + for depth in depths { + let mut s = edged(mode, depth); + let (pixels, steps) = state(&s); + s.execute("paint.healingBrush", stroke()).unwrap_or_else(|e| panic!("{mode:?}/{depth:?}: {e}")); + let after = state(&s); + assert!(after.0 != pixels, "{mode:?}/{depth:?}: healed"); + assert_eq!(after.1, steps + 1, "{mode:?}/{depth:?}: one history step"); + } + } +} + +#[test] +fn unmeasured_modes_never_get_the_indexed_refusal() { + for mode in [ColorMode::Bitmap, ColorMode::Duotone, ColorMode::Multichannel] { + let mut s = edged(mode, SampleType::U8); + let err = s.execute("paint.healingBrush", stroke()).err().map(|e| e.to_string()); + assert_ne!(err.as_deref(), Some(MESSAGE), "{mode:?}"); + } +} diff --git a/crates/ui-egui/src/healing_indexed_alert_tests.rs b/crates/ui-egui/src/healing_indexed_alert_tests.rs new file mode 100644 index 00000000000..727caf11f50 --- /dev/null +++ b/crates/ui-egui/src/healing_indexed_alert_tests.rs @@ -0,0 +1,104 @@ +//! #12112: in an Indexed Color document an ⌥-click and a stroke with the Healing Brush open the +//! reference application's alert with OK and change neither the clone source, the pixels nor the +//! History. RGB documents keep healing, and the Clone Stamp (not measured) keeps setting its source. + +use egui::Modifiers; +use serde_json::{Value, json}; + +use super::tests::{app, stripes}; +use super::*; +use crate::canvas::{ToolEvent, tool_event}; +use crate::state::DialogKind; + +const MESSAGE: &str = "Could not set the clone source because it does not work with Indexed Color images (convert image to RGB Color mode to edit)."; + +fn to_indexed(app: &mut PhotocraftApp) { + app.run("image.mode.indexedColor", json!({"palette": "exact", "forced": "none", "dither": "none"})).unwrap(); +} + +fn click(app: &mut PhotocraftApp, x: f64, mods: Modifiers) { + tool_event(app, ToolEvent::Down { x, y: 30.0, pressure: 1.0 }, mods); + tool_event(app, ToolEvent::Up { x, y: 30.0 }, mods); +} + +fn alerts(app: &PhotocraftApp) -> Vec { + app.ui.dialogs.iter().filter(|d| d.kind == DialogKind::Error).filter_map(|d| d.fields.get("message").and_then(Value::as_str).map(str::to_string)).collect() +} + +/// The clone source (the options bar's and the engine's), the layer's pixels and the History length. +type State = (Option<[f64; 2]>, Option<[f64; 2]>, Vec, usize); + +fn state(app: &PhotocraftApp) -> State { + let st = app.session.active().unwrap(); + let pixels = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().read_region(st.doc.bounds()); + (app.ui.clone_source, app.session.presets.clone.active().source, pixels, st.history.past_len()) +} + +/// The alert is up once; OK closes it and leaves `before` as it was. +fn alerted_once(app: &mut PhotocraftApp, before: &State) { + assert_eq!(alerts(app), [MESSAGE]); + assert!(state(app) == *before, "the source, the pixels and the History are unchanged"); + let id = app.ui.dialogs.iter().find(|d| d.kind == DialogKind::Error).map(|d| d.id).unwrap(); + crate::dialogs::confirm(app, id).unwrap(); + assert!(alerts(app).is_empty()); + assert!(state(app) == *before, "OK changes nothing"); +} + +/// An Indexed Color document with stripes to heal, the Healing Brush active. +fn indexed() -> PhotocraftApp { + let mut app = app(); + stripes(&mut app, 4, "pixels"); + to_indexed(&mut app); + app.ui.tool = Tool::Healing; + app +} + +#[test] +fn an_alt_click_alerts_and_sets_no_source() { + let mut app = indexed(); + let before = state(&app); + click(&mut app, 70.0, Modifiers::ALT); + alerted_once(&mut app, &before); +} + +#[test] +fn a_click_alerts_and_heals_nothing() { + let mut app = app(); + stripes(&mut app, 4, "pixels"); + app.ui.tool = Tool::Healing; + // The source set while the document was RGB: the stroke must still be refused. + click(&mut app, 70.0, Modifiers::ALT); + to_indexed(&mut app); + let before = state(&app); + assert!(before.0.is_some(), "the source is set"); + tool_event(&mut app, ToolEvent::Down { x: 20.0, y: 30.0, pressure: 1.0 }, Modifiers::NONE); + assert!(crate::canvas::live_retouch_points(&app).is_none(), "no live preview"); + tool_event(&mut app, ToolEvent::Up { x: 20.0, y: 30.0 }, Modifiers::NONE); + alerted_once(&mut app, &before); + // Without a source too: the same alert, not the status-bar hint. + let mut app = indexed(); + let before = state(&app); + click(&mut app, 20.0, Modifiers::NONE); + assert!(!app.ui.status_error, "{}", app.ui.status); + alerted_once(&mut app, &before); +} + +#[test] +fn an_rgb_document_keeps_healing_and_the_clone_stamp_its_source() { + let mut app = app(); + stripes(&mut app, 4, "pixels"); + app.ui.tool = Tool::Healing; + click(&mut app, 70.0, Modifiers::ALT); + let before = state(&app); + tool_event(&mut app, ToolEvent::Down { x: 20.0, y: 30.0, pressure: 1.0 }, Modifiers::NONE); + assert!(crate::canvas::live_retouch_points(&app).is_some(), "the live preview"); + tool_event(&mut app, ToolEvent::Up { x: 20.0, y: 30.0 }, Modifiers::NONE); + assert!(alerts(&app).is_empty()); + assert_eq!(state(&app).3, before.3 + 1, "one Healing Brush state"); + // Not measured in the reference: the Clone Stamp's ⌥-click in an Indexed document is kept. + let mut app = indexed(); + app.ui.tool = Tool::CloneStamp; + click(&mut app, 70.0, Modifiers::ALT); + assert!(alerts(&app).is_empty()); + assert_eq!(app.ui.clone_source, Some([70.0, 30.0])); +} diff --git a/crates/ui-egui/src/tools/retouch_ui.rs b/crates/ui-egui/src/tools/retouch_ui.rs index 175909908e8..58aac368dbb 100644 --- a/crates/ui-egui/src/tools/retouch_ui.rs +++ b/crates/ui-egui/src/tools/retouch_ui.rs @@ -27,6 +27,10 @@ pub(crate) fn alt_flipped(tool: Tool, alt: bool) -> Tool { /// `points` and `target`): shared by the live preview and the commit. `None` when no source /// point is set yet. pub(crate) fn clone_params(app: &PhotocraftApp) -> Option { + // Refused there (`healing_refused`): no live preview either. + if healing_in_indexed(app, app.ui.tool) { + return None; + } let o = &app.ui.tool_options; // The options bar's Mode is the tool's own brush's. let mode = crate::blend_keys::brush_tool_mode(app.ui.tool, app.session.tools.brush.mode); @@ -216,6 +220,9 @@ pub(crate) fn finish_stroke_with(app: &mut PhotocraftApp, tool: Tool, points: &[ Tool::ColorReplacement => ("paint.colorReplacement", color_replacement_params(app)), Tool::PatternStamp => ("paint.patternStamp", pattern_stamp_params(app, points)), Tool::Healing | Tool::CloneStamp => { + if healing_refused(app, tool) { + return true; + } let Some(p) = clone_params(app) else { // Option-click on the Mac, Alt-click on Windows. app.ui.status = if cfg!(target_os = "macos") { @@ -650,8 +657,28 @@ pub fn report_smart_result(app: &mut PhotocraftApp, res: Result, } } +/// The Healing Brush in an Indexed Color document: the reference's alert with OK at the ⌥-click +/// and at a stroke, and nothing changes (#12112). The engine refuses the stroke with the same words. +fn healing_refused(app: &mut PhotocraftApp, tool: Tool) -> bool { + if !healing_in_indexed(app, tool) { + return false; + } + crate::brush_refusal::prompt( + app, + tl!("Could not set the clone source because it does not work with Indexed Color images (convert image to RGB Color mode to edit)."), + ); + true +} + +fn healing_in_indexed(app: &PhotocraftApp, tool: Tool) -> bool { + tool == Tool::Healing && app.session.active().is_some_and(|st| st.doc.mode == photocraft_doc::ColorMode::Indexed) +} + /// ⌥-click with Clone Stamp / Healing Brush sets the source. pub fn set_source(app: &mut PhotocraftApp, x: f64, y: f64) { + if healing_refused(app, app.active_tool()) { + return; + } app.ui.clone_source = Some([x.round(), y.round()]); app.ui.clone_offset = None; let _ = app.run("cloneSource.set", json!({"source": [x.round(), y.round()]})); @@ -1176,3 +1203,7 @@ mod smudge_mode_bar_tests; #[cfg(test)] #[path = "../sponge_flow_field_tests.rs"] mod sponge_flow_field_tests; + +#[cfg(test)] +#[path = "../healing_indexed_alert_tests.rs"] +mod healing_indexed_alert_tests; diff --git a/crates/ui-egui/src/tools/retouch_ui/tests.rs b/crates/ui-egui/src/tools/retouch_ui/tests.rs index cae060ba6e6..a6eba29bd24 100644 --- a/crates/ui-egui/src/tools/retouch_ui/tests.rs +++ b/crates/ui-egui/src/tools/retouch_ui/tests.rs @@ -1,7 +1,7 @@ use super::*; use crate::canvas::{ToolEvent, tool_event}; -fn app() -> PhotocraftApp { +pub(super) fn app() -> PhotocraftApp { let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); app.run("file.new", json!({"width": 100, "height": 60})).unwrap(); app.run("tools.setBrush", json!({"brush": {"size": 16, "hardness": 1.0}})).unwrap(); @@ -22,7 +22,7 @@ fn active(app: &PhotocraftApp) -> &photocraft_doc::Layer { st.active_layer.and_then(|id| st.doc.layer(id)).unwrap() } -fn stripes(app: &mut PhotocraftApp, step: usize, target: &str) { +pub(super) fn stripes(app: &mut PhotocraftApp, step: usize, target: &str) { for x in (0..100).step_by(step) { app.run("paint.pencil", json!({"points": [[x, 0], [x, 60]], "size": 2, "color": "#606060", "target": target})).unwrap(); }