using Chronos.Core.Imaging; using Chronos.Core.Motion; using Chronos.Testing; namespace Chronos.Core.Tests.Motion; public sealed class MotionBlurTests { private const int Size = 128; private readonly TestContext _context; public MotionBlurTests(TestContext context) => _context = context; [Theory] [Case(180.0, 90.0, 0.4330127018922193)] [Case(180.0, 0.0, 0.5)] [Case(360.0, 0.0, 1.0)] [Case(180.0, 180.0, 0.0)] [Case(180.0, 270.0, 0.0)] [Case(90.0, 54.0, 0.2)] public void BlurFractionComposesShutterAngles(double target, double real, double expected) => Assert.Approximately(expected, ShutterAngle.BlurFraction(target, real), 1e-12); [Test] public void RealShutterAngleFromExposure() { Assert.Approximately(180, ShutterAngle.FromExposure(1, 2), 1e-12); Assert.Approximately(3.6, ShutterAngle.FromExposure(1 / 250.0, 0.4), 1e-12); } [Test] public void ZeroVectorsLeaveTheImageIdentical() { using var source = Texture(); using var motion = new MotionField(Size, Size, 2); using var output = new ImageBuffer(Size, Size); new MotionBlurRenderer().Render(source, motion, 0.5, output); Assert.True(source.Data.SequenceEqual(output.Data)); } [Test] public void PointBlursIntoASegmentWithItsEnergy() { using var source = new ImageBuffer(Size, Size, 1); source[0, 64, 64] = 1f; using var motion = new MotionField(Size, Size, 4); motion.Fill(20, 0); using var output = new ImageBuffer(Size, Size, 1); new MotionBlurRenderer().Render(source, motion, 0.5, output); double total = 0; var columns = new HashSet(); for (var y = 0; y < Size; y++) { for (var x = 0; x < Size; x++) { var value = output[0, x, y]; total += value; if (value <= 1e-6f) continue; Assert.Equal(64, y); columns.Add(x); } } _context.Log($"point energy after blur {total:F6}, segment columns {columns.Min()}..{columns.Max()}"); Assert.Approximately(1, total, 1e-4); Assert.InRange(columns.Min(), 58, 60); Assert.InRange(columns.Max(), 68, 70); } [Test] public void EnergyIsPreservedOnTexture() { using var source = Texture(); using var motion = new MotionField(Size, Size, 2); for (var j = 0; j < motion.Rows; j++) for (var i = 0; i < motion.Columns; i++) { // A smooth swirl: magnitude up to ~6 px. var k = j * motion.Columns + i; motion.U[k] = (float)(6 * double.Sin(j * 0.1)); motion.V[k] = (float)(4 * double.Cos(i * 0.08)); } using var output = new ImageBuffer(Size, Size); new MotionBlurRenderer().Render(source, motion, 1.0, output); for (var c = 0; c < 3; c++) { var before = Mean(source.Plane(c), 16); var after = Mean(output.Plane(c), 16); _context.Log($"channel {c}: mean {before:F6} → {after:F6} ({(after - before) / before:P3})"); Assert.Approximately(before, after, 2e-3 * before); } Assert.False(source.Data.SequenceEqual(output.Data)); } private static double Mean(ReadOnlySpan plane, int margin) { double sum = 0; var n = 0; for (var y = margin; y < Size - margin; y++) for (var x = margin; x < Size - margin; x++) { sum += plane[y * Size + x]; n++; } return sum / n; } private static ImageBuffer Texture() => SyntheticImages.ToRgb(SyntheticImages.Crop(SyntheticImages.Natural(Size, seed: 31), Size, 0, 0, Size, Size), Size, Size); }