Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

image.mode.bitmap: an unknown halftone shape is a bad-params error (#1842)

xsvm9b69ba89aa30
diff --git a/crates/algo/src/quantize.rs b/crates/algo/src/quantize.rs
index 3b6feb20f..589c9f196 100644
--- a/crates/algo/src/quantize.rs
+++ b/crates/algo/src/quantize.rs
@@ -483,15 +483,16 @@ pub enum HalftoneShape {
}
impl HalftoneShape {
- pub fn from_id(s: &str) -> Self {
- match s {
+ pub fn from_id(s: &str) -> Option<Self> {
+ Some(match s {
+ "round" => HalftoneShape::Round,
"ellipse" => HalftoneShape::Ellipse,
"line" => HalftoneShape::Line,
"square" => HalftoneShape::Square,
"diamond" => HalftoneShape::Diamond,
"cross" => HalftoneShape::Cross,
- _ => HalftoneShape::Round,
- }
+ _ => return None,
+ })
}
}
diff --git a/crates/engine/src/mode_cmds.rs b/crates/engine/src/mode_cmds.rs
index f9ee2154e..ad8ca0179 100644
--- a/crates/engine/src/mode_cmds.rs
+++ b/crates/engine/src/mode_cmds.rs
@@ -346,7 +346,8 @@ fn bitmap(s: &mut Session, p: &Value) -> Result<Value> {
"diffusion" => BitmapMethod::Diffusion,
"halftone" => {
let freq = num(p, "frequency", 53.0).clamp(1.0, 999.0);
- BitmapMethod::Halftone { cell: (dpi / freq).max(2.0), angle: num(p, "angle", 45.0), shape: HalftoneShape::from_id(str_or(p, "shape", "round")) }
+ 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 }
}
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.rs
index af9eb7989..850da98f8 100644
--- a/crates/engine/src/mode_cmds/tests.rs
+++ b/crates/engine/src/mode_cmds/tests.rs
@@ -211,6 +211,13 @@ fn bitmap_methods_need_grayscale() {
s.execute("edit.undo", json!({})).unwrap();
}
assert!(s.execute("image.mode.bitmap", json!({"method": "nope"})).is_err());
+ // An unlisted halftone shape is a bad-params error, not a silent Round (#507).
+ assert!(s.execute("image.mode.bitmap", json!({"method": "halftone", "shape": "hexagon"})).is_err());
+ assert_eq!(doc(&s).mode, ColorMode::Grayscale);
+ for shape in ["round", "ellipse", "line", "square", "diamond", "cross"] {
+ s.execute("image.mode.bitmap", json!({"method": "halftone", "shape": shape})).unwrap();
+ s.execute("edit.undo", json!({})).unwrap();
+ }
}
#[test]