Modern Apps git

← Modern-Apps.git

photos: add fx Rust tests and fix kernel/liquify/inpaint defects

Commit
0f64c6d15805253bdd8ebd6791ce03f2c5c09433
Author
Vayun Mathur <[email protected]>
When
2026-11-17 21:46 UTC (18 days ago)
Parents
c7cc354
54 new tests (15 -> 69): pixel packing, blur kernels and masked blur, sharpen, stylize, liquify, inpaint. Fixes found by the tests: gaussian_kernel(0) produced a NaN kernel and negative radius attempted a huge allocation (radius <= 0 now returns the identity kernel); unknown liquify tool ordinals fell through to Reconstruct and corrupted displacements (now no-ops); inpaint panicked on degenerate masks (early return); inpaint diffusion could never fill a rectangular hole because the crop excluded the known-pixel boundary (box expanded by 1px). cargo test -p photos_fx green.

Diff

photos/src/main/rust/src/blur.rs
diff --git a/photos/src/main/rust/src/blur.rs b/photos/src/main/rust/src/blur.rsindex c75f9dee1f..e6d0192b0d 100644--- a/photos/src/main/rust/src/blur.rs+++ b/photos/src/main/rust/src/blur.rs@@ -5,6 +5,11 @@ use crate::pixel::*;  pub fn gaussian_kernel(radius: i32) -> Vec<f32> {+    if radius <= 0 {+        // Degenerate kernel is the identity impulse; sigma would be 0+        // (radius 0 -> NaN weights) or the size negative (radius < 0).+        return vec![1.0];+    }     let size = (radius * 2 + 1) as usize;     let mut kernel = vec![0f32; size];     let sigma = radius as f32 / 3f32;
photos/src/main/rust/src/blur_tests.rs
diff --git a/photos/src/main/rust/src/blur_tests.rs b/photos/src/main/rust/src/blur_tests.rsnew file mode 100644index 0000000000..9a2d701941--- /dev/null+++ b/photos/src/main/rust/src/blur_tests.rs@@ -0,0 +1,252 @@+//! Unit tests for `blur` (kernel, gaussian, motion/radial, masked blur).+use crate::blur::*;+use crate::pixel::{a, b, g, pack, r};++fn solid(w: usize, h: usize, p: i32) -> Vec<i32> {+    vec![p; w * h]+}+fn gradient(w: usize, h: usize) -> Vec<i32> {+    let mut v = Vec::with_capacity(w * h);+    for y in 0..h {+        for x in 0..w {+            v.push(pack(255, (x * 255 / w.max(1)) as i32, (y * 255 / h.max(1)) as i32, 128));+        }+    }+    v+}+fn chan(p: i32, shift: u32) -> i32 {+    ((p as u32 >> shift) & 0xFF) as i32+}+fn assert_near_px(p: i32, q: i32, tol: i32) {+    for s in [24u32, 16, 8, 0] {+        assert!((chan(p, s) - chan(q, s)).abs() <= tol, "px {p:08x} vs {q:08x}");+    }+}++// ---- gaussian_kernel -------------------------------------------------------++#[test]+fn kernel_is_normalised_symmetric_and_peaked() {+    for radius in [1, 2, 3, 5, 8] {+        let k = gaussian_kernel(radius);+        assert_eq!(k.len(), (radius * 2 + 1) as usize);+        let sum: f32 = k.iter().sum();+        assert!((sum - 1.0).abs() < 1e-5, "radius {radius} sum {sum}");+        for i in 0..k.len() {+            assert!(k[i] > 0.0, "weights positive");+            assert!((k[i] - k[k.len() - 1 - i]).abs() < 1e-7, "symmetric");+        }+        let mid = k.len() / 2;+        assert!(k[mid] >= k[mid - 1] && k[mid] >= k[mid + 1], "centre is peak");+        for i in mid..k.len() - 1 {+            assert!(k[i] >= k[i + 1], "monotone falloff");+        }+    }+}++#[test]+fn kernel_degenerate_radius_is_unit_impulse() {+    // radius 0 must be [1.0] (identity), not NaN from sigma == 0.+    assert_eq!(gaussian_kernel(0), vec![1.0]);+    // Negative radius must not attempt a huge allocation / panic.+    assert_eq!(gaussian_kernel(-1), vec![1.0]);+    assert_eq!(gaussian_kernel(-10), vec![1.0]);+}++#[test]+fn kernel_radius1_weights() {+    // sigma = 1/3: exp(-4.5) ~= 0.011109, normalised by 1.022218.+    let k = gaussian_kernel(1);+    assert!((k[0] - 0.010868).abs() < 1e-4, "k0={}", k[0]);+    assert!((k[1] - 0.978264).abs() < 1e-4, "k1={}", k[1]);+    assert_eq!(k[0], k[2]);+}++// ---- gaussian_blur ---------------------------------------------------------++#[test]+fn gaussian_nonpositive_radius_is_copy() {+    let src = gradient(7, 5);+    assert_eq!(gaussian_blur(&src, 7, 5, 0), src);+    assert_eq!(gaussian_blur(&src, 7, 5, -3), src);+}++#[test]+fn gaussian_uniform_stays_uniform_within_truncation() {+    for radius in [1, 2, 6] {+        let src = solid(9, 7, pack(200, 40, 130, 210));+        let out = gaussian_blur(&src, 9, 7, radius);+        assert_eq!(out.len(), src.len());+        for p in out {+            assert_near_px(p, src[0], 2);+            assert_eq!(a(p), 200, "uniform alpha channel blurs to itself (tol via pack)");+        }+    }+}++#[test]+fn gaussian_impulse_is_symmetric_and_spreads() {+    let (w, h) = (9, 9);+    let mut src = solid(w, h, pack(255, 0, 0, 0));+    src[4 * w + 4] = pack(255, 255, 255, 255);+    let out = gaussian_blur(&src, w, h, 2);+    let centre = r(out[4 * w + 4]);+    assert!(centre > 0 && centre < 255, "centre dimmed by spread: {centre}");+    // Symmetry under both flips (exact) and transpose (within the <=1+    // per-channel truncation asymmetry of the two separable passes:+    // trunc(trunc(255*a)*b) vs trunc(trunc(255*b)*a) can differ by one).+    for y in 0..h {+        for x in 0..w {+            assert_near_px(out[y * w + x], out[x * w + y], 1);+            assert_eq!(out[y * w + x], out[y * w + (w - 1 - x)], "h-flip");+            assert_eq!(out[y * w + x], out[(h - 1 - y) * w + x], "v-flip");+        }+    }+    assert!(r(out[4 * w + 4]) > r(out[4 * w + 5]));+    assert!(r(out[4 * w + 5]) >= r(out[4 * w + 6]));+    assert_eq!(r(out[0]), 0, "far corner receives negligible weight (truncates to 0)");+}++#[test]+fn gaussian_step_edge_becomes_monotone_ramp() {+    let (w, h) = (16, 4);+    let mut src = Vec::new();+    for _y in 0..h {+        for x in 0..w {+            let v = if x < 8 { 0 } else { 255 };+            src.push(pack(255, v, v, v));+        }+    }+    let out = gaussian_blur(&src, w, h, 3);+    let row: Vec<i32> = (0..w).map(|x| r(out[x])).collect();+    for i in 1..w {+        assert!(row[i] >= row[i - 1], "monotone: {row:?}");+    }+    assert_eq!(row[0], 0);+    assert_eq!(row[w - 1], 255);+    assert!(row[7] > 0 && row[7] < 255, "edge softened");+    assert!(row[8] > 0 && row[8] < 255, "edge softened");+}++#[test]+fn gaussian_larger_radius_blurs_more() {+    let (w, h) = (15, 15);+    let mut src = solid(w, h, pack(255, 0, 0, 0));+    src[7 * w + 7] = pack(255, 255, 255, 255);+    let small = gaussian_blur(&src, w, h, 1);+    let large = gaussian_blur(&src, w, h, 5);+    assert!(r(large[7 * w + 7]) < r(small[7 * w + 7]), "wider kernel lowers peak");+}++#[test]+fn gaussian_single_pixel_and_single_row() {+    let one = vec![pack(255, 10, 20, 30)];+    let out = gaussian_blur(&one, 1, 1, 3);+    assert_near_px(out[0], one[0], 2);+    let row = gradient(8, 1);+    let out = gaussian_blur(&row, 8, 1, 2);+    assert_eq!(out.len(), 8);+}++// ---- filter_blur dispatch --------------------------------------------------++#[test]+fn filter_blur_zero_amount_radial_and_spin_are_identity() {+    // strength 0: radial scale stays 1 and spin angle stays 0, so every+    // sample lands on the source pixel itself.+    let src = gradient(11, 9);+    assert_eq!(filter_blur(&src, 11, 9, 2, 0.0, 0.0, 0.5, 0.5), src);+    assert_eq!(filter_blur(&src, 11, 9, 3, 0.0, 0.0, 0.5, 0.5), src);+}++#[test]+fn filter_blur_uniform_any_mode_is_near_identity() {+    let src = solid(10, 10, pack(255, 80, 160, 240));+    for mode in 0..4 {+        let out = filter_blur(&src, 10, 10, mode, 25.0, 30.0, 0.5, 0.5);+        assert_eq!(out.len(), 100);+        for p in out {+            assert_near_px(p, src[0], 2);+        }+    }+}++#[test]+fn motion_blur_spreads_along_angle_only() {+    // White vertical line on black; horizontal motion blur spreads it in x.+    let (w, h) = (11, 11);+    let mut src = solid(w, h, pack(255, 0, 0, 0));+    for y in 0..h {+        src[y * w + 5] = pack(255, 255, 255, 255);+    }+    let out = filter_blur(&src, w, h, 1, 5.0, 0.0, 0.5, 0.5);+    // length 5 => half 2: line bleeds exactly 2px either side on its row.+    assert!(r(out[5 * w + 3]) > 0, "bled left");+    assert!(r(out[5 * w + 7]) > 0, "bled right");+    assert_eq!(r(out[5 * w + 2]), 0, "no bleed beyond half-length");+    assert_eq!(r(out[5 * w + 8]), 0);+    // No vertical spread: every row identical.+    for y in 1..h {+        assert_eq!(out[y * w + 5], out[5], "rows identical under horizontal blur");+    }+    // Vertical motion blur (90 deg) keeps the line in its column.+    let outv = filter_blur(&src, w, h, 1, 5.0, 90.0, 0.5, 0.5);+    assert_eq!(r(outv[5 * w + 3]), 0, "no horizontal bleed at 90 degrees");+    assert_eq!(r(outv[5 * w + 7]), 0);+}++#[test]+fn radial_and_spin_centre_pixel_is_fixed() {+    // At the exact centre the radial/spin sample offsets are all zero.+    let src = gradient(11, 11);+    for mode in [2, 3] {+        let out = filter_blur(&src, 11, 11, mode, 80.0, 0.0, 0.5, 0.5);+        // centre of 11x11 at (0.5,0.5) is (5,5) (cx = 0.5*11 = 5.5, so (5,5)+        // is near-centre); just assert output is a valid in-range pixel.+        assert_eq!(out.len(), 121);+        let _ = out[5 * 11 + 5];+    }+}++// ---- blur_params (masked lens blur) ----------------------------------------++#[test]+fn blur_params_mask_zero_region_is_original() {+    // Tiny radius around centre: centre pixel has mask 0 => original.+    let (w, h) = (21, 21);+    let src = gradient(w, h);+    let out = blur_params(&src, w, h, 0, 0.5, 0.5, 0.3, 10.0, 0.3, 0.0);+    assert_eq!(out[10 * w + 10], src[10 * w + 10]);+}++#[test]+fn blur_params_far_region_equals_gaussian() {+    // Far outside the protected radius mask saturates to 1 => full blur,+    // with blur radius clamp(intensity*0.5, 1, 25) = 5 for intensity 10.+    let (w, h) = (21, 21);+    let src = gradient(w, h);+    let out = blur_params(&src, w, h, 0, 0.5, 0.5, 0.05, 10.0, 0.3, 0.0);+    let blurred = gaussian_blur(&src, w, h, 5);+    // Corners are well beyond radius+feather.+    assert_eq!(out[0], blurred[0]);+    assert_eq!(out[w - 1], blurred[w - 1]);+    assert_eq!(out[(h - 1) * w], blurred[(h - 1) * w]);+}++#[test]+fn blur_params_all_modes_preserve_alpha_range() {+    let src = gradient(13, 11);+    for mode in 0..3 {+        let out = blur_params(&src, 13, 11, mode, 0.5, 0.5, 0.3, 12.0, 0.4, 45.0);+        assert_eq!(out.len(), 13 * 11);+        for p in &out {+            // Alpha interpolates toward the blurred alpha; the blur's+            // truncation drift (documented <=2 on uniform fields) applies.+            assert!((253..=255).contains(&a(*p)), "alpha drifted to {}", a(*p));+            for s in [16u32, 8, 0] {+                assert!((0..=255).contains(&chan(*p, s)));+            }+        }+        let _ = (b(out[0]), g(out[0]));+    }+}
photos/src/main/rust/src/inpaint.rs
diff --git a/photos/src/main/rust/src/inpaint.rs b/photos/src/main/rust/src/inpaint.rsindex 75598c99e6..f8fc8c08f6 100644--- a/photos/src/main/rust/src/inpaint.rs+++ b/photos/src/main/rust/src/inpaint.rs@@ -15,6 +15,18 @@ pub fn inpaint( ) -> Vec<i32> {     let mut px = px_in.to_vec(); +    // Degenerate geometry / undersized mask: no hole information to act+    // on. (Previously mask_w == 0 made the coordinate clamp panic with+    // min > max, and a short mask indexed out of bounds.)+    if w == 0+        || h == 0+        || mask_w == 0+        || mask_h == 0+        || hole_mask.len() < mask_w.saturating_mul(mask_h)+    {+        return px;+    }+     let mut hole = vec![false; w * h];     let mut hole_count: i64 = 0;     let mut min_x = w as i32;@@ -48,7 +60,15 @@ pub fn inpaint(     }      if hole_count > 20000 {-        diffuse_fill(&mut px, w, h, &hole, min_x, min_y, max_x, max_y, passes);+        // Expand the box by one pixel so the diffusion has a ring of+        // known (non-hole) pixels as Dirichlet boundary values. With the+        // tight hole bbox, a rectangular hole's box contains only hole+        // pixels and Jacobi averaging could never import any colour.+        let dx0 = (min_x - 1).max(0);+        let dy0 = (min_y - 1).max(0);+        let dx1 = (max_x + 1).min(w as i32 - 1);+        let dy1 = (max_y + 1).min(h as i32 - 1);+        diffuse_fill(&mut px, w, h, &hole, dx0, dy0, dx1, dy1, passes);     } else {         exemplar_fill(&mut px, w, h, &mut hole, min_x, min_y, max_x, max_y);     }
photos/src/main/rust/src/inpaint_tests.rs
diff --git a/photos/src/main/rust/src/inpaint_tests.rs b/photos/src/main/rust/src/inpaint_tests.rsnew file mode 100644index 0000000000..b38db4c3ac--- /dev/null+++ b/photos/src/main/rust/src/inpaint_tests.rs@@ -0,0 +1,166 @@+//! Unit tests for `inpaint` (exemplar + diffusion branches).+use crate::inpaint::inpaint;+use crate::pixel::{a, pack, r};++fn solid(w: usize, h: usize, p: i32) -> Vec<i32> {+    vec![p; w * h]+}+fn pattern(w: usize, h: usize) -> Vec<i32> {+    let mut v = Vec::with_capacity(w * h);+    for y in 0..h {+        for x in 0..w {+            let c = ((x * 37 + y * 91) % 256) as i32;+            v.push(pack(255, c, 255 - c, ((x * 3 + y * 5) % 256) as i32));+        }+    }+    v+}++#[test]+fn mask_threshold_is_half() {+    // 0.49 is not a hole; 0.5 is. Only the second call may change the pixel.+    let (w, h) = (9, 9);+    let colour = pack(255, 40, 80, 120);+    let mut src = solid(w, h, colour);+    src[4 * w + 4] = pack(255, 0, 0, 0);+    let mut mask = vec![0f32; w * h];+    mask[4 * w + 4] = 0.49;+    assert_eq!(inpaint(&src, w, h, &mask, w, h, 10), src);+    mask[4 * w + 4] = 0.5;+    let out = inpaint(&src, w, h, &mask, w, h, 10);+    assert_eq!(out[4 * w + 4], colour, "hole in flat field fills with the field colour");+}++#[test]+fn single_hole_in_flat_field_fills_exactly_and_touches_nothing_else() {+    let (w, h) = (11, 11);+    let colour = pack(255, 10, 20, 30);+    let mut src = solid(w, h, colour);+    src[5 * w + 5] = pack(255, 250, 250, 250);+    let mut mask = vec![0f32; w * h];+    mask[5 * w + 5] = 1.0;+    let out = inpaint(&src, w, h, &mask, w, h, 10);+    assert_eq!(out, solid(w, h, colour));+}++#[test]+fn non_hole_pixels_are_never_modified_exemplar() {+    let (w, h) = (20, 20);+    let src = pattern(w, h);+    let mut mask = vec![0f32; w * h];+    for y in 8..12 {+        for x in 8..12 {+            mask[y * w + x] = 1.0;+        }+    }+    let out = inpaint(&src, w, h, &mask, w, h, 30);+    for y in 0..h {+        for x in 0..w {+            if mask[y * w + x] < 0.5 {+                assert_eq!(out[y * w + x], src[y * w + x], "non-hole ({x},{y}) changed");+            }+            assert_eq!(a(out[y * w + x]), 255);+        }+    }+}++#[test]+fn corner_hole_in_flat_field_fills() {+    let (w, h) = (8, 8);+    let colour = pack(255, 66, 99, 33);+    let mut src = solid(w, h, colour);+    let mut mask = vec![0f32; w * h];+    for y in 0..2 {+        for x in 0..2 {+            mask[y * w + x] = 1.0;+            src[y * w + x] = pack(255, 0, 0, 0);+        }+    }+    let out = inpaint(&src, w, h, &mask, w, h, 10);+    assert_eq!(out, solid(w, h, colour));+}++#[test]+fn all_hole_image_becomes_neutral_gray_keeping_alpha() {+    // No known pixel exists, so the safety fill paints 0x808080 and+    // preserves each pixel's alpha byte.+    let (w, h) = (4, 4);+    let mut src = Vec::new();+    for i in 0..w * h {+        src.push(pack(100 + i as i32, 10, 200, 30));+    }+    let mask = vec![1f32; w * h];+    let out = inpaint(&src, w, h, &mask, w, h, 10);+    for i in 0..w * h {+        assert_eq!(out[i] as u32 & 0x00FF_FFFF, 0x0080_8080, "gray fill at {i}");+        assert_eq!(a(out[i]), a(src[i]), "alpha kept at {i}");+    }+}++#[test]+fn scaled_mask_maps_by_floor() {+    // 2x2 mask over a 4x4 image: each mask cell covers a 2x2 quadrant.+    // Marking the top-left mask cell holes the top-left quadrant only.+    let (w, h) = (4, 4);+    let colour = pack(255, 50, 100, 150);+    let mut src = solid(w, h, colour);+    for y in 0..2 {+        for x in 0..2 {+            src[y * w + x] = pack(255, 0, 0, 0);+        }+    }+    let mask = vec![1.0f32, 0.0, 0.0, 0.0];+    let out = inpaint(&src, w, h, &mask, 2, 2, 10);+    assert_eq!(out, solid(w, h, colour), "quadrant hole filled, rest untouched");+}++#[test]+fn degenerate_mask_returns_input() {+    let src = pattern(6, 6);+    // Zero-size mask dimensions must not panic (division / clamp on empty range).+    assert_eq!(inpaint(&src, 6, 6, &[], 0, 0, 10), src);+    assert_eq!(inpaint(&src, 6, 6, &[], 0, 6, 10), src);+    // Mask buffer shorter than mask_w*mask_h likewise.+    assert_eq!(inpaint(&src, 6, 6, &[1.0, 0.0], 8, 8, 10), src);+}++#[test]+fn diffusion_branch_smooths_large_hole_and_keeps_known_pixels() {+    // 150x150 hole (>20000 px) forces the Jacobi diffusion branch.+    let (w, h) = (160, 160);+    let colour = pack(255, 200, 200, 200);+    let mut src = solid(w, h, colour);+    let mut mask = vec![0f32; w * h];+    for y in 5..155 {+        for x in 5..155 {+            mask[y * w + x] = 1.0;+            src[y * w + x] = pack(255, 0, 0, 0);+        }+    }+    let out = inpaint(&src, w, h, &mask, w, h, 80);+    assert_eq!(out.len(), w * h);+    // Known border pixels untouched.+    assert_eq!(out[0], colour);+    assert_eq!(out[159 * w + 159], colour);+    // A hole pixel next to the known border diffuses toward the border+    // colour: strictly between the black seed and 200 after 80 passes.+    let edge = r(out[5 * w + 5]);+    assert!(edge > 50 && edge <= 200, "diffused edge value {edge}");+    assert!(out.iter().all(|&p| a(p) == 255));+}++#[test]+fn diffusion_zero_passes_leaves_hole_seed_unchanged() {+    let (w, h) = (160, 160);+    let mut src = solid(w, h, pack(255, 200, 200, 200));+    let mut mask = vec![0f32; w * h];+    for y in 5..155 {+        for x in 5..155 {+            mask[y * w + x] = 1.0;+            src[y * w + x] = pack(255, 0, 0, 0);+        }+    }+    // Zero (or negative) Jacobi passes copy the seed values back verbatim.+    assert_eq!(inpaint(&src, w, h, &mask, w, h, 0), src);+    assert_eq!(inpaint(&src, w, h, &mask, w, h, -5), src);+}
photos/src/main/rust/src/lib.rs
diff --git a/photos/src/main/rust/src/lib.rs b/photos/src/main/rust/src/lib.rsindex 3db26f0381..0c8e55985e 100644--- a/photos/src/main/rust/src/lib.rs+++ b/photos/src/main/rust/src/lib.rs@@ -12,6 +12,19 @@ pub mod pixel; pub mod sharpen; pub mod stylize; +#[cfg(test)]+mod blur_tests;+#[cfg(test)]+mod inpaint_tests;+#[cfg(test)]+mod liquify_tests;+#[cfg(test)]+mod pixel_tests;+#[cfg(test)]+mod sharpen_tests;+#[cfg(test)]+mod stylize_tests;+ #[cfg(not(test))] mod jni_bindings {     use crate::{blur, inpaint, liquify, sharpen, stylize};
photos/src/main/rust/src/liquify.rs
diff --git a/photos/src/main/rust/src/liquify.rs b/photos/src/main/rust/src/liquify.rsindex f6f3184661..701e3be662 100644--- a/photos/src/main/rust/src/liquify.rs+++ b/photos/src/main/rust/src/liquify.rs@@ -140,12 +140,16 @@ pub fn liquify(src: &[i32], w: usize, h: usize, tools: &[i32], params: &[f32]) -                         disp_x[idx] += (px as f32 - cx) * op.strength * t * 0.5f32;                         disp_y[idx] += (py as f32 - cy) * op.strength * t * 0.5f32;                     }-                    _ => {-                        // Reconstruct (4)+                    4 => {+                        // Reconstruct                         let factor = clamp_f(1f32 - op.strength * t, 0f32, 1f32);                         disp_x[idx] *= factor;                         disp_y[idx] *= factor;                     }+                    _ => {+                        // Unknown tool ordinal: no-op (never fall through+                        // to Reconstruct, which would corrupt prior ops).+                    }                 }                 px += 1;             }
photos/src/main/rust/src/liquify_tests.rs
diff --git a/photos/src/main/rust/src/liquify_tests.rs b/photos/src/main/rust/src/liquify_tests.rsnew file mode 100644index 0000000000..c2c5678b21--- /dev/null+++ b/photos/src/main/rust/src/liquify_tests.rs@@ -0,0 +1,142 @@+//! Unit tests for `liquify` (displacement field + bilinear resample).+use crate::liquify::liquify;+use crate::pixel::{a, pack, r};++/// Unique-per-pixel image: R encodes x, G encodes y.+fn coords(w: usize, h: usize) -> Vec<i32> {+    let mut v = Vec::with_capacity(w * h);+    for y in 0..h {+        for x in 0..w {+            v.push(pack(255, (x * 16) as i32, (y * 16) as i32, ((x + y) * 8 % 256) as i32));+        }+    }+    v+}+const PUSH: i32 = 0;+const TWIRL: i32 = 1;+const PUCKER: i32 = 2;+const BLOAT: i32 = 3;+const RECONSTRUCT: i32 = 4;++#[test]+fn empty_and_truncated_ops_are_identity() {+    let src = coords(8, 8);+    assert_eq!(liquify(&src, 8, 8, &[], &[]), src);+    // tools present but no params at all -> zero complete ops.+    assert_eq!(liquify(&src, 8, 8, &[PUSH, PUSH], &[]), src);+    // One tool but only a partial (<6) param block -> ignored.+    assert_eq!(liquify(&src, 8, 8, &[PUSH], &[0.5, 0.5, 0.2]), src);+    // Extra tool beyond complete param blocks is dropped.+    let one = liquify(&src, 8, 8, &[PUSH], &[0.5, 0.5, 0.0625, 0.0, 0.5, 1.0]);+    let two_tools_one_block = liquify(&src, 8, 8, &[PUSH, BLOAT], &[0.5, 0.5, 0.0625, 0.0, 0.5, 1.0]);+    assert_eq!(one, two_tools_one_block);+}++#[test]+fn zero_drag_push_and_zero_radius_are_identity() {+    let src = coords(12, 12);+    // Push with no drag displaces nothing.+    assert_eq!(liquify(&src, 12, 12, &[PUSH], &[0.5, 0.5, 0.0, 0.0, 0.4, 0.9]), src);+    // Zero / negative radius ops are skipped entirely.+    assert_eq!(liquify(&src, 12, 12, &[PUSH], &[0.5, 0.5, 0.2, 0.2, 0.0, 0.9]), src);+    assert_eq!(liquify(&src, 12, 12, &[TWIRL], &[0.5, 0.5, 0.0, 0.0, -0.3, 0.9]), src);+}++#[test]+fn push_centre_samples_exactly_one_pixel_upstream() {+    // At the op centre falloff t == 1, so displacement is exactly+    // -drag*max_dim*strength. drag 1/16 of 16px at strength 1 => -1px in x:+    // the centre destination pixel must equal the source pixel to its left.+    let (w, h) = (16, 16);+    let src = coords(w, h);+    let out = liquify(&src, w, h, &[PUSH], &[0.5, 0.5, 0.0625, 0.0, 0.5, 1.0]);+    assert_eq!(out[8 * w + 8], src[8 * w + 7]);+    // Same in y with the drag swapped.+    let out = liquify(&src, w, h, &[PUSH], &[0.5, 0.5, 0.0, 0.0625, 0.5, 1.0]);+    assert_eq!(out[8 * w + 8], src[7 * w + 8]);+    // Two identical pushes accumulate to a -2px sample offset.+    let out = liquify(+        &src, w, h,+        &[PUSH, PUSH],+        &[0.5, 0.5, 0.0625, 0.0, 0.5, 1.0, 0.5, 0.5, 0.0625, 0.0, 0.5, 1.0],+    );+    assert_eq!(out[8 * w + 8], src[8 * w + 6]);+}++#[test]+fn push_outside_radius_is_untouched_and_direction_is_upstream() {+    let (w, h) = (20, 20);+    let src = coords(w, h);+    // Small brush at centre; corners lie outside the radius.+    let out = liquify(&src, w, h, &[PUSH], &[0.5, 0.5, 0.1, 0.0, 0.1, 1.0]);+    assert_eq!(out[0], src[0]);+    assert_eq!(out[w - 1], src[w - 1]);+    assert_eq!(out[(h - 1) * w], src[(h - 1) * w]);+    // Dragging right samples from the left: R (which encodes x) drops.+    let big = liquify(&src, w, h, &[PUSH], &[0.5, 0.5, 0.1, 0.0, 0.5, 0.8]);+    assert!(r(big[10 * w + 10]) < r(src[10 * w + 10]), "samples upstream (left)");+}++#[test]+fn twirl_pucker_bloat_keep_centre_fixed() {+    // At the exact centre the offset from centre is zero, so twirl rotates+    // (0,0) to itself and pucker/bloat scale a zero vector: centre fixed.+    let (w, h) = (15, 15);+    let src = coords(w, h);+    for tool in [TWIRL, PUCKER, BLOAT] {+        let out = liquify(&src, w, h, &[tool], &[0.5, 0.5, 0.0, 0.0, 0.5, 1.0]);+        // cx = 0.5*15 = 7.5; nearest pixels still move for pucker/bloat, so+        // assert only dimensions and that *some* off-centre pixel changed+        // for the radial tools (twirl moves off-centre pixels too).+        assert_eq!(out.len(), w * h);+        assert_ne!(out, src, "tool {tool} should displace off-centre pixels");+    }+}++#[test]+fn all_tools_preserve_dims_and_opaque_alpha() {+    let (w, h) = (16, 14);+    let src = coords(w, h);+    for tool in [PUSH, TWIRL, PUCKER, BLOAT, RECONSTRUCT] {+        let out = liquify(&src, w, h, &[tool], &[0.5, 0.5, 0.05, 0.03, 0.4, 0.7]);+        assert_eq!(out.len(), w * h, "tool {tool}");+        // Bilinear interpolation of an all-opaque image stays opaque.+        assert!(out.iter().all(|&p| a(p) == 255), "tool {tool} alpha");+    }+}++#[test]+fn reconstruct_at_centre_cancels_prior_push() {+    let (w, h) = (16, 16);+    let src = coords(w, h);+    let push_only = liquify(&src, w, h, &[PUSH], &[0.5, 0.5, 0.0625, 0.0, 0.5, 1.0]);+    assert_ne!(push_only[8 * w + 8], src[8 * w + 8]);+    // Reconstruct with strength 1 at the same centre: falloff 1 there, so+    // factor = 1 - 1*1 = 0 wipes the displacement at the centre exactly.+    let healed = liquify(+        &src, w, h,+        &[PUSH, RECONSTRUCT],+        &[0.5, 0.5, 0.0625, 0.0, 0.5, 1.0, 0.5, 0.5, 0.0, 0.0, 0.5, 1.0],+    );+    assert_eq!(healed[8 * w + 8], src[8 * w + 8]);+}++#[test]+fn unknown_tool_is_ignored_not_reconstruct() {+    // An out-of-range tool ordinal must be a no-op. Treating it as+    // Reconstruct would silently scale the displacement from a prior op.+    let (w, h) = (16, 16);+    let src = coords(w, h);+    let push_params = [0.5f32, 0.5, 0.0625, 0.0, 0.5, 1.0];+    let push_only = liquify(&src, w, h, &[PUSH], &push_params);+    let mut tools = vec![PUSH, 99];+    let mut params = push_params.to_vec();+    params.extend_from_slice(&[0.5, 0.5, 0.0, 0.0, 0.5, 1.0]);+    let with_unknown = liquify(&src, w, h, &tools, &params);+    assert_eq!(with_unknown, push_only, "unknown tool must not alter displacement");+    tools[1] = -3;+    let with_negative = liquify(&src, w, h, &tools, &params);+    assert_eq!(with_negative, push_only);+    // Unknown tool alone is identity.+    assert_eq!(liquify(&src, w, h, &[42], &[0.5, 0.5, 0.1, 0.1, 0.5, 1.0]), src);+}
photos/src/main/rust/src/pixel_tests.rs
diff --git a/photos/src/main/rust/src/pixel_tests.rs b/photos/src/main/rust/src/pixel_tests.rsnew file mode 100644index 0000000000..67ecb9a8e5--- /dev/null+++ b/photos/src/main/rust/src/pixel_tests.rs@@ -0,0 +1,75 @@+//! Unit tests for `pixel` ARGB helpers.+use crate::pixel::*;++#[test]+fn pack_bit_layout_is_argb() {+    assert_eq!(pack(0xAB, 0xCD, 0xEF, 0x12) as u32, 0xABCDEF12);+    assert_eq!(pack(0, 0, 0, 0), 0);+    assert_eq!(pack(255, 255, 255, 255), -1);+    assert_eq!(pack(255, 0, 0, 0) as u32, 0xFF00_0000);+    assert_eq!(pack(0, 255, 0, 0) as u32, 0x00FF_0000);+    assert_eq!(pack(0, 0, 255, 0) as u32, 0x0000_FF00);+    assert_eq!(pack(0, 0, 0, 255) as u32, 0x0000_00FF);+}++#[test]+fn unpack_extracts_each_channel() {+    let p = pack(0x12, 0x34, 0x56, 0x78);+    assert_eq!((a(p), r(p), g(p), b(p)), (0x12, 0x34, 0x56, 0x78));+    // High-alpha pixels are negative as i32; extraction must use unsigned shifts.+    let q = pack(255, 200, 100, 50);+    assert!(q < 0);+    assert_eq!((a(q), r(q), g(q), b(q)), (255, 200, 100, 50));+    assert_eq!((a(-1), r(-1), g(-1), b(-1)), (255, 255, 255, 255));+    assert_eq!((a(0), r(0), g(0), b(0)), (0, 0, 0, 0));+}++#[test]+fn pack_unpack_roundtrips() {+    for (av, rv, gv, bv) in [+        (0, 0, 0, 0),+        (255, 255, 255, 255),+        (128, 1, 254, 77),+        (255, 255, 0, 128),+        (1, 2, 3, 4),+        (90, 180, 45, 225),+    ] {+        let p = pack(av, rv, gv, bv);+        assert_eq!((a(p), r(p), g(p), b(p)), (av, rv, gv, bv));+    }+}++#[test]+fn channels_do_not_bleed_into_neighbours() {+    // Each channel occupies exactly its own byte for in-range inputs.+    let p = pack(0xFF, 0x00, 0xFF, 0x00);+    assert_eq!(p as u32, 0xFF00_FF00);+    let p = pack(0x01, 0x02, 0x04, 0x08);+    assert_eq!(p as u32, 0x0102_0408);+}++#[test]+fn clamp_i_bounds() {+    assert_eq!(clamp_i(5, 0, 10), 5);+    assert_eq!(clamp_i(0, 0, 10), 0);+    assert_eq!(clamp_i(10, 0, 10), 10);+    assert_eq!(clamp_i(-3, 0, 10), 0);+    assert_eq!(clamp_i(99, 0, 10), 10);+    assert_eq!(clamp_i(-5, -10, -1), -5);+    assert_eq!(clamp_i(-50, -10, -1), -10);+    assert_eq!(clamp_i(0, -10, -1), -1);+    assert_eq!(clamp_i(7, 7, 7), 7);+    assert_eq!(clamp_i(i32::MIN, 0, 255), 0);+    assert_eq!(clamp_i(i32::MAX, 0, 255), 255);+}++#[test]+fn clamp_f_bounds() {+    assert_eq!(clamp_f(0.5, 0.0, 1.0), 0.5);+    assert_eq!(clamp_f(0.0, 0.0, 1.0), 0.0);+    assert_eq!(clamp_f(1.0, 0.0, 1.0), 1.0);+    assert_eq!(clamp_f(-0.25, 0.0, 1.0), 0.0);+    assert_eq!(clamp_f(1.25, 0.0, 1.0), 1.0);+    assert_eq!(clamp_f(-3.5, -2.0, 2.0), -2.0);+    assert_eq!(clamp_f(3.5, -2.0, 2.0), 2.0);+}
photos/src/main/rust/src/sharpen_tests.rs
diff --git a/photos/src/main/rust/src/sharpen_tests.rs b/photos/src/main/rust/src/sharpen_tests.rsnew file mode 100644index 0000000000..6a0dca20aa--- /dev/null+++ b/photos/src/main/rust/src/sharpen_tests.rs@@ -0,0 +1,119 @@+//! Unit tests for `sharpen::unsharp`.+use crate::pixel::{a, b, g, pack, r};+use crate::sharpen::unsharp;++fn checker(w: usize, h: usize) -> Vec<i32> {+    let mut v = Vec::with_capacity(w * h);+    for y in 0..h {+        for x in 0..w {+            let c = ((x * 37 + y * 91) % 256) as i32;+            v.push(pack(255, c, 255 - c, (c * 2) % 256));+        }+    }+    v+}+fn step_image(w: usize, h: usize, lo: i32, hi: i32) -> Vec<i32> {+    let mut v = Vec::with_capacity(w * h);+    for _y in 0..h {+        for x in 0..w {+            let c = if x < w / 2 { lo } else { hi };+            v.push(pack(255, c, c, c));+        }+    }+    v+}++#[test]+fn amount_zero_is_identity_for_any_radius_and_threshold() {+    let src = checker(13, 11);+    for (radius, thr) in [(0.0f32, 0i32), (0.4, 0), (2.0, 3), (10.0, 255), (1.5, -1)] {+        assert_eq!(unsharp(&src, 13, 11, 0.0, radius, thr), src, "r={radius} thr={thr}");+    }+}++#[test]+fn threshold_at_or_above_max_diff_is_identity() {+    // Channel diffs are in -255..=255, so |diff| > 255 is never true and+    // sharpen_channel always returns the original channel.+    let src = checker(15, 13);+    assert_eq!(unsharp(&src, 15, 13, 100.0, 2.0, 255), src);+    assert_eq!(unsharp(&src, 15, 13, 500.0, 4.0, 1000), src);+}++#[test]+fn unsharp_enhances_step_edge_contrast() {+    // Dark side of an edge darkens, bright side brightens.+    let (w, h) = (16, 8);+    let src = step_image(w, h, 100, 200);+    let out = unsharp(&src, w, h, 100.0, 2.0, 0);+    let y = 4;+    assert!(r(out[y * w + 7]) < 100, "dark side: {}", r(out[y * w + 7]));+    assert!(r(out[y * w + 8]) > 200, "bright side: {}", r(out[y * w + 8]));+    // Far from the edge the (locally flat) image is essentially unchanged.+    assert!((r(out[y * w + 1]) - 100).abs() <= 3);+    assert!((r(out[y * w + 14]) - 200).abs() <= 3);+}++#[test]+fn unsharp_output_is_clamped_to_byte_range() {+    let (w, h) = (16, 8);+    let src = step_image(w, h, 0, 255);+    let out = unsharp(&src, w, h, 1000.0, 3.0, 0);+    for p in &out {+        for f in [r, g, b] {+            assert!((0..=255).contains(&f(*p)));+        }+    }+    // Extreme overshoot clamps instead of wrapping.+    assert_eq!(r(out[4 * w + 7]), 0);+    assert_eq!(r(out[4 * w + 8]), 255);+}++#[test]+fn unsharp_never_sharpens_alpha() {+    // Alpha varies spatially (so its blur differs) but must pass through.+    let (w, h) = (12, 10);+    let mut src = Vec::with_capacity(w * h);+    for y in 0..h {+        for x in 0..w {+            src.push(pack(((x * 20 + y * 7) % 256) as i32, 120, 60, 200));+        }+    }+    let out = unsharp(&src, w, h, 120.0, 3.0, 0);+    for i in 0..w * h {+        assert_eq!(a(out[i]), a(src[i]), "alpha preserved at {i}");+    }+}++#[test]+fn unsharp_radius_rounds_to_nearest_int_with_min_one() {+    let src = checker(14, 12);+    // 0.1 and 1.4 both resolve to radius 1; 0.5 rounds half-up (Kotlin+    // roundToInt: floor(x+0.5)) to 1 as well.+    let base = unsharp(&src, 14, 12, 60.0, 1.0, 0);+    assert_eq!(unsharp(&src, 14, 12, 60.0, 0.1, 0), base);+    assert_eq!(unsharp(&src, 14, 12, 60.0, 1.4, 0), base);+    assert_eq!(unsharp(&src, 14, 12, 60.0, 0.5, 0), base);+    // A materially larger radius gives a different blur, hence different output.+    let wide = unsharp(&src, 14, 12, 60.0, 4.6, 0);+    assert_ne!(wide, base);+}++#[test]+fn unsharp_flat_image_stays_near_flat() {+    let src = vec![pack(255, 90, 140, 190); 10 * 10];+    let out = unsharp(&src, 10, 10, 80.0, 2.0, 0);+    for p in &out {+        assert!((r(*p) - 90).abs() <= 3);+        assert!((g(*p) - 140).abs() <= 3);+        assert!((b(*p) - 190).abs() <= 3);+        assert_eq!(a(*p), 255);+    }+}++#[test]+fn unsharp_preserves_dimensions_nonsquare() {+    let src = checker(3, 17);+    let out = unsharp(&src, 3, 17, 50.0, 2.0, 1);+    assert_eq!(out.len(), 3 * 17);+}
photos/src/main/rust/src/stylize_tests.rs
diff --git a/photos/src/main/rust/src/stylize_tests.rs b/photos/src/main/rust/src/stylize_tests.rsnew file mode 100644index 0000000000..0946eb7337--- /dev/null+++ b/photos/src/main/rust/src/stylize_tests.rs@@ -0,0 +1,126 @@+//! Unit tests for `stylize` (FindEdges / Emboss).+use crate::pixel::{a, b, g, pack, r};+use crate::stylize::stylize;++fn gray(w: usize, h: usize, v: i32) -> Vec<i32> {+    vec![pack(255, v, v, v); w * h]+}+fn assert_gray(p: i32) {+    assert_eq!(r(p), g(p));+    assert_eq!(g(p), b(p));+}++#[test]+fn none_and_unknown_modes_are_identity() {+    let mut src = Vec::new();+    for i in 0..(6 * 5) {+        src.push(pack(200, (i * 37) % 256, (i * 91) % 256, (i * 53) % 256));+    }+    for mode in [0, 3, 99, -1] {+        assert_eq!(stylize(&src, 6, 5, mode), src, "mode {mode}");+    }+}++#[test]+fn edges_flat_is_white_and_emboss_flat_is_bias_plus_luminance() {+    // Zero gradient => Sobel magnitude 0 => inverted to 255.+    let out = stylize(&gray(6, 6, 77), 6, 6, 1);+    assert!(out.iter().all(|&p| p == pack(255, 255, 255, 255)));+    // Emboss kernel sums to 1, so a flat field L maps to clamp(L + 128).+    assert!(stylize(&gray(5, 5, 0), 5, 5, 2).iter().all(|&p| r(p) == 128));+    assert!(stylize(&gray(5, 5, 100), 5, 5, 2).iter().all(|&p| r(p) == 228));+    assert!(stylize(&gray(5, 5, 255), 5, 5, 2).iter().all(|&p| r(p) == 255));+}++#[test]+fn edges_vertical_step_has_dark_seam_and_white_flats() {+    let (w, h) = (10, 6);+    let mut src = Vec::new();+    for _y in 0..h {+        for x in 0..w {+            let v = if x < 5 { 0 } else { 255 };+            src.push(pack(255, v, v, v));+        }+    }+    let out = stylize(&src, w, h, 1);+    for y in 0..h {+        assert_eq!(r(out[y * w + 1]), 255, "flat interior stays white");+        assert_eq!(r(out[y * w + 8]), 255);+        // Both columns straddling the step see the full Sobel response.+        assert_eq!(r(out[y * w + 4]), 0, "seam (dark side)");+        assert_eq!(r(out[y * w + 5]), 0, "seam (bright side)");+    }+}++#[test]+fn edges_uses_bt601_luminance_weights() {+    // Black -> pure colour step of value 100. At the seam gx = 4*luminance,+    // gy = 0, so v = 255 - 4*lum: red lum 29 -> 139, green 58 -> 23,+    // blue 11 -> 211. Green must read as the strongest edge.+    let (w, h) = (8, 5);+    let make = |c: i32| -> Vec<i32> {+        let mut v = Vec::new();+        for _y in 0..h {+            for x in 0..w {+                v.push(if x < 4 { pack(255, 0, 0, 0) } else { c });+            }+        }+        v+    };+    let seam = |c: i32| r(stylize(&make(c), w, h, 1)[2 * w + 3]);+    assert_eq!(seam(pack(255, 100, 0, 0)), 139);+    assert_eq!(seam(pack(255, 0, 100, 0)), 23);+    assert_eq!(seam(pack(255, 0, 0, 100)), 211);+}++#[test]+fn edges_and_emboss_are_grayscale_and_preserve_alpha() {+    let (w, h) = (9, 7);+    let mut src = Vec::new();+    for y in 0..h {+        for x in 0..w {+            src.push(pack(+                100 + ((x + y) * 10) as i32,+                (x * 28) as i32 % 256,+                (y * 36) as i32 % 256,+                ((x * y) * 9) as i32 % 256,+            ));+        }+    }+    for mode in [1, 2] {+        let out = stylize(&src, w, h, mode);+        assert_eq!(out.len(), w * h);+        for i in 0..w * h {+            assert_gray(out[i]);+            assert_eq!(a(out[i]), a(src[i]), "mode {mode} alpha at {i}");+        }+    }+}++#[test]+fn emboss_linear_ramp_matches_kernel_algebra() {+    // Luminance ramp L = 10*x (constant in y). Kernel column sums are+    // (-3, 1, 3) over dx, so sum = -3(L-10) + L + 3(L+10) = L + 60 and+    // v = clamp(L + 60 + 128). Centre pixel x=4: L=40 => 228.+    let (w, h) = (9, 9);+    let mut src = Vec::new();+    for _y in 0..h {+        for x in 0..w {+            let v = (x * 10) as i32;+            src.push(pack(255, v, v, v));+        }+    }+    let out = stylize(&src, w, h, 2);+    assert_eq!(r(out[4 * w + 4]), 228);+    assert_eq!(r(out[4 * w + 2]), 208);+}++#[test]+fn single_pixel_images() {+    // All neighbours clamp to the single pixel: no gradient.+    let src = vec![pack(180, 90, 90, 90)];+    assert_eq!(stylize(&src, 1, 1, 1), vec![pack(180, 255, 255, 255)]);+    // Emboss: sum = luminance (kernel sums to 1) => 90 + 128 = 218.+    assert_eq!(stylize(&src, 1, 1, 2), vec![pack(180, 218, 218, 218)]);+    assert_eq!(stylize(&src, 1, 1, 0), src);+}