Merge pull request #13043 from rmsff/fix/red-eye-refuses-non-rgb-12575

Red Eye: refuse CMYK, Grayscale and Indexed Color documents
This commit is contained in:
David Mann authored and GitHub committed 2026-11-11 01:26:57 +01:00
commit c882bfd832
3 files changed
+147 -19

No files matched your search

+27 -8
View File
@@ -16,6 +16,9 @@ use crate::{EngineError, Result, Session};
const CMD: &str = "paint.redEye";
const MISS: &str = "no red-eye pixels near the click";
/// The reference's alert for a Red Eye click in a CMYK, Grayscale or Indexed Color document (#12575).
pub const MODE_REFUSAL: &str = "Could not use the red eye removal tool because it only works in full color modes.";
fn bad(msg: impl Into<String>) -> EngineError {
EngineError::BadParams { cmd: CMD.into(), msg: msg.into() }
}
@@ -226,7 +229,17 @@ fn enabled(s: &Session) -> std::result::Result<(), String> {
Ok(())
}
/// By the document's mode, not its pixel format: Indexed Color pixels are stored as RGB. Bitmap,
/// Duotone and Multichannel were not measured and keep running.
fn mode_refusal(s: &Session) -> Result<()> {
match s.active().map(|d| d.doc.mode) {
Some(ColorMode::Cmyk | ColorMode::Grayscale | ColorMode::Indexed) => Err(EngineError::Other(MODE_REFUSAL.into())),
_ => Ok(()),
}
}
fn red_eye(s: &mut Session, p: &Value) -> Result<Value> {
mode_refusal(s)?;
let pts = clicks(p)?;
let pupil_raw = num(p, "pupilSize", 50.0)?;
let darken = num(p, "darken", 50.0)?;
@@ -407,7 +420,7 @@ mod tests {
}
#[test]
fn cmyk_document_keeps_untouched_pixels_bit_exact() {
fn cmyk_document_is_refused_and_keeps_its_pixels_bit_exact() {
let mut s = session(64, 64, 8, "cmyk");
assert_eq!(s.active().unwrap().doc.pixel_format().mode, ColorMode::Cmyk);
// Rich black (all four inks) around the eye: a naive RGBA round trip regenerates K.
@@ -438,25 +451,31 @@ mod tests {
let surf = d.doc.layer(d.active_layer.unwrap()).unwrap().surface().unwrap();
surf.read_region(Rect::new(x, y, x + 1, y + 1))
};
let probes = [(32, 18), (2, 2), (50, 40)];
let probes = [(32, 26), (32, 18), (2, 2), (50, 40)];
let before: Vec<Vec<f32>> = probes.iter().map(|&(x, y)| raw(&s, x, y)).collect();
let r = s.execute("paint.redEye", json!({"x": 32, "y": 32})).unwrap();
assert!(r["pixels"].as_u64().unwrap() > 0, "{r}");
let past = s.active().unwrap().history.past_len();
let err = s.execute("paint.redEye", json!({"x": 32, "y": 32})).unwrap_err().to_string();
assert_eq!(err, MODE_REFUSAL);
for (&(x, y), b) in probes.iter().zip(&before) {
assert_eq!(&raw(&s, x, y), b, "untouched CMYK pixel ({x},{y}) inside the window changed");
assert_eq!(&raw(&s, x, y), b, "CMYK pixel ({x},{y}) changed");
}
assert!(rgba(&s, 32, 26)[0] < 0.7, "pupil corrected");
assert_eq!(s.active().unwrap().history.past_len(), past, "no history state");
}
#[test]
fn gray_document_misses() {
fn gray_document_is_refused() {
let mut s = session(24, 24, 16, "gray");
paint_layer(&mut s, |_, _| [0.5, 0.5, 0.5, 1.0]);
assert_eq!(s.active().unwrap().doc.pixel_format().mode, ColorMode::Grayscale);
assert!(s.execute("paint.redEye", json!({"x": 12, "y": 12})).is_err());
let err = s.execute("paint.redEye", json!({"x": 12, "y": 12})).unwrap_err().to_string();
assert_eq!(err, MODE_REFUSAL);
}
}
#[cfg(test)]
#[path = "redeye_history_state_tests.rs"]
mod redeye_history_state_tests;
#[cfg(test)]
#[path = "redeye_color_mode_tests.rs"]
mod redeye_color_mode_tests;
@@ -0,0 +1,93 @@
//! #12575: the Red Eye tool refuses CMYK, Grayscale and Indexed Color documents with the
//! reference's alert text and leaves the document and History unchanged. RGB (8, 16, 32 bit) and
//! Lab still correct the pupil; Bitmap, Duotone and Multichannel were not measured and keep
//! today's behaviour (no refusal).
use serde_json::{Value, json};
use super::*;
fn new_doc(mode: &str, depth: u64) -> Session {
let mut s = Session::new();
s.execute("file.new", json!({"width": 64, "height": 64, "mode": mode, "depth": depth})).unwrap();
s
}
/// A red pupil on its own layer.
fn red_pupil(s: &mut Session) {
s.execute("layer.new.layer", json!({})).unwrap();
s.execute("paint.pencil", json!({"points": [[32, 32]], "size": 14, "hardness": 100, "color": "#f21818"})).unwrap();
}
fn assert_refused(s: &mut Session, params: Value, case: &str) {
let (doc, steps) = (s.active().unwrap().doc.clone(), s.active().unwrap().history.past_len());
let err = s.execute("paint.redEye", params).unwrap_err().to_string();
assert_eq!(err, MODE_REFUSAL, "{case}");
assert!(std::sync::Arc::ptr_eq(&doc, &s.active().unwrap().doc), "{case}: document unchanged");
assert_eq!(s.active().unwrap().history.past_len(), steps, "{case}: no history state");
}
#[test]
fn the_refusal_is_the_reference_alert_text() {
assert_eq!(MODE_REFUSAL, "Could not use the red eye removal tool because it only works in full color modes.");
}
#[test]
fn cmyk_and_grayscale_documents_are_refused() {
for (mode, depths) in [("cmyk", &[8u64, 16][..]), ("gray", &[8, 16, 32][..])] {
for &depth in depths {
let mut s = new_doc(mode, depth);
red_pupil(&mut s);
assert_refused(&mut s, json!({"x": 32, "y": 32}), &format!("{mode} {depth} bit"));
}
}
}
#[test]
fn indexed_color_documents_are_refused() {
let mut s = new_doc("rgb", 8);
red_pupil(&mut s);
s.execute("image.mode.indexedColor", json!({"palette": "exact", "dither": "none", "forced": "none"})).unwrap();
assert_eq!(s.active().unwrap().doc.mode, ColorMode::Indexed);
assert_refused(&mut s, json!({"x": 32, "y": 32}), "indexed");
assert_refused(&mut s, json!({"points": [[32, 32], [10, 10]]}), "indexed, a points batch");
}
/// Converting to Indexed Color flattens the document, so a script's layer id is gone: the mode
/// is refused before the layer is looked up.
#[test]
fn the_refusal_comes_before_the_layer_lookup() {
let mut s = new_doc("rgb", 8);
red_pupil(&mut s);
let id = s.active().unwrap().active_layer.unwrap();
s.execute("image.mode.indexedColor", json!({"palette": "exact", "dither": "none", "forced": "none"})).unwrap();
assert_refused(&mut s, json!({"x": 32, "y": 32, "layer": id.0}), "a flattened layer id");
}
#[test]
fn rgb_and_lab_documents_still_correct_the_pupil() {
for (mode, depth) in [("rgb", 8), ("rgb", 16), ("rgb", 32), ("lab", 8), ("lab", 16)] {
let mut s = new_doc(mode, depth);
red_pupil(&mut s);
let steps = s.active().unwrap().history.past_len();
let r = s.execute("paint.redEye", json!({"x": 32, "y": 32})).unwrap_or_else(|e| panic!("{mode} {depth}: {e}"));
assert!(r["pixels"].as_u64().unwrap() > 0, "{mode} {depth}: {r}");
assert_eq!(s.active().unwrap().history.past_len(), steps + 1, "{mode} {depth}");
}
}
#[test]
fn unmeasured_modes_are_not_refused() {
let mut multi = new_doc("rgb", 8);
red_pupil(&mut multi);
multi.execute("image.mode.multichannel", json!({})).unwrap();
let mut bitmap = new_doc("gray", 8);
bitmap.execute("image.mode.bitmap", json!({})).unwrap();
let mut duotone = new_doc("gray", 8);
duotone.execute("image.mode.duotone", json!({"type": "monotone"})).unwrap();
for (mut s, mode) in [(multi, ColorMode::Multichannel), (bitmap, ColorMode::Bitmap), (duotone, ColorMode::Duotone)] {
assert_eq!(s.active().unwrap().doc.mode, mode);
let r = s.execute("paint.redEye", json!({"x": 32, "y": 32}));
assert!(r.as_ref().err().is_none_or(|e| e.to_string() != MODE_REFUSAL), "{mode:?}: {r:?}");
}
}
+27 -11
View File
@@ -425,6 +425,32 @@ fn mixer_press_failed(app: &mut PhotocraftApp, message: String) {
}
}
/// A Red Eye click. CMYK, Grayscale and Indexed Color documents get the reference's alert
/// (#12575), once even if more clicks arrive while it is open (`ui.pointer` can send them).
fn red_eye_click(app: &mut PhotocraftApp, x: f64, y: f64) {
use photocraft_engine::redeye_cmds::MODE_REFUSAL;
let o = app.ui.tool_options.clone();
let r = app.run(
"paint.redEye",
json!({
"x": x.floor(),
"y": y.floor(),
"pupilSize": o.red_eye_pupil_size.clamp(1.0, 100.0),
"darken": o.red_eye_darken.clamp(0.0, 100.0),
"target": paint_target(app),
}),
);
if r.is_err_and(|e| e == MODE_REFUSAL) {
app.ui.status.clear();
app.ui.status_error = false;
let message = tl!(MODE_REFUSAL);
let kind = crate::state::DialogKind::Error;
if !app.ui.dialogs.iter().any(|d| d.kind == kind && d.fields.get("message").and_then(|m| m.as_str()) == Some(message)) {
app.ui.open_dialog(kind, json!({ "message": message }).as_object().cloned().unwrap_or_default());
}
}
}
/// The Brush's chosen Clear paints as Normal on the Background, and its Mode then reads Normal
/// (#11897): at the press of a Brush stroke on the Background's own pixels, Clear (chosen in the
/// options bar, not a held `~`) is turned off, so the stroke is sent as Normal. A Background whose
@@ -584,17 +610,7 @@ pub(super) fn tool_event_with_view(app: &mut PhotocraftApp, ev: ToolEvent, mods:
return;
}
Tool::RedEye => {
let o = app.ui.tool_options.clone();
let _ = app.run(
"paint.redEye",
json!({
"x": x.floor(),
"y": y.floor(),
"pupilSize": o.red_eye_pupil_size.clamp(1.0, 100.0),
"darken": o.red_eye_darken.clamp(0.0, 100.0),
"target": paint_target(app),
}),
);
red_eye_click(app, x, y);
return;
}
Tool::PolygonLasso => {