using Chronos.Core.Imaging;
using Chronos.Core.Imaging.Geometry;
using Chronos.Core.Project;
namespace Chronos.Core.Render.Diagnostics;
/// Orientation, straighten and output-proportion checks (owner review 1, R5/R6).
public static partial class SelfTest
{
private static IEnumerable GeometryChecks()
{
yield return Check("proc.orient", "A quarter turn with a mirror transposes the frame pixel for pixel", async context =>
{
var source = Synthetic(3);
var project = Plain with { Camera = new CameraSettings { Rotate = 90, Mirror = true } };
var frames = await RenderDecodedAsync(context, Input(source, project), Preset("png-sequence", 0, 0)).ConfigureAwait(false);
using var reference = source.Sequence.Render(1);
// A turn right then a mirror is the transpose: view (u, v) shows frame (v, u).
using var expected = new ImageBuffer(reference.Height, reference.Width, reference.Channels, clear: false);
for (var c = 0; c < reference.Channels; c++)
{
for (var v = 0; v < expected.Height; v++)
{
for (var u = 0; u < expected.Width; u++) expected[c, u, v] = reference[c, v, u];
}
}
var size = (frames[1].Width, frames[1].Height);
var psnr = size == (expected.Width, expected.Height) ? Psnr(expected, frames[1]) : 0;
DisposeAll(frames);
Require(size == (64, 96) && psnr > 60, Invariant($"{size}, PSNR {psnr:0.#} dB"));
return "transposed 64×96";
});
yield return Check("proc.straighten", "Straighten turns the view with no border and decodes larger by its cover zoom", async context =>
{
var source = Synthetic(3, o => o with { Width = 480, Height = 320 });
var project = Plain with { Camera = new CameraSettings { Straighten = 10 } };
var frames = await RenderDecodedAsync(context, Input(source, project), Preset("png-sequence", 96, 64)).ConfigureAwait(false);
var orientation = FrameOrientation.Of(project.Camera, 480, 320);
using var full = source.Sequence.Render(1);
var view = new GeometrySettings { OutputWidth = 96, OutputHeight = 64, Warp = orientation.ViewToFrame };
using var expected = GeometryStage.Apply(full, view);
// At full size (no minifying kernel), every pixel of the view is covered by the frame.
using var coverage = GeometryStage.Apply(full, view with { OutputWidth = 480, OutputHeight = 320, Border = BorderMode.Transparent });
var alpha = coverage.Plane(3).ToArray().Min();
var psnr = Psnr(expected, frames[1]);
DisposeAll(frames);
var edge = (int)double.Ceiling(96 * orientation.CoverZoom);
Require(psnr > 28 && alpha >= 1 - 1e-6 && source.LargestRenderRequest == edge,
Invariant($"PSNR {psnr:0.#} dB, coverage {alpha:0.######}, decoded at {source.LargestRenderRequest} px (expected {edge})"));
return Invariant($"PSNR {psnr:0.#} dB, zoom {orientation.CoverZoom:0.###}, decode {edge} px");
});
yield return Check("proc.portrait", "A 9:16 aspect turns a fixed-size preset to portrait (proportions win)", async context =>
{
var source = Synthetic(3);
var project = Plain with { Camera = new CameraSettings { Aspect = Manual("9:16") } };
var frames = await RenderDecodedAsync(context, Input(source, project), Preset("png-sequence", 96, 64)).ConfigureAwait(false);
using var reference = source.Sequence.Render(1);
var crop = AspectRatio.FitCrop(RectD.FromSize(96, 64), 9.0 / 16);
using var expected = GeometryStage.Apply(reference, new GeometrySettings { Crop = crop, OutputWidth = 54, OutputHeight = 96 });
var size = (frames[1].Width, frames[1].Height);
var psnr = size == (54, 96) ? Psnr(expected, frames[1]) : 0;
DisposeAll(frames);
Require(size == (54, 96) && psnr > 60, Invariant($"{size}, PSNR {psnr:0.#} dB"));
return "96×64 box → 54×96";
});
}
}