using Titano.App.Editor; using Titano.Core.Imaging; using Titano.Core.Project; using Titano.Testing; namespace Titano.App.Tests.Editor; /// The corrected-frame preview through the render pipeline at preview size. public sealed class PreviewRendererTests { private static (PreviewRenderer Renderer, FakeSequenceSource Source) Create() { var content = EditorFixture.Content(EditorFixture.Sunset); var source = new FakeSequenceSource(EditorFixture.Sunset, content); return (new PreviewRenderer(content, source), source); } private static ProjectSettings Deflickered() { var p = new ProjectSettings(); return p with { Exposure = p.Exposure with { Deflicker = p.Exposure.Deflicker.WithManual(true), HighlightKnee = null } }; } [Test] public async Task DeflickerFlattensThePreviewedLuminance() { var (renderer, _) = Create(); var project = Deflickered(); var corrections = await renderer.PrepareAsync(project, CancellationToken.None); Assert.Same(corrections, renderer.Cached(project)); Assert.Null(renderer.Cached(new ProjectSettings()), "other exposure settings need their own preparation"); var corrected = new List(); var original = new List(); for (var i = 4; i < 20; i++) { using var frame = await renderer.RenderAsync(new PreviewRequest(i, project, 64, PreviewFraming.Output), corrections, CancellationToken.None); Assert.NotNull(frame); corrected.Add(LogMean(frame!.Corrected)); original.Add(LogMean(frame.Original)); } Assert.Less(Spread(corrected), Spread(original) * 0.6, "flicker removed between the steps"); } [Test] public async Task OutputFramingFollowsTheExportAspect() { var (renderer, _) = Create(); var project = new ProjectSettings(); project = project with { Export = project.Export with { Preset = project.Export.Preset.WithManual("square-1440") } }; using var output = await renderer.RenderAsync(new PreviewRequest(0, project, 80, PreviewFraming.Output), null, CancellationToken.None); Assert.Equal(80, output!.Corrected.Width, "the square output at the preview's long edge"); Assert.Equal(80, output.Corrected.Height); Assert.Equal(output.Corrected.Width, output.Original.Width); using var source = await renderer.RenderAsync(new PreviewRequest(0, project, 80, PreviewFraming.Source), null, CancellationToken.None); Assert.Equal(80, source!.Corrected.Width); Assert.Approximately(64.0 / 96, source.Crop.Width, 0.02, "the square crop keeps two thirds of the 3:2 frame"); Assert.Approximately(1, source.Crop.Height, 0.02); Assert.Approximately((1 - 64.0 / 96) / 2, source.Crop.X, 0.02, "centred"); } [Test] public async Task VectorsDescribeTheMotionToTheNextFrame() { var (renderer, _) = Create(); using var frame = await renderer.RenderAsync(new PreviewRequest(10, new ProjectSettings(), 96, PreviewFraming.Source, Vectors: true), null, CancellationToken.None); Assert.NotEmpty(frame!.Vectors); Assert.True(frame.Vectors.All(v => v.X >= 0 && v.X < 96 && v.Y >= 0 && v.Y < 64)); using var last = await renderer.RenderAsync(new PreviewRequest(EditorFixture.Sunset.Count - 1, new ProjectSettings(), 96, PreviewFraming.Source, true), null, CancellationToken.None); Assert.Empty(last!.Vectors); } [Test] public async Task ACancelledPreviewStopsWhileDecoding() { var (renderer, source) = Create(); source.DecodeGate = new TaskCompletionSource().Task; using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100)); await Assert.ThrowsAsync(() => renderer.RenderAsync(new PreviewRequest(0, new ProjectSettings(), 64, PreviewFraming.Output), null, cts.Token)); } [Test] public async Task AnUnknownFrameHasNoPreview() { var (renderer, _) = Create(); Assert.Null(await renderer.RenderAsync(new PreviewRequest(-1, new ProjectSettings(), 64, PreviewFraming.Output), null, CancellationToken.None)); Assert.Null(await renderer.RenderAsync(new PreviewRequest(10_000, new ProjectSettings(), 64, PreviewFraming.Output), null, CancellationToken.None)); } [Test] public async Task PreparedDecodesAreReused() { var (renderer, source) = Create(); var project = new ProjectSettings(); project = project with { Exposure = project.Exposure with { WhiteBalanceSmoothing = project.Exposure.WhiteBalanceSmoothing.WithManual(true) } }; await renderer.PrepareAsync(project, CancellationToken.None); var decodes = source.Decodes; Assert.GreaterOrEqual(decodes, EditorFixture.Sunset.Count, "white balance is measured on every frame"); await renderer.PrepareAsync(project with { Exposure = project.Exposure with { ExposureOffsetEv = 0.5 } }, CancellationToken.None); Assert.Equal(decodes, source.Decodes); } private static double LogMean(ImageBuffer image) { double sum = 0; var n = image.PlaneLength; for (var i = 0; i < n; i++) sum += 0.2126 * image.Plane(0)[i] + 0.7152 * image.Plane(1)[i] + 0.0722 * image.Plane(2)[i]; return Math.Log2(sum / n); } /// Standard deviation of the frame-to-frame differences (flicker, not the slow ramp). private static double Spread(List values) { var diffs = values.Zip(values.Skip(1), (a, b) => b - a).ToList(); var mean = diffs.Average(); return Math.Sqrt(diffs.Sum(d => (d - mean) * (d - mean)) / diffs.Count); } }