RenderPreparation measures (or reuses cached) luminance, colour, region and camera-path series and turns them into per-frame corrections. RenderPipeline renders planned frames through exposure, one geometry resample, flow synthesis, motion blur, stacking and the knee, overlapping encoding with processing; cancellation and failures leave no output and no unobserved exceptions. Batch renders share one preparation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
84 lines
4.2 KiB
C#
84 lines
4.2 KiB
C#
using Titano.Core.Diagnostics.Synthetic;
|
||
using Titano.Core.Export;
|
||
using Titano.Core.Imaging;
|
||
using Titano.Core.Project;
|
||
using Titano.Core.Render;
|
||
using Titano.Core.Render.Diagnostics;
|
||
using Titano.Core.Video;
|
||
using Titano.Core.Video.Managed;
|
||
|
||
namespace Titano.Core.Tests.Render;
|
||
|
||
/// <summary>Small synthetic renders shared by the render tests.</summary>
|
||
internal static class RenderFixture
|
||
{
|
||
/// <summary>No module and no knee: the output equals the source.</summary>
|
||
public static ProjectSettings Plain { get; } = new() { Exposure = new ExposureSettings { HighlightKnee = null } };
|
||
|
||
public static SyntheticFrameSource Source(int frames = 12, double flickerEv = 0, Func<int, (double X, double Y)>? motion = null, int width = 96, int height = 64,
|
||
Func<SyntheticSequenceOptions, SyntheticSequenceOptions>? 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);
|
||
}
|
||
|
||
/// <summary>Renders into a capturing encoder (PNG-sequence capability) and returns the captured frames.</summary>
|
||
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(), "titano-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 };
|
||
|
||
/// <summary>A built-in preset's values at an explicit size (0×0 = source size).</summary>
|
||
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()]);
|
||
|
||
/// <summary>Peak signal-to-noise ratio of the 8-bit sRGB encodings.</summary>
|
||
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));
|
||
}
|
||
|
||
/// <summary>Synchronous progress sink (Progress<T> would post to the thread pool).</summary>
|
||
public sealed class Sink<T>(Action<T> report) : IProgress<T>
|
||
{
|
||
public void Report(T value) => report(value);
|
||
}
|
||
|
||
/// <summary>Throws an <see cref="IOException"/> for one frame, as an unreadable file would.</summary>
|
||
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<ImageBuffer> GetFrameAsync(int index, int maxLongEdge, DecodePurpose purpose, CancellationToken cancellationToken) =>
|
||
index == failAt ? throw new IOException($"frame {index} is unreadable") : inner.GetFrameAsync(index, maxLongEdge, purpose, cancellationToken);
|
||
}
|
||
}
|