Skip to content

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

Content-Aware Scale: enlarging a 1 px wide or tall layer no longer panics (#387)

Armaan Sandhu9cf7c42f1d62
diff --git a/crates/algo/src/seam.rs b/crates/algo/src/seam.rs
index 2d5ad2874..f1c648ec9 100644
--- a/crates/algo/src/seam.rs
+++ b/crates/algo/src/seam.rs
@@ -143,7 +143,8 @@ pub fn carve_width_with(w: usize, h: usize, ch: usize, img: &[f32], protect: Opt
let mut tprot = prot.clone();
let mut dup: Vec<Vec<usize>> = vec![Vec::with_capacity(k); h];
for _ in 0..k {
- if tw <= 1 {
+ // A 1 px wide image's only seam is its one column: take it, so it is duplicated.
+ if tw == 0 {
break;
}
ctl.check()?;
@@ -360,6 +361,20 @@ mod tests {
assert!((0..4).all(|y| out[y * 8..y * 8 + 8].iter().any(|v| (*v - 0.51).abs() < 1e-6)));
}
+ #[test]
+ fn one_pixel_lines_enlarge_by_repeating() {
+ // Width 1 has no seam to spare: its one column is repeated. Height 1 likewise (through
+ // the transposed pass), alone or with the other axis.
+ let col = [0.1f32, 0.2, 0.3, 0.4, 0.5];
+ assert_eq!(carve(1, 5, 1, &col, None, 4, 5), col.iter().flat_map(|v| [*v; 4]).collect::<Vec<_>>());
+ assert_eq!(carve(5, 1, 1, &col, None, 5, 3), col.repeat(3));
+ assert_eq!(carve(1, 1, 3, &[0.7, 0.8, 0.9], None, 3, 2), [0.7, 0.8, 0.9].repeat(6));
+ for (w, h, nw, nh) in [(1, 5, 6, 10), (1, 17, 6, 34), (16, 1, 21, 2), (7, 1, 3, 8), (2, 1, 1, 8)] {
+ let img = vec![0.5f32; w * h * 4];
+ assert_eq!(carve(w, h, 4, &img, Some(&vec![1.0; w * h]), nw, nh).len(), nw * nh * 4, "{w}x{h} -> {nw}x{nh}");
+ }
+ }
+
#[test]
fn skin_rule() {
let px = [0.86f32, 0.62, 0.5, 0.2, 0.4, 0.8];
diff --git a/crates/engine/src/edit_menu_cmds/tests.rs b/crates/engine/src/edit_menu_cmds/tests.rs
index 4e5cbb07c..3004e19a2 100644
--- a/crates/engine/src/edit_menu_cmds/tests.rs
+++ b/crates/engine/src/edit_menu_cmds/tests.rs
@@ -184,6 +184,31 @@ fn content_aware_scale_params() {
assert!(s.execute("edit.contentAwareScale", json!({"protect": "no such channel"})).is_err());
}
+#[test]
+fn content_aware_scale_enlarges_a_one_pixel_line() {
+ // A 1 px wide (or tall) layer has no seam to spare, so enlarging it used to return the line
+ // unchanged and crash or write a short buffer. The line's single column (row) is repeated.
+ for (line, params, to) in [
+ (Rect::new(10, 0, 11, 48), json!({"width": 5}), [5, 48]),
+ (Rect::new(10, 0, 11, 48), json!({"width": 6, "height": 30}), [6, 30]),
+ (Rect::new(0, 7, 64, 8), json!({"height": 4}), [64, 4]),
+ (Rect::new(0, 7, 64, 8), json!({"width": 20, "height": 3}), [20, 3]),
+ ] {
+ let mut s = session(8);
+ s.execute("layer.new.layer", json!({})).unwrap();
+ s.edit("paint", |doc, active| {
+ doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap().fill_rect(line, &[1.0, 0.0, 0.0, 1.0]);
+ Ok(())
+ })
+ .unwrap();
+ let r = s.execute("edit.contentAwareScale", params.clone()).unwrap();
+ assert_eq!(r["to"], json!(to), "{params}");
+ let b = active(&s).surface().unwrap().content_bounds();
+ assert_eq!([b.width(), b.height()], to, "{params}");
+ assert_eq!(px(&s, b.x0 + b.width() as i32 - 1, b.y0 + b.height() as i32 - 1), vec![1.0, 0.0, 0.0, 1.0], "{params}");
+ }
+}
+
fn square_path(x: f64, y: f64, w: f64) -> Path {
Path::new(vec![Subpath::polygon(&[(x, y), (x + w, y), (x + w, y + w), (x, y + w)])])
}