diff --git a/crates/develop/README.md b/crates/develop/README.md index 4b85d3305..99f382998 100644 --- a/crates/develop/README.md +++ b/crates/develop/README.md @@ -20,6 +20,12 @@ Each module comes with a test that its output is LightCraft's at the base commit [`craft-render-parity`](../render-parity/README.md) at `Tolerance::BASELINE`. The expected pictures in `tests/fixtures/` were written by LightCraft's own crate at that commit, on the scene the test builds. +Where the code differs from LightCraft's, the output for every input LightCraft handled is the same: + +- `inpaint::complete_with` returns `None` for a patch radius wider than the image before computing with it (LightCraft + reaches the same `None`, or overflows on a huge radius), returns an image with no pixels at once, and reports + `Cancelled` when any one poll said so, including a poll made on a worker thread. + ## Not here yet The rest of the pipeline: colour, raster, the sensor stage, raw loading and the render stages. No app uses this crate diff --git a/crates/develop/src/inpaint.rs b/crates/develop/src/inpaint.rs index 460dfa307..853eaa51b 100644 --- a/crates/develop/src/inpaint.rs +++ b/crates/develop/src/inpaint.rs @@ -49,6 +49,50 @@ mod tests { let _ = complete(2, 2, 1, &[0.5; 4], &[true, false, false, false], &CompleteParams::default()); } + #[test] + fn a_patch_radius_no_image_could_hold_finds_nothing_to_copy() { + let mut hole = vec![false; 32 * 32]; + hole[16 * 32 + 16] = true; + for patch_radius in [16, usize::MAX / 2, usize::MAX] { + let p = CompleteParams { patch_radius, ..CompleteParams::default() }; + assert_eq!(complete(32, 32, 1, &[0.5; 32 * 32], &hole, &p), None, "radius {patch_radius}"); + } + // A radius the image has room for still fills. + let p = CompleteParams { patch_radius: 5, ..CompleteParams::default() }; + assert!(complete(32, 32, 1, &[0.5; 32 * 32], &hole, &p).is_some()); + } + + #[test] + fn an_image_with_no_pixels_is_returned_without_walking_its_rows() { + let p = CompleteParams::default(); + assert_eq!(complete_with(0, usize::MAX, 1, &[], &[], &p, &|| false), Ok(Some(Vec::new()))); + assert_eq!(complete_with(usize::MAX, 0, 3, &[], &[], &p, &|| false), Ok(Some(Vec::new()))); + } + + /// Whichever poll says "cancelled", and only that one, the call ends in `Err`: also when it was a + /// worker's poll, which can stop the worker but not return. + #[test] + fn one_cancelled_poll_anywhere_cancels_the_call() { + use std::sync::atomic::{AtomicUsize, Ordering}; + let (w, h) = (48usize, 48usize); + let img: Vec = (0..w * h).map(|i| ((i % w) as f32 * 0.37).sin() * 0.5 + 0.5).collect(); + let hole: Vec = (0..w * h).map(|i| (i % w).abs_diff(24) < 5 && (i / w).abs_diff(24) < 5).collect(); + let p = CompleteParams::default(); + let polls = AtomicUsize::new(0); + let count = || { + polls.fetch_add(1, Ordering::Relaxed); + false + }; + assert!(matches!(complete_with(w, h, 1, &img, &hole, &p, &count), Ok(Some(_)))); + let total = polls.load(Ordering::Relaxed); + assert!(total > 8, "the scene polls in the workers too: {total}"); + for nth in 0..total { + let polls = AtomicUsize::new(0); + let once = || polls.fetch_add(1, Ordering::Relaxed) == nth; + assert_eq!(complete_with(w, h, 1, &img, &hole, &p, &once), Err(Cancelled), "poll {nth} of {total}"); + } + } + #[test] fn cancellation_stops() { let img = vec![0.5f32; 64 * 64]; diff --git a/crates/develop/src/inpaint/membrane.rs b/crates/develop/src/inpaint/membrane.rs index 4589d2347..6c517a285 100644 --- a/crates/develop/src/inpaint/membrane.rs +++ b/crates/develop/src/inpaint/membrane.rs @@ -190,7 +190,7 @@ mod tests { #[test] fn a_zero_area_image_comes_back_unchanged_whatever_its_channel_count() { assert!(membrane_fill(0, 5, usize::MAX, &[], &[]).is_empty()); - assert!(membrane_fill(3, 0, 1 << 40, &[], &[]).is_empty()); + assert!(membrane_fill(3, 0, usize::MAX / 2, &[], &[]).is_empty()); } #[test] diff --git a/crates/develop/src/inpaint/patchmatch.rs b/crates/develop/src/inpaint/patchmatch.rs index 5e9c73cb5..175a5f69d 100644 --- a/crates/develop/src/inpaint/patchmatch.rs +++ b/crates/develop/src/inpaint/patchmatch.rs @@ -23,6 +23,7 @@ use crate::inpaint::Cancelled; use crate::inpaint::membrane::membrane_fill; +use std::sync::atomic::{AtomicBool, Ordering}; /// Below this many grid cells a level runs single-threaded (thread hand-off costs more than it saves). #[cfg(not(target_arch = "wasm32"))] @@ -377,12 +378,31 @@ pub fn complete_with( p: &CompleteParams, cancelled: &(dyn Fn() -> bool + Sync), ) -> Result>, Cancelled> { + // The workers poll `cancelled` too and can only stop early, not return an error. One `true` from it, + // wherever it was seen, is kept, so the next check out here ends the call and a half-finished pass + // is never returned as a result. + let seen = AtomicBool::new(false); + let cancelled: &(dyn Fn() -> bool + Sync) = &|| { + if !seen.load(Ordering::Relaxed) && cancelled() { + seen.store(true, Ordering::Relaxed); + } + seen.load(Ordering::Relaxed) + }; let check = || if cancelled() { Err(Cancelled) } else { Ok(()) }; if ch == 0 || Some(hole.len()) != w.checked_mul(h) || Some(img.len()) != hole.len().checked_mul(ch) { return Ok(None); } + // No pixels (a zero width or height, whatever the other is): nothing to scan and nothing to fill. + if hole.is_empty() { + return Ok(Some(img.to_vec())); + } let Some((bx0, by0, bx1, by1)) = hole_bbox(w, h, hole) else { return Ok(Some(img.to_vec())) }; let r = p.patch_radius.max(1); + // A patch wider than the image has no place to be copied from, which is the `None` the search below + // would reach anyway. Said here, before `2 * r + 1` and `r as i32` can overflow on an absurd radius. + if r > (w.min(h) - 1) / 2 { + return Ok(None); + } let psz = 2 * r + 1; // Pyramid: shrink until the hole is a few patches across. let mut levels = vec![Level { w, h, img: img.to_vec(), hole: hole.to_vec() }];