feat(motion): add gather-filter motion blur with shutter-angle composition
ShutterAngle computes the real angle from exposure and interval and the blur fraction sqrt(target^2 - real^2)/360 (zero once the real angle reaches the target). MotionBlurRenderer averages bilinear taps along each pixel's motion vector with a tap count adaptive to the length. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
2 files changed
+248
No files matched your search
@@ -0,0 +1,143 @@
|
||||
using Titano.Core.Imaging;
|
||||
|
||||
namespace Titano.Core.Motion;
|
||||
|
||||
/// <summary>Shutter-angle arithmetic for synthetic motion blur.</summary>
|
||||
public static class ShutterAngle
|
||||
{
|
||||
/// <summary>Shutter angle actually used by the camera, 360° × exposure / interval.</summary>
|
||||
public static double FromExposure(double exposureSeconds, double intervalSeconds)
|
||||
{
|
||||
ArgumentOutOfRangeException.ThrowIfNegative(exposureSeconds);
|
||||
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(intervalSeconds);
|
||||
return 360.0 * exposureSeconds / intervalSeconds;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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).
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Tuning for <see cref="MotionBlurRenderer"/>.</summary>
|
||||
public sealed record MotionBlurOptions
|
||||
{
|
||||
/// <summary>Samples per pixel of blur length (≥ 1 keeps the bilinear taps overlapping).</summary>
|
||||
public double TapsPerPixel { get; init; } = 1.0;
|
||||
|
||||
/// <summary>Upper bound on samples per pixel.</summary>
|
||||
public int MaxTaps { get; init; } = 64;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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; }
|
||||
|
||||
/// <summary>
|
||||
/// Renders <paramref name="source"/> blurred along <paramref name="motion"/> (frame-to-frame vectors)
|
||||
/// scaled by <paramref name="blurFraction"/> (see <see cref="ShutterAngle.BlurFraction"/>).
|
||||
/// </summary>
|
||||
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<float> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user