Skip to content

EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust

Refactor raster for strict Clippy checks

Dylan M. Taylore55e2883971f
diff --git a/crates/raster/src/inpaint.rs b/crates/raster/src/inpaint.rs
index 15bdaa0dd..c64de3375 100644
--- a/crates/raster/src/inpaint.rs
+++ b/crates/raster/src/inpaint.rs
@@ -607,38 +607,44 @@ pub struct FillInput {
pub references: Vec<(usize, Image)>,
}
-/// Trace pixel (x, y) of frame `t` through `flows` (`step` = +1 forward with `fwd`, −1 backward
-/// with `bwd`) to the nearest frame where it is visible: (sampled colour, frames travelled).
-fn trace(
- t: usize,
+/// Shared frame and flow data for tracing pixels through a sequence.
+struct TraceContext<'a> {
+ fwd: &'a [Flow],
+ bwd: &'a [Flow],
+ known: &'a (dyn Fn(usize, i64, i64) -> bool + Sync),
+ frames: &'a [&'a Image],
+ max: usize,
+}
+
+/// Starting frame, point, and direction for a flow trace.
+struct TraceStart {
+ frame: usize,
x: f64,
y: f64,
step: i64,
- fwd: &[Flow],
- bwd: &[Flow],
- known: &dyn Fn(usize, i64, i64) -> bool,
- frames: &[&Image],
- max: usize,
-) -> Option<([f32; 4], usize)> {
- let n = frames.len();
- let (w, h) = (frames[0].width as f64, frames[0].height as f64);
- let (mut px, mut py) = (x, y);
- let mut k = t as i64;
- for d in 1..=max {
- let v = if step > 0 {
+}
+
+/// Trace a point to the nearest visible frame: (sampled colour, frames travelled).
+fn trace(context: &TraceContext<'_>, start: TraceStart) -> Option<([f32; 4], usize)> {
+ let n = context.frames.len();
+ let (w, h) = (context.frames[0].width as f64, context.frames[0].height as f64);
+ let (mut px, mut py) = (start.x, start.y);
+ let mut k = start.frame as i64;
+ for d in 1..=context.max {
+ let v = if start.step > 0 {
if k as usize + 1 >= n {
return None;
}
- fwd[k as usize].at(px, py)
+ context.fwd[k as usize].at(px, py)
} else {
if k == 0 {
return None;
}
- bwd[k as usize - 1].at(px, py)
+ context.bwd[k as usize - 1].at(px, py)
};
px += v[0] as f64;
py += v[1] as f64;
- k += step;
+ k += start.step;
if px < 0.0 || py < 0.0 || px >= w || py >= h {
return None;
}
@@ -646,9 +652,9 @@ fn trace(
let (x0, y0) = ((px - 0.5).floor() as i64, (py - 0.5).floor() as i64);
let ok = [(x0, y0), (x0 + 1, y0), (x0, y0 + 1), (x0 + 1, y0 + 1)]
.iter()
- .all(|&(xx, yy)| known(k as usize, xx.clamp(0, w as i64 - 1), yy.clamp(0, h as i64 - 1)));
+ .all(|&(xx, yy)| (context.known)(k as usize, xx.clamp(0, w as i64 - 1), yy.clamp(0, h as i64 - 1)));
if ok {
- return Some((frames[k as usize].sample_bilinear_clamped(px, py), d));
+ return Some((context.frames[k as usize].sample_bilinear_clamped(px, py), d));
}
}
None
@@ -697,6 +703,7 @@ pub fn fill_sequence(input: &FillInput, o: &FillOpts, progress: &mut dyn FnMut(u
// 2. Propagation (and the lighting-correction border ring).
let refs: Vec<&Image> = frames.iter().collect();
let known = |k: usize, x: i64, y: i64| !holes[k][y as usize * w + x as usize];
+ let trace_context = TraceContext { fwd: &fwd, bwd: &bwd, known: &known, frames: &refs, max: n };
let ring_r = 2usize;
let propagated: Vec<(Image, Vec<bool>)> = (0..n)
.into_par_iter()
@@ -708,8 +715,8 @@ pub fn fill_sequence(input: &FillInput, o: &FillOpts, progress: &mut dyn FnMut(u
}
let sample = |i: usize| -> Option<[f32; 4]> {
let (x, y) = ((i % w) as f64 + 0.5, (i / w) as f64 + 0.5);
- let f = trace(t, x, y, 1, &fwd, &bwd, &known, &refs, n);
- let b = trace(t, x, y, -1, &fwd, &bwd, &known, &refs, n);
+ let f = trace(&trace_context, TraceStart { frame: t, x, y, step: 1 });
+ let b = trace(&trace_context, TraceStart { frame: t, x, y, step: -1 });
match (f, b) {
(Some((a, da)), Some((c, dc))) => {
let (wa, wc) = (1.0 / da as f32, 1.0 / dc as f32);
diff --git a/crates/raster/src/warp.rs b/crates/raster/src/warp.rs
index c16480865..9f6de31ce 100644
--- a/crates/raster/src/warp.rs
+++ b/crates/raster/src/warp.rs
@@ -203,22 +203,17 @@ fn union(r: [usize; 4], q: [usize; 4]) -> [usize; 4] {
}
}
-/// Fast affine rows: `blend(dst, src, x, y)` is only called for visible source pixels. Returns
-/// the rectangle of the pixels it blended.
-#[allow(clippy::too_many_arguments)]
-fn affine_rows(
- dst: &mut Image,
- img: &Image,
- inv: &Mat3,
+/// Sampling, blending and destination bounds for an affine row operation.
+struct AffineRows {
sampling: Sampling,
- op: f32,
+ opacity: f32,
quantize: Option<f32>,
- x0: usize,
- x1: usize,
- y0: usize,
- y1: usize,
- blend: impl Fn(Px, Px, u32, u32) -> Px + Sync,
-) -> [usize; 4] {
+ bounds: [usize; 4],
+}
+
+/// Blend visible source samples and return the rectangle of changed pixels.
+fn affine_rows(dst: &mut Image, img: &Image, inv: &Mat3, rows: AffineRows, blend: impl Fn(Px, Px, u32, u32) -> Px + Sync) -> [usize; 4] {
+ let AffineRows { sampling, opacity: op, quantize, bounds: [x0, x1, y0, y1] } = rows;
let levels = quantize.map(|l| (l, 1.0 / l));
let w = dst.width as usize;
let m = inv.0;
@@ -363,22 +358,28 @@ fn composite_warp_impl(
if let Some(k) = accumulate
&& affine
{
- let r = affine_rows(dst, img, &inv, opts.sampling, 1.0, None, x0, x1, y0, y1, |d, s, _, _| {
+ let r = affine_rows(dst, img, &inv, AffineRows { sampling: opts.sampling, opacity: 1.0, quantize: None, bounds: [x0, x1, y0, y1] }, |d, s, _, _| {
[d[0] + s[0] * k, d[1] + s[1] * k, d[2] + s[2] * k, d[3] + s[3] * k]
});
return changed(r);
}
if allow_fast && affine && !mode.is_stencil() {
let r = match mode {
- BlendMode::Normal => affine_rows(dst, img, &inv, opts.sampling, op, quantize, x0, x1, y0, y1, |d, s, _, _| {
- let k = 1.0 - s[3];
- [s[0] + d[0] * k, s[1] + d[1] * k, s[2] + d[2] * k, s[3] + d[3] * k]
- }),
+ BlendMode::Normal => {
+ affine_rows(dst, img, &inv, AffineRows { sampling: opts.sampling, opacity: op, quantize, bounds: [x0, x1, y0, y1] }, |d, s, _, _| {
+ let k = 1.0 - s[3];
+ [s[0] + d[0] * k, s[1] + d[1] * k, s[2] + d[2] * k, s[3] + d[3] * k]
+ })
+ }
BlendMode::Dissolve | BlendMode::DancingDissolve => {
let seed = opts.seed;
- affine_rows(dst, img, &inv, opts.sampling, op, quantize, x0, x1, y0, y1, |d, s, x, y| blend_pixel(mode, d, s, hash_noise(x, y, seed)))
+ affine_rows(dst, img, &inv, AffineRows { sampling: opts.sampling, opacity: op, quantize, bounds: [x0, x1, y0, y1] }, |d, s, x, y| {
+ blend_pixel(mode, d, s, hash_noise(x, y, seed))
+ })
}
- _ => affine_rows(dst, img, &inv, opts.sampling, op, quantize, x0, x1, y0, y1, |d, s, _, _| blend_pixel(mode, d, s, 0.5)),
+ _ => affine_rows(dst, img, &inv, AffineRows { sampling: opts.sampling, opacity: op, quantize, bounds: [x0, x1, y0, y1] }, |d, s, _, _| {
+ blend_pixel(mode, d, s, 0.5)
+ }),
};
return changed(r);
}