PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Content-Aware Fill: no tinted blotch in a smooth gradient (#4708)
Gert Pellin - GPE09db9f14247d
diff --git a/crates/algo/src/content_aware.rs b/crates/algo/src/content_aware.rsindex 5000ad2e7..ecdb30862 100644--- a/crates/algo/src/content_aware.rs+++ b/crates/algo/src/content_aware.rs@@ -662,6 +662,26 @@ mod tests {assert!(err < 0.12, "mean error {err}");}+ /// A vertical sky gradient (each row one colour) filled at every adaptation level stays the+ /// gradient: no tinted blotch where the copied rows' colours were shifted per channel.+ #[test]+ fn color_adaptation_leaves_a_smooth_gradient_untinted() {+ let (w, h) = (120, 60);+ let row = |y: usize| {+ let t = y as f32 / (h - 1) as f32;+ [0.27 + 0.45 * t, 0.53 + 0.32 * t, 0.86 + 0.1 * t]+ };+ let img: Vec<f32> = (0..w * h).flat_map(|i| row(i / w)).collect();+ let hole: Vec<bool> = (0..w * h).map(|i| (50..70).contains(&(i % w)) && (20..40).contains(&(i / w))).collect();+ let src = vec![true; w * h];+ for level in ["none", "default", "high", "veryHigh"] {+ let out = fill(w, h, 3, &img, &hole, &src, &FillOptions { color_adaptation: color_level(level), ..Default::default() });+ let (out, img) = (&out, &img);+ let worst = (0..w * h).filter(|i| hole[*i]).flat_map(|i| (0..3).map(move |c| (out[i * 3 + c] - img[i * 3 + c]).abs())).fold(0.0f32, f32::max);+ assert!(worst < 3.0 / 255.0, "{level}: off by {}", worst * 255.0);+ }+ }+/// I.i.d. noise: every value occurs once, so a filled pixel equal to an image pixel was copied.fn noise(w: usize, h: usize, seed: u64) -> Vec<f32> {let mut z = seed;diff --git a/crates/algo/src/inpaint.rs b/crates/algo/src/inpaint.rsindex 37e91da49..d7d43c032 100644--- a/crates/algo/src/inpaint.rs+++ b/crates/algo/src/inpaint.rs@@ -508,6 +508,9 @@ struct Solver<'a> {ctl: photocraft_raster::Interrupt<'a>,}+/// A channel deviation under which a patch counts as flat for [`fit`] (about 5 levels of 255).+const FLAT: f32 = 0.02;+/// Gain and bias of one channel for target moments `(Σt, Σt²)` and source moments `(Σs, Σs²)`/// over `n` samples, within the limits.#[inline]@@ -516,7 +519,20 @@ pub(crate) fn fit(a: Adapt, n: f32, st: f32, stt: f32, ss: f32, sss: f32) -> (f3let vt = (stt / n - mt * mt).max(0.0);let vs = (sss / n - ms * ms).max(0.0);let g = if vs > 1e-10 { (vt / vs).sqrt() } else { 1.0 };- let g = g.clamp(a.gain.0, a.gain.1);+ let (mut lo, mut hi) = a.gain;+ // Nearly flat patches of about the same colour (their means within the bias of each other):+ // the ratio of their deviations is noise, so match the mean first, keeping the gain where the+ // bias can still bring the source's mean to the target's. Gained about zero, a gain at its+ // limit moved bright means further than the bias could bring back, and each channel by a+ // different amount: a tinted blotch in a smooth gradient. Textured patches, and flat ones+ // whose colours differ more (Content-Aware Move's Color), are fitted as before.+ if vt.max(vs) < FLAT * FLAT && (mt - ms).abs() <= a.bias && ms > 1e-6 {+ let (glo, ghi) = ((mt - a.bias) / ms, (mt + a.bias) / ms);+ if glo.max(lo) <= ghi.min(hi) {+ (lo, hi) = (glo.max(lo), ghi.min(hi));+ }+ }+ let g = g.clamp(lo, hi);let b = (mt - g * ms).clamp(-a.bias, a.bias);(g, b)}@@ -1959,6 +1975,21 @@ fn min_cut(across: usize, along: usize, err: impl Fn(usize, usize) -> f32) -> Vemod tests {use super::*;+ /// The mean is matched first: on a nearly flat bright patch with a noisy deviation ratio the+ /// gain stays where the bias can still reach the target's mean.+ #[test]+ fn fit_matches_the_mean_before_the_contrast() {+ let a = Adapt { gain: (0.9, 1.1), bias: 0.04 };+ // Target mean 0.70, source mean 0.70; deviations 0.01 vs 0.004 (ratio 2.5).+ let n = 25.0;+ let (mt, ms, sdt, sds) = (0.70f32, 0.70f32, 0.01f32, 0.004f32);+ let (g, b) = fit(a, n, mt * n, (sdt * sdt + mt * mt) * n, ms * n, (sds * sds + ms * ms) * n);+ assert!((g * ms + b - mt).abs() < 1e-5, "mean {} vs {mt} (g {g}, b {b})", g * ms + b);+ // Where the mean can't be reached anyway, the limits hold as before.+ let (g, b) = fit(a, n, 0.2 * n, 0.04 * n, 0.9 * n, 0.81 * n);+ assert!((a.gain.0..=a.gain.1).contains(&g) && b.abs() <= a.bias + 1e-6);+ }+/// Smooth gradient plus deterministic fine noise.fn texture(w: usize, h: usize) -> Vec<f32> {let mut rng = Rng::new(7);