Separate inpaint source-centre collection from completion

This commit is contained in:
Dylan M. Taylor
2026-11-18 14:54:05 -05:00
parent cff0d5de78
commit 7ba969d1bc
2 changed files with 16 additions and 12 deletions
+1 -1
View File
@@ -49,7 +49,7 @@ mod tests {
let img: Vec<f32> = (0..256).map(|i| if i % 16 < 8 { -f32::MAX } else { f32::MAX }).collect();
let out = membrane_fill(16, 16, 1, &img, &hole);
for ((&actual, &original), &unknown) in out.iter().zip(&img).zip(&hole) {
assert!(actual.is_finite() && actual >= -f32::MAX && actual <= f32::MAX);
assert!(actual.is_finite() && (-f32::MAX..=f32::MAX).contains(&actual));
if !unknown {
assert_eq!(actual.to_bits(), original.to_bits());
}
+15 -11
View File
@@ -132,6 +132,20 @@ fn supported_grid(w: usize, h: usize) -> bool {
.is_some_and(|bytes| bytes <= isize::MAX as usize)
}
/// Reserve the already counted source centres exactly, then retain row-major order.
fn source_centres(w: usize, valid: &[bool], count: usize, check: &impl Fn() -> Result<(), Cancelled>) -> Result<Vec<(i32, i32)>, Cancelled> {
let mut centres = Vec::with_capacity(count);
for (i, &is_valid) in valid.iter().enumerate() {
if i % (w * 32) == 0 {
check()?;
}
if is_valid {
centres.push(((i % w) as i32, (i / w) as i32));
}
}
Ok(centres)
}
/// Summed-area table of a boolean grid (`(w+1) × (h+1)`).
fn integral(w: usize, h: usize, m: &[bool]) -> Vec<u32> {
let mut s = vec![0u32; (w + 1) * (h + 1)];
@@ -488,17 +502,7 @@ pub fn complete_with(
nnf.clear();
continue;
}
// Count first and reserve exactly, avoiding both geometric capacity overflow and
// allocating coordinates for pixels that cannot be source centres.
let mut valid_list = Vec::with_capacity(valid_count);
for (i, &is_valid) in valid.iter().enumerate() {
if i % (lw * 32) == 0 {
check()?;
}
if is_valid {
valid_list.push(((i % lw) as i32, (i / lw) as i32));
}
}
let valid_list = source_centres(lw, &valid, valid_count, &check)?;
let mut solver = Solver { w: lw, h: lh, ch, r: ri, img: &mut limg, hole: &lhole, valid, valid_list, targets, ctl: cancelled };
// NNF init: upsampled from the coarser level where possible, random otherwise.
let mut rng = Rng::new(p.seed ^ (li as u64) << 20);