Skip to content

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

Perspective Crop and Rotate Arbitrary keep the Background opaque (#7138)

David Mann193f495e5a0f
diff --git a/crates/engine/src/mode_cmds.rs b/crates/engine/src/mode_cmds.rs
index fe22a192c..9c51731a2 100644
--- a/crates/engine/src/mode_cmds.rs
+++ b/crates/engine/src/mode_cmds.rs
@@ -75,7 +75,8 @@ pub(crate) fn rotate_arbitrary(s: &mut Session, p: &Value) -> Result<Value> {
if deg.rem_euclid(360.0).abs() < 1e-9 {
return Ok(json!({"width": s.active().map(|d| d.doc.size.width), "height": s.active().map(|d| d.doc.size.height)}));
}
- let bg = s.tools.background;
+ // The Background stays opaque, whatever the background colour's alpha.
+ let bg = [s.tools.background[0], s.tools.background[1], s.tools.background[2], 1.0];
// Color Sampler points turn with the canvas (the same map the edit applies to the geometry).
{
let old = s.active().ok_or(EngineError::NoDocument)?.doc.size;
diff --git a/crates/engine/src/mode_cmds/tests.rs b/crates/engine/src/mode_cmds/tests.rs
index d6d020699..fad300e27 100644
--- a/crates/engine/src/mode_cmds/tests.rs
+++ b/crates/engine/src/mode_cmds/tests.rs
@@ -81,6 +81,15 @@ fn rotate_arbitrary_other_modes() {
}
}
+#[test]
+fn rotate_arbitrary_keeps_the_background_opaque_with_a_translucent_background_colour() {
+ let mut s = session(40, 20, 8, "rgb");
+ s.execute("tools.setColors", json!({"background": "#00ff0080"})).unwrap();
+ s.execute("image.rotation.arbitrary", json!({"angle": 30})).unwrap();
+ let corner = doc(&s).layers[0].surface().unwrap().rgba(0, 0);
+ assert_eq!(corner, [0.0, 1.0, 0.0, 1.0], "the uncovered corner is opaque background colour");
+}
+
#[test]
fn rotate_arbitrary_rotates_grouped_locked_children_and_preserves_lock() {
let mut s = Session::new();
diff --git a/crates/engine/src/perspective_crop_cmds.rs b/crates/engine/src/perspective_crop_cmds.rs
index 8acaf42ce..776b2b4d0 100644
--- a/crates/engine/src/perspective_crop_cmds.rs
+++ b/crates/engine/src/perspective_crop_cmds.rs
@@ -188,7 +188,8 @@ fn warp_layers(layers: &mut [Layer], h: &Homography, interp: Interp, clip: Rect,
fn apply(doc: &mut Document, q: &[[f64; 2]; 4], h: &Homography, size: (u32, u32), interp: Interp, bg: [f32; 4]) -> Result<()> {
let (w, ht) = size;
let clip = Rect::from_xywh(0, 0, w, ht);
- let bg = photocraft_raster::from_rgba(&doc.pixel_format(), bg);
+ // The Background stays opaque, whatever the background colour's alpha.
+ let bg = photocraft_raster::from_rgba(&doc.pixel_format(), [bg[0], bg[1], bg[2], 1.0]);
let background_id = doc.background().map(|l| l.id);
warp_layers(&mut doc.layers, h, interp, clip, &bg, background_id)?;
for ch in doc.channels.iter_mut().chain(doc.quick_mask.as_mut()) {
@@ -298,6 +299,18 @@ mod tests {
}
}
+ /// Corners past the canvas: the uncovered Background is the background colour, opaque even
+ /// when that colour has alpha (the Background stays opaque).
+ #[test]
+ fn uncovered_background_is_opaque() {
+ let q = [[-20.0, -20.0], [180.0, -20.0], [180.0, 140.0], [-20.0, 140.0]];
+ let mut s = skewed_ramp(SampleType::U8, &q, 100.0, 80.0);
+ s.execute("tools.setColors", json!({"background": "#00ff0080"})).unwrap();
+ s.execute(CMD, json!({"corners": q, "width": 100, "height": 80})).unwrap();
+ let corner = s.active().unwrap().doc.layers[0].surface().unwrap().rgba(0, 0);
+ assert_eq!(corner, [0.0, 1.0, 0.0, 1.0]);
+ }
+
/// Bad quads and params are refused with an error, and the document is untouched.
#[test]
fn perspective_crop_rejects_bad_quads() {