From c8bef7880c47e9ca99e36d60d37c78770e853180 Mon Sep 17 00:00:00 2001 From: hnoerenberg Date: Mon, 16 Nov 2026 17:53:24 +0100 Subject: [PATCH] fix(algo): keep the 7x7 patch when the sampling area is not the Rectangular one #14191 measured Rectangular, and Auto where it keeps that same area. Auto where its content-based choice differs from Rectangular is marked not measured and keeps today's radius. fill_cow now takes the 11x11 patch only when the source mask is every pixel outside the hole. --- crates/algo/src/content_aware.rs | 13 ++++-- .../content_aware/structure_continue_tests.rs | 44 +++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/crates/algo/src/content_aware.rs b/crates/algo/src/content_aware.rs index 0eb8cbb6c09..51cdfd436e0 100644 --- a/crates/algo/src/content_aware.rs +++ b/crates/algo/src/content_aware.rs @@ -249,8 +249,8 @@ pub fn fill_owned( /// 20 px stripes: the default 7x7 patch fits inside one stripe or inside one band gap, so it /// carries no phase information and the completion decides from colour alone and runs one region /// into the other. An 11x11 patch covers a feature and the one next to it, which is what keeps -/// their phase through the hole. Every other option set is marked "not measured" in #14191 and -/// keeps [`CompleteParams`]' default radius. +/// their phase through the hole. Every other option set, and a sampling area other than the +/// Rectangular one, is marked "not measured" in #14191 and keeps [`CompleteParams`]' default radius. const MEASURED_STRUCTURE_PATCH_RADIUS: usize = 5; /// Whether `opts` holds the option set #14191 measured on its test document: Default colour @@ -259,6 +259,13 @@ fn structure_measured(opts: &FillOptions) -> bool { opts.color_adaptation == color_level("default") && opts.rotations.is_empty() && !opts.scale && !opts.mirror } +/// Whether `source` is the Rectangular sampling area of the buffer: every pixel outside the hole. +/// #14191 measured Rectangular, and Auto where it keeps that same area; Auto where its +/// content-based choice differs (or any other area) is not measured. +fn rectangular_area(hole: &[bool], source: &[bool]) -> bool { + hole.iter().zip(source).all(|(h, s)| *h || *s) +} + #[allow(clippy::too_many_arguments)] fn fill_cow( w: usize, @@ -277,7 +284,7 @@ fn fill_cow( return Ok(img.into_owned()); } let mut params = CompleteParams { seed: opts.seed, adapt: opts.color_adaptation, ..Default::default() }; - if structure_measured(opts) { + if structure_measured(opts) && rectangular_area(hole, source) { params.patch_radius = MEASURED_STRUCTURE_PATCH_RADIUS; } let gap = 2 * params.patch_radius + 2; diff --git a/crates/algo/src/content_aware/structure_continue_tests.rs b/crates/algo/src/content_aware/structure_continue_tests.rs index 42ce605c12a..30ff8111aed 100644 --- a/crates/algo/src/content_aware/structure_continue_tests.rs +++ b/crates/algo/src/content_aware/structure_continue_tests.rs @@ -304,3 +304,47 @@ fn unmeasured_geometric_adaptation_keeps_todays_fill() { check_unmeasured(what, &opts, &want); } } + +#[test] +fn measured_auto_area_is_the_rectangular_one() { + // What the measured Auto and Rectangular fills above rest on: on this document Auto keeps the + // whole window, as in the reference application. + let (img, hole, ww) = window_buffers(); + assert_eq!(auto_sampling(ww, 400, 3, &img, &hole), hole.iter().map(|h| !h).collect::>()); +} + +/// #14191 marks Auto on a document where its content-based choice differs from Rectangular as not +/// measured, so that fill keeps today's 7x7 patch even with the measured options. The document: the +/// measured one with a white column 40 px wide at x 440 to 480 in the window, which Auto leaves +/// out. Today's fill is the completion with [`CompleteParams`]' default radius on the same roles. +#[test] +fn auto_fill_whose_area_differs_from_rectangular_keeps_todays_patch_radius() { + let (mut img, hole, ww) = window_buffers(); + let column = |i: usize| (440..480).contains(&(window().x0 as usize + i % ww)); + for (i, px) in img.chunks_mut(3).enumerate() { + if column(i) { + px.copy_from_slice(&[250.0 / 255.0; 3]); + } + } + let source = auto_sampling(ww, 400, 3, &img, &hole); + let rectangular: Vec = hole.iter().map(|h| !h).collect(); + assert_ne!(source, rectangular, "Auto must choose an area other than the Rectangular one here"); + let opts = FillOptions::default(); + let out = fill(ww, 400, 3, &img, &hole, &source, &opts); + let roles: Vec = (0..ww * 400) + .map(|i| { + if hole[i] { + Px::Hole + } else if source[i] { + Px::Source + } else { + Px::Known + } + }) + .collect(); + let params = CompleteParams { seed: opts.seed, adapt: opts.color_adaptation, ..Default::default() }; + assert_eq!(params.patch_radius, 3, "today's radius"); + let today = complete_masked(ww, 400, 3, &img, &roles, ¶ms, &Interrupt::NONE).unwrap().unwrap(); + let moved = out.iter().zip(&today).filter(|(a, b)| a != b).count(); + assert_eq!(moved, 0, "{moved} channel values differ from today's radius-3 fill"); +}