diff --git a/crates/engine/src/cmd/retouch_cmds/dab.rs b/crates/engine/src/cmd/retouch_cmds/dab.rs index 73bb03986d..c9b4c27eb3 100644 --- a/crates/engine/src/cmd/retouch_cmds/dab.rs +++ b/crates/engine/src/cmd/retouch_cmds/dab.rs @@ -696,3 +696,7 @@ impl LiveDab { #[cfg(test)] #[path = "dodge_refuses_indexed_tests.rs"] mod dodge_refuses_indexed_tests; + +#[cfg(test)] +#[path = "sharpen_refuses_indexed_tests.rs"] +mod sharpen_refuses_indexed_tests; diff --git a/crates/engine/src/cmd/retouch_cmds/dodge_refuses_indexed_tests.rs b/crates/engine/src/cmd/retouch_cmds/dodge_refuses_indexed_tests.rs index a4da05e112..c2e54642e4 100644 --- a/crates/engine/src/cmd/retouch_cmds/dodge_refuses_indexed_tests.rs +++ b/crates/engine/src/cmd/retouch_cmds/dodge_refuses_indexed_tests.rs @@ -12,7 +12,7 @@ const INDEXED: &str = "Could not use the dodge tool because it does not work wit const BITMAP: &str = "Could not use the dodge tool because it does not work with Bitmap images (convert image to Grayscale mode to edit)."; /// A session with one 64 × 64 document of `mode` and `depth`, filled mid grey. -fn session(mode: ColorMode, depth: SampleType) -> Session { +pub(super) fn session(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 @@ -22,11 +22,11 @@ fn stroke() -> Value { json!({"points": [[20, 32], [44, 32]], "size": 20, "hardness": 100, "range": "midtones", "exposure": 100}) } -fn steps(s: &Session) -> usize { +pub(super) fn steps(s: &Session) -> usize { s.active().unwrap().history.past_len() } -fn pixels(s: &Session) -> Vec { +pub(super) fn pixels(s: &Session) -> Vec { 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() diff --git a/crates/engine/src/cmd/retouch_cmds/sharpen_refuses_indexed_tests.rs b/crates/engine/src/cmd/retouch_cmds/sharpen_refuses_indexed_tests.rs new file mode 100644 index 0000000000..9a793d11a2 --- /dev/null +++ b/crates/engine/src/cmd/retouch_cmds/sharpen_refuses_indexed_tests.rs @@ -0,0 +1,72 @@ +//! #12953: the Sharpen tool refuses Indexed Color and Bitmap documents with the reference +//! application's messages, changing nothing and adding no history; the live stroke is refused the +//! same way. RGB, Grayscale, CMYK and Lab at every depth, Multichannel and Duotone keep sharpening. + +use photocraft_color::{ColorMode, SampleType}; +use serde_json::json; + +use super::dodge_refuses_indexed_tests::{pixels, session, steps}; +use super::*; + +const INDEXED: &str = "Could not use the sharpen tool because it does not work with Indexed Color images (convert image to RGB Color mode to edit)."; +const BITMAP: &str = "Could not use the sharpen tool because it does not work with Bitmap images (convert image to Grayscale mode to edit)."; + +/// The grey document with its left half black: an edge for the stroke to sharpen. +fn edged(mode: ColorMode, depth: SampleType) -> Session { + let mut s = session(mode, depth); + 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": [[20, 32], [44, 32]], "size": 20, "hardness": 100, "strength": 100, "protectDetail": false}) +} + +/// How far apart the two pixels across the edge are, summed over the native channels. +fn edge_contrast(s: &Session) -> f32 { + let surf = s.active().unwrap().doc.layers[0].surface().unwrap(); + surf.pixel(31, 32).iter().zip(surf.pixel(32, 32)).map(|(a, b)| (a - b).abs()).sum() +} + +#[test] +fn indexed_and_bitmap_documents_refuse_with_the_reference_words() { + // Per mode: the command's error, the live stroke's error, pixels kept, history kept. + let got = [ColorMode::Indexed, ColorMode::Bitmap].map(|mode| { + let mut s = edged(mode, SampleType::U8); + let (before, n) = (pixels(&s), steps(&s)); + let err = s.execute("paint.sharpen", stroke()).err().map(|e| e.to_string()); + let live = LiveDab::begin(&s, "paint.sharpen", &stroke()).err().map(|e| e.to_string()); + (err, live, pixels(&s) == before, steps(&s) == n) + }); + let want = [INDEXED, BITMAP].map(|m| (Some(m.to_string()), Some(m.to_string()), true, true)); + assert_eq!(got, want); +} + +#[test] +fn other_modes_keep_sharpening_at_every_depth() { + let depths = [SampleType::U8, SampleType::U16, SampleType::F32]; + let measured = [ColorMode::Rgb, ColorMode::Grayscale, ColorMode::Cmyk, ColorMode::Lab].into_iter().flat_map(|m| depths.map(|d| (m, d))); + // Not measured in the reference: they keep today's behaviour. + let unmeasured = [(ColorMode::Multichannel, SampleType::U8), (ColorMode::Duotone, SampleType::U8)]; + for (mode, depth) in measured.chain(unmeasured) { + let mut s = edged(mode, depth); + let (contrast, n) = (edge_contrast(&s), steps(&s)); + let live = LiveDab::begin(&s, "paint.sharpen", &stroke()).map(|_| ()); + s.execute("paint.sharpen", stroke()).unwrap_or_else(|e| panic!("{mode:?}/{depth:?}: {e}")); + assert!(edge_contrast(&s) > contrast, "{mode:?}/{depth:?}: the edge got sharper ({contrast} -> {})", edge_contrast(&s)); + assert_eq!(steps(&s), n + 1, "{mode:?}/{depth:?}: one history step"); + assert!(live.is_ok(), "{mode:?}/{depth:?}: live"); + } +} + +#[test] +fn dodge_keeps_its_refusal_and_blur_never_gets_the_sharpen_text() { + let mut s = edged(ColorMode::Indexed, SampleType::U8); + let err = s.execute("paint.dodge", json!({"points": [[20, 32], [44, 32]], "size": 20, "exposure": 100})).unwrap_err().to_string(); + assert!(err.starts_with("Could not use the dodge tool"), "{err}"); + // Blur may get its own refusal (#12656), never Sharpen's. + let blur = s.execute("paint.blur", stroke()).err().map(|e| e.to_string()).unwrap_or_default(); + assert!(!blur.contains("sharpen tool"), "{blur}"); +}