use thiserror::Error;
#[derive(Debug, Error)]
pub enum MotionBlurError {
#[error("Invalid motion blur configuration: {0}")]
InvalidConfig(String),
#[error("Invalid image data: {0}")]
InvalidImage(String),
#[error("No frames supplied for accumulation")]
EmptyFrames,
#[error("Dimension mismatch: expected {expected}, got {got}")]
DimensionMismatch {
expected: usize,
got: usize,
},
#[error("Empty image buffer")]
EmptyImage,
#[error("Image buffer {actual} bytes does not match {width}×{height}×4 = {expected}")]
InvalidDimensions {
actual: usize,
expected: usize,
width: u32,
height: u32,
},
#[error("Motion vector buffer length {mv_len} does not match image pixels {px_len}×2")]
MotionVectorMismatch {
mv_len: usize,
px_len: usize,
},
#[error("Invalid sample count {samples}: must be >= 1")]
InvalidSampleCount {
samples: usize,
},
#[error("Shutter angle {angle} out of range (0, 360]")]
InvalidShutterAngle {
angle: f32,
},
#[error("empty input")]
EmptyInput,
}
pub fn subframe_t(i: usize, n: usize) -> f32 {
i as f32 / (n.saturating_sub(1).max(1)) as f32
}
pub fn lerp_position(start: [f32; 3], end: [f32; 3], t: f32) -> [f32; 3] {
[
start[0] + (end[0] - start[0]) * t,
start[1] + (end[1] - start[1]) * t,
start[2] + (end[2] - start[2]) * t,
]
}
pub fn lerp_quaternion(q0: [f32; 4], q1: [f32; 4], t: f32) -> [f32; 4] {
let dot = q0[0] * q1[0] + q0[1] * q1[1] + q0[2] * q1[2] + q0[3] * q1[3];
let q1 = if dot < 0.0 {
[-q1[0], -q1[1], -q1[2], -q1[3]]
} else {
q1
};
[
q0[0] + (q1[0] - q0[0]) * t,
q0[1] + (q1[1] - q0[1]) * t,
q0[2] + (q1[2] - q0[2]) * t,
q0[3] + (q1[3] - q0[3]) * t,
]
}
pub fn normalize_quaternion(q: [f32; 4]) -> [f32; 4] {
let mag_sq = q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3];
if mag_sq < 1e-9_f32 * 1e-9_f32 {
return [0.0, 0.0, 0.0, 1.0];
}
let inv = 1.0 / mag_sq.sqrt();
[q[0] * inv, q[1] * inv, q[2] * inv, q[3] * inv]
}
#[derive(Debug, Clone)]
pub struct CameraMotion {
pub position_start: [f32; 3],
pub position_end: [f32; 3],
pub rotation_start: [f32; 4],
pub rotation_end: [f32; 4],
}
impl CameraMotion {
pub fn new(
pos_start: [f32; 3],
pos_end: [f32; 3],
rot_start: [f32; 4],
rot_end: [f32; 4],
) -> Self {
Self {
position_start: pos_start,
position_end: pos_end,
rotation_start: rot_start,
rotation_end: rot_end,
}
}
pub fn stationary(position: [f32; 3], rotation: [f32; 4]) -> Self {
Self {
position_start: position,
position_end: position,
rotation_start: rotation,
rotation_end: rotation,
}
}
pub fn position_at(&self, t: f32) -> [f32; 3] {
lerp_position(self.position_start, self.position_end, t)
}
pub fn rotation_at(&self, t: f32) -> [f32; 4] {
let lerped = lerp_quaternion(self.rotation_start, self.rotation_end, t);
normalize_quaternion(lerped)
}
pub fn translation_distance(&self) -> f32 {
let dx = self.position_end[0] - self.position_start[0];
let dy = self.position_end[1] - self.position_start[1];
let dz = self.position_end[2] - self.position_start[2];
(dx * dx + dy * dy + dz * dz).sqrt()
}
pub fn is_stationary(&self) -> bool {
let pos_same = (self.position_start[0] - self.position_end[0]).abs() < 1e-6
&& (self.position_start[1] - self.position_end[1]).abs() < 1e-6
&& (self.position_start[2] - self.position_end[2]).abs() < 1e-6;
let rot_same = (self.rotation_start[0] - self.rotation_end[0]).abs() < 1e-6
&& (self.rotation_start[1] - self.rotation_end[1]).abs() < 1e-6
&& (self.rotation_start[2] - self.rotation_end[2]).abs() < 1e-6
&& (self.rotation_start[3] - self.rotation_end[3]).abs() < 1e-6;
pos_same && rot_same
}
}
#[derive(Debug, Clone)]
pub struct AccumulationConfig {
pub num_samples: usize,
pub shutter_open: f32,
pub uniform_weights: bool,
}
impl Default for AccumulationConfig {
fn default() -> Self {
Self {
num_samples: 8,
shutter_open: 1.0,
uniform_weights: true,
}
}
}
impl AccumulationConfig {
pub fn validate(&self) -> Result<(), MotionBlurError> {
if self.num_samples < 1 {
return Err(MotionBlurError::InvalidConfig(
"num_samples must be at least 1".to_string(),
));
}
if self.shutter_open <= 0.0 || self.shutter_open > 1.0 {
return Err(MotionBlurError::InvalidConfig(format!(
"shutter_open must be in (0, 1], got {}",
self.shutter_open
)));
}
Ok(())
}
pub fn sample_weights(&self) -> Vec<f32> {
let n = self.num_samples.max(1);
if self.uniform_weights || n == 1 {
return vec![1.0 / n as f32; n];
}
let mut weights: Vec<f32> = (0..n)
.map(|i| (1 + i.min(n.saturating_sub(1).saturating_sub(i))) as f32)
.collect();
let total: f32 = weights.iter().sum();
if total > 0.0 {
let inv = 1.0 / total;
for w in &mut weights {
*w *= inv;
}
}
weights
}
pub fn subframe_times(&self) -> Vec<f32> {
let n = self.num_samples.max(1);
let shutter = self.shutter_open.clamp(0.0, 1.0);
(0..n)
.map(|i| {
let base = subframe_t(i, n); 0.5 + (base - 0.5) * shutter
})
.collect()
}
}
#[derive(Debug, Clone)]
pub struct VelocityField {
pub width: usize,
pub height: usize,
pub velocities: Vec<f32>,
}
impl VelocityField {
pub fn new(width: usize, height: usize) -> Self {
Self {
width,
height,
velocities: vec![0.0_f32; width * height * 2],
}
}
pub fn get(&self, x: usize, y: usize) -> Result<[f32; 2], MotionBlurError> {
if x >= self.width || y >= self.height {
return Err(MotionBlurError::DimensionMismatch {
expected: self.width * self.height,
got: y * self.width + x,
});
}
let idx = (y * self.width + x) * 2;
Ok([self.velocities[idx], self.velocities[idx + 1]])
}
pub fn set(&mut self, x: usize, y: usize, vx: f32, vy: f32) -> Result<(), MotionBlurError> {
if x >= self.width || y >= self.height {
return Err(MotionBlurError::DimensionMismatch {
expected: self.width * self.height,
got: y * self.width + x,
});
}
let idx = (y * self.width + x) * 2;
self.velocities[idx] = vx;
self.velocities[idx + 1] = vy;
Ok(())
}
pub fn max_magnitude(&self) -> f32 {
let mut max = 0.0_f32;
for chunk in self.velocities.chunks_exact(2) {
let mag = (chunk[0] * chunk[0] + chunk[1] * chunk[1]).sqrt();
if mag > max {
max = mag;
}
}
max
}
pub fn mean_magnitude(&self) -> f32 {
let n_pixels = self.width * self.height;
if n_pixels == 0 {
return 0.0;
}
let sum: f32 = self
.velocities
.chunks_exact(2)
.map(|chunk| (chunk[0] * chunk[0] + chunk[1] * chunk[1]).sqrt())
.sum();
sum / n_pixels as f32
}
pub fn from_position_maps(
width: usize,
height: usize,
pos_prev: &[f32],
pos_curr: &[f32],
projection: &[[f32; 4]; 4],
) -> Result<Self, MotionBlurError> {
let expected = width * height * 3;
if pos_prev.len() != expected {
return Err(MotionBlurError::DimensionMismatch {
expected,
got: pos_prev.len(),
});
}
if pos_curr.len() != expected {
return Err(MotionBlurError::DimensionMismatch {
expected,
got: pos_curr.len(),
});
}
let n_pixels = width * height;
let mut field = Self::new(width, height);
for i in 0..n_pixels {
let base3 = i * 3;
let p_prev = [
pos_prev[base3],
pos_prev[base3 + 1],
pos_prev[base3 + 2],
1.0_f32,
];
let p_curr = [
pos_curr[base3],
pos_curr[base3 + 1],
pos_curr[base3 + 2],
1.0_f32,
];
let clip_prev = project_point(projection, p_prev);
let clip_curr = project_point(projection, p_curr);
const W_EPS: f32 = 1e-5;
if clip_prev[3].abs() < W_EPS || clip_curr[3].abs() < W_EPS {
continue;
}
let ndc_prev_x = clip_prev[0] / clip_prev[3];
let ndc_prev_y = clip_prev[1] / clip_prev[3];
let ndc_curr_x = clip_curr[0] / clip_curr[3];
let ndc_curr_y = clip_curr[1] / clip_curr[3];
let vx = (ndc_curr_x - ndc_prev_x) * (width as f32 / 2.0);
let vy = (ndc_curr_y - ndc_prev_y) * (height as f32 / 2.0);
let base2 = i * 2;
field.velocities[base2] = vx;
field.velocities[base2 + 1] = vy;
}
Ok(field)
}
}
fn project_point(m: &[[f32; 4]; 4], p: [f32; 4]) -> [f32; 4] {
let mut out = [0.0_f32; 4];
for (row_idx, row) in m.iter().enumerate() {
out[row_idx] = row[0] * p[0] + row[1] * p[1] + row[2] * p[2] + row[3] * p[3];
}
out
}
#[derive(Debug, Clone)]
pub struct VelocityBlurConfig {
pub num_samples: usize,
pub velocity_scale: f32,
pub max_radius: f32,
}
impl Default for VelocityBlurConfig {
fn default() -> Self {
Self {
num_samples: 16,
velocity_scale: 1.0,
max_radius: 32.0,
}
}
}
pub(crate) fn bilinear_sample(
image: &[f32],
width: usize,
height: usize,
sx: f32,
sy: f32,
) -> [f32; 3] {
if width == 0 || height == 0 {
return [0.0; 3];
}
let sx = if width <= 1 {
0.0
} else {
sx.clamp(0.0, width as f32 - 1.0 - f32::EPSILON)
};
let sy = if height <= 1 {
0.0
} else {
sy.clamp(0.0, height as f32 - 1.0 - f32::EPSILON)
};
let x0 = sx.floor() as usize;
let y0 = sy.floor() as usize;
let x1 = (x0 + 1).min(width - 1);
let y1 = (y0 + 1).min(height - 1);
let tx = sx - x0 as f32;
let ty = sy - y0 as f32;
let inv_tx = 1.0 - tx;
let inv_ty = 1.0 - ty;
let w00 = inv_tx * inv_ty;
let w10 = tx * inv_ty;
let w01 = inv_tx * ty;
let w11 = tx * ty;
let idx00 = (y0 * width + x0) * 3;
let idx10 = (y0 * width + x1) * 3;
let idx01 = (y1 * width + x0) * 3;
let idx11 = (y1 * width + x1) * 3;
let mut out = [0.0_f32; 3];
for c in 0..3 {
out[c] = w00 * image[idx00 + c]
+ w10 * image[idx10 + c]
+ w01 * image[idx01 + c]
+ w11 * image[idx11 + c];
}
out
}
pub fn apply_velocity_blur(
image: &[f32],
width: usize,
height: usize,
velocity: &VelocityField,
config: &VelocityBlurConfig,
) -> Result<Vec<f32>, MotionBlurError> {
let expected = width * height * 3;
if image.len() != expected {
return Err(MotionBlurError::InvalidImage(format!(
"expected {} elements, got {}",
expected,
image.len()
)));
}
if velocity.width != width || velocity.height != height {
return Err(MotionBlurError::DimensionMismatch {
expected: width * height,
got: velocity.width * velocity.height,
});
}
if config.num_samples == 0 {
return Err(MotionBlurError::InvalidConfig(
"num_samples must be at least 1".to_string(),
));
}
let n = config.num_samples;
let inv_n = 1.0 / n as f32;
let mut output = vec![0.0_f32; expected];
for py in 0..height {
for px in 0..width {
let pixel_idx = py * width + px;
let vel_base = pixel_idx * 2;
let mut vx = velocity.velocities[vel_base] * config.velocity_scale;
let mut vy = velocity.velocities[vel_base + 1] * config.velocity_scale;
let mag = (vx * vx + vy * vy).sqrt();
if mag > config.max_radius {
let scale = config.max_radius / mag;
vx *= scale;
vy *= scale;
}
let px_f = px as f32;
let py_f = py as f32;
let mut acc = [0.0_f32; 3];
for s in 0..n {
let t = if n == 1 {
0.0
} else {
s as f32 / (n - 1) as f32 - 0.5
};
let sx = px_f + vx * t;
let sy = py_f + vy * t;
let sample = bilinear_sample(image, width, height, sx, sy);
acc[0] += sample[0];
acc[1] += sample[1];
acc[2] += sample[2];
}
let out_base = pixel_idx * 3;
output[out_base] = acc[0] * inv_n;
output[out_base + 1] = acc[1] * inv_n;
output[out_base + 2] = acc[2] * inv_n;
}
}
Ok(output)
}
#[derive(Debug, Clone)]
pub struct AccumulatedBlur {
pub width: usize,
pub height: usize,
pub image: Vec<f32>,
pub num_samples: usize,
}
pub fn accumulate_frames(
frames: &[Vec<f32>],
width: usize,
height: usize,
config: &AccumulationConfig,
) -> Result<AccumulatedBlur, MotionBlurError> {
if frames.is_empty() {
return Err(MotionBlurError::EmptyFrames);
}
let expected = width * height * 3;
for (fi, frame) in frames.iter().enumerate() {
if frame.len() != expected {
return Err(MotionBlurError::DimensionMismatch {
expected,
got: frame.len(),
});
}
let _ = fi; }
config.validate()?;
let weights = config.sample_weights();
let n = frames.len().min(weights.len());
let mut output = vec![0.0_f32; expected];
let mut weight_sum = 0.0_f32;
for i in 0..n {
let w = weights[i];
let frame = &frames[i];
for (out, &src) in output.iter_mut().zip(frame.iter()) {
*out += w * src;
}
weight_sum += w;
}
if weight_sum > 0.0 && (weight_sum - 1.0).abs() > 1e-6 {
let inv = 1.0 / weight_sum;
for v in &mut output {
*v *= inv;
}
}
Ok(AccumulatedBlur {
width,
height,
image: output,
num_samples: n,
})
}
#[derive(Debug, Clone)]
pub struct MotionBlurStats {
pub mean_blur_magnitude: f32,
pub max_blur_magnitude: f32,
pub blurred_pixel_fraction: f32,
pub effective_samples: f32,
}
pub fn compute_motion_stats(
velocity: &VelocityField,
config: &VelocityBlurConfig,
) -> MotionBlurStats {
let n_pixels = velocity.width * velocity.height;
if n_pixels == 0 {
return MotionBlurStats {
mean_blur_magnitude: 0.0,
max_blur_magnitude: 0.0,
blurred_pixel_fraction: 0.0,
effective_samples: config.num_samples as f32,
};
}
let mut sum_mag = 0.0_f32;
let mut max_mag = 0.0_f32;
let mut blurred_count = 0usize;
for chunk in velocity.velocities.chunks_exact(2) {
let vx = chunk[0] * config.velocity_scale;
let vy = chunk[1] * config.velocity_scale;
let mag = (vx * vx + vy * vy).sqrt().min(config.max_radius);
sum_mag += mag;
if mag > max_mag {
max_mag = mag;
}
if mag > 0.5 {
blurred_count += 1;
}
}
let mean_mag = sum_mag / n_pixels as f32;
let blurred_pixel_fraction = blurred_count as f32 / n_pixels as f32;
let effective_samples = if mean_mag < 1e-6 {
1.0
} else {
let utilisation = (mean_mag / config.max_radius).min(1.0);
1.0 + (config.num_samples as f32 - 1.0) * utilisation
};
MotionBlurStats {
mean_blur_magnitude: mean_mag,
max_blur_magnitude: max_mag,
blurred_pixel_fraction,
effective_samples,
}
}
#[cfg(test)]
mod tests {
use super::*;
const EPS: f32 = 1e-5;
fn approx(a: f32, b: f32) -> bool {
(a - b).abs() < EPS
}
#[test]
fn test_subframe_t_n1_i0() {
assert!(approx(subframe_t(0, 1), 0.0), "subframe_t(0,1) should be 0");
}
#[test]
fn test_subframe_t_n4_i0() {
assert!(approx(subframe_t(0, 4), 0.0));
}
#[test]
fn test_subframe_t_n4_i3() {
assert!(approx(subframe_t(3, 4), 1.0));
}
#[test]
fn test_subframe_t_midpoint() {
assert!(approx(subframe_t(1, 3), 0.5));
}
#[test]
fn test_lerp_position_t0() {
let start = [1.0, 2.0, 3.0];
let end = [4.0, 5.0, 6.0];
let r = lerp_position(start, end, 0.0);
assert!(approx(r[0], 1.0) && approx(r[1], 2.0) && approx(r[2], 3.0));
}
#[test]
fn test_lerp_position_t1() {
let start = [1.0, 2.0, 3.0];
let end = [4.0, 5.0, 6.0];
let r = lerp_position(start, end, 1.0);
assert!(approx(r[0], 4.0) && approx(r[1], 5.0) && approx(r[2], 6.0));
}
#[test]
fn test_lerp_position_midpoint() {
let start = [0.0, 0.0, 0.0];
let end = [2.0, 4.0, 6.0];
let r = lerp_position(start, end, 0.5);
assert!(approx(r[0], 1.0) && approx(r[1], 2.0) && approx(r[2], 3.0));
}
#[test]
fn test_lerp_quaternion_t0() {
let q0 = [0.0, 0.0, 0.0, 1.0];
let q1 = [1.0, 0.0, 0.0, 0.0];
let r = lerp_quaternion(q0, q1, 0.0);
for (a, b) in r.iter().zip(q0.iter()) {
assert!(approx(*a, *b));
}
}
#[test]
fn test_lerp_quaternion_t1() {
let q0 = [0.0, 0.0, 0.0, 1.0];
let q1 = [1.0, 0.0, 0.0, 0.0];
let r = lerp_quaternion(q0, q1, 1.0);
for (a, b) in r.iter().zip(q1.iter()) {
assert!(approx(*a, *b));
}
}
#[test]
fn test_normalize_quaternion_identity() {
let q = [0.0_f32, 0.0, 0.0, 1.0];
let r = normalize_quaternion(q);
assert!(approx(r[3], 1.0));
}
#[test]
fn test_normalize_quaternion_zero_returns_identity() {
let q = [0.0_f32, 0.0, 0.0, 0.0];
let r = normalize_quaternion(q);
assert!(approx(r[0], 0.0) && approx(r[1], 0.0) && approx(r[2], 0.0) && approx(r[3], 1.0));
}
#[test]
fn test_normalize_quaternion_unit_length() {
let q = lerp_quaternion([0.0, 0.0, 0.0, 1.0], [1.0, 0.0, 0.0, 0.0], 0.5);
let n = normalize_quaternion(q);
let mag_sq = n[0] * n[0] + n[1] * n[1] + n[2] * n[2] + n[3] * n[3];
assert!((mag_sq - 1.0).abs() < 1e-5, "mag_sq = {}", mag_sq);
}
#[test]
fn test_camera_motion_stationary_is_stationary() {
let cm = CameraMotion::stationary([1.0, 2.0, 3.0], [0.0, 0.0, 0.0, 1.0]);
assert!(
cm.is_stationary(),
"Stationary camera must be is_stationary()"
);
}
#[test]
fn test_camera_motion_moving_not_stationary() {
let cm = CameraMotion::new(
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 0.0, 1.0],
);
assert!(!cm.is_stationary(), "Moving camera must NOT be stationary");
}
#[test]
fn test_camera_motion_position_at_t0_and_t1() {
let cm = CameraMotion::new(
[0.0, 0.0, 0.0],
[3.0, 6.0, 9.0],
[0.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 0.0, 1.0],
);
let p0 = cm.position_at(0.0);
let p1 = cm.position_at(1.0);
assert!(approx(p0[0], 0.0) && approx(p0[1], 0.0) && approx(p0[2], 0.0));
assert!(approx(p1[0], 3.0) && approx(p1[1], 6.0) && approx(p1[2], 9.0));
}
#[test]
fn test_camera_motion_position_at_midpoint() {
let cm = CameraMotion::new(
[0.0, 0.0, 0.0],
[2.0, 4.0, 6.0],
[0.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 0.0, 1.0],
);
let p = cm.position_at(0.5);
assert!(approx(p[0], 1.0) && approx(p[1], 2.0) && approx(p[2], 3.0));
}
#[test]
fn test_camera_motion_rotation_at_endpoints() {
let r0 = [0.0_f32, 0.0, 0.0, 1.0];
let r1 = [
0.0_f32,
0.0,
std::f32::consts::FRAC_1_SQRT_2,
std::f32::consts::FRAC_1_SQRT_2,
];
let cm = CameraMotion::new([0.0; 3], [0.0; 3], r0, r1);
let at0 = cm.rotation_at(0.0);
let at1 = cm.rotation_at(1.0);
let mag0_sq = at0.iter().map(|v| v * v).sum::<f32>();
let mag1_sq = at1.iter().map(|v| v * v).sum::<f32>();
assert!((mag0_sq - 1.0).abs() < 1e-5, "rotation_at(0) not unit");
assert!((mag1_sq - 1.0).abs() < 1e-5, "rotation_at(1) not unit");
}
#[test]
fn test_camera_motion_translation_distance() {
let cm = CameraMotion::new(
[0.0, 0.0, 0.0],
[3.0, 4.0, 0.0],
[0.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 0.0, 1.0],
);
let dist = cm.translation_distance();
assert!(
approx(dist, 5.0),
"3-4-5 triangle: expected 5.0, got {}",
dist
);
}
#[test]
fn test_accumulation_config_validate_valid() {
let cfg = AccumulationConfig::default();
assert!(cfg.validate().is_ok());
}
#[test]
fn test_accumulation_config_validate_zero_samples() {
let cfg = AccumulationConfig {
num_samples: 0,
..AccumulationConfig::default()
};
assert!(matches!(
cfg.validate(),
Err(MotionBlurError::InvalidConfig(_))
));
}
#[test]
fn test_accumulation_config_validate_bad_shutter() {
let cfg = AccumulationConfig {
shutter_open: 0.0,
..AccumulationConfig::default()
};
assert!(matches!(
cfg.validate(),
Err(MotionBlurError::InvalidConfig(_))
));
let cfg2 = AccumulationConfig {
shutter_open: 1.5,
..AccumulationConfig::default()
};
assert!(matches!(
cfg2.validate(),
Err(MotionBlurError::InvalidConfig(_))
));
}
#[test]
fn test_sample_weights_uniform_sum_to_one() {
let cfg = AccumulationConfig {
num_samples: 8,
uniform_weights: true,
..AccumulationConfig::default()
};
let weights = cfg.sample_weights();
assert_eq!(weights.len(), 8);
let sum: f32 = weights.iter().sum();
assert!((sum - 1.0).abs() < 1e-5, "uniform weights sum = {}", sum);
}
#[test]
fn test_sample_weights_ramp_sum_to_one() {
let cfg = AccumulationConfig {
num_samples: 9,
uniform_weights: false,
..AccumulationConfig::default()
};
let weights = cfg.sample_weights();
assert_eq!(weights.len(), 9);
let sum: f32 = weights.iter().sum();
assert!((sum - 1.0).abs() < 1e-5, "ramp weights sum = {}", sum);
}
#[test]
fn test_sample_weights_ramp_peaks_at_centre() {
let cfg = AccumulationConfig {
num_samples: 5,
uniform_weights: false,
..AccumulationConfig::default()
};
let weights = cfg.sample_weights();
let max_w = weights.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
assert!(
approx(weights[2], max_w),
"Centre weight should be maximum; weights = {:?}",
weights
);
}
#[test]
fn test_subframe_times_full_shutter_matches_subframe_t() {
let cfg = AccumulationConfig {
num_samples: 6,
shutter_open: 1.0,
..AccumulationConfig::default()
};
let times = cfg.subframe_times();
assert_eq!(times.len(), 6);
for (i, &t) in times.iter().enumerate() {
assert!(approx(t, subframe_t(i, 6)), "index {i}: got {t}");
}
}
#[test]
fn test_subframe_times_partial_shutter_compresses_window() {
let cfg = AccumulationConfig {
num_samples: 5,
shutter_open: 0.5,
..AccumulationConfig::default()
};
let times = cfg.subframe_times();
assert!(approx(times[0], 0.25), "got {}", times[0]);
assert!(
approx(*times.last().unwrap(), 0.75),
"got {:?}",
times.last()
);
for &t in × {
assert!(
(0.25..=0.75).contains(&t),
"t={t} outside compressed window"
);
}
}
#[test]
fn test_subframe_times_zero_shutter_collapses_to_midpoint() {
let cfg = AccumulationConfig {
num_samples: 4,
shutter_open: 0.0,
..AccumulationConfig::default()
};
let times = cfg.subframe_times();
for &t in × {
assert!(approx(t, 0.5), "expected all samples at t=0.5, got {t}");
}
}
#[test]
fn test_velocity_field_new_all_zero() {
let vf = VelocityField::new(4, 3);
assert_eq!(vf.velocities.len(), 4 * 3 * 2);
assert!(vf.velocities.iter().all(|&v| v == 0.0));
}
#[test]
fn test_velocity_field_get_set_roundtrip() {
let mut vf = VelocityField::new(5, 5);
vf.set(2, 3, 1.5, -2.5).expect("set should succeed");
let [vx, vy] = vf.get(2, 3).expect("get should succeed");
assert!(approx(vx, 1.5) && approx(vy, -2.5));
}
#[test]
fn test_velocity_field_out_of_bounds_error() {
let vf = VelocityField::new(4, 4);
assert!(vf.get(4, 0).is_err());
assert!(vf.get(0, 4).is_err());
}
#[test]
fn test_velocity_field_max_magnitude() {
let mut vf = VelocityField::new(3, 3);
vf.set(1, 1, 3.0, 4.0).expect("set ok"); assert!(approx(vf.max_magnitude(), 5.0));
}
#[test]
fn test_velocity_field_mean_magnitude_zero() {
let vf = VelocityField::new(4, 4);
assert!(approx(vf.mean_magnitude(), 0.0));
}
#[test]
fn test_velocity_field_mean_magnitude_nonzero() {
let mut vf = VelocityField::new(2, 1);
vf.set(0, 0, 3.0, 4.0).expect("set ok");
let mean = vf.mean_magnitude();
assert!(approx(mean, 2.5), "mean_magnitude = {}", mean);
}
#[test]
fn test_apply_velocity_blur_zero_velocity_identity() {
let w = 4_usize;
let h = 4_usize;
let image: Vec<f32> = (0..w * h * 3).map(|i| i as f32 / 100.0).collect();
let velocity = VelocityField::new(w, h); let config = VelocityBlurConfig {
num_samples: 8,
..VelocityBlurConfig::default()
};
let output = apply_velocity_blur(&image, w, h, &velocity, &config)
.expect("apply_velocity_blur failed");
assert_eq!(output.len(), image.len());
for (i, (&a, &b)) in image.iter().zip(output.iter()).enumerate() {
assert!(
(a - b).abs() < 1e-4,
"pixel {} channel: expected {}, got {}",
i,
a,
b
);
}
}
#[test]
fn test_apply_velocity_blur_nonzero_produces_change() {
let w = 8_usize;
let h = 4_usize;
let image: Vec<f32> = (0..w * h * 3)
.map(|i| if (i / 3) % 2 == 0 { 1.0 } else { 0.0 })
.collect();
let mut velocity = VelocityField::new(w, h);
for y in 0..h {
for x in 0..w {
velocity.set(x, y, 4.0, 0.0).expect("set ok");
}
}
let config = VelocityBlurConfig {
num_samples: 4,
velocity_scale: 1.0,
max_radius: 32.0,
};
let output = apply_velocity_blur(&image, w, h, &velocity, &config)
.expect("apply_velocity_blur failed");
let changed = image
.iter()
.zip(output.iter())
.any(|(&a, &b)| (a - b).abs() > 1e-4);
assert!(
changed,
"Non-zero velocity should produce a different image"
);
}
#[test]
fn test_apply_velocity_blur_centroid_stays_at_source_pixel() {
let w = 16_usize;
let h = 1_usize;
let mut image = vec![0.0_f32; w * h * 3];
let src_x = 8_usize;
image[src_x * 3] = 1.0;
image[src_x * 3 + 1] = 1.0;
image[src_x * 3 + 2] = 1.0;
let mut velocity = VelocityField::new(w, h);
for x in 0..w {
velocity.set(x, 0, 6.0, 0.0).expect("set ok");
}
let config = VelocityBlurConfig {
num_samples: 32,
velocity_scale: 1.0,
max_radius: 32.0,
};
let output = apply_velocity_blur(&image, w, h, &velocity, &config)
.expect("apply_velocity_blur failed");
let (mut weighted_sum, mut weight_total) = (0.0_f32, 0.0_f32);
for x in 0..w {
let v = output[x * 3];
weighted_sum += x as f32 * v;
weight_total += v;
}
assert!(weight_total > 1e-3, "expected some non-zero output energy");
let centroid = weighted_sum / weight_total;
assert!(
(centroid - src_x as f32).abs() < 1.0,
"expected blur centroid near source pixel {src_x}, got {centroid}"
);
}
#[test]
fn test_accumulate_frames_single_frame_identity() {
let w = 4_usize;
let h = 3_usize;
let image: Vec<f32> = (0..w * h * 3).map(|i| i as f32 / 50.0).collect();
let frames = vec![image.clone()];
let config = AccumulationConfig {
num_samples: 1,
..AccumulationConfig::default()
};
let result = accumulate_frames(&frames, w, h, &config).expect("accumulate_frames failed");
assert_eq!(result.image.len(), image.len());
for (a, b) in image.iter().zip(result.image.iter()) {
assert!((a - b).abs() < 1e-5, "single frame: got {b}, expected {a}");
}
assert_eq!(result.num_samples, 1);
}
#[test]
fn test_accumulate_frames_two_frames_uniform_average() {
let w = 2_usize;
let h = 2_usize;
let frame0 = vec![0.0_f32; w * h * 3];
let frame1 = vec![1.0_f32; w * h * 3];
let frames = vec![frame0, frame1];
let config = AccumulationConfig {
num_samples: 2,
uniform_weights: true,
..AccumulationConfig::default()
};
let result = accumulate_frames(&frames, w, h, &config).expect("accumulate_frames failed");
for &v in &result.image {
assert!(
approx(v, 0.5),
"uniform average of 0 and 1 should be 0.5, got {v}"
);
}
}
#[test]
fn test_accumulate_frames_empty_returns_error() {
let frames: Vec<Vec<f32>> = vec![];
let config = AccumulationConfig::default();
let result = accumulate_frames(&frames, 4, 4, &config);
assert!(matches!(result, Err(MotionBlurError::EmptyFrames)));
}
#[test]
fn test_accumulate_frames_dimension_mismatch_error() {
let w = 4_usize;
let h = 4_usize;
let frames = vec![vec![0.5_f32; w * h * 3 + 1]];
let config = AccumulationConfig {
num_samples: 1,
..AccumulationConfig::default()
};
let result = accumulate_frames(&frames, w, h, &config);
assert!(matches!(
result,
Err(MotionBlurError::DimensionMismatch { .. })
));
}
#[test]
fn test_compute_motion_stats_zero_velocity() {
let vf = VelocityField::new(4, 4);
let config = VelocityBlurConfig::default();
let stats = compute_motion_stats(&vf, &config);
assert!(approx(stats.mean_blur_magnitude, 0.0));
assert!(approx(stats.max_blur_magnitude, 0.0));
assert!(approx(stats.blurred_pixel_fraction, 0.0));
assert!(approx(stats.effective_samples, 1.0));
}
#[test]
fn test_compute_motion_stats_utilization_scales_with_magnitude() {
let mut vf = VelocityField::new(2, 1);
vf.velocities = vec![4.0, 0.0, 4.0, 0.0]; let config = VelocityBlurConfig {
num_samples: 9,
max_radius: 8.0,
velocity_scale: 1.0,
};
let stats = compute_motion_stats(&vf, &config);
assert!(
approx(stats.effective_samples, 5.0),
"expected 5.0, got {}",
stats.effective_samples
);
}
#[test]
fn test_bilinear_sample_zero_dimension_is_black() {
assert_eq!(bilinear_sample(&[], 0, 4, 0.0, 0.0), [0.0; 3]);
assert_eq!(bilinear_sample(&[], 4, 0, 0.0, 0.0), [0.0; 3]);
assert_eq!(bilinear_sample(&[], 0, 0, 2.5, -3.0), [0.0; 3]);
}
#[test]
fn test_bilinear_sample_matches_public_mb_reexport() {
let image: Vec<f32> = (0..(3 * 2 * 3)).map(|i| i as f32 * 0.03).collect();
for &(sx, sy) in &[
(0.0_f32, 0.0_f32),
(1.25, 0.5),
(2.75, 1.9),
(-4.0, 7.0),
(1.0, 1.0),
] {
let here = bilinear_sample(&image, 3, 2, sx, sy);
let there = crate::mb_pipeline::mb_bilinear_sample(&image, 3, 2, sx, sy);
assert_eq!(here, there, "divergence at ({sx}, {sy})");
}
}
#[test]
fn test_bilinear_sample_single_pixel_axes() {
let one_row = [0.1_f32, 0.2, 0.3, 0.4, 0.5, 0.6];
let got = bilinear_sample(&one_row, 2, 1, 0.5, 9.0);
assert!(approx(got[0], 0.25), "got {got:?}");
let one_col = [0.1_f32, 0.2, 0.3, 0.4, 0.5, 0.6];
let got = bilinear_sample(&one_col, 1, 2, 9.0, 0.5);
assert!(approx(got[0], 0.25), "got {got:?}");
}
}