mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-19 06:12:03 +00:00
Merge pull request #3822 from arnav-hiwarkar/fix/3464-drop-shadow-softness-range
Drop Shadow: Softness takes After Effects' range, and keeps its blur past the padding budget
This commit is contained in:
@@ -726,7 +726,6 @@ mod tests {
|
||||
("ec.blur.channel", "blueBlurriness", 3462),
|
||||
("ec.blur.channel", "alphaBlurriness", 3462),
|
||||
("ec.obsolete.gaussianlegacy", "blurriness", 3463),
|
||||
("ec.perspective.dropshadow", "softness", 3464),
|
||||
("ec.blur.compound", "maximumBlur", 3465),
|
||||
("ec.blur.ccradial", "amount", 3468),
|
||||
("ec.time.pixelmotionblur", "shutterAngle", 3481),
|
||||
|
||||
@@ -92,8 +92,8 @@ use effectcraft_project::build::Ids;
|
||||
use effectcraft_project::{GroupKind, ParamUi, PropGroup, Property};
|
||||
pub use effectcraft_raster::{AuxChannels, Image};
|
||||
pub use misc::{
|
||||
GLOW_GAIN, GLOW_KNEE, INVERT_ALPHA, INVERT_CHANNELS, glow_ab_t, glow_operation, glow_sigma, linear_wipe_axis, linear_wipe_sigma, radial_wipe_sigma,
|
||||
radial_wipe_wedge, venetian_cover, venetian_origin,
|
||||
GLOW_GAIN, GLOW_KNEE, INVERT_ALPHA, INVERT_CHANNELS, drop_shadow_pad, glow_ab_t, glow_operation, glow_sigma, linear_wipe_axis, linear_wipe_sigma,
|
||||
radial_wipe_sigma, radial_wipe_wedge, venetian_cover, venetian_origin,
|
||||
};
|
||||
pub use sim::particle_state;
|
||||
pub use sim3::{PgBlit, PgPlan, playground_plan, playground_state};
|
||||
|
||||
+104
-13
@@ -3,7 +3,7 @@
|
||||
use effectcraft_color::luminance;
|
||||
use effectcraft_keyframe::Value;
|
||||
use effectcraft_project::ParamUi;
|
||||
use effectcraft_raster::{Image, gaussian_blur, hash_noise};
|
||||
use effectcraft_raster::{Image, gaussian_blur, gaussian_blur_unbounded_at, hash_noise};
|
||||
use rayon::prelude::*;
|
||||
|
||||
use crate::{Buf, EffectCtx, EffectSpec, col, num, p, popup, slider};
|
||||
@@ -283,6 +283,12 @@ fn glow(ctx: &EffectCtx, mut b: Buf) -> Buf {
|
||||
b
|
||||
}
|
||||
|
||||
/// The margin Drop Shadow asks for around a layer: its Distance and its blur's reach (three
|
||||
/// boxes of radius about σ = Softness / 2), both in buffer pixels. Shared with the GPU path.
|
||||
pub fn drop_shadow_pad(dist: f64, soft: f64) -> u32 {
|
||||
((dist + soft * 1.5).ceil() as u32).saturating_add(2)
|
||||
}
|
||||
|
||||
fn drop_shadow(ctx: &EffectCtx, mut b: Buf) -> Buf {
|
||||
let color = ctx.params.color("color");
|
||||
let opacity = ctx.params.f("opacity") as f32 / 100.0;
|
||||
@@ -290,22 +296,41 @@ fn drop_shadow(ctx: &EffectCtx, mut b: Buf) -> Buf {
|
||||
let dist = ctx.params.f("distance") * b.scale;
|
||||
let soft = ctx.params.f("softness") * b.scale;
|
||||
let only = ctx.params.b("shadowOnly");
|
||||
let pad = (dist + soft * 1.5).ceil() as u32 + 2;
|
||||
let pad = drop_shadow_pad(dist, soft);
|
||||
// Whether the buffer holds the whole blur. The pixel budget (#2261) cuts the margin of a
|
||||
// large Softness (After Effects' range reaches 30000, #3464); the shadow is then blurred
|
||||
// before it is moved, as if the buffer went on with transparency, rather than dropping what
|
||||
// the move or the blur takes past the buffer's edge, which would leave it far too faint or
|
||||
// gone. An adjustment layer's buffer is the composition, as before.
|
||||
let mut holds_blur = true;
|
||||
if !ctx.adjustment {
|
||||
let (w0, h0) = (b.img.width, b.img.height);
|
||||
b.pad(pad);
|
||||
let need = pad.saturating_mul(2);
|
||||
holds_blur = b.img.width.saturating_sub(w0) >= need && b.img.height.saturating_sub(h0) >= need;
|
||||
}
|
||||
let (dx, dy) = (dir.sin() * dist, -dir.cos() * dist);
|
||||
let mut sh = Image::new(b.img.width, b.img.height);
|
||||
let src = &b.img;
|
||||
sh.rows_mut().for_each(|(y, row)| {
|
||||
for (x, px) in row.iter_mut().enumerate() {
|
||||
let a = src.sample_bilinear(x as f64 + 0.5 - dx, y as f64 + 0.5 - dy)[3] * opacity * color[3];
|
||||
*px = [color[0] * a, color[1] * a, color[2] * a, a];
|
||||
}
|
||||
});
|
||||
if soft > 0.0 {
|
||||
sh = gaussian_blur(&sh, soft / 2.0, soft / 2.0, false);
|
||||
}
|
||||
let shadow = |sh: &mut Image, (dx, dy): (f64, f64)| {
|
||||
sh.rows_mut().for_each(|(y, row)| {
|
||||
for (x, px) in row.iter_mut().enumerate() {
|
||||
let a = src.sample_bilinear(x as f64 + 0.5 - dx, y as f64 + 0.5 - dy)[3] * opacity * color[3];
|
||||
*px = [color[0] * a, color[1] * a, color[2] * a, a];
|
||||
}
|
||||
});
|
||||
};
|
||||
let mut sh = if soft > 0.0 && !holds_blur {
|
||||
// Only the fraction of the move is sampled (into one more column and row); the blur
|
||||
// moves the rest.
|
||||
let (ix, iy) = (dx.floor(), dy.floor());
|
||||
let mut near = Image::new(src.width.saturating_add(1), src.height.saturating_add(1));
|
||||
shadow(&mut near, (dx - ix, dy - iy));
|
||||
gaussian_blur_unbounded_at(&near, soft / 2.0, soft / 2.0, [ix as i64, iy as i64], src.width, src.height)
|
||||
} else {
|
||||
let mut sh = Image::new(src.width, src.height);
|
||||
shadow(&mut sh, (dx, dy));
|
||||
if soft > 0.0 { gaussian_blur(&sh, soft / 2.0, soft / 2.0, false) } else { sh }
|
||||
};
|
||||
if !only {
|
||||
sh.data.par_iter_mut().zip(b.img.data.par_iter()).for_each(|(s, o)| {
|
||||
let k = 1.0 - o[3];
|
||||
@@ -872,7 +897,7 @@ pub fn specs() -> Vec<EffectSpec> {
|
||||
p("opacity", "Opacity", num(50.0), slider(0.0, 100.0, 0.0, 100.0, 0)),
|
||||
p("direction", "Direction", num(135.0), ParamUi::Angle),
|
||||
p("distance", "Distance", num(5.0), slider(0.0, 32000.0, 0.0, 120.0, 1)),
|
||||
p("softness", "Softness", num(0.0), slider(0.0, 1000.0, 0.0, 250.0, 1)),
|
||||
p("softness", "Softness", num(0.0), slider(0.0, 30000.0, 0.0, 250.0, 1)),
|
||||
p("shadowOnly", "Shadow Only", Value::Bool(false), ParamUi::Checkbox),
|
||||
],
|
||||
drop_shadow,
|
||||
@@ -1037,6 +1062,72 @@ mod tests {
|
||||
assert_ne!(row(2.0), row(2.4));
|
||||
}
|
||||
|
||||
/// #3464: Softness takes After Effects' valid range, 0..30000; the slider still shows 0..250.
|
||||
#[test]
|
||||
fn drop_shadow_softness_takes_the_after_effects_range() {
|
||||
let spec = crate::find("ec.perspective.dropshadow").unwrap();
|
||||
let ui = &spec.params.iter().find(|p| p.id == "softness").unwrap().ui;
|
||||
assert_eq!(*ui, slider(0.0, 30000.0, 0.0, 250.0, 1));
|
||||
}
|
||||
|
||||
/// #3464: a Softness far past the range (an expression can set one) used to overflow the
|
||||
/// padding's `u32` (a panic in debug builds). It renders, finite.
|
||||
#[test]
|
||||
fn drop_shadow_takes_a_huge_softness() {
|
||||
for softness in [1e12, f64::INFINITY] {
|
||||
let layer = effectcraft_raster::Image::filled(8, 8, [1.0, 1.0, 1.0, 1.0]);
|
||||
let out = run_fx("ec.perspective.dropshadow", &[("softness", num(softness))], layer, 0.0, EffectEnv::default());
|
||||
assert!(out.img.data.iter().flatten().all(|v| v.is_finite()), "softness {softness}");
|
||||
}
|
||||
}
|
||||
|
||||
/// #3464: with the margin cut, a Distance that takes the whole layer past the buffer's edge
|
||||
/// still leaves the shadow's blur inside it. A 64 × 48 layer moved 3000 px right, Softness
|
||||
/// 8000: at the layer's centre the shadow is the Gaussian's share there (it was 0 when the
|
||||
/// layer was moved out of the buffer before the blur).
|
||||
#[test]
|
||||
fn drop_shadow_moved_past_a_cut_margin_keeps_its_blur() {
|
||||
let (w, h, dist, softness) = (64u32, 48u32, 3000.0, 8000.0);
|
||||
let layer = effectcraft_raster::Image::filled(w, h, [1.0, 1.0, 1.0, 1.0]);
|
||||
let vals = [("softness", num(softness)), ("opacity", num(100.0)), ("distance", num(dist)), ("direction", num(90.0)), ("shadowOnly", Value::Bool(true))];
|
||||
let out = run_fx("ec.perspective.dropshadow", &vals, layer, 0.0, EffectEnv::default());
|
||||
assert!(out.offset[0] + f64::from(w) + dist > f64::from(out.img.width), "the move takes the layer past the buffer");
|
||||
let sigma = softness / 2.0;
|
||||
// The Gaussian's share of a `len`-pixel side, at `d` pixels from its centre.
|
||||
let share = |len: u32, d: f64| {
|
||||
let sum: f64 = (0..len).map(|i| (-((f64::from(i) + 0.5 - f64::from(len) / 2.0 + d) / sigma).powi(2) / 2.0).exp()).sum();
|
||||
sum / (sigma * (2.0 * std::f64::consts::PI).sqrt())
|
||||
};
|
||||
let want = share(w, dist) * share(h, 0.0);
|
||||
let at = |v: f64, half: u32| (v + f64::from(half)) as i64;
|
||||
let got = f64::from(out.img.get(at(out.offset[0], w / 2), at(out.offset[1], h / 2))[3]);
|
||||
assert!((0.85..1.1).contains(&(got / want)), "centre alpha {got}, Gaussian {want}");
|
||||
}
|
||||
|
||||
/// #3464: past the padding budget (#2261) the shadow keeps its whole blur (σ = Softness / 2)
|
||||
/// up to the buffer's edge. A 64 × 48 layer's shadow at its centre is the Gaussian's share
|
||||
/// of the layer, less the 6 % three boxes' peak is under a Gaussian's; dropping what the
|
||||
/// boxes spread past the buffer left it at 0.11 of that at 8000.
|
||||
#[test]
|
||||
fn drop_shadow_keeps_its_blur_past_the_padding_budget() {
|
||||
let (w, h) = (64u32, 48u32);
|
||||
for softness in [500.0, 4000.0, 8000.0, 30000.0] {
|
||||
let layer = effectcraft_raster::Image::filled(w, h, [1.0, 1.0, 1.0, 1.0]);
|
||||
let vals = [("softness", num(softness)), ("opacity", num(100.0)), ("distance", num(0.0)), ("shadowOnly", Value::Bool(true))];
|
||||
let out = run_fx("ec.perspective.dropshadow", &vals, layer, 0.0, EffectEnv::default());
|
||||
assert!(out.img.data.iter().flatten().all(|v| v.is_finite()), "softness {softness}");
|
||||
let sigma = softness / 2.0;
|
||||
let share = |len: u32| {
|
||||
let sum: f64 = (0..len).map(|i| (-((f64::from(i) + 0.5 - f64::from(len) / 2.0) / sigma).powi(2) / 2.0).exp()).sum();
|
||||
sum / (sigma * (2.0 * std::f64::consts::PI).sqrt())
|
||||
};
|
||||
let want = share(w) * share(h);
|
||||
let at = |v: f64, half: u32| (v + f64::from(half)) as i64;
|
||||
let got = f64::from(out.img.get(at(out.offset[0], w / 2), at(out.offset[1], h / 2))[3]);
|
||||
assert!((0.85..1.02).contains(&(got / want)), "softness {softness}: centre alpha {got}, Gaussian {want}");
|
||||
}
|
||||
}
|
||||
|
||||
/// #3460: Blurriness goes up to After Effects' 30000. There the padding stays within the
|
||||
/// effect pixel budget, every pixel stays finite, and Repeat Edge Pixels still repeats the
|
||||
/// layer's edges: a layer white on the left and black on the right blurs to an even grey.
|
||||
|
||||
@@ -60,6 +60,7 @@ const ROWS: &[(u32, &str, &str, f64, f64)] = &[
|
||||
(3471, "ec.distort.transform", "scaleHeight", -30000.0, 30000.0),
|
||||
(3471, "ec.distort.transform", "scaleWidth", -30000.0, 30000.0),
|
||||
(3470, "ec.distort.turbulentdisplace", "evolutionOptions/randomSeed", 0.0, 100000.0),
|
||||
(3464, "ec.perspective.dropshadow", "softness", 0.0, 30000.0),
|
||||
(3467, "ec.blur.radial", "amount", 0.0, 1000.0),
|
||||
(3460, "ec.blur.gaussian", "blurriness", 0.0, 30000.0),
|
||||
(3469, "ec.distort.turbulentdisplace", "amount", -10000.0, 10000.0),
|
||||
|
||||
@@ -411,8 +411,12 @@ fn drop_shadow(e: &mut Enc, ctx: &EffectCtx, mut b: GBuf) -> Option<GBuf> {
|
||||
let dist = ctx.params.f("distance") * b.scale;
|
||||
let soft = ctx.params.f("softness") * b.scale;
|
||||
let only = ctx.params.b("shadowOnly");
|
||||
let pad = (dist + soft * 1.5).ceil() as u32 + 2;
|
||||
let pad = effectcraft_effects::drop_shadow_pad(dist, soft);
|
||||
if !ctx.adjustment {
|
||||
// A margin cut to the pixel budget: the CPU blurs on past the buffer's edge (#3464).
|
||||
if soft > 0.0 && effectcraft_effects::pad_within_budget(b.img.width, b.img.height, pad) < pad {
|
||||
return None;
|
||||
}
|
||||
b.pad(e, pad)?;
|
||||
}
|
||||
let (dx, dy) = (dir.sin() * dist, -dir.cos() * dist);
|
||||
|
||||
@@ -760,6 +760,39 @@ fn box_blur_horizontal_rows_match_cpu_beyond_256() {
|
||||
}
|
||||
}
|
||||
|
||||
/// #3464: Drop Shadow's Softness reaches 30000. While the padded buffer holds the blur the GPU
|
||||
/// runs it and matches the CPU (1000, the old maximum, on a 70 × 44 layer). Once the pixel
|
||||
/// budget cuts the padding only the CPU blurs past the buffer's edge, so the GPU declines the
|
||||
/// effect; the render then takes it on the CPU at 8 and 32 bpc, with and without Shadow Only.
|
||||
#[test]
|
||||
fn drop_shadow_large_softness_matches_cpu() {
|
||||
effect_case("ec.perspective.dropshadow", &[("distance", n(6.0)), ("softness", n(1000.0)), ("opacity", n(100.0))]);
|
||||
let Some(g) = gpu() else { return };
|
||||
for only in [false, true] {
|
||||
let vals = [("distance", n(6.0)), ("softness", n(30000.0)), ("opacity", n(100.0)), ("shadowOnly", Value::Bool(only))];
|
||||
let spec = effectcraft_effects::find("ec.perspective.dropshadow").unwrap();
|
||||
let mut params = effectcraft_effects::Params {
|
||||
values: spec.params.iter().map(|ps| (ps.id.to_string(), effectcraft_effects::default_value(ps, [70.0, 44.0]))).collect(),
|
||||
};
|
||||
for (k, v) in &vals {
|
||||
params.values.insert(k.to_string(), v.clone());
|
||||
}
|
||||
let ctx = effectcraft_effects::EffectCtx { params: ¶ms, time: 0.0, layer_size: [70.0, 44.0], seed: 11, adjustment: false, env: Default::default() };
|
||||
let buf = effectcraft_effects::Buf { img: pattern(7, 70, 44), offset: [0.0, 0.0], scale: 1.0 };
|
||||
let step = effectcraft_render::FxStep { spec, ctx };
|
||||
assert!(effectcraft_render::Accelerator::effects(g, &[step], &buf, None).is_none(), "the GPU hands a cut margin to the CPU");
|
||||
for depth in [BitDepth::Bpc8, BitDepth::Bpc32] {
|
||||
let mut s = Scene::new(depth);
|
||||
let bg = s.solid([0.15, 0.1, 0.2], 97, 61);
|
||||
s.push(bg);
|
||||
let mut l = s.footage(70, 44);
|
||||
s.effect(&mut l, "ec.perspective.dropshadow", &vals);
|
||||
s.push(l);
|
||||
check(&format!("drop shadow 30000 only {only} {depth:?}"), compare_at(&s, opts(), Tick::ZERO), 0.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// #3460: a box radius far past the image (Gaussian Blur's at Blurriness 30000 is about 15000 px)
|
||||
/// starts each running sum as the CPU does, the pixels inside once and the repeated edges
|
||||
/// counted, so the GPU matches it along rows and columns, with and without repeated edges.
|
||||
|
||||
@@ -182,6 +182,141 @@ pub fn gaussian_blur(img: &Image, sigma_x: f64, sigma_y: f64, repeat: bool) -> I
|
||||
box_passes(img, &rx, &ry, repeat)
|
||||
}
|
||||
|
||||
/// [`gaussian_blur`] with transparent edges, as if the image went on with transparency on
|
||||
/// every side: what a box pass spreads past the edge still feeds the passes after it, where
|
||||
/// [`gaussian_blur`] drops it. For blurs that reach past the buffer holding them (an effect's
|
||||
/// padding cut to its pixel budget), which [`gaussian_blur`] leaves far too faint once the
|
||||
/// boxes are wider than the buffer. A line costs O(length) whatever the sigma.
|
||||
pub fn gaussian_blur_unbounded(img: &Image, sigma_x: f64, sigma_y: f64) -> Image {
|
||||
gaussian_blur_unbounded_at(img, sigma_x, sigma_y, [0, 0], img.width, img.height)
|
||||
}
|
||||
|
||||
/// [`gaussian_blur_unbounded`] of `img` moved by `shift` pixels, seen through a `width` ×
|
||||
/// `height` window at the origin: output `(x, y)` is the blurred plane at
|
||||
/// `(x − shift[0], y − shift[1])`. Nothing is cut before the blur, so the tails of content
|
||||
/// that lands outside the window still reach into it.
|
||||
pub fn gaussian_blur_unbounded_at(img: &Image, sigma_x: f64, sigma_y: f64, shift: [i64; 2], width: u32, height: u32) -> Image {
|
||||
let (w, h, ow, oh) = (img.width as usize, img.height as usize, width as usize, height as usize);
|
||||
if w == 0 || h == 0 || ow == 0 || oh == 0 {
|
||||
return Image::new(width, height);
|
||||
}
|
||||
let (rx, ry) = (unbounded_radii(sigma_x).unwrap_or_default(), unbounded_radii(sigma_y).unwrap_or_default());
|
||||
// Horizontal: each source row into a row of the window's width.
|
||||
let mut rows = vec![[0.0f32; 4]; ow.saturating_mul(h)];
|
||||
rows.par_chunks_mut(ow).zip(img.data.par_chunks(w)).for_each(|(o, src)| boxes_unbounded(src, &rx, shift[0], o));
|
||||
// Vertical: each of those columns into a column of the window's height.
|
||||
let cols: Vec<Vec<Px>> = (0..ow)
|
||||
.into_par_iter()
|
||||
.map(|x| {
|
||||
let col: Vec<Px> = rows.iter().skip(x).step_by(ow).copied().collect();
|
||||
let mut o = vec![[0.0f32; 4]; oh];
|
||||
boxes_unbounded(&col, &ry, shift[1], &mut o);
|
||||
o
|
||||
})
|
||||
.collect();
|
||||
// At most three buffers at once: the rows go before the output comes.
|
||||
drop(rows);
|
||||
let mut out = Image::new(width, height);
|
||||
out.data.par_chunks_mut(ow).enumerate().for_each(|(y, row)| {
|
||||
for (o, col) in row.iter_mut().zip(&cols) {
|
||||
if let Some(v) = col.get(y) {
|
||||
*o = *v;
|
||||
}
|
||||
}
|
||||
});
|
||||
out
|
||||
}
|
||||
|
||||
/// Above this sigma (pixels) [`gaussian_blur_unbounded`] blurs as if it were this: far wider
|
||||
/// than any buffer ([`crate::MAX_PADDED_SIDE`]), and small enough for [`box_radii`]'s `i64`
|
||||
/// arithmetic (which overflows near 9e8).
|
||||
const UNBOUNDED_SIGMA_MAX: f64 = 1e7;
|
||||
|
||||
/// [`gaussian_blur`]'s box radii for `sigma` without the zero ones (each an identity), or
|
||||
/// `None` where it doesn't blur (and for a sigma that isn't a number).
|
||||
fn unbounded_radii(sigma: f64) -> Option<Vec<i64>> {
|
||||
if sigma.is_nan() || sigma <= 0.05 {
|
||||
return None;
|
||||
}
|
||||
let radii: Vec<i64> = box_radii(sigma.min(UNBOUNDED_SIGMA_MAX), 3).into_iter().filter_map(|r| i64::try_from(r).ok()).filter(|&r| r > 0).collect();
|
||||
(!radii.is_empty()).then_some(radii)
|
||||
}
|
||||
|
||||
/// Transparent-edged box passes of radii `r` (up to three, none zero) over `line`, as if it
|
||||
/// went on with zeros, moved by `shift`: `out[x]` is the blurred line at `x − shift`.
|
||||
///
|
||||
/// One box is a difference of prefix sums, `(S(x + r) − S(x − r − 1)) / (2r + 1)`, so `k` boxes
|
||||
/// are `2^k` signed terms of the `k`-th prefix sum `Sk` (`S0` is the line). Before the line
|
||||
/// every `Sk` is 0; past it, `j = m − n + 1` steps on with `T` the line's total, they are
|
||||
/// polynomials: `S0(m) = 0`, `S1(m) = T`, `S2(m) = S2(n − 1) + j·T`,
|
||||
/// `S3(m) = S3(n − 1) + j·S2(n − 1) + T·j(j + 1)/2`. The sums are `f64`. `Sk` grows like `n^k`,
|
||||
/// so the cancelling terms leave a rounding error near `ε·n^k / (2r + 1)^k` of the values:
|
||||
/// under 1e-8 for any line a blur too wide for its buffer runs on.
|
||||
fn boxes_unbounded(line: &[Px], r: &[i64], shift: i64, out: &mut [Px]) {
|
||||
let (n, k) = (line.len(), r.len());
|
||||
let Ok(n_i) = i64::try_from(n) else { return };
|
||||
if n == 0 || k > 3 {
|
||||
return;
|
||||
}
|
||||
// sums[o] = the (o + 1)-th prefix sum so far.
|
||||
let mut sums = [[0.0f64; 4]; 3];
|
||||
let mut sk = Vec::with_capacity(n);
|
||||
for p in line {
|
||||
let v = p.map(f64::from);
|
||||
for c in 0..4 {
|
||||
sums[0][c] += v[c];
|
||||
sums[1][c] += sums[0][c];
|
||||
sums[2][c] += sums[1][c];
|
||||
}
|
||||
sk.push(match k {
|
||||
0 => v,
|
||||
_ => sums[k - 1],
|
||||
});
|
||||
}
|
||||
let at = |m: i64| -> [f64; 4] {
|
||||
if m < 0 {
|
||||
return [0.0; 4];
|
||||
}
|
||||
if let Some(v) = usize::try_from(m).ok().and_then(|i| sk.get(i)) {
|
||||
return *v;
|
||||
}
|
||||
let j = (m - n_i + 1) as f64;
|
||||
std::array::from_fn(|c| match k {
|
||||
0 => 0.0,
|
||||
1 => sums[0][c],
|
||||
2 => sums[1][c] + j * sums[0][c],
|
||||
_ => sums[2][c] + j * sums[1][c] + sums[0][c] * j * (j + 1.0) / 2.0,
|
||||
})
|
||||
};
|
||||
// The offsets and signs: each box adds `S(x + r)` and subtracts `S(x − r − 1)`.
|
||||
let terms: Vec<(i64, f64)> = (0..1usize << k)
|
||||
.map(|bits| {
|
||||
r.iter().enumerate().fold((0i64.saturating_sub(shift), 1.0f64), |(at, sign), (b, &ri)| {
|
||||
if bits >> b & 1 == 0 { (at.saturating_add(ri), sign) } else { (at.saturating_sub(ri.saturating_add(1)), -sign) }
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let norm: f64 = r.iter().map(|&ri| 2.0 * ri as f64 + 1.0).product();
|
||||
// The boxes read the line within `reach` of `x − shift`. Outside that the result is 0, and
|
||||
// the polynomials far past the line would only cancel to their rounding error.
|
||||
let reach = r.iter().fold(0i64, |s, &ri| s.saturating_add(ri));
|
||||
for (x, o) in (0i64..).zip(out.iter_mut()) {
|
||||
let c = x.saturating_sub(shift);
|
||||
if c.saturating_add(reach) < 0 || c.saturating_sub(reach) >= n_i {
|
||||
*o = [0.0; 4];
|
||||
continue;
|
||||
}
|
||||
let mut v = [0.0f64; 4];
|
||||
for &(offset, sign) in &terms {
|
||||
let s = at(x.saturating_add(offset));
|
||||
for c in 0..4 {
|
||||
v[c] += sign * s[c];
|
||||
}
|
||||
}
|
||||
*o = v.map(|v| (v / norm) as f32);
|
||||
}
|
||||
}
|
||||
|
||||
/// Below this sigma (pixels) three integer-radius box passes are too coarse (sigma under ~0.6 is
|
||||
/// a no-op and the result steps between a few shapes), so [`gaussian_blur_exact`] is used instead.
|
||||
const EXACT_SIGMA_BELOW: f64 = 3.0;
|
||||
@@ -566,4 +701,107 @@ mod tests {
|
||||
assert_eq!(r.len(), 3);
|
||||
assert!(r.iter().all(|&v| (7..=10).contains(&v)), "{r:?}");
|
||||
}
|
||||
|
||||
/// #3464: moving the image before [`gaussian_blur_unbounded`] and blurring it moved with
|
||||
/// [`gaussian_blur_unbounded_at`] agree, also when the move puts all of it outside the window
|
||||
/// (its tails still reach in) and along an axis that isn't blurred.
|
||||
#[test]
|
||||
fn unbounded_gaussian_at_a_shift_matches_blurring_the_moved_image() {
|
||||
let mut img = Image::new(6, 5);
|
||||
for (i, p) in img.data.iter_mut().enumerate() {
|
||||
let v = ((i * 37) % 11) as f32 / 11.0;
|
||||
*p = [v, 0.5 * v, 1.0 - v, 1.0];
|
||||
}
|
||||
let (ow, oh) = (14u32, 9u32);
|
||||
for (sx, sy, shift) in [(3.0, 2.0, [4i64, 2i64]), (5.0, 0.0, [-3, 1]), (12.0, 12.0, [-20, 16]), (8.0, 8.0, [25, -12])] {
|
||||
let got = gaussian_blur_unbounded_at(&img, sx, sy, shift, ow, oh);
|
||||
// The moved image in a canvas large enough to hold it and the window, blurred.
|
||||
let off = [40i64, 40i64];
|
||||
let mut canvas = Image::new(100, 100);
|
||||
for y in 0..img.height {
|
||||
for x in 0..img.width {
|
||||
let (cx, cy) = (i64::from(x) + shift[0] + off[0], i64::from(y) + shift[1] + off[1]);
|
||||
canvas.set(cx as u32, cy as u32, img.get(i64::from(x), i64::from(y)));
|
||||
}
|
||||
}
|
||||
let want = gaussian_blur_unbounded(&canvas, sx, sy).crop(off[0], off[1], ow, oh);
|
||||
assert!(got.data.iter().flatten().any(|v| *v > 1e-4), "{shift:?}: something reaches the window");
|
||||
for (a, b) in got.data.iter().flatten().zip(want.data.iter().flatten()) {
|
||||
assert!((a - b).abs() <= 1e-5, "σ ({sx}, {sy}) {shift:?}: {a} vs {b}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// #3464: a window the blur doesn't reach stays exactly transparent, however far the move
|
||||
/// (far past the line, the cancelling polynomials left a rounding residue of about 5).
|
||||
#[test]
|
||||
fn unbounded_gaussian_at_a_far_shift_is_exactly_transparent() {
|
||||
let img = Image::filled(1, 1, [1.0, 1.0, 1.0, 1.0]);
|
||||
for shift in [[-1_000_000_030i64, 0i64], [0, 1_000_000_030], [i64::MIN, i64::MAX]] {
|
||||
let out = gaussian_blur_unbounded_at(&img, 1.5, 1.5, shift, 4, 3);
|
||||
assert!(out.data.iter().flatten().all(|v| *v == 0.0), "{shift:?}");
|
||||
}
|
||||
// Next to the window the tail still reaches in.
|
||||
assert!(gaussian_blur_unbounded_at(&img, 1.5, 1.5, [-1, 0], 4, 3).get(0, 0)[3] > 0.0);
|
||||
}
|
||||
|
||||
/// #3464: a sigma past any buffer, or infinite, blurs as the widest one does: finite, no
|
||||
/// overflow in the radii. A NaN sigma leaves the axis alone.
|
||||
#[test]
|
||||
fn unbounded_gaussian_takes_any_sigma() {
|
||||
let mut img = Image::new(9, 7);
|
||||
img.set(4, 3, [1.0, 0.5, 0.25, 1.0]);
|
||||
let wide = gaussian_blur_unbounded(&img, 1e7, 1e7);
|
||||
for (sx, sy) in [(f64::INFINITY, f64::INFINITY), (1e300, 1e12)] {
|
||||
assert_eq!(gaussian_blur_unbounded(&img, sx, sy).data, wide.data, "σ ({sx}, {sy})");
|
||||
}
|
||||
assert!(wide.data.iter().flatten().all(|v| v.is_finite()));
|
||||
assert_eq!(gaussian_blur_unbounded(&img, f64::NAN, f64::NAN).data, img.data);
|
||||
}
|
||||
|
||||
/// #3464: [`gaussian_blur_unbounded`] is [`gaussian_blur`] on the image padded with enough
|
||||
/// transparency to hold every pass, cropped back: what a pass spreads past the edge comes
|
||||
/// back in the passes after it. Sigmas far wider than the image too, along each axis.
|
||||
#[test]
|
||||
fn unbounded_gaussian_matches_a_buffer_padded_past_its_reach() {
|
||||
let pattern = |w: u32, h: u32| {
|
||||
let mut img = Image::new(w, h);
|
||||
for (i, p) in img.data.iter_mut().enumerate() {
|
||||
let v = ((i * 7919) % 101) as f32 / 101.0;
|
||||
*p = [v, v * 0.5, 1.0 - v, (v + 0.3).min(1.0)];
|
||||
}
|
||||
img
|
||||
};
|
||||
let cases = [
|
||||
(pattern(23, 17), 4.0f64, 0.0f64, 1e-4),
|
||||
(pattern(23, 17), 0.0, 6.0, 1e-4),
|
||||
(pattern(23, 17), 7.5, 3.2, 1e-4),
|
||||
(pattern(23, 17), 60.0, 45.0, 1e-4),
|
||||
// Zero radii (an identity) and one non-zero box among them.
|
||||
(pattern(23, 17), 0.5, 0.0, 1e-6),
|
||||
(pattern(23, 17), 0.0, 1.1, 1e-5),
|
||||
// The reference's f32 running sums drift over its 90 000-pixel line (by about 1.5e-4
|
||||
// of the value).
|
||||
(pattern(40, 1), 15000.0, 0.0, 1e-3),
|
||||
(pattern(1, 40), 0.0, 15000.0, 1e-3),
|
||||
];
|
||||
for (img, sx, sy, tol) in cases {
|
||||
// Three boxes of radius about σ each reach at most 3σ + 2. Padded by hand, along
|
||||
// the blurred axes only (`Image::padded` pads both and caps a side at 30000).
|
||||
let reach = |s: f64| if s > 0.0 { (s * 3.0 + 4.0).ceil() as u32 } else { 0 };
|
||||
let (px, py) = (reach(sx), reach(sy));
|
||||
let mut big = Image::new(img.width + 2 * px, img.height + 2 * py);
|
||||
for y in 0..img.height {
|
||||
for x in 0..img.width {
|
||||
big.set(x + px, y + py, img.get(x as i64, y as i64));
|
||||
}
|
||||
}
|
||||
let want = gaussian_blur(&big, sx, sy, false).crop(px as i64, py as i64, img.width, img.height);
|
||||
let got = gaussian_blur_unbounded(&img, sx, sy);
|
||||
let peak = want.data.iter().flatten().fold(0.0f32, |m, v| m.max(v.abs()));
|
||||
for (a, b) in got.data.iter().flatten().zip(want.data.iter().flatten()) {
|
||||
assert!((a - b).abs() <= tol * peak.max(1e-6), "σ ({sx}, {sy}) on {}×{}: {a} vs {b} (peak {peak})", img.width, img.height);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,8 +18,8 @@ use effectcraft_color::{BlendMode, blend_pixel};
|
||||
use rayon::prelude::*;
|
||||
|
||||
pub use blur::{
|
||||
RADIAL_BLUR_SAMPLES, box_blur, directional_blur, gaussian_blur, gaussian_blur_exact, gaussian_is_exact, radial_blur, radial_blur_spin_degrees,
|
||||
radial_blur_zoom_scale,
|
||||
RADIAL_BLUR_SAMPLES, box_blur, directional_blur, gaussian_blur, gaussian_blur_exact, gaussian_blur_unbounded, gaussian_blur_unbounded_at,
|
||||
gaussian_is_exact, radial_blur, radial_blur_spin_degrees, radial_blur_zoom_scale,
|
||||
};
|
||||
pub use channels3d::AuxChannels;
|
||||
pub use warp::{Sampling, WarpOpts, accumulate_warp, composite_warp, composite_warp_changed, composite_warp_reference, resample};
|
||||
|
||||
@@ -64,6 +64,7 @@ the GPU is performance, not parity.
|
||||
|
||||
| Date | Change | Summary |
|
||||
|---|---|---|
|
||||
| 2026-11-17 | minor | Drop Shadow: Softness takes After Effects' recorded valid range, 0..30000 (default and 0..250 slider unchanged). Where the padding budget cuts the margin, the shadow keeps its full blur (σ = Softness / 2) up to the buffer's edge, where it is cut; that case renders on the CPU |
|
||||
| 2026-11-17 | minor | Radial Blur: Amount takes After Effects' valid range 0..1000 (slider and default unchanged); Zoom past 200 spreads its samples from the centre outwards instead of sampling the layer mirrored through the centre (CPU and GPU; not yet measured against After Effects past Amount 200) |
|
||||
| 2026-11-17 | minor | Gaussian Blur: Blurriness takes After Effects' recorded valid range 0..30000 (was 0..3000; default and 0..50 slider unchanged). Padding stays within the effect pixel budget at the top of the range (CPU and GPU), and Roto Brush analyses 0.24 saved below a Gaussian Blur still match (#3460) |
|
||||
| 2026-11-17 | minor | Turbulent Displace: Amount takes After Effects' recorded valid range, −10000..10000 (default and the −200..200 slider unchanged). Past it (an expression's value) Amount is bounded to 2^24 px and a NaN Amount leaves the layer unchanged, so padding and sampling stay finite (CPU and GPU). Locked pinning on a layer under a pixel across no longer crashes |
|
||||
|
||||
Reference in New Issue
Block a user