PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Bitmap mode: validate the halftone screen angle (#4214) (#4215)
David Mann468d1c622a3b
diff --git a/crates/engine/src/mode_cmds.rs b/crates/engine/src/mode_cmds.rsindex 319a6e533..fe22a192c 100644--- a/crates/engine/src/mode_cmds.rs+++ b/crates/engine/src/mode_cmds.rs@@ -27,6 +27,14 @@ fn num(p: &Value, key: &str, default: f32) -> f32 {p.get(key).and_then(Value::as_f64).map_or(default, |v| v as f32)}+/// A finite number param, `default` when absent or null; anything else is a bad-params error.+fn finite_num(p: &Value, cmd: &str, key: &str, default: f64) -> Result<f64> {+ match p.get(key) {+ None | Some(Value::Null) => Ok(default),+ Some(v) => v.as_f64().filter(|f| f.is_finite()).ok_or_else(|| bad(cmd, format!("`{key}` must be a number"))),+ }+}+fn str_or<'a>(p: &'a Value, key: &str, default: &'a str) -> &'a str {p.get(key).and_then(Value::as_str).unwrap_or(default)}@@ -388,9 +396,14 @@ fn bitmap(s: &mut Session, p: &Value) -> Result<Value> {"pattern" => BitmapMethod::Pattern,"diffusion" => BitmapMethod::Diffusion,"halftone" => {- let freq = num(p, "frequency", 53.0).clamp(1.0, 999.0);+ // Photoshop's Halftone Screen: frequency 1..999 lines/inch, angle -180..180 degrees.+ let freq = finite_num(p, CMD, "frequency", 53.0)?.clamp(1.0, 999.0) as f32;+ let angle = finite_num(p, CMD, "angle", 45.0)?;+ if !(-180.0..=180.0).contains(&angle) {+ return Err(bad(CMD, format!("angle {angle} is outside -180..180")));+ }let shape = HalftoneShape::from_id(str_or(p, "shape", "round")).ok_or_else(|| bad(CMD, "shape: round|ellipse|line|square|diamond|cross"))?;- BitmapMethod::Halftone { cell: (dpi / freq).max(2.0), angle: num(p, "angle", 45.0), shape }+ BitmapMethod::Halftone { cell: (dpi / freq).max(2.0), angle: angle as f32, shape }}m => {return Err(bad(CMD, format!("method `{m}` (threshold|pattern|diffusion|halftone)")));diff --git a/crates/engine/src/mode_cmds/tests.rs b/crates/engine/src/mode_cmds/tests.rsindex 8ac4e6d33..d6d020699 100644--- a/crates/engine/src/mode_cmds/tests.rs+++ b/crates/engine/src/mode_cmds/tests.rs@@ -291,6 +291,38 @@ fn bitmap_methods_need_grayscale() {}}+#[test]+fn bitmap_halftone_rejects_bad_angles_and_frequencies_without_editing_the_document() {+ // Photoshop's Halftone Screen takes an angle in -180..180 degrees (#4214): 1e39 used to become+ // inf, then NaN in the screen rotation, and an almost white bitmap landed in history.+ let mut s = session(16, 16, 8, "grayscale");+ let before = s.active().unwrap().history.past_len();+ let bad = [+ ("angle", json!(1e39)),+ ("angle", json!(500)),+ ("angle", json!(-181)),+ ("angle", json!(180.01)),+ ("angle", json!("x")),+ ("frequency", json!("x")),+ ("frequency", json!(true)),+ ];+ for (key, value) in bad {+ let mut p = json!({"method": "halftone"});+ p[key] = value.clone();+ assert!(s.execute("image.mode.bitmap", p).is_err(), "{key}: {value}");+ assert_eq!(s.active().unwrap().history.past_len(), before, "{key}: {value}");+ assert_eq!(doc(&s).mode, ColorMode::Grayscale, "{key}: {value}");+ }+ for angle in [-180.0, 30.0, 180.0] {+ s.execute("image.mode.bitmap", json!({"method": "halftone", "angle": angle})).unwrap();+ assert_eq!(doc(&s).mode, ColorMode::Bitmap, "{angle}");+ s.execute("edit.undo", json!({})).unwrap();+ }+ // Frequency stays clamped to the documented 1..999 lines/inch.+ s.execute("image.mode.bitmap", json!({"method": "halftone", "frequency": 1e39})).unwrap();+ assert_eq!(doc(&s).mode, ColorMode::Bitmap);+}+#[test]fn duotone_inks_and_display() {let mut s = session(16, 8, 8, "gray");