Skip to content

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

Rotate Canvas Arbitrary: keep the angle at full precision, so rotated crops land on the pixel (#7369) (#7370)

David Manna4c7b5ef8965
diff --git a/crates/engine/src/mode_cmds.rs b/crates/engine/src/mode_cmds.rs
index 840f1c609..4625368ff 100644
--- a/crates/engine/src/mode_cmds.rs
+++ b/crates/engine/src/mode_cmds.rs
@@ -63,7 +63,8 @@ pub fn rotated_size(size: Size, deg: f64) -> Size {
pub(crate) fn rotate_arbitrary(s: &mut Session, p: &Value) -> Result<Value> {
const CMD: &str = "image.rotation.arbitrary";
- let angle = f64::from(num(p, "angle", 0.0));
+ // At full precision: an f32 angle can grow the canvas 1 px past what `rotated_size` gives callers.
+ let angle = p.get("angle").and_then(Value::as_f64).unwrap_or(0.0);
if !angle.is_finite() || angle.abs() > 3600.0 {
return Err(bad(CMD, "angle out of range"));
}
diff --git a/crates/engine/src/mode_cmds/tests.rs b/crates/engine/src/mode_cmds/tests.rs
index a78a11db7..d506128d7 100644
--- a/crates/engine/src/mode_cmds/tests.rs
+++ b/crates/engine/src/mode_cmds/tests.rs
@@ -81,6 +81,18 @@ fn rotate_arbitrary_other_modes() {
}
}
+#[test]
+fn rotate_arbitrary_keeps_the_full_precision_angle() {
+ // Rounded to f32 this angle crosses a whole pixel: 1003 px instead of 1002, so a rotated crop
+ // that sized its canvas with `rotated_size` at f64 would land 1 px off.
+ let angle = 0.114_706_514_493_370_71;
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 1000, "height": 1000})).unwrap();
+ let r = s.execute("image.rotation.arbitrary", json!({"angle": angle})).unwrap();
+ assert_eq!(rotated_size(Size::new(1000, 1000), angle), Size::new(1002, 1002));
+ assert_eq!((r["width"].as_u64(), r["height"].as_u64()), (Some(1002), Some(1002)));
+}
+
#[test]
fn rotate_arbitrary_rotates_grouped_locked_children_and_preserves_lock() {
let mut s = Session::new();