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); } [Test] public async Task SolveSettingsReuseTheMeasurementsWithoutDecoding() { var (renderer, source) = Create(); var project = Sunset(); await renderer.PrepareAsync(project, CancellationToken.None); var decodes = source.Decodes; Assert.Greater(decodes, 0, "stabilisation and colour are measured once"); foreach (var strength in new[] { 0.3, 0.6, 0.9 }) { var changed = project with { Exposure = project.Exposure with { DeflickerStrength = project.Exposure.DeflickerStrength.WithManual(strength), TransitionLength = project.Exposure.TransitionLength.WithManual(6) } }; await renderer.PrepareAsync(changed, CancellationToken.None); } Assert.Equal(decodes, source.Decodes, "a strength or transition change re-solves from the measured series"); } [Test] public async Task WithoutAnalysisNothingIsMeasuredUntilAModuleNeedsIt() { var content = EditorFixture.Content(EditorFixture.Sunset, deep: false); var source = new FakeSequenceSource(EditorFixture.Sunset, content); var renderer = new PreviewRenderer(content, source); await renderer.PrepareAsync(new ProjectSettings(), CancellationToken.None); Assert.Equal(0, source.Decodes, "no module on, nothing to measure"); var project = Deflickered(); await renderer.PrepareAsync(project, CancellationToken.None); var decodes = source.Decodes; Assert.GreaterOrEqual(decodes, EditorFixture.Sunset.Count, "deflicker measures the luminance once"); await renderer.PrepareAsync(project with { Exposure = project.Exposure with { DeflickerStrength = project.Exposure.DeflickerStrength.WithManual(0.4) } }, CancellationToken.None); Assert.Equal(decodes, source.Decodes); } [Test] public async Task AReSolveMatchesAFreshPreparation() { var (warm, _) = Create(); var project = Sunset(); await warm.PrepareAsync(project, CancellationToken.None); var changed = project with { Exposure = project.Exposure with { DeflickerStrength = project.Exposure.DeflickerStrength.WithManual(0.5), ExposureOffsetEv = 0.3 } }; var resolved = await warm.PrepareAsync(changed, CancellationToken.None); var (cold, _) = Create(); var fresh = await cold.PrepareAsync(changed, CancellationToken.None); Assert.Equal(fresh.ExposureEv.Length, resolved.ExposureEv.Length); for (var i = 0; i < fresh.ExposureEv.Length; i++) Assert.Approximately(fresh.ExposureEv[i], resolved.ExposureEv[i], 1e-9, $"frame {i}"); Assert.NotNull(resolved.WhiteBalance); for (var i = 0; i < fresh.WhiteBalance!.Length; i++) Assert.Equal(fresh.WhiteBalance[i], resolved.WhiteBalance![i]); Assert.Equal(fresh.StabilizationZoom, resolved.StabilizationZoom); Assert.NotNull(resolved.Stabilization); Assert.SequenceEqual(fresh.Stabilization!, resolved.Stabilization!); } [Test] public async Task ActualSizeRendersTheSourcePixels() { var (renderer, _) = Create(); var edge = int.Max(EditorFixture.Sunset.Options.Width, EditorFixture.Sunset.Options.Height); using var frame = await renderer.RenderAsync(new PreviewRequest(3, new ProjectSettings(), edge, PreviewFraming.Source, Vectors: true), null, CancellationToken.None); Assert.Equal(EditorFixture.Sunset.Options.Width, frame!.Corrected.Width, "one picture pixel per source pixel"); Assert.Equal(EditorFixture.Sunset.Options.Height, frame.Corrected.Height); Assert.NotEmpty(frame.Vectors); Assert.True(frame.Vectors.All(v => v.X < frame.Corrected.Width && v.Y < frame.Corrected.Height), "vectors in picture pixels"); } [Test] public async Task TheWaveformIsMeasuredOnRequest() { var (renderer, _) = Create(); using var plain = await renderer.RenderAsync(new PreviewRequest(3, new ProjectSettings(), 64, PreviewFraming.Output), null, CancellationToken.None); Assert.Null(plain!.Waveform); using var frame = await renderer.RenderAsync(new PreviewRequest(3, new ProjectSettings(), 64, PreviewFraming.Output, Waveform: true), null, CancellationToken.None); var waveform = Assert.NotNull(frame!.Waveform); Assert.Equal(frame.Corrected.Width, waveform.Columns); Assert.True(waveform.Density.Any(d => d > 0)); } /// A sunset project as the advisor leaves it: deflicker, holy grail, white balance and stabilisation on. private static ProjectSettings Sunset() { var p = new ProjectSettings(); return p with { Exposure = p.Exposure with { Deflicker = p.Exposure.Deflicker.WithManual(true), HolyGrail = p.Exposure.HolyGrail.WithManual(true), WhiteBalanceSmoothing = p.Exposure.WhiteBalanceSmoothing.WithManual(true), }, Motion = p.Motion with { Stabilize = p.Motion.Stabilize.WithManual(true) }, }; } 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); } }