using Chronos.Core.Imaging;
namespace Chronos.Core.Motion;
/// Shutter-angle arithmetic for synthetic motion blur.
public static class ShutterAngle
{
/// Shutter angle actually used by the camera, 360° × exposure / interval.
public static double FromExposure(double exposureSeconds, double intervalSeconds)
{
ArgumentOutOfRangeException.ThrowIfNegative(exposureSeconds);
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(intervalSeconds);
return 360.0 * exposureSeconds / intervalSeconds;
}
///
/// Fraction of the frame-to-frame motion to smear so that the real exposure composed with the synthetic
/// blur behaves like the target shutter: √(target² − real²) / 360, and 0 when the real angle already
/// reaches the target (the blur would only be added on top of blur that is already there).
///
public static double BlurFraction(double targetDegrees, double realDegrees)
{
ArgumentOutOfRangeException.ThrowIfNegative(targetDegrees);
ArgumentOutOfRangeException.ThrowIfNegative(realDegrees);
if (realDegrees >= targetDegrees) return 0;
return double.Sqrt(targetDegrees * targetDegrees - realDegrees * realDegrees) / 360.0;
}
}
/// Tuning for .
public sealed record MotionBlurOptions
{
/// Samples per pixel of blur length (≥ 1 keeps the bilinear taps overlapping).
public double TapsPerPixel { get; init; } = 1.0;
/// Upper bound on samples per pixel.
public int MaxTaps { get; init; } = 64;
}
///
/// Motion blur by a gather filter: every output pixel averages bilinear samples of the source along a
/// segment centred on it, oriented by its motion vector and as long as the vector times the blur fraction
/// (a box shutter: uniform weights, tap count adaptive to the length). Equal weights and bilinear taps
/// preserve energy; zero vectors copy the pixel. Rows run in parallel.
///
public sealed class MotionBlurRenderer
{
private const double MinimumLength = 1e-3;
public MotionBlurRenderer(MotionBlurOptions? options = null)
{
Options = options ?? new MotionBlurOptions();
ArgumentOutOfRangeException.ThrowIfLessThan(Options.TapsPerPixel, 0.25);
ArgumentOutOfRangeException.ThrowIfLessThan(Options.MaxTaps, 2);
}
public MotionBlurOptions Options { get; }
///
/// Renders blurred along (frame-to-frame vectors)
/// scaled by (see ).
///
public void Render(ImageBuffer source, MotionField motion, double blurFraction, ImageBuffer destination, CancellationToken cancellationToken = default)
{
if (destination.Width != source.Width || destination.Height != source.Height || destination.Channels != source.Channels)
throw new ArgumentException("Destination must match the source.", nameof(destination));
if (motion.ImageWidth != source.Width || motion.ImageHeight != source.Height)
throw new ArgumentException("Motion field describes a different image size.", nameof(motion));
ArgumentOutOfRangeException.ThrowIfNegative(blurFraction);
if (blurFraction == 0)
{
source.CopyTo(destination);
return;
}
var w = source.Width;
var h = source.Height;
var channels = source.Channels;
var plane = source.PlaneLength;
var src = source.Storage;
var dst = destination.Storage;
var tapsPerPixel = Options.TapsPerPixel;
var maxTaps = Options.MaxTaps;
void Row(int y)
{
Span sum = stackalloc float[4];
for (var x = 0; x < w; x++)
{
var o = y * w + x;
var (mu, mv) = motion.Sample(x, y);
var du = mu * blurFraction;
var dv = mv * blurFraction;
var length = double.Sqrt(du * du + dv * dv);
if (length < MinimumLength)
{
for (var c = 0; c < channels; c++) dst[c * plane + o] = src[c * plane + o];
continue;
}
var taps = int.Clamp((int)double.Ceiling(length * tapsPerPixel) + 1, 2, maxTaps);
sum.Clear();
for (var k = 0; k < taps; k++)
{
var t = k / (double)(taps - 1) - 0.5;
var sx = double.Clamp(x + t * du, 0, w - 1);
var sy = double.Clamp(y + t * dv, 0, h - 1);
var x0 = int.Min((int)sx, int.Max(w - 2, 0));
var y0 = int.Min((int)sy, int.Max(h - 2, 0));
var x1 = int.Min(x0 + 1, w - 1);
var y1 = int.Min(y0 + 1, h - 1);
var fx = (float)(sx - x0);
var fy = (float)(sy - y0);
var w00 = (1 - fx) * (1 - fy);
var w10 = fx * (1 - fy);
var w01 = (1 - fx) * fy;
var w11 = fx * fy;
int i00 = y0 * w + x0, i10 = y0 * w + x1, i01 = y1 * w + x0, i11 = y1 * w + x1;
for (var c = 0; c < channels; c++)
{
var p = c * plane;
sum[c] += w00 * src[p + i00] + w10 * src[p + i10] + w01 * src[p + i01] + w11 * src[p + i11];
}
}
var inv = 1f / taps;
for (var c = 0; c < channels; c++) dst[c * plane + o] = sum[c] * inv;
}
}
if ((long)w * h < PlaneOps.ParallelThreshold)
{
for (var y = 0; y < h; y++)
{
cancellationToken.ThrowIfCancellationRequested();
Row(y);
}
}
else
{
Parallel.For(0, h, new ParallelOptions { CancellationToken = cancellationToken }, Row);
}
}
}