using Chronos.Core.Diagnostics.Synthetic;
using Chronos.Core.Export;
using Chronos.Core.Imaging;
using Chronos.Core.Project;
using Chronos.Core.Render;
using Chronos.Core.Render.Diagnostics;
using Chronos.Core.Video;
using Chronos.Core.Video.Managed;
namespace Chronos.Core.Tests.Render;
/// Small synthetic renders shared by the render tests.
internal static class RenderFixture
{
/// No module and no knee: the output equals the source.
public static ProjectSettings Plain { get; } = new() { Exposure = new ExposureSettings { HighlightKnee = null } };
public static SyntheticFrameSource Source(int frames = 12, double flickerEv = 0, Func? motion = null, int width = 96, int height = 64,
Func? configure = null)
{
var options = new SyntheticSequenceOptions
{
Width = width,
Height = height,
FrameCount = frames,
StartEv = 0,
EndEv = 1,
FlickerEv = flickerEv,
Seed = 5,
};
return new SyntheticFrameSource(SyntheticSequence.Create(configure is null ? options : configure(options)), motion);
}
/// Renders into a capturing encoder (PNG-sequence capability) and returns the captured frames.
public static async Task<(CapturedEncode Encode, RenderResult Result)> CaptureAsync(RenderInput input, ResolvedExport? export = null, RenderOptions? options = null,
CancellationToken cancellationToken = default)
{
var capture = new CapturingEncoderProvider("capture", new EncoderCapability(VideoCodec.PngSequence, "Capture", false, 8192, 8192, true));
var target = new RenderTarget(export ?? Preset("png-sequence"), Path.Combine(Path.GetTempPath(), "chronos-capture-" + Guid.NewGuid().ToString("N")));
var result = await Pipeline(capture).RenderAsync(input, target, options, cancellationToken: cancellationToken);
return (capture.Encodes.Single(), result);
}
public static RenderInput Input(SyntheticFrameSource source, ProjectSettings? project = null) =>
new() { Source = source, Frames = source.Metadata(), Project = project ?? Plain };
/// A built-in preset's values at an explicit size (0×0 = source size).
public static ResolvedExport Preset(string presetId, int width = 96, int height = 64) =>
ExportResolver.FromPreset(ExportPresets.Find(presetId)!) with { Width = width, Height = height };
public static RenderTarget Target(string folder, ResolvedExport export, string name = "out") =>
new(export, Path.Combine(folder, name + (export.Container == ContainerFormat.ImageSequence ? string.Empty : VideoCodecInfo.FileExtension(export.Codec))));
public static RenderPipeline Pipeline(params IVideoEncoderProvider[] extra) =>
new([.. extra, new ManagedEncoderProvider()]);
/// Peak signal-to-noise ratio of the 8-bit sRGB encodings.
public static double Psnr(ImageBuffer expected, ImageBuffer actual)
{
var a = PixelConvert.ToInterleaved8(expected, 3);
var b = PixelConvert.ToInterleaved8(actual, 3);
double sum = 0;
for (var i = 0; i < a.Length; i++) sum += (a[i] - b[i]) * (a[i] - b[i]);
return sum == 0 ? double.PositiveInfinity : 10 * Math.Log10(255.0 * 255 / (sum / a.Length));
}
/// Synchronous progress sink (Progress<T> would post to the thread pool).
public sealed class Sink(Action report) : IProgress
{
public void Report(T value) => report(value);
}
/// Throws an for one frame, as an unreadable file would.
public sealed class FailingSource(IRenderFrameSource inner, int failAt) : IRenderFrameSource
{
public int FrameCount => inner.FrameCount;
public int Width => inner.Width;
public int Height => inner.Height;
public ValueTask GetFrameAsync(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken) =>
index == failAt ? throw new IOException($"frame {index} is unreadable") : inner.GetFrameAsync(index, maxLongEdge, purpose, cancellationToken);
}
}