using Chronos.Core.Analysis; using Chronos.Core.Diagnostics.Synthetic; using Chronos.Core.Imaging; using Chronos.Core.Imaging.Geometry; using Chronos.Core.Motion; using Chronos.Core.Project; using Chronos.Core.Render; using Chronos.Core.Render.Diagnostics; using Chronos.Testing; using static Chronos.Core.Tests.Render.RenderFixture; namespace Chronos.Core.Tests.Render; public sealed class RenderProcessingTests { private readonly TestContext _context; public RenderProcessingTests(TestContext context) => _context = context; private static ProjectSettings With(ExposureSettings? exposure = null, MotionSettings? motion = null, CameraSettings? camera = null, StackSettings? stack = null, TimingSettings? timing = null) => Plain with { Exposure = exposure ?? Plain.Exposure, Motion = motion ?? new MotionSettings(), Camera = camera ?? new CameraSettings(), Stack = stack ?? new StackSettings(), Timing = timing ?? new TimingSettings(), }; private static Auto On(T value) => new Auto().WithManual(value); private static double[] LogLuminance(IEnumerable frames) => [.. frames.Select(f => FrameStatistics.MeasureLogLuminance(f))]; private static double Rms(IEnumerable values) { var v = values.ToArray(); var mean = v.Average(); return Math.Sqrt(v.Sum(x => (x - mean) * (x - mean)) / v.Length); } /// The §20.4 deflicker gate measured on rendered frames (same recipe as the analysis gate). [Test] public async Task DeflickerRemovesFlickerAndKeepsTheRamp() { var source = Source(200, configure: o => o with { Seed = 3, StartEv = -2, EndEv = 1, FlickerEv = 0.1 }); var project = With(new ExposureSettings { Deflicker = On(true), DeflickerWindow = On(21), HighlightKnee = null }); var (encode, _) = await CaptureAsync(Input(source, project), cancellationToken: _context.Cancellation); var truth = source.Sequence.SceneEv; var measured = Analysis.AnalysisFixtures.MeasureSeries(source.Sequence); var output = LogLuminance(encode.Frames); var before = Analysis.AnalysisFixtures.FrameToFrameResidualRms(measured, truth); var after = Analysis.AnalysisFixtures.FrameToFrameResidualRms(output, truth); var rampError = Analysis.AnalysisFixtures.MaxDeviation(output, truth); _context.Log($"flicker {before:F4} → {after:F4} EV ({1 - after / before:P0}); max ramp deviation {rampError:F4} EV"); Assert.GreaterOrEqual(1 - after / before, 0.6); Assert.LessOrEqual(rampError, 0.05); } [Test] public async Task HolyGrailTurnsExposureStepsIntoRamps() { var source = Source(80, configure: o => o with { ExposureSteps = [new SyntheticExposureStep(40, 1.0)], StartEv = 0, EndEv = 0 }); var project = With(new ExposureSettings { HolyGrail = On(true), Deflicker = On(true), HighlightKnee = null }); var (encode, _) = await CaptureAsync(Input(source, project), cancellationToken: _context.Cancellation); var output = LogLuminance(encode.Frames); var steps = output.Zip(output.Skip(1), (a, b) => Math.Abs(b - a)).ToArray(); _context.Log($"largest output step {steps.Max():F3} EV"); Assert.LessOrEqual(steps.Max(), 0.3 + 0.02); Assert.Approximately(-1.0, output[^1] - output[0], 0.1, "the step is kept, only spread out"); } [Test] public async Task ExcludedFramesDoNotCreateExposureSteps() { // Frame 10 is a test shot one stop darker; excluding it must leave no holy-grail step behind. var source = Source(30, configure: o => o with { ExposureSteps = [new SyntheticExposureStep(10, 1.0), new SyntheticExposureStep(11, -1.0)], StartEv = 0, EndEv = 0 }); var project = With(new ExposureSettings { HolyGrail = On(true), HighlightKnee = null }); var all = await RenderPreparation.PrepareAsync(Input(source, project), cancellationToken: _context.Cancellation); var excluded = await RenderPreparation.PrepareAsync(Input(source, project with { ExcludedFrames = [10] }), cancellationToken: _context.Cancellation); Assert.Count(2, all.Report.Transitions); Assert.Empty(excluded.Report.Transitions); Assert.True(double.IsNaN(excluded.MeasuredLogLuminance[10])); } [Test] public async Task WhiteBalanceSmoothingRemovesColourJitter() { var source = Source(48, configure: o => o with { WhiteBalanceJitter = 0.1 }); var (encode, _) = await CaptureAsync(Input(source, With(new ExposureSettings { WhiteBalanceSmoothing = On(true), HighlightKnee = null })), cancellationToken: _context.Cancellation); double Ratio(ImageBuffer f) { var stats = FrameStatistics.Measure(f); return Math.Log2(stats.MeanRed / stats.MeanGreen); } var before = Enumerable.Range(0, 48).Select(i => { using var f = source.Sequence.Render(i); return Ratio(f); }).ToArray(); var after = encode.Frames.Select(Ratio).ToArray(); double Jitter(double[] r) => Rms(r.Zip(r.Skip(1), (a, b) => b - a)); _context.Log($"R/G jitter {Jitter(before):F4} → {Jitter(after):F4}"); Assert.Less(Jitter(after), 0.3 * Jitter(before)); } [Test] public async Task RegionDeflickerCorrectsSkyAndLandSeparately() { var source = Source(48, configure: o => o with { SkyFlickerEv = 0.3, LandFlickerEv = 0.3, StartEv = 0, EndEv = 0 }); var landMask = new float[96 * 64]; for (var y = 0; y < 64; y++) { for (var x = 0; x < 96; x++) landMask[y * 96 + x] = source.Sequence.IsSky(x, y) ? 0 : 1; } double LandResidual(List frames) => Rms(frames.Select(f => FrameStatistics.MeasureLogLuminance(f, landMask))); var global = await CaptureAsync(Input(source, With(new ExposureSettings { Deflicker = On(true), HighlightKnee = null })), cancellationToken: _context.Cancellation); var regions = await CaptureAsync(Input(source, With(new ExposureSettings { Deflicker = On(true), Regions = On(RegionMode.Horizon), HighlightKnee = null })), cancellationToken: _context.Cancellation); var g = LandResidual(global.Encode.Frames); var r = LandResidual(regions.Encode.Frames); _context.Log($"land residual global {g:F3} EV, regions {r:F3} EV"); Assert.Less(r, 0.5 * g); var prepared = await RenderPreparation.PrepareAsync(Input(source, With(new ExposureSettings { Deflicker = On(true), Regions = On(RegionMode.Horizon) })), cancellationToken: _context.Cancellation); Assert.Equal(RegionMethod.Horizon, prepared.Report.RegionMethod); Assert.NotNull(prepared.SkyEv); } [Test] public async Task ExportAspectCropsTheCentreAndCameraCropMovesIt() { var source = Source(4); using var reference = source.Sequence.Render(2); var (square, _) = await CaptureAsync(Input(source), Preset("png-sequence", 64, 64), cancellationToken: _context.Cancellation); using (var expected = GeometryStage.Apply(reference, new GeometrySettings { Crop = new RectD(16, 0, 64, 64), OutputWidth = 64, OutputHeight = 64 })) Assert.Greater(Psnr(expected, square.Frames[2]), 60); var quarter = With(camera: new CameraSettings { Crop = [0.5, 0.5, 0.5, 0.5] }); var (corner, result) = await CaptureAsync(Input(source, quarter), Preset("png-sequence", 0, 0), cancellationToken: _context.Cancellation); Assert.Equal((48, 32), (result.Width, result.Height)); using (var expected = GeometryStage.Apply(reference, new GeometrySettings { Crop = new RectD(48, 32, 48, 32) })) Assert.Greater(Psnr(expected, corner.Frames[2]), 60); } /// /// A crop in the view frame keeps its content through every orientation press: the capture after the press is the /// plain capture turned, mirrored or flipped the same way. /// [Theory] [Case(OrientationOp.RotateRight, 1, false, false)] [Case(OrientationOp.RotateLeft, 3, false, false)] [Case(OrientationOp.Mirror, 0, true, false)] [Case(OrientationOp.Flip, 0, false, true)] public async Task OrientationPressesKeepTheCroppedContent(OrientationOp op, int turns, bool mirror, bool flip) { var source = Source(4); var framed = With(camera: new CameraSettings { Crop = [0.5, 0.25, 0.5, 0.5], Aspect = On("3:2") }); var (plain, before) = await CaptureAsync(Input(source, framed), Preset("png-sequence", 0, 0), cancellationToken: _context.Cancellation); Assert.Equal((48, 32), (before.Width, before.Height)); var pressed = OrientationEdit.Apply(framed, op); var (moved, after) = await CaptureAsync(Input(source, pressed), Preset("png-sequence", 0, 0), cancellationToken: _context.Cancellation); Assert.Empty(after.Warnings); using var expected = Geometry.FrameOrientationTests.Permuted(plain.Frames[2], turns, mirror, flip); Assert.Equal((expected.Width, expected.Height), (after.Width, after.Height)); var psnr = Psnr(expected, moved.Frames[2]); _context.Log($"{op}: {psnr:F1} dB"); Assert.Greater(psnr, 60); Assert.True(expected.Data.SequenceEqual(moved.Frames[2].Data), "a quarter turn, a mirror and a flip move whole pixels"); } [Test] public async Task APortraitAspectTurnsAFixedPresetToPortrait() { var source = Source(4); using var reference = source.Sequence.Render(2); var project = With(camera: new CameraSettings { Aspect = On("9:16") }); var (encode, result) = await CaptureAsync(Input(source, project), Preset("png-sequence", 96, 64), cancellationToken: _context.Cancellation); Assert.Equal((54, 96), (result.Width, result.Height)); Assert.DoesNotContain("Export.Render.AspectFromPreset", result.Warnings.Select(w => w.Key)); using var expected = GeometryStage.Apply(reference, new GeometrySettings { Crop = new RectD(30, 0, 36, 64), OutputWidth = 54, OutputHeight = 96 }); Assert.Greater(Psnr(expected, encode.Frames[2]), 60); } [Test] public async Task StraightenLevelsTheViewAndDecodesLarger() { var source = Source(4, width: 480, height: 320); var project = With(camera: new CameraSettings { Straighten = -6 }); var (encode, result) = await CaptureAsync(Input(source, project), Preset("png-sequence", 96, 64), cancellationToken: _context.Cancellation); var orientation = FrameOrientation.Of(project.Camera, 480, 320); Assert.Equal((int)double.Ceiling(96 * orientation.CoverZoom), source.LargestRenderRequest); Assert.Empty(result.Warnings); using var full = source.Sequence.Render(1); using var expected = GeometryStage.Apply(full, new GeometrySettings { OutputWidth = 96, OutputHeight = 64, Warp = orientation.ViewToFrame }); var psnr = Psnr(expected, encode.Frames[1]); _context.Log($"straighten −6° vs a direct warp: {psnr:F1} dB, cover zoom {orientation.CoverZoom:F4}"); Assert.Greater(psnr, 28); } [Test] public async Task VirtualCameraZoomsAndTheDecodeSizeFollowsTheNeed() { var plain = Source(4, width: 480, height: 320); await CaptureAsync(Input(plain), Preset("png-sequence", 96, 64), cancellationToken: _context.Cancellation); Assert.Equal(96, plain.LargestRenderRequest); var zoomed = Source(4, width: 480, height: 320); var camera = With(camera: new CameraSettings { Keyframes = [new CameraKey(0, 0.5, 0.5, 2, 0), new CameraKey(3, 0.5, 0.5, 2, 0)] }); var (encode, _) = await CaptureAsync(Input(zoomed, camera), Preset("png-sequence", 96, 64), cancellationToken: _context.Cancellation); Assert.Equal(192, zoomed.LargestRenderRequest); var capped = Source(4, width: 480, height: 320); await CaptureAsync(Input(capped, camera), Preset("png-sequence", 96, 64), new RenderOptions { MaxWorkingLongEdge = 120 }, _context.Cancellation); Assert.Equal(120, capped.LargestRenderRequest); using var full = zoomed.Sequence.Render(1); using var expected = GeometryStage.Apply(full, new GeometrySettings { Crop = new RectD(120, 80, 240, 160), OutputWidth = 96, OutputHeight = 64 }); var psnr = Psnr(expected, encode.Frames[1]); _context.Log($"zoom ×2 vs direct crop: {psnr:F1} dB"); Assert.Greater(psnr, 28); } [Test] public async Task ExposureOffsetAndKneeAreApplied() { var source = Source(3, configure: o => o with { StartEv = 2, EndEv = 2 }); var (raw, _) = await CaptureAsync(Input(source), cancellationToken: _context.Cancellation); var (brighter, _) = await CaptureAsync(Input(source, With(new ExposureSettings { ExposureOffsetEv = 1, HighlightKnee = null })), cancellationToken: _context.Cancellation); for (var i = 0; i < raw.Frames[0].Data.Length; i += 13) Assert.Approximately(2 * raw.Frames[0].Data[i], brighter.Frames[0].Data[i], 1e-5); Assert.Greater(raw.Frames[0].Data.ToArray().Max(), 1f); var (kneed, _) = await CaptureAsync(Input(source, With(new ExposureSettings { HighlightKnee = 0.8 })), cancellationToken: _context.Cancellation); Assert.Less(kneed.Frames[0].Data.ToArray().Max(), 1f); for (var i = 0; i < raw.Frames[0].Data.Length; i += 13) { if (raw.Frames[0].Data[i] < 0.5f) Assert.Approximately(raw.Frames[0].Data[i], kneed.Frames[0].Data[i], 1e-6); } } [Test] public async Task InterpolatedFramesFollowTheMotion() { Func pan = i => (2.0 * i, 0); var source = Source(6, motion: pan, configure: o => o with { StartEv = 0, EndEv = 0 }); var timing = new TimingSettings { Mode = On(TimingMode.Interpolated), FrameRate = new Auto().WithManual("15") }; var export = Preset("png-sequence") with { FrameRate = new Chronos.Core.Video.Rational(30, 1) }; var (encode, result) = await CaptureAsync(Input(source, With(timing: timing)), export, cancellationToken: _context.Cancellation); Assert.Equal(11, result.FrameCount); Assert.Equal(12, encode.Durations.Sum() / 3000); var truth = Source(6, motion: i => (2.0 * i + 1, 0), configure: o => o with { StartEv = 0, EndEv = 0 }); using var shifted = await truth.GetFrameAsync(2, 0, DecodePurpose.Render, _context.Cancellation); double Interior(ImageBuffer a, ImageBuffer b) { using var ca = GeometryStage.Apply(a, new GeometrySettings { Crop = new RectD(12, 4, 72, 56) }); using var cb = GeometryStage.Apply(b, new GeometrySettings { Crop = new RectD(12, 4, 72, 56) }); return Psnr(ca, cb); } var synthesised = Interior(shifted, encode.Frames[5]); var nearest = Interior(shifted, encode.Frames[4]); _context.Log($"synthesised frame {synthesised:F1} dB vs nearest whole frame {nearest:F1} dB"); Assert.Greater(synthesised, 30); Assert.Greater(synthesised, nearest + 5); } [Test] public async Task MotionBlurSmearsAlongTheMotionOnly() { var blur = new MotionSettings { MotionBlur = On(true), TargetShutterAngle = On(360.0) }; var moving = Source(6, motion: i => (3.0 * i, 0), configure: o => o with { StartEv = 0, EndEv = 0 }); var sharp = await CaptureAsync(Input(moving), cancellationToken: _context.Cancellation); var blurred = await CaptureAsync(Input(moving, With(motion: blur)), cancellationToken: _context.Cancellation); double Gradient(ImageBuffer f, int dx, int dy) { double sum = 0; var g = f.Plane(1); for (var y = 8; y < f.Height - 8; y++) { for (var x = 8; x < f.Width - 8; x++) sum += Math.Abs(g[y * f.Width + x] - g[(y + dy) * f.Width + x + dx]); } return sum; } var horizontal = Gradient(blurred.Encode.Frames[3], 1, 0) / Gradient(sharp.Encode.Frames[3], 1, 0); var vertical = Gradient(blurred.Encode.Frames[3], 0, 1) / Gradient(sharp.Encode.Frames[3], 0, 1); _context.Log($"gradient ratios: horizontal {horizontal:F3}, vertical {vertical:F3}"); Assert.Less(horizontal, 0.75); Assert.Greater(vertical, horizontal + 0.1); var still = Source(6, configure: o => o with { StartEv = 0, EndEv = 0 }); var a = await CaptureAsync(Input(still), cancellationToken: _context.Cancellation); var b = await CaptureAsync(Input(still, With(motion: blur)), cancellationToken: _context.Cancellation); Assert.Greater(Psnr(a.Encode.Frames[3], b.Encode.Frames[3]), 40); } [Test] public async Task StabilisationRemovesShake() { var random = new Random(11); var shake = Enumerable.Range(0, 24).Select(_ => (X: random.NextDouble() * 6 - 3, Y: random.NextDouble() * 6 - 3)).ToArray(); var source = new PatternFrameSource(320, 192, 24, i => shake[i]); var project = With(motion: new MotionSettings { Stabilize = On(true), StabilizeSmoothing = On(12.0) }); var input = new RenderInput { Source = source, Frames = source.Metadata(), Project = project }; static double Steps(IReadOnlyList<(double X, double Y)> p) => Math.Sqrt(Enumerable.Range(1, p.Count - 1).Average(k => Math.Pow(p[k].X - p[k - 1].X, 2) + Math.Pow(p[k].Y - p[k - 1].Y, 2))); var prepared = await RenderPreparation.PrepareAsync(input, cancellationToken: _context.Cancellation); var residual = Enumerable.Range(0, 24).Select(k => { var (x, y) = prepared.Stabilization![k].Apply(159.5, 95.5); return (shake[k].X + x - 159.5, shake[k].Y + y - 95.5); }).ToArray(); _context.Log($"camera path steps {Steps(shake):F2} px → {Steps(residual):F3} px after correction; zoom {prepared.StabilizationZoom:F3}"); Assert.Less(Steps(residual), 0.1 * Steps(shake)); Assert.InRange(prepared.StabilizationZoom, 1.01, 1.2); var (encode, result) = await CaptureAsync(input, Preset("png-sequence", 0, 0), cancellationToken: _context.Cancellation); Assert.Equal((320, 192), (result.Width, result.Height)); using var stabilizer = new Stabilizer(320, 192); var output = encode.Frames.Select(f => stabilizer.AddFrame(f).Transform).Skip(1).ToArray(); var after = Math.Sqrt(output.Average(m => m.Tx * m.Tx + m.Ty * m.Ty)); _context.Log($"rendered frame-to-frame shift {after:F3} px"); Assert.Less(after, 0.1 * Steps(shake) * prepared.StabilizationZoom); } [Test] public async Task MedianStackRemovesTransientsAndStarTrailsNeverDarken() { var source = Source(9, configure: o => o with { StartEv = 0, EndEv = 0 }); var input = Input(source, With(stack: new StackSettings { Mode = StackSetting.Median, Length = 5 })) with { Source = new TransientSource(source, 4) }; var (median, _) = await CaptureAsync(input, cancellationToken: _context.Cancellation); Assert.Equal(9, median.Frames.Count); using var clean = source.Sequence.Render(4); Assert.Greater(Psnr(clean, median.Frames[4]), 45); var stars = Source(12, motion: i => (1.0 * i, 0), configure: o => o with { StarField = true, StartEv = 0, EndEv = 0, StarCount = 60 }); var (trails, _) = await CaptureAsync(Input(stars, With(stack: new StackSettings { Mode = StackSetting.StarTrails, Length = 0 })), cancellationToken: _context.Cancellation); var luminance = LogLuminance(trails.Frames); Assert.Monotonic(luminance); for (var k = 1; k < trails.Frames.Count; k++) { var previous = trails.Frames[k - 1].Data; var current = trails.Frames[k].Data; for (var i = 0; i < current.Length; i++) Assert.True(current[i] >= previous[i] - 1e-6f, $"pixel {i} darkened at frame {k}"); } } [Test] public async Task PrecomputedAnalysisSkipsTheMeasuringPass() { var source = Source(30, flickerEv: 0.2); var project = With(new ExposureSettings { Deflicker = On(true), HighlightKnee = null }); var measured = await RenderPreparation.PrepareAsync(Input(source, project), cancellationToken: _context.Cancellation); Assert.Equal(30, measured.Report.MeasuredFrames); var cached = new PrecomputedAnalysis { LogLuminance = measured.MeasuredLogLuminance }; var decodes = source.Decodes; var reused = await RenderPreparation.PrepareAsync(Input(source, project) with { Analysis = cached }, cancellationToken: _context.Cancellation); Assert.Equal(0, reused.Report.MeasuredFrames); Assert.Equal(decodes, source.Decodes); Assert.SequenceEqual(measured.ExposureEv, reused.ExposureEv); var target = measured.TargetLogLuminance(); Assert.Approximately(measured.MeasuredLogLuminance[7] + measured.ExposureEv[7], target[7], 1e-12); static double[] Steps(double[] v) => [.. v.Zip(v.Skip(1), (a, b) => b - a)]; Assert.Less(Rms(Steps(target)), 0.5 * Rms(Steps(measured.MeasuredLogLuminance))); var wrongLength = await RenderPreparation.PrepareAsync(Input(source, project) with { Analysis = new PrecomputedAnalysis { LogLuminance = new double[3] } }, cancellationToken: _context.Cancellation); Assert.Equal(30, wrongLength.Report.MeasuredFrames); Assert.SequenceEqual(measured.ExposureEv, wrongLength.ExposureEv); var nothing = await RenderPreparation.PrepareAsync(Input(source), cancellationToken: _context.Cancellation); Assert.Equal(0, nothing.Report.MeasuredFrames); Assert.True(nothing.ExposureEv.All(e => e == 0)); } [Test] public async Task HeldFramesKeepTheirDurationOnConstantRateOutputs() { var source = Source(10, configure: o => o with { TimeShifts = [new SyntheticTimeShift(5, 20)] }); var project = With(timing: new TimingSettings { Mode = On(TimingMode.Adaptive) }); var export = Preset("mjpeg-avi"); var dir = _context.CreateTempDirectory(); var result = await Pipeline().RenderAsync(Input(source, project), Target(dir, export), cancellationToken: _context.Cancellation); var avi = AviReader.Read(File.ReadAllBytes(result.OutputPath)); Assert.Equal(10, result.FrameCount); Assert.Equal(12, avi.Frames.Count); // frame 4 lasts three periods (30 s ÷ 10 s) Assert.Approximately(12 / 25.0, result.DurationSeconds, 1e-9); } [Test] public void SetupReportsGeometryEncoderPlanAndEstimates() { var source = Source(48, width: 480, height: 320); var input = Input(source, With(new ExposureSettings { Deflicker = On(true) }, motion: new MotionSettings { MotionBlur = On(true) })); var setup = Pipeline().Setup(input, Target("/exports", Preset("gif-preview", 480, 270))); Assert.True(setup.Encoder.IsReady); Assert.Equal((480, 270), (setup.Geometry.OutputWidth, setup.Geometry.OutputHeight)); Assert.Equal(48, setup.Plan.Frames.Count); Assert.True(setup.NeedsPreparation); var workload = setup.Workload(); Assert.Equal(48, workload.FlowFrames); Assert.Equal(48, workload.AnalysisFrames); Assert.Equal(480L * 320, workload.WorkingPixels); var estimate = setup.Estimate(); Assert.Greater(estimate.Bytes, 0); Assert.Greater(estimate.Time.TotalSeconds, 0); } /// Adds a bright square to one frame (a passing aircraft light). private sealed class TransientSource(IRenderFrameSource inner, int frame) : IRenderFrameSource { public int FrameCount => inner.FrameCount; public int Width => inner.Width; public int Height => inner.Height; public async ValueTask GetFrameAsync(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken) { var image = await inner.GetFrameAsync(index, maxLongEdge, purpose, cancellationToken); if (index != frame) return image; for (var c = 0; c < 3; c++) { for (var y = 20; y < 30; y++) { for (var x = 40; x < 50; x++) image[c, x, y] = 1; } } return image; } } }