feat(render): orient, straighten and let the chosen proportions win in the render geometry
Owner review 1, phase 4, package P1: the Core geometry behind the visual crop (R5) and the orientation controls (R6). - RenderGeometry: one warp stab⁻¹ · O⁻¹ · V with the crop and the keyframe centres in the view frame T (turned, mirrored, flipped, straightened); the output size comes from RenderSize, so a fixed preset adapts to the aspect (4K at 9:16 = 2160×3840) and AspectFromPreset is gone; the decode grows by the straighten cover zoom; ViewFor, ViewToFrame, EditableCrop and FootprintInView get their real bodies; a rotation that is not a quarter turn and an out-of-range straighten render as none or clamped, with their warnings. - FrameOrientation: the cover zoom is taken on the pixel-centre extents, so every output pixel samples inside the frame (full alpha coverage at ±15°, never a smeared border). - CropFrame: crops are written on the 12-decimal grid OrientationEdit remaps on, so four turns or two mirrors give back the same text; a stored crop is read value by value as the render reads it. - Advisor: the aspect (and the horizon framing) is advised on the view turned by the camera (SequenceContext.Rotate/Flip, ForCamera); the analyser passes the project's camera. - Self-test: proc.orient, proc.straighten, proc.portrait. - Tests: exact pixel permutations for turns, mirror and flip, the orientation algebra, straighten against an analytic ramp and its full coverage, Create sizes, a crop keeping its content through every press, CropFrame move/resize/refit/rows, OrientationEdit round trips and its agreement with FrameOrientation, the turned-view advice, determinism. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e4364c4f25
commit
bee66da63a
18 files changed
+1052
-67
No files matched your search
@@ -449,10 +449,11 @@ internal sealed class AdvisorRules
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Aspect 16:9 for landscape sources (the reason says how much height or width the crop keeps), 9:16 for portrait
|
||||
/// ones. When a confident horizon exists and the crop gives up
|
||||
/// height, the crop is placed so the horizon sits on the nearest third of the output frame (recommended only when the
|
||||
/// placement moves at least a fifth of the slack away from centred).
|
||||
/// Aspect 16:9 for landscape views (the reason says how much height or width the crop keeps), 9:16 for portrait
|
||||
/// ones, the view being the source turned by the camera's quarter turns (<see cref="SequenceContext.Rotate"/>). When a
|
||||
/// confident horizon exists, the view is not turned sideways and the crop gives up height, the crop is placed so the
|
||||
/// horizon sits on the nearest third of the output frame (recommended only when the placement moves at least a fifth
|
||||
/// of the slack away from centred).
|
||||
/// </summary>
|
||||
private void Camera()
|
||||
{
|
||||
@@ -462,19 +463,22 @@ internal sealed class AdvisorRules
|
||||
Missing(section, AdvisorParameters.Aspect, AdvisorKeys.MissingSourceSize);
|
||||
return;
|
||||
}
|
||||
if (_source.Height > _source.Width)
|
||||
var turns = FrameOrientation.TurnsOf(_context.Rotate);
|
||||
var (width, height) = turns % 2 == 1 ? (_source.Height, _source.Width) : _source;
|
||||
if (height > width)
|
||||
{
|
||||
Add(section, AdvisorParameters.Aspect, "9:16", 0.7, AdvisorKeys.AspectPortrait);
|
||||
return;
|
||||
}
|
||||
var ratio = (double)_source.Width / _source.Height / (16.0 / 9);
|
||||
var ratio = (double)width / height / (16.0 / 9);
|
||||
var kept = double.Min(1, ratio);
|
||||
if (double.Abs(ratio - 1) < 0.01) Add(section, AdvisorParameters.Aspect, "16:9", 0.9, AdvisorKeys.AspectNative);
|
||||
else if (ratio < 1) Add(section, AdvisorParameters.Aspect, "16:9", 0.7, AdvisorKeys.AspectLandscape, _source.Width, _source.Height, R(100 * ratio, 0));
|
||||
else Add(section, AdvisorParameters.Aspect, "16:9", 0.6, AdvisorKeys.AspectWide, _source.Width, _source.Height, R(100 / ratio, 0));
|
||||
else if (ratio < 1) Add(section, AdvisorParameters.Aspect, "16:9", 0.7, AdvisorKeys.AspectLandscape, width, height, R(100 * ratio, 0));
|
||||
else Add(section, AdvisorParameters.Aspect, "16:9", 0.6, AdvisorKeys.AspectWide, width, height, R(100 / ratio, 0));
|
||||
|
||||
if (kept >= 0.99 || _deep?.Regions is not { Method: RegionMethod.Horizon, Confidence: >= RegionConfidence, Horizon: { Length: > 0 } horizon }) return;
|
||||
var row = horizon.Average();
|
||||
if (kept >= 0.99 || turns % 2 == 1 || _deep?.Regions is not { Method: RegionMethod.Horizon, Confidence: >= RegionConfidence, Horizon: { Length: > 0 } horizon }) return;
|
||||
// The horizon's rows are the decoded frame's; a half turn or a flip shows them upside down.
|
||||
var row = (turns == 2) ^ _context.Flip ? 1 - horizon.Average() : horizon.Average();
|
||||
var slack = 1 - kept;
|
||||
var third = double.Abs(row - (slack / 2 + kept / 3)) <= double.Abs(row - (slack / 2 + 2 * kept / 3)) ? 1.0 / 3 : 2.0 / 3;
|
||||
var top = double.Clamp(row - third * kept, 0, slack);
|
||||
|
||||
@@ -144,6 +144,22 @@ public sealed record SequenceContext
|
||||
/// <summary>Wanted video length ("fit to N seconds"), when the user set one.</summary>
|
||||
public double? TargetDurationSeconds { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// The camera's quarter turns in degrees clockwise (<see cref="CameraSettings.Rotate"/>): the aspect and the horizon
|
||||
/// framing are advised on the view as shown, the source turned by them.
|
||||
/// </summary>
|
||||
public int Rotate { get; init; }
|
||||
|
||||
/// <summary>The camera's top and bottom swapped (<see cref="CameraSettings.Flip"/>), for the horizon framing.</summary>
|
||||
public bool Flip { get; init; }
|
||||
|
||||
/// <summary>This context for the view of <paramref name="camera"/> (its <see cref="Rotate"/> and <see cref="Flip"/>).</summary>
|
||||
public SequenceContext ForCamera(CameraSettings camera)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(camera);
|
||||
return this with { Rotate = camera.Rotate, Flip = camera.Flip };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Context from the archive's frame rows: count, dominant oriented size, preview-only count with the dominant oriented
|
||||
/// size of those frames' previews, median shutter time.
|
||||
|
||||
@@ -40,7 +40,7 @@ public static class CropFrame
|
||||
/// </summary>
|
||||
public static RectD Of(double[]? crop)
|
||||
{
|
||||
if (crop is not [var x0, var y0, var w0, var h0] || !double.IsFinite(x0 + y0 + w0 + h0)) return Whole;
|
||||
if (crop is not [var x0, var y0, var w0, var h0] || !(double.IsFinite(x0) && double.IsFinite(y0) && double.IsFinite(w0) && double.IsFinite(h0))) return Whole;
|
||||
var x = double.Clamp(x0, 0, 1);
|
||||
var y = double.Clamp(y0, 0, 1);
|
||||
var w = double.Min(w0, 1 - x);
|
||||
@@ -48,8 +48,11 @@ public static class CropFrame
|
||||
return w <= 1e-3 || h <= 1e-3 ? Whole : new RectD(x, y, w, h);
|
||||
}
|
||||
|
||||
/// <summary>A box as <c>CameraSettings.Crop</c>; null for the whole view.</summary>
|
||||
public static double[]? ToCrop(RectD box) => IsWhole(box) ? null : [box.X, box.Y, box.Width, box.Height];
|
||||
/// <summary>
|
||||
/// A box as <c>CameraSettings.Crop</c>, null for the whole view; its values rounded to 12 decimals, the grid
|
||||
/// <see cref="OrientationEdit"/> remaps on, so turning four times (or mirroring twice) gives back the same text.
|
||||
/// </summary>
|
||||
public static double[]? ToCrop(RectD box) => IsWhole(box) ? null : [Grid(box.X), Grid(box.Y), Grid(box.Width), Grid(box.Height)];
|
||||
|
||||
/// <summary>The crop shows the whole view (null, invalid, or covering [0,1]²): nothing is cut.</summary>
|
||||
public static bool IsWhole(double[]? crop) => IsWhole(Of(crop));
|
||||
@@ -154,6 +157,9 @@ public static class CropFrame
|
||||
/// <summary>The view has room for the box to move along x (<paramref name="horizontal"/>) or y.</summary>
|
||||
public static bool HasSlack(RectD box, bool horizontal) => (horizontal ? 1 - box.Width : 1 - box.Height) > 1e-9;
|
||||
|
||||
/// <summary>12 decimals, never −0 (which JSON would write as "-0").</summary>
|
||||
private static double Grid(double value) => double.Round(value, 12) + 0.0;
|
||||
|
||||
private static bool IsWhole(RectD box) => box.X <= 0 && box.Y <= 0 && box.Right >= 1 && box.Bottom >= 1;
|
||||
|
||||
/// <summary>A box of the given size moved inside [0,1]².</summary>
|
||||
|
||||
@@ -25,7 +25,8 @@ public sealed record FrameOrientation
|
||||
FrameWidth = frameWidth;
|
||||
FrameHeight = frameHeight;
|
||||
(ViewWidth, ViewHeight) = turns % 2 == 1 ? (frameHeight, frameWidth) : (frameWidth, frameHeight);
|
||||
CoverZoom = CoverZoomOf(straighten, ViewWidth, ViewHeight);
|
||||
// On the pixel-centre extents, so every output pixel centre samples between frame pixel centres (full coverage).
|
||||
CoverZoom = CoverZoomOf(straighten, int.Max(1, ViewWidth - 1), int.Max(1, ViewHeight - 1));
|
||||
FrameToView = Straightening(1) * MirrorFlip() * QuarterTurns();
|
||||
ViewToFrame = QuarterTurns().Invert() * MirrorFlip() * Straightening(-1);
|
||||
}
|
||||
@@ -52,7 +53,11 @@ public sealed record FrameOrientation
|
||||
|
||||
public int ViewHeight { get; }
|
||||
|
||||
/// <summary>The straighten's cover zoom c = cos|θ| + max(W'/H', H'/W')·sin|θ| (1 without straighten); never stored.</summary>
|
||||
/// <summary>
|
||||
/// The straighten's cover zoom c = cos|θ| + max(W'/H', H'/W')·sin|θ| (1 without straighten), never stored, taken on the
|
||||
/// pixel-centre extents (W' − 1)×(H' − 1): the view's outermost pixel centres land on the frame's, so a resample needs no
|
||||
/// sample outside the frame (with a transparent border, every pixel is fully covered).
|
||||
/// </summary>
|
||||
public double CoverZoom { get; }
|
||||
|
||||
/// <summary>Frame pixel-centre coordinates to view pixel-centre coordinates (O).</summary>
|
||||
|
||||
@@ -1,7 +1,67 @@
|
||||
using Chronos.Core.Imaging;
|
||||
using Chronos.Core.Imaging.Geometry;
|
||||
using Chronos.Core.Project;
|
||||
|
||||
namespace Chronos.Core.Render.Diagnostics;
|
||||
|
||||
/// <summary>Orientation, straighten and output-proportion checks (owner review 1, R5/R6).</summary>
|
||||
public static partial class SelfTest
|
||||
{
|
||||
private static IEnumerable<SelfTestCheck> GeometryChecks() => [];
|
||||
private static IEnumerable<SelfTestCheck> 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";
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -3,21 +3,24 @@ using Chronos.Core.Export;
|
||||
using Chronos.Core.Imaging.Geometry;
|
||||
using Chronos.Core.Motion;
|
||||
using Chronos.Core.Project;
|
||||
using Chronos.Core.Video;
|
||||
|
||||
namespace Chronos.Core.Render;
|
||||
|
||||
/// <summary>
|
||||
/// Output size, decode size and per-frame warp of a render. <see cref="CameraSettings.Crop"/> is a normalised
|
||||
/// [x, y, width, height] rectangle of the camera view; the output aspect is the export's when it fixes both dimensions,
|
||||
/// otherwise the camera aspect (source = frame aspect). Virtual-camera keyframes are keyed to source frame positions, so
|
||||
/// they stay attached to the content when timing, frame rate or fitting change. The stabiliser correction, the camera
|
||||
/// pose (zoomed by the stabilisation zoom) and the crop become one output→source map, resampled once.
|
||||
/// Output size, decode size and per-frame warp of a render. The decoded frame is turned, mirrored, flipped and
|
||||
/// straightened into the view frame T (<see cref="FrameOrientation"/>); <see cref="CameraSettings.Crop"/> is a normalised
|
||||
/// [x, y, width, height] rectangle of T and the keyframe centres are normalised in it. The chosen proportions win
|
||||
/// (owner review 1, R5): the output ratio is the camera aspect, a fixed-size preset adapting its size to it
|
||||
/// (<see cref="RenderSize"/>), and "source" keeps the preset's ratio (else the view's). Virtual-camera keyframes are keyed
|
||||
/// to source frame positions, so they stay attached to the content when timing, frame rate or fitting change. The
|
||||
/// stabiliser correction, the orientation, the camera pose (zoomed by the stabilisation zoom) and the crop become one
|
||||
/// output→source map, <c>stab⁻¹ · O⁻¹ · V</c>, resampled once.
|
||||
/// </summary>
|
||||
public sealed class RenderGeometry
|
||||
{
|
||||
private static readonly CameraState Neutral = new(0.5, 0.5, 1, 0);
|
||||
private readonly VirtualCamera? _camera;
|
||||
private readonly FrameOrientation _orientation;
|
||||
private readonly RectD _crop;
|
||||
private readonly double _offsetX;
|
||||
private readonly double _offsetY;
|
||||
@@ -27,12 +30,13 @@ public sealed class RenderGeometry
|
||||
private readonly double _maxZoom;
|
||||
private readonly int _maxWorkingLongEdge;
|
||||
|
||||
private RenderGeometry(VirtualCamera? camera, RectD crop, double offsetX, double offsetY, double stabilizationZoom, int sourceWidth, int sourceHeight,
|
||||
private RenderGeometry(VirtualCamera? camera, FrameOrientation orientation, RectD crop, double offsetX, double offsetY, double stabilizationZoom, int sourceWidth, int sourceHeight,
|
||||
int outputWidth, int outputHeight, double fittedWidth, double fittedHeight, double maxZoom, int maxWorkingLongEdge, IReadOnlyList<RenderWarning> warnings,
|
||||
double outputRatio)
|
||||
{
|
||||
OutputRatio = outputRatio;
|
||||
_camera = camera;
|
||||
_orientation = orientation;
|
||||
_crop = crop;
|
||||
_offsetX = offsetX;
|
||||
_offsetY = offsetY;
|
||||
@@ -57,10 +61,13 @@ public sealed class RenderGeometry
|
||||
/// Width of the view frame T at full resolution (<see cref="FrameOrientation"/>): the source turned, mirrored and
|
||||
/// straightened. <see cref="CameraSettings.Crop"/> and the keyframe centres are normalised in it.
|
||||
/// </summary>
|
||||
public int ViewWidth => SourceWidth;
|
||||
public int ViewWidth => _orientation.ViewWidth;
|
||||
|
||||
/// <inheritdoc cref="ViewWidth"/>
|
||||
public int ViewHeight => SourceHeight;
|
||||
public int ViewHeight => _orientation.ViewHeight;
|
||||
|
||||
/// <summary>The camera's orientation at full resolution, its values as rendered (<see cref="FrameOrientation.Of(CameraSettings, int, int)"/>).</summary>
|
||||
public FrameOrientation Orientation => _orientation;
|
||||
|
||||
/// <summary>The output ratio (width / height) the crop is fitted to (<see cref="RenderSize.Ratio"/>), before rounding to whole pixels.</summary>
|
||||
public double OutputRatio { get; }
|
||||
@@ -81,8 +88,9 @@ public sealed class RenderGeometry
|
||||
public IReadOnlyList<RenderWarning> Warnings { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Long edge to decode source frames at: enough that the most zoomed-in view still has one source pixel per output
|
||||
/// pixel, never more than the source, capped by <see cref="RenderOptions.MaxWorkingLongEdge"/>.
|
||||
/// Long edge to decode source frames at: enough that the most zoomed-in view (keyframe zoom × stabilisation zoom ×
|
||||
/// straighten cover zoom) still has one source pixel per output pixel, never more than the source, capped by
|
||||
/// <see cref="RenderOptions.MaxWorkingLongEdge"/>. Quarter turns do not change the long edge.
|
||||
/// </summary>
|
||||
public int WorkingLongEdge
|
||||
{
|
||||
@@ -105,46 +113,27 @@ public sealed class RenderGeometry
|
||||
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(sourceWidth);
|
||||
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(sourceHeight);
|
||||
var warnings = new List<RenderWarning>();
|
||||
var orientation = OrientationOf(camera, sourceWidth, sourceHeight, warnings);
|
||||
var crop = NormalisedCrop(camera.Crop, warnings);
|
||||
if (!AspectRatio.TryParse(camera.Aspect.Resolve("source"), out var aspect))
|
||||
{
|
||||
if (!AspectRatio.TryParse(camera.Aspect.Resolve("source"), out _))
|
||||
warnings.Add(new RenderWarning("Export.Render.AspectIgnored", camera.Aspect.Resolve(string.Empty)));
|
||||
aspect = AspectRatio.Source;
|
||||
}
|
||||
var fixedSize = export.Width > 0 && export.Height > 0;
|
||||
var ratio = fixedSize ? export.Width / (double)export.Height : aspect.Resolve(sourceWidth, sourceHeight);
|
||||
if (fixedSize && !aspect.IsSource && double.Abs(aspect.Resolve(sourceWidth, sourceHeight) / ratio - 1) > 0.01)
|
||||
warnings.Add(new RenderWarning("Export.Render.AspectFromPreset", aspect.ToString(), string.Create(CultureInfo.InvariantCulture, $"{export.Width}:{export.Height}")));
|
||||
var cropPixels = new RectD(crop.X * sourceWidth, crop.Y * sourceHeight, crop.Width * sourceWidth, crop.Height * sourceHeight);
|
||||
// The chosen proportions win: a fixed-size preset is a box the aspect is fitted in (RenderSize).
|
||||
var ratio = RenderSize.Ratio(camera, export, sourceWidth, sourceHeight);
|
||||
var (viewWidth, viewHeight) = (orientation.ViewWidth, orientation.ViewHeight);
|
||||
var cropPixels = new RectD(crop.X * viewWidth, crop.Y * viewHeight, crop.Width * viewWidth, crop.Height * viewHeight);
|
||||
var fitted = AspectRatio.FitCrop(cropPixels, ratio, camera.OffsetX, camera.OffsetY);
|
||||
|
||||
int width, height;
|
||||
if (fixedSize) (width, height) = (export.Width, export.Height);
|
||||
else if (export.Width > 0) (width, height) = (export.Width, (int)double.Round(export.Width / ratio));
|
||||
else if (export.Height > 0) (width, height) = ((int)double.Round(export.Height * ratio), export.Height);
|
||||
else
|
||||
{
|
||||
var scale = maxWorkingLongEdge > 0 ? double.Min(1, maxWorkingLongEdge / double.Max(fitted.Width, fitted.Height)) : 1;
|
||||
(width, height) = ((int)double.Round(fitted.Width * scale), (int)double.Round(fitted.Height * scale));
|
||||
}
|
||||
if (!VideoCodecInfo.IsManaged(export.Codec))
|
||||
{
|
||||
width &= ~1;
|
||||
height &= ~1;
|
||||
}
|
||||
width = int.Max(VideoCodecInfo.IsManaged(export.Codec) ? 1 : 2, width);
|
||||
height = int.Max(VideoCodecInfo.IsManaged(export.Codec) ? 1 : 2, height);
|
||||
var (width, height) = RenderSize.Of(camera, export, sourceWidth, sourceHeight, maxWorkingLongEdge);
|
||||
|
||||
var cameraPath = Camera(camera.Keyframes, warnings);
|
||||
var maxZoom = (cameraPath?.Keyframes.Max(k => k.Zoom) ?? 1) * stabilizationZoom;
|
||||
return new RenderGeometry(cameraPath, crop, camera.OffsetX, camera.OffsetY, stabilizationZoom, sourceWidth, sourceHeight, width, height,
|
||||
var maxZoom = (cameraPath?.Keyframes.Max(k => k.Zoom) ?? 1) * stabilizationZoom * orientation.CoverZoom;
|
||||
return new RenderGeometry(cameraPath, orientation, crop, camera.OffsetX, camera.OffsetY, stabilizationZoom, sourceWidth, sourceHeight, width, height,
|
||||
fitted.Width, fitted.Height, maxZoom, maxWorkingLongEdge, warnings, ratio);
|
||||
}
|
||||
|
||||
/// <summary>The same geometry rendering into a different output size (e.g. capped by the encoder).</summary>
|
||||
public RenderGeometry WithOutputSize(int width, int height) => width == OutputWidth && height == OutputHeight
|
||||
? this
|
||||
: new RenderGeometry(_camera, _crop, _offsetX, _offsetY, _stabilizationZoom, SourceWidth, SourceHeight, width, height, _fittedWidth, _fittedHeight,
|
||||
: new RenderGeometry(_camera, _orientation, _crop, _offsetX, _offsetY, _stabilizationZoom, SourceWidth, SourceHeight, width, height, _fittedWidth, _fittedHeight,
|
||||
_maxZoom, _maxWorkingLongEdge, Warnings, OutputRatio);
|
||||
|
||||
/// <summary>Camera pose at a (fractional) source position, including the stabilisation zoom.</summary>
|
||||
@@ -154,14 +143,21 @@ public sealed class RenderGeometry
|
||||
return pose with { Zoom = pose.Zoom * _stabilizationZoom };
|
||||
}
|
||||
|
||||
/// <summary>Geometry of one decoded frame of <paramref name="frameWidth"/>×<paramref name="frameHeight"/> pixels.</summary>
|
||||
/// <summary>
|
||||
/// Geometry of one decoded frame of <paramref name="frameWidth"/>×<paramref name="frameHeight"/> pixels: the crop in the
|
||||
/// view frame, and the warp <c>stab⁻¹ · O⁻¹ · V</c> from the camera view (the pose in the view frame) through the view
|
||||
/// frame to the stabilised and then the decoded frame.
|
||||
/// </summary>
|
||||
/// <param name="stabilization">The frame's stabiliser correction at this frame size (source → stabilised).</param>
|
||||
public GeometrySettings For(int frameWidth, int frameHeight, double sourcePosition, Affine2D stabilization)
|
||||
{
|
||||
var view = VirtualCamera.ViewToSource(PoseAt(sourcePosition), frameWidth, frameHeight);
|
||||
var orientation = _orientation.Resized(frameWidth, frameHeight);
|
||||
var (viewWidth, viewHeight) = (orientation.ViewWidth, orientation.ViewHeight);
|
||||
var view = VirtualCamera.ViewToSource(PoseAt(sourcePosition), viewWidth, viewHeight);
|
||||
if (!orientation.IsIdentity) view = orientation.ViewToFrame * view;
|
||||
return new GeometrySettings
|
||||
{
|
||||
Crop = new RectD(_crop.X * frameWidth, _crop.Y * frameHeight, _crop.Width * frameWidth, _crop.Height * frameHeight),
|
||||
Crop = new RectD(_crop.X * viewWidth, _crop.Y * viewHeight, _crop.Width * viewWidth, _crop.Height * viewHeight),
|
||||
Aspect = AspectRatio.Source,
|
||||
OffsetX = _offsetX,
|
||||
OffsetY = _offsetY,
|
||||
@@ -181,7 +177,7 @@ public sealed class RenderGeometry
|
||||
{
|
||||
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(frameWidth);
|
||||
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(frameHeight);
|
||||
return Affine2D.Identity;
|
||||
return _orientation.Resized(frameWidth, frameHeight).ViewToFrame;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -192,12 +188,14 @@ public sealed class RenderGeometry
|
||||
{
|
||||
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(outputWidth);
|
||||
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(outputHeight);
|
||||
var orientation = _orientation.Resized(frameWidth, frameHeight);
|
||||
return new GeometrySettings
|
||||
{
|
||||
Crop = RectD.FromSize(orientation.ViewWidth, orientation.ViewHeight),
|
||||
Aspect = AspectRatio.Source,
|
||||
OutputWidth = outputWidth,
|
||||
OutputHeight = outputHeight,
|
||||
Warp = ViewToFrame(frameWidth, frameHeight),
|
||||
Warp = orientation.ViewToFrame,
|
||||
Border = BorderMode.Clamp,
|
||||
};
|
||||
}
|
||||
@@ -212,21 +210,34 @@ public sealed class RenderGeometry
|
||||
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(frameWidth);
|
||||
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(frameHeight);
|
||||
if (plan is not { } p) return new RectD(0, 0, 1, 1);
|
||||
var toView = ViewToFrame(frameWidth, frameHeight).Invert() * p.OutputToSource;
|
||||
var orientation = _orientation.Resized(frameWidth, frameHeight);
|
||||
var toView = orientation.FrameToView * p.OutputToSource;
|
||||
double left = double.MaxValue, top = double.MaxValue, right = double.MinValue, bottom = double.MinValue;
|
||||
foreach (var (u, v) in new[] { (-0.5, -0.5), (p.OutputWidth - 0.5, -0.5), (-0.5, p.OutputHeight - 0.5), (p.OutputWidth - 0.5, p.OutputHeight - 0.5) })
|
||||
{
|
||||
var (x, y) = toView.Apply(u, v);
|
||||
(left, top, right, bottom) = (double.Min(left, x + 0.5), double.Min(top, y + 0.5), double.Max(right, x + 0.5), double.Max(bottom, y + 0.5));
|
||||
}
|
||||
var (viewWidth, viewHeight) = ViewSizeOf(frameWidth, frameHeight);
|
||||
var (viewWidth, viewHeight) = (orientation.ViewWidth, orientation.ViewHeight);
|
||||
left = double.Clamp(left / viewWidth, 0, 1);
|
||||
top = double.Clamp(top / viewHeight, 0, 1);
|
||||
return new RectD(left, top, double.Clamp(right / viewWidth, 0, 1) - left, double.Clamp(bottom / viewHeight, 0, 1) - top);
|
||||
}
|
||||
|
||||
/// <summary>The view frame's size for a decoded frame of the given size.</summary>
|
||||
private static (int Width, int Height) ViewSizeOf(int frameWidth, int frameHeight) => (frameWidth, frameHeight);
|
||||
/// <summary>
|
||||
/// The camera's orientation as rendered: a rotation that is not a quarter turn counts as none
|
||||
/// (<c>Export.Render.RotationIgnored</c>), a straighten beyond ±15° (or not finite) is clamped
|
||||
/// (<c>Export.Render.StraightenClamped</c>).
|
||||
/// </summary>
|
||||
private static FrameOrientation OrientationOf(CameraSettings camera, int sourceWidth, int sourceHeight, List<RenderWarning> warnings)
|
||||
{
|
||||
if (!FrameOrientation.IsValidRotate(camera.Rotate))
|
||||
warnings.Add(new RenderWarning("Export.Render.RotationIgnored", camera.Rotate.ToString(CultureInfo.InvariantCulture)));
|
||||
if (!FrameOrientation.IsValidStraighten(camera.Straighten))
|
||||
warnings.Add(new RenderWarning("Export.Render.StraightenClamped", camera.Straighten.ToString(CultureInfo.InvariantCulture),
|
||||
FrameOrientation.StraightenOf(camera.Straighten).ToString(CultureInfo.InvariantCulture)));
|
||||
return FrameOrientation.Of(camera, sourceWidth, sourceHeight);
|
||||
}
|
||||
|
||||
private static RectD NormalisedCrop(double[]? crop, List<RenderWarning> warnings)
|
||||
{
|
||||
|
||||
@@ -137,9 +137,9 @@ internal sealed class SequenceAnalyser(ChronosDatabase db, ProcessingOptions opt
|
||||
}
|
||||
try
|
||||
{
|
||||
// The frame rate follows the user's "fit to N seconds" when one is set (BRIEF §10.3).
|
||||
// The frame rate follows the user's "fit to N seconds" when one is set (BRIEF §10.3), the aspect the camera's turns.
|
||||
changed = await projects.UpdateLockedAsync(paths, sequence,
|
||||
settings => Advisor.Apply(settings, Advisor.Recommend(light, report, context with { TargetDurationSeconds = settings.Timing.FitToSeconds })),
|
||||
settings => Advisor.Apply(settings, Advisor.Recommend(light, report, context.ForCamera(settings.Camera) with { TargetDurationSeconds = settings.Timing.FitToSeconds })),
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (InvalidDataException ex)
|
||||
|
||||
@@ -326,6 +326,27 @@ public sealed class AdvisorTests(TestContext context)
|
||||
Assert.Equal("3840x2160", Value(wide, AdvisorParameters.Resolution));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TheAspectIsAdvisedOnTheTurnedView()
|
||||
{
|
||||
var (light, sequence) = Inputs(new SyntheticFrameSource(DeepScenes.HolyGrail()));
|
||||
var turned = sequence.ForCamera(new CameraSettings { Rotate = 90 });
|
||||
Assert.Equal(90, turned.Rotate);
|
||||
var portrait = Advisor.Recommend(light, null, turned);
|
||||
Assert.Equal("9:16", Value(portrait, AdvisorParameters.Aspect));
|
||||
Assert.Equal(AdvisorKeys.AspectPortrait, portrait.Find(AdvisorParameters.Aspect)!.ReasonKey);
|
||||
Assert.Equal("16:9", Value(Advisor.Recommend(light, null, sequence with { Rotate = 180 }), AdvisorParameters.Aspect));
|
||||
|
||||
// A portrait source turned a quarter is landscape again, its reason naming the view's size.
|
||||
var landscape = Advisor.Recommend(light, null, sequence with { SourceWidth = 4000, SourceHeight = 6000, Rotate = 270 });
|
||||
Assert.Equal("16:9", Value(landscape, AdvisorParameters.Aspect));
|
||||
Assert.SequenceEqual([6000.0, 4000.0, 84.0], landscape.Find(AdvisorParameters.Aspect)!.ReasonArgs.Select(Convert.ToDouble));
|
||||
|
||||
// Applied to a turned project, the advice matches what a turn of the advised aspect gives.
|
||||
var project = Advisor.Apply(new ProjectSettings { Camera = new CameraSettings { Rotate = 90 } }, portrait);
|
||||
Assert.Equal("9:16", project.Camera.Aspect.AutoValue);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task RecommendationsAreDeterministic()
|
||||
{
|
||||
|
||||
@@ -45,6 +45,18 @@ public sealed class Affine2DTests
|
||||
Assert.Within(double.Cos(0.7), double.Sin(0.7), vx, vy, 1e-15);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AQuarterTurnRotationIsNotExact()
|
||||
{
|
||||
// Why FrameOrientation writes its quarter turns as integer matrices: cos(π/2) is about 6e-17, not 0.
|
||||
var rotation = Affine2D.Rotation(double.Pi / 2);
|
||||
Assert.NotEqual(0.0, rotation.M11);
|
||||
Assert.Approximately(0, rotation.M11, 1e-16);
|
||||
var turn = new Affine2D(0, -1, 1, 0, 0, 0);
|
||||
Assert.Equal(Affine2D.Identity, turn * turn * turn * turn);
|
||||
Assert.Equal(new Affine2D(0, 1, -1, 0, 0, 0), turn.Invert());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SimilarityDecomposes()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,174 @@
|
||||
using Chronos.Core.Imaging.Geometry;
|
||||
using Chronos.Testing;
|
||||
|
||||
namespace Chronos.Core.Tests.Geometry;
|
||||
|
||||
public sealed class CropFrameTests
|
||||
{
|
||||
/// <summary>9:16 on a 6000×4000 view, in normalised units: 0.5625 × 4000 / 6000.</summary>
|
||||
private const double Portrait = 0.375;
|
||||
|
||||
[Test]
|
||||
public void TheLargestBoxIsCentredAndLocked()
|
||||
{
|
||||
AssertBox(new RectD(0.3125, 0, 0.375, 1), CropFrame.Largest(Portrait));
|
||||
AssertBox(new RectD(0, 0, 1, 1), CropFrame.Largest(1));
|
||||
AssertBox(new RectD(0, 0.25, 1, 0.5), CropFrame.Largest(2));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => CropFrame.Largest(0));
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => CropFrame.Largest(double.NaN));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void StoredCropsReadAsTheRenderReadsThem()
|
||||
{
|
||||
AssertBox(new RectD(0, 0, 1, 1), CropFrame.Of(null));
|
||||
AssertBox(new RectD(0, 0, 1, 1), CropFrame.Of([0.1, 0.2]));
|
||||
AssertBox(new RectD(0, 0, 1, 1), CropFrame.Of([0.1, double.NaN, 0.3, 0.3]));
|
||||
AssertBox(new RectD(0, 0, 1, 1), CropFrame.Of([0.5, 0.5, 0.0001, 0.5]));
|
||||
AssertBox(new RectD(0.5, 0, 0.5, 0.25), CropFrame.Of([0.5, -1, 0.8, 0.25]));
|
||||
Assert.True(CropFrame.IsWhole(null));
|
||||
Assert.True(CropFrame.IsWhole([0, 0, 1, 1]));
|
||||
Assert.False(CropFrame.IsWhole([0, 0, 0.9, 1]));
|
||||
Assert.Null(CropFrame.ToCrop(new RectD(0, 0, 1, 1)));
|
||||
Assert.SequenceEqual([0.25, 0.125, 0.5, 0.5], CropFrame.ToCrop(new RectD(0.25, 0.125, 0.5, 0.5))!);
|
||||
// Written on a 12-decimal grid, never −0.
|
||||
Assert.SequenceEqual([0.333333333333, 0, 0.5, 0.5], CropFrame.ToCrop(new RectD(1 / 3.0, -0.0, 0.5, 0.5))!);
|
||||
Assert.False(double.IsNegative(CropFrame.ToCrop(new RectD(-0.0, 0.2, 0.5, 0.5))![0]));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void LockedBakesTheOffsetsOfALegacyCrop()
|
||||
{
|
||||
// A free crop of the whole view with the box pushed right: the render's FitCrop, normalised.
|
||||
AssertBox(new RectD(0.625, 0, 0.375, 1), CropFrame.Locked(null, Portrait, offsetX: 1));
|
||||
AssertBox(new RectD(0.3125, 0, 0.375, 1), CropFrame.Locked(null, Portrait, double.NaN, double.NaN));
|
||||
var box = CropFrame.Locked([0.2, 0.1, 0.4, 0.8], Portrait, -1, 0);
|
||||
AssertBox(new RectD(0.2, 0.1, 0.3, 0.8), box);
|
||||
// A box the editor wrote comes back unchanged.
|
||||
AssertBox(box, CropFrame.Locked(CropFrame.ToCrop(box), Portrait));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MovingStaysInsideAndKeepsTheSize()
|
||||
{
|
||||
var box = new RectD(0.3, 0.2, 0.3, 0.4);
|
||||
AssertBox(new RectD(0.4, 0.15, 0.3, 0.4), CropFrame.Move(box, 0.1, -0.05));
|
||||
AssertBox(new RectD(0.7, 0, 0.3, 0.4), CropFrame.Move(box, 2, -1));
|
||||
AssertBox(new RectD(0, 0.6, 0.3, 0.4), CropFrame.Move(box, -5, 5));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[Case(CropHandle.BottomRight, 0.9, 0.95, false)]
|
||||
[Case(CropHandle.TopLeft, 0.0, 0.0, false)]
|
||||
[Case(CropHandle.TopRight, 2.0, -3.0, false)]
|
||||
[Case(CropHandle.Left, 0.31, 0.5, false)]
|
||||
[Case(CropHandle.Top, 0.5, -1.0, false)]
|
||||
[Case(CropHandle.Right, 0.4, 0.5, false)]
|
||||
[Case(CropHandle.Bottom, 0.5, 0.3, false)]
|
||||
[Case(CropHandle.BottomLeft, 0.1, 0.9, true)]
|
||||
[Case(CropHandle.Right, 5.0, 0.5, true)]
|
||||
[Case(CropHandle.TopLeft, 0.45, 0.45, true)]
|
||||
public void ResizingKeepsTheRatioTheMinimumAndTheFrame(CropHandle handle, double x, double y, bool aboutCentre)
|
||||
{
|
||||
var start = new RectD(0.35, 0.3, 0.15, 0.4);
|
||||
var box = CropFrame.ResizeFrom(start, handle, x, y, Portrait, aboutCentre);
|
||||
Assert.Approximately(Portrait, box.Width / box.Height, 1e-12, "the ratio stays locked");
|
||||
Assert.True(box.X >= -1e-12 && box.Y >= -1e-12 && box.Right <= 1 + 1e-12 && box.Bottom <= 1 + 1e-12, $"{box} lies inside the view");
|
||||
Assert.GreaterOrEqual(box.Height, CropFrame.Largest(Portrait).Height * CropFrame.MinimumShare - 1e-12);
|
||||
if (aboutCentre)
|
||||
{
|
||||
Assert.Approximately(start.CenterX, box.CenterX, 1e-12);
|
||||
Assert.Approximately(start.CenterY, box.CenterY, 1e-12);
|
||||
return;
|
||||
}
|
||||
// The opposite corner or edge stays where it was; an edge handle keeps the box centred on the other axis.
|
||||
switch (handle)
|
||||
{
|
||||
case CropHandle.BottomRight:
|
||||
Assert.Within(start.X, start.Y, box.X, box.Y, 1e-12);
|
||||
break;
|
||||
case CropHandle.TopLeft:
|
||||
Assert.Within(start.Right, start.Bottom, box.Right, box.Bottom, 1e-12);
|
||||
break;
|
||||
case CropHandle.TopRight:
|
||||
Assert.Within(start.X, start.Bottom, box.X, box.Bottom, 1e-12);
|
||||
break;
|
||||
case CropHandle.Left:
|
||||
Assert.Within(start.Right, start.CenterY, box.Right, box.CenterY, 1e-12);
|
||||
break;
|
||||
case CropHandle.Right:
|
||||
Assert.Within(start.X, start.CenterY, box.X, box.CenterY, 1e-12);
|
||||
break;
|
||||
case CropHandle.Top:
|
||||
Assert.Within(start.CenterX, start.Bottom, box.CenterX, box.Bottom, 1e-12);
|
||||
break;
|
||||
case CropHandle.Bottom:
|
||||
Assert.Within(start.CenterX, start.Y, box.CenterX, box.Y, 1e-12);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ResizingFollowsThePointer()
|
||||
{
|
||||
var start = new RectD(0.35, 0.3, 0.15, 0.4);
|
||||
// Bottom-right to (0.5375, 0.8): both axes ask for a height of 0.5 (0.1875 / 0.375 and 0.8 − 0.3).
|
||||
AssertBox(new RectD(0.35, 0.3, 0.1875, 0.5), CropFrame.ResizeFrom(start, CropHandle.BottomRight, 0.5375, 0.8, Portrait));
|
||||
// Dragged past the anchor: the minimum box.
|
||||
var least = CropFrame.ResizeFrom(start, CropHandle.BottomRight, 0, 0, Portrait);
|
||||
Assert.Approximately(0.1, least.Height, 1e-12);
|
||||
// Far out: as large as fits from the anchor (0.7 of height below it).
|
||||
Assert.Approximately(0.7, CropFrame.ResizeFrom(start, CropHandle.BottomRight, 1, 1, Portrait).Height, 1e-12);
|
||||
// The inside and no handle leave the box.
|
||||
Assert.Equal(start, CropFrame.ResizeFrom(start, CropHandle.Inside, 0.9, 0.9, Portrait));
|
||||
Assert.Equal(start, CropFrame.ResizeFrom(start, CropHandle.None, 0.9, 0.9, Portrait));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ClampAndRefitKeepTheCentre()
|
||||
{
|
||||
// A 16:9 box (in units of a square view) refitted to 1:1 keeps its centre and its area.
|
||||
var wide = new RectD(0.2, 0.3, 0.64, 0.36);
|
||||
var square = CropFrame.Refit(wide, 1);
|
||||
Assert.Approximately(square.Width, square.Height, 1e-12);
|
||||
Assert.Within(wide.CenterX, wide.CenterY, square.CenterX, square.CenterY, 1e-12);
|
||||
Assert.Approximately(wide.Width * wide.Height, square.Width * square.Height, 1e-12);
|
||||
|
||||
// Near an edge the refitted box moves inside rather than leaving the view.
|
||||
var edge = CropFrame.Refit(new RectD(0.8, 0.1, 0.2, 0.8), 1);
|
||||
Assert.Approximately(1, edge.Right, 1e-12);
|
||||
Assert.Approximately(edge.Width, edge.Height, 1e-12);
|
||||
|
||||
// Too large or too small: the largest box, the minimum.
|
||||
AssertBox(CropFrame.Largest(Portrait), CropFrame.Clamp(new RectD(-1, -1, 3, 3), Portrait));
|
||||
Assert.Approximately(0.1, CropFrame.Clamp(new RectD(0.5, 0.5, 0.001, 0.001), Portrait).Height, 1e-12);
|
||||
AssertBox(CropFrame.Largest(Portrait), CropFrame.Clamp(new RectD(double.NaN, 0, 0, 0), Portrait));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RowsRoundTrip()
|
||||
{
|
||||
foreach (var (size, x, y) in new[] { (1.0, 0.0, 0.0), (0.5, -1.0, 1.0), (0.8, 0.25, -0.5), (0.1, 1.0, 0.3) })
|
||||
{
|
||||
var box = CropFrame.FromRows(size, x, y, Portrait);
|
||||
Assert.Approximately(Portrait, box.Width / box.Height, 1e-12);
|
||||
var rows = CropFrame.ToRows(box, Portrait);
|
||||
Assert.Approximately(size, rows.Size, 1e-12);
|
||||
Assert.Approximately(x, rows.X, 1e-12);
|
||||
// The largest portrait box fills the height: no vertical slack, so y reads 0.
|
||||
Assert.Approximately(size == 1 ? 0 : y, rows.Y, 1e-12);
|
||||
AssertBox(box, CropFrame.FromRows(rows.Size, rows.X, rows.Y, Portrait));
|
||||
}
|
||||
AssertBox(CropFrame.Largest(Portrait), CropFrame.FromRows(double.NaN, double.NaN, 0, Portrait));
|
||||
Assert.True(CropFrame.HasSlack(CropFrame.Largest(Portrait), horizontal: true));
|
||||
Assert.False(CropFrame.HasSlack(CropFrame.Largest(Portrait), horizontal: false));
|
||||
}
|
||||
|
||||
private static void AssertBox(RectD expected, RectD actual)
|
||||
{
|
||||
Assert.Approximately(expected.X, actual.X, 1e-12, $"x of {actual}");
|
||||
Assert.Approximately(expected.Y, actual.Y, 1e-12, $"y of {actual}");
|
||||
Assert.Approximately(expected.Width, actual.Width, 1e-12, $"width of {actual}");
|
||||
Assert.Approximately(expected.Height, actual.Height, 1e-12, $"height of {actual}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
using Chronos.Core.Imaging;
|
||||
using Chronos.Core.Imaging.Geometry;
|
||||
using Chronos.Core.Project;
|
||||
using Chronos.Core.Render;
|
||||
using Chronos.Core.Tests.Render;
|
||||
using Chronos.Testing;
|
||||
|
||||
namespace Chronos.Core.Tests.Geometry;
|
||||
|
||||
public sealed class FrameOrientationTests
|
||||
{
|
||||
private const int Width = 48;
|
||||
private const int Height = 32;
|
||||
|
||||
[Theory]
|
||||
[Case(90, false, false)]
|
||||
[Case(180, false, false)]
|
||||
[Case(270, false, false)]
|
||||
[Case(-90, false, false)]
|
||||
[Case(0, true, false)]
|
||||
[Case(0, false, true)]
|
||||
[Case(90, true, false)]
|
||||
[Case(270, false, true)]
|
||||
[Case(180, true, true)]
|
||||
public void TurnsMirrorsAndFlipsPermuteThePixelsExactly(int rotate, bool mirror, bool flip)
|
||||
{
|
||||
using var frame = Texture(Width, Height, seed: 7);
|
||||
var camera = new CameraSettings { Rotate = rotate, Mirror = mirror, Flip = flip };
|
||||
var geometry = RenderGeometry.Create(camera, RenderFixture.Preset("png-sequence", 0, 0), Width, Height);
|
||||
var turns = FrameOrientation.TurnsOf(rotate);
|
||||
Assert.Equal(turns % 2 == 1 ? (Height, Width) : (Width, Height), (geometry.OutputWidth, geometry.OutputHeight));
|
||||
Assert.Equal((geometry.OutputWidth, geometry.OutputHeight), (geometry.ViewWidth, geometry.ViewHeight));
|
||||
Assert.Empty(geometry.Warnings);
|
||||
|
||||
using var view = GeometryStage.Apply(frame, geometry.For(Width, Height, 0, Affine2D.Identity));
|
||||
using var expected = Permuted(frame, turns, mirror, flip);
|
||||
Assert.Equal((expected.Width, expected.Height), (view.Width, view.Height));
|
||||
Assert.True(expected.Data.SequenceEqual(view.Data), "the view is an exact permutation of the frame");
|
||||
|
||||
// The editor's Source framing (the whole view frame) is the same permutation.
|
||||
using var whole = GeometryStage.Apply(frame, geometry.ViewFor(Width, Height, view.Width, view.Height));
|
||||
Assert.True(expected.Data.SequenceEqual(whole.Data));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void QuarterTurnsAreExactIntegerMaps()
|
||||
{
|
||||
Assert.Equal(new Affine2D(0, -1, 1, 0, Height - 1, 0), FrameOrientation.Of(90, false, false, 0, Width, Height).FrameToView);
|
||||
Assert.Equal(new Affine2D(-1, 0, 0, -1, Width - 1, Height - 1), FrameOrientation.Of(180, false, false, 0, Width, Height).FrameToView);
|
||||
Assert.Equal(new Affine2D(0, 1, -1, 0, 0, Width - 1), FrameOrientation.Of(270, false, false, 0, Width, Height).FrameToView);
|
||||
foreach (var rotate in new[] { 0, 90, 180, 270 })
|
||||
{
|
||||
foreach (var (mirror, flip) in new[] { (false, false), (true, false), (false, true), (true, true) })
|
||||
{
|
||||
var o = FrameOrientation.Of(rotate, mirror, flip, 0, Width, Height);
|
||||
Assert.Equal(Affine2D.Identity, o.ViewToFrame * o.FrameToView);
|
||||
Assert.Equal(Affine2D.Identity, o.FrameToView * o.ViewToFrame);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void OrientationAlgebraHolds()
|
||||
{
|
||||
// mirror² = flip² = I.
|
||||
var mirror = FrameOrientation.Of(0, true, false, 0, Width, Height).FrameToView;
|
||||
var flip = FrameOrientation.Of(0, false, true, 0, Width, Height).FrameToView;
|
||||
Assert.Equal(Affine2D.Identity, mirror * mirror);
|
||||
Assert.Equal(Affine2D.Identity, flip * flip);
|
||||
|
||||
// turn⁴ = I: each quarter turn takes the previous view (its size swapped) as its frame.
|
||||
var turn = Affine2D.Identity;
|
||||
var (w, h) = (Width, Height);
|
||||
for (var k = 0; k < 4; k++)
|
||||
{
|
||||
turn = FrameOrientation.Of(90, false, false, 0, w, h).FrameToView * turn;
|
||||
(w, h) = (h, w);
|
||||
if (k < 3) Assert.NotEqual(Affine2D.Identity, turn);
|
||||
}
|
||||
Assert.Equal(Affine2D.Identity, turn);
|
||||
|
||||
// Parity: a turn right then a mirror is a turn left then a flip, the transpose; mirror and flip together are a half turn.
|
||||
var transpose = new Affine2D(0, 1, 1, 0, 0, 0);
|
||||
Assert.Equal(transpose, FrameOrientation.Of(90, true, false, 0, Width, Height).FrameToView);
|
||||
Assert.Equal(transpose, FrameOrientation.Of(270, false, true, 0, Width, Height).FrameToView);
|
||||
Assert.Equal(FrameOrientation.Of(180, false, false, 0, Width, Height).FrameToView, FrameOrientation.Of(0, true, true, 0, Width, Height).FrameToView);
|
||||
Assert.True(FrameOrientation.Of(360, false, false, 0, Width, Height).IsIdentity);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[Case(7.5, 0)]
|
||||
[Case(-12.0, 0)]
|
||||
[Case(15.0, 90)]
|
||||
public void StraightenMatchesTheAnalyticRamp(double degrees, int rotate)
|
||||
{
|
||||
using var ramp = new ImageBuffer(96, 64, 1);
|
||||
for (var y = 0; y < 64; y++)
|
||||
for (var x = 0; x < 96; x++)
|
||||
ramp[0, x, y] = 0.1f + 0.01f * x + 0.005f * y;
|
||||
var orientation = FrameOrientation.Of(rotate, false, false, degrees, 96, 64);
|
||||
using var view = GeometryStage.Apply(ramp, new GeometrySettings
|
||||
{
|
||||
Crop = RectD.FromSize(orientation.ViewWidth, orientation.ViewHeight),
|
||||
OutputWidth = orientation.ViewWidth,
|
||||
OutputHeight = orientation.ViewHeight,
|
||||
Warp = orientation.ViewToFrame,
|
||||
});
|
||||
Assert.Equal((orientation.ViewWidth, orientation.ViewHeight), (view.Width, view.Height));
|
||||
var checkedPixels = 0;
|
||||
for (var v = 0; v < view.Height; v++)
|
||||
{
|
||||
for (var u = 0; u < view.Width; u++)
|
||||
{
|
||||
var (sx, sy) = orientation.ViewToFrame.Apply(u, v);
|
||||
// Every sample lies inside the frame's pixel centres (the cover zoom), within rounding.
|
||||
Assert.True(sx >= -1e-9 && sx <= 95 + 1e-9 && sy >= -1e-9 && sy <= 63 + 1e-9, $"view ({u}, {v}) samples ({sx}, {sy})");
|
||||
if (sx < 1 || sx >= 93 || sy < 1 || sy >= 61) continue; // all 4×4 taps inside: the ramp is reproduced exactly
|
||||
Assert.Approximately(0.1 + 0.01 * sx + 0.005 * sy, view[0, u, v], 2e-6);
|
||||
checkedPixels++;
|
||||
}
|
||||
}
|
||||
Assert.Greater(checkedPixels, view.Width * view.Height / 2);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[Case(15.0, 0)]
|
||||
[Case(-15.0, 0)]
|
||||
[Case(15.0, 90)]
|
||||
[Case(-15.0, 270)]
|
||||
[Case(0.3, 0)]
|
||||
public void StraightenNeverShowsABorder(double degrees, int rotate)
|
||||
{
|
||||
using var frame = Texture(96, 64, seed: 3);
|
||||
var camera = new CameraSettings { Rotate = rotate, Straighten = degrees };
|
||||
var geometry = RenderGeometry.Create(camera, RenderFixture.Preset("png-sequence", 0, 0), 96, 64);
|
||||
var settings = geometry.For(96, 64, 0, Affine2D.Identity) with { Border = BorderMode.Transparent };
|
||||
using var view = GeometryStage.Apply(frame, settings);
|
||||
var alpha = view.Plane(3).ToArray();
|
||||
Assert.GreaterOrEqual(alpha.Min(), 1 - 1e-6, "every output pixel is fully covered");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CoverZoomFollowsTheFormula()
|
||||
{
|
||||
Assert.Equal(1.0, FrameOrientation.CoverZoomOf(0, 1920, 1080));
|
||||
var (sin, cos) = double.SinCos(15 * double.Pi / 180);
|
||||
Assert.Approximately(cos + 16 / 9.0 * sin, FrameOrientation.CoverZoomOf(15, 1920, 1080), 1e-12);
|
||||
Assert.Approximately(cos + 16 / 9.0 * sin, FrameOrientation.CoverZoomOf(-15, 1080, 1920), 1e-12);
|
||||
// On the pixel-centre extents of the view.
|
||||
var o = FrameOrientation.Of(0, false, false, 15, 1920, 1080);
|
||||
Assert.Approximately(cos + 1919 / 1079.0 * sin, o.CoverZoom, 1e-12);
|
||||
Assert.Equal(o.CoverZoom, FrameOrientation.Of(90, false, false, 15, 1080, 1920).CoverZoom);
|
||||
Assert.Equal(1.0, FrameOrientation.Of(90, true, false, 0, 1920, 1080).CoverZoom);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ValuesAreNormalisedAndResizedKeepsThem()
|
||||
{
|
||||
Assert.Equal(3, FrameOrientation.TurnsOf(-90));
|
||||
Assert.Equal(1, FrameOrientation.TurnsOf(450));
|
||||
Assert.Equal(0, FrameOrientation.TurnsOf(45));
|
||||
Assert.Equal(15.0, FrameOrientation.StraightenOf(40));
|
||||
Assert.Equal(-15.0, FrameOrientation.StraightenOf(-15.5));
|
||||
Assert.Equal(0.0, FrameOrientation.StraightenOf(double.NaN));
|
||||
Assert.False(FrameOrientation.IsValidStraighten(double.PositiveInfinity));
|
||||
|
||||
var full = FrameOrientation.Of(-90, true, false, 4, 6000, 4000);
|
||||
var small = full.Resized(600, 400);
|
||||
Assert.Equal((3, true, false, 4.0), (small.Turns, small.Mirror, small.Flip, small.Straighten));
|
||||
Assert.Equal((400, 600), (small.ViewWidth, small.ViewHeight));
|
||||
Assert.Same(full, full.Resized(6000, 4000));
|
||||
Assert.True(FrameOrientation.Identity(10, 10).IsIdentity);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InvalidValuesRenderAsNoneWithAWarning()
|
||||
{
|
||||
var export = RenderFixture.Preset("png-sequence", 0, 0);
|
||||
var skewed = RenderGeometry.Create(new CameraSettings { Rotate = 45 }, export, 96, 64);
|
||||
Assert.True(skewed.Orientation.IsIdentity);
|
||||
Assert.Equal((96, 64), (skewed.OutputWidth, skewed.OutputHeight));
|
||||
Assert.Equal("Export.Render.RotationIgnored", skewed.Warnings.Single().Key);
|
||||
Assert.SequenceEqual(["45"], skewed.Warnings.Single().Args);
|
||||
|
||||
var steep = RenderGeometry.Create(new CameraSettings { Straighten = 20 }, export, 96, 64);
|
||||
Assert.Equal(15.0, steep.Orientation.Straighten);
|
||||
Assert.Equal("Export.Render.StraightenClamped", steep.Warnings.Single().Key);
|
||||
Assert.SequenceEqual(["20", "15"], steep.Warnings.Single().Args);
|
||||
|
||||
var broken = RenderGeometry.Create(new CameraSettings { Straighten = double.NaN }, export, 96, 64);
|
||||
Assert.True(broken.Orientation.IsIdentity);
|
||||
Assert.SequenceEqual(["NaN", "0"], broken.Warnings.Single().Args);
|
||||
}
|
||||
|
||||
/// <summary>A seeded RGB texture.</summary>
|
||||
internal static ImageBuffer Texture(int width, int height, int seed)
|
||||
{
|
||||
var random = new Random(seed);
|
||||
var image = new ImageBuffer(width, height, 3);
|
||||
for (var i = 0; i < image.Data.Length; i++) image.Data[i] = (float)random.NextDouble();
|
||||
return image;
|
||||
}
|
||||
|
||||
/// <summary>The frame turned right <paramref name="turns"/> times, then mirrored and flipped, by moving pixels.</summary>
|
||||
internal static ImageBuffer Permuted(ImageBuffer frame, int turns, bool mirror, bool flip)
|
||||
{
|
||||
var image = frame.Clone();
|
||||
for (var k = 0; k < turns; k++) image = Replace(image, Move(image, image.Height, image.Width, (src, u, v) => (v, src.Height - 1 - u)));
|
||||
if (mirror) image = Replace(image, Move(image, image.Width, image.Height, (src, u, v) => (src.Width - 1 - u, v)));
|
||||
if (flip) image = Replace(image, Move(image, image.Width, image.Height, (src, u, v) => (u, src.Height - 1 - v)));
|
||||
return image;
|
||||
}
|
||||
|
||||
private static ImageBuffer Replace(ImageBuffer old, ImageBuffer next)
|
||||
{
|
||||
old.Dispose();
|
||||
return next;
|
||||
}
|
||||
|
||||
/// <summary>A new image whose pixel (u, v) is the source pixel <paramref name="from"/>(u, v).</summary>
|
||||
private static ImageBuffer Move(ImageBuffer src, int width, int height, Func<ImageBuffer, int, int, (int X, int Y)> from)
|
||||
{
|
||||
var dst = new ImageBuffer(width, height, src.Channels);
|
||||
for (var c = 0; c < src.Channels; c++)
|
||||
{
|
||||
for (var v = 0; v < height; v++)
|
||||
{
|
||||
for (var u = 0; u < width; u++)
|
||||
{
|
||||
var (x, y) = from(src, u, v);
|
||||
dst[c, u, v] = src[c, x, y];
|
||||
}
|
||||
}
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
}
|
||||
@@ -119,6 +119,19 @@ public sealed class GeometryStageTests
|
||||
Assert.Approximately(0.5, small[0, x, y], 0.02);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IntegerMapsMovePixelsExactly()
|
||||
{
|
||||
// A transpose (a quarter turn with a mirror) samples every output pixel at a source pixel centre.
|
||||
using var source = Texture(sigma: 1.0);
|
||||
using var output = new ImageBuffer(Size, Size, 3);
|
||||
GeometryStage.Warp(source, output, new Affine2D(0, 1, 1, 0, 0, 0));
|
||||
for (var c = 0; c < 3; c++)
|
||||
for (var y = 0; y < Size; y++)
|
||||
for (var x = 0; x < Size; x++)
|
||||
Assert.Equal(source[c, y, x], output[c, x, y]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CancellationStopsTheWarp()
|
||||
{
|
||||
|
||||
@@ -0,0 +1,179 @@
|
||||
using Chronos.Core.Imaging.Geometry;
|
||||
using Chronos.Core.Project;
|
||||
using Chronos.Testing;
|
||||
|
||||
namespace Chronos.Core.Tests.Geometry;
|
||||
|
||||
public sealed class OrientationEditTests
|
||||
{
|
||||
private static readonly OrientationOp[] Ops = [OrientationOp.RotateLeft, OrientationOp.RotateRight, OrientationOp.Mirror, OrientationOp.Flip];
|
||||
|
||||
private static ProjectSettings Framed { get; } = new()
|
||||
{
|
||||
Camera = new CameraSettings
|
||||
{
|
||||
Crop = CropFrame.ToCrop(CropFrame.FromRows(0.7, 1 / 3.0, -0.2, 16 / 9.0 * 4000 / 6000)),
|
||||
Aspect = new Auto<string>().WithManual("16:9").WithAdvice("3:2", null),
|
||||
OffsetX = 0.25,
|
||||
OffsetY = -0.5,
|
||||
Keyframes = [new CameraKey(0, 0.25, 0.4, 1.5, 3), new CameraKey(9, 0.6, 0.125, 2, -1.5, "Linear")],
|
||||
Straighten = 2.5,
|
||||
},
|
||||
PendingFrames = [new PendingFrame { Sha256 = "ab", Keyframes = [new CameraKey(4, 0.75, 0.5, 1, 2)] }],
|
||||
};
|
||||
|
||||
[Test]
|
||||
public void FourRightTurnsGiveBackTheText()
|
||||
{
|
||||
var text = Framed.ToJson();
|
||||
var settings = Framed;
|
||||
for (var k = 0; k < 4; k++)
|
||||
{
|
||||
settings = OrientationEdit.Apply(settings, OrientationOp.RotateRight);
|
||||
Assert.Equal((k + 1) % 4 * 90, settings.Camera.Rotate);
|
||||
if (k < 3) Assert.NotEqual(text, settings.ToJson());
|
||||
}
|
||||
Assert.Equal(text, settings.ToJson());
|
||||
|
||||
// Without a straighten, the turns leave nothing of format 2 behind.
|
||||
var plain = new ProjectSettings { Camera = new CameraSettings { Crop = [0.1, 0.2, 0.3, 0.4] } };
|
||||
var turned = plain;
|
||||
for (var k = 0; k < 4; k++) turned = OrientationEdit.Apply(turned, OrientationOp.RotateRight);
|
||||
Assert.Equal(plain.ToJson(), turned.ToJson());
|
||||
Assert.Equal(ProjectSettings.BaseVersion, turned.WrittenFormat);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EveryPressIsUndoneByItsOpposite()
|
||||
{
|
||||
var text = Framed.ToJson();
|
||||
foreach (var start in new[] { Framed, OrientationEdit.Apply(Framed, OrientationOp.Mirror), OrientationEdit.Apply(Framed, OrientationOp.RotateLeft) })
|
||||
{
|
||||
var before = start.ToJson();
|
||||
Assert.Equal(before, OrientationEdit.Apply(OrientationEdit.Apply(start, OrientationOp.RotateRight), OrientationOp.RotateLeft).ToJson());
|
||||
Assert.Equal(before, OrientationEdit.Apply(OrientationEdit.Apply(start, OrientationOp.Mirror), OrientationOp.Mirror).ToJson());
|
||||
Assert.Equal(before, OrientationEdit.Apply(OrientationEdit.Apply(start, OrientationOp.Flip), OrientationOp.Flip).ToJson());
|
||||
}
|
||||
Assert.Equal(text, Framed.ToJson());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MirrorMovesTheCropAndNegatesTheAngles()
|
||||
{
|
||||
var camera = new CameraSettings
|
||||
{
|
||||
Crop = [0.1, 0.2, 0.3, 0.4],
|
||||
OffsetX = 0.5,
|
||||
OffsetY = 0.25,
|
||||
Keyframes = [new CameraKey(0, 0.2, 0.7, 2, 5)],
|
||||
Straighten = 3,
|
||||
};
|
||||
var mirrored = OrientationEdit.Apply(camera, OrientationOp.Mirror);
|
||||
Assert.True(mirrored.Mirror);
|
||||
Assert.SequenceEqual([0.6, 0.2, 0.3, 0.4], mirrored.Crop!);
|
||||
Assert.Equal((-0.5, 0.25), (mirrored.OffsetX, mirrored.OffsetY));
|
||||
Assert.Equal(new CameraKey(0, 0.8, 0.7, 2, -5), mirrored.Keyframes.Single());
|
||||
Assert.Equal(-3.0, mirrored.Straighten);
|
||||
Assert.Equal(camera.Aspect, mirrored.Aspect);
|
||||
|
||||
var flipped = OrientationEdit.Apply(camera, OrientationOp.Flip);
|
||||
Assert.True(flipped.Flip);
|
||||
Assert.SequenceEqual([0.1, 0.4, 0.3, 0.4], flipped.Crop!);
|
||||
Assert.Equal((0.5, -0.25), (flipped.OffsetX, flipped.OffsetY));
|
||||
Assert.Equal(new CameraKey(0, 0.2, 0.3, 2, -5), flipped.Keyframes.Single());
|
||||
|
||||
// A zero stays a zero (JSON would write −0 as "-0").
|
||||
Assert.False(double.IsNegative(OrientationEdit.Apply(new CameraSettings(), OrientationOp.Mirror).Straighten));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AQuarterTurnTransposesTheCropAndTheAspect()
|
||||
{
|
||||
var camera = new CameraSettings
|
||||
{
|
||||
Crop = [0.1, 0.2, 0.3, 0.4],
|
||||
Aspect = new Auto<string>().WithManual("16:9").WithAdvice("4:3", null),
|
||||
OffsetX = 0.5,
|
||||
OffsetY = 0.25,
|
||||
Keyframes = [new CameraKey(0, 0.2, 0.7, 2, 5)],
|
||||
Straighten = 3,
|
||||
};
|
||||
var right = OrientationEdit.Apply(camera, OrientationOp.RotateRight);
|
||||
Assert.Equal(90, right.Rotate);
|
||||
Assert.SequenceEqual([0.4, 0.1, 0.4, 0.3], right.Crop!);
|
||||
Assert.Equal((-0.25, 0.5), (right.OffsetX, right.OffsetY));
|
||||
Assert.Equal(new CameraKey(0, 0.3, 0.2, 2, 5), right.Keyframes.Single());
|
||||
Assert.Equal("9:16", right.Aspect.Manual);
|
||||
Assert.Equal("3:4", right.Aspect.AutoValue);
|
||||
Assert.Equal(3.0, right.Straighten);
|
||||
|
||||
var left = OrientationEdit.Apply(camera, OrientationOp.RotateLeft);
|
||||
Assert.Equal(270, left.Rotate);
|
||||
Assert.SequenceEqual([0.2, 0.6, 0.4, 0.3], left.Crop!);
|
||||
|
||||
// "source", a square and text that cannot be read stay as they are.
|
||||
foreach (var aspect in new[] { "source", "1:1", "wide" })
|
||||
Assert.Equal(aspect, OrientationEdit.Transposed(new Auto<string>().WithManual(aspect)).Manual);
|
||||
Assert.Null(OrientationEdit.Transposed(new Auto<string>()).Manual);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ATurnSeenThroughOneMirrorIsStoredTheOtherWay()
|
||||
{
|
||||
Assert.Equal(270, OrientationEdit.Apply(new CameraSettings { Mirror = true }, OrientationOp.RotateRight).Rotate);
|
||||
Assert.Equal(90, OrientationEdit.Apply(new CameraSettings { Flip = true }, OrientationOp.RotateLeft).Rotate);
|
||||
Assert.Equal(90, OrientationEdit.Apply(new CameraSettings { Mirror = true, Flip = true }, OrientationOp.RotateRight).Rotate);
|
||||
Assert.Equal(0, OrientationEdit.Apply(new CameraSettings { Rotate = 90 }, OrientationOp.RotateLeft).Rotate);
|
||||
Assert.Equal(0, OrientationEdit.Apply(new CameraSettings { Rotate = -90 }, OrientationOp.RotateRight).Rotate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Each press moves the view as the user sees it: a frame point shown at normalised (x, y) before the press is shown
|
||||
/// at <see cref="OrientationEdit.MapPoint"/>(x, y) after it, whatever the stored turn, mirror, flip and straighten.
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void PressesMoveThePictureAsShown()
|
||||
{
|
||||
const int width = 60, height = 40;
|
||||
foreach (var rotate in new[] { 0, 90, 180, 270 })
|
||||
{
|
||||
foreach (var (mirror, flip) in new[] { (false, false), (true, false), (false, true), (true, true) })
|
||||
{
|
||||
foreach (var straighten in new[] { 0.0, 6.5 })
|
||||
{
|
||||
var camera = new CameraSettings { Rotate = rotate, Mirror = mirror, Flip = flip, Straighten = straighten };
|
||||
var before = FrameOrientation.Of(camera, width, height);
|
||||
foreach (var op in Ops)
|
||||
{
|
||||
var after = FrameOrientation.Of(OrientationEdit.Apply(camera, op), width, height);
|
||||
foreach (var (px, py) in new[] { (0.0, 0.0), (59.0, 0.0), (12.5, 30.0), (30.0, 20.0), (44.0, 7.25) })
|
||||
{
|
||||
var (bx, by) = Normalised(before, px, py);
|
||||
var (ax, ay) = Normalised(after, px, py);
|
||||
var (ex, ey) = OrientationEdit.MapPoint(bx, by, op);
|
||||
Assert.Within(ex, ey, ax, ay, 1e-9, $"{op} on rotate {rotate}, mirror {mirror}, flip {flip}, straighten {straighten} at ({px}, {py})");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ParkedKeyframesMoveWithTheProject()
|
||||
{
|
||||
var turned = OrientationEdit.Apply(Framed, OrientationOp.RotateRight);
|
||||
Assert.Equal(new CameraKey(4, 0.5, 0.75, 1, 2), turned.PendingFrames!.Single().Keyframes!.Single());
|
||||
Assert.Equal("ab", turned.PendingFrames!.Single().Sha256);
|
||||
var mirrored = OrientationEdit.Apply(Framed, OrientationOp.Mirror);
|
||||
Assert.Equal(new CameraKey(4, 0.25, 0.5, 1, -2), mirrored.PendingFrames!.Single().Keyframes!.Single());
|
||||
Assert.Null(OrientationEdit.Apply(new ProjectSettings(), OrientationOp.Flip).PendingFrames);
|
||||
}
|
||||
|
||||
/// <summary>A frame pixel centre in normalised edge coordinates of the view.</summary>
|
||||
private static (double X, double Y) Normalised(FrameOrientation orientation, double x, double y)
|
||||
{
|
||||
var (vx, vy) = orientation.FrameToView.Apply(x, y);
|
||||
return ((vx + 0.5) / orientation.ViewWidth, (vy + 0.5) / orientation.ViewHeight);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
using Chronos.Core.Export;
|
||||
using Chronos.Core.Imaging;
|
||||
using Chronos.Core.Imaging.Geometry;
|
||||
using Chronos.Core.Project;
|
||||
using Chronos.Core.Render;
|
||||
using Chronos.Core.Tests.Geometry;
|
||||
using Chronos.Testing;
|
||||
|
||||
namespace Chronos.Core.Tests.Render;
|
||||
|
||||
public sealed class RenderGeometryTests
|
||||
{
|
||||
[Test]
|
||||
public void TheChosenProportionsWinOverAFixedPreset()
|
||||
{
|
||||
var portrait = RenderGeometry.Create(Camera("9:16"), Export("4k-h264"), 6000, 4000);
|
||||
Assert.Equal((2160, 3840), (portrait.OutputWidth, portrait.OutputHeight));
|
||||
Assert.Approximately(9 / 16.0, portrait.OutputRatio, 1e-12);
|
||||
Assert.Empty(portrait.Warnings);
|
||||
|
||||
var source = RenderGeometry.Create(new CameraSettings(), Export("4k-h264"), 6000, 4000);
|
||||
Assert.Equal((3840, 2160), (source.OutputWidth, source.OutputHeight));
|
||||
Assert.Approximately(16 / 9.0, source.OutputRatio, 1e-12);
|
||||
|
||||
// Every case agrees with RenderSize, which the server routes jobs with.
|
||||
foreach (var aspect in new[] { "source", "9:16", "1:1", "4:5", "21:9" })
|
||||
{
|
||||
foreach (var camera in new[] { Camera(aspect), Camera(aspect) with { Rotate = 90 } })
|
||||
{
|
||||
foreach (var preset in new[] { "4k-h264", "1080p-h264", "vertical-1080x1920", "png-sequence" })
|
||||
{
|
||||
var geometry = RenderGeometry.Create(camera, Export(preset), 6000, 4000);
|
||||
Assert.Equal(RenderSize.Of(camera, Export(preset), 6000, 4000), (geometry.OutputWidth, geometry.OutputHeight), $"{aspect} {preset} {camera.Rotate}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AQuarterTurnSwapsTheViewAndLocksTheCropInIt()
|
||||
{
|
||||
var turned = RenderGeometry.Create(new CameraSettings { Rotate = 90 }, Export("png-sequence"), 6000, 4000);
|
||||
Assert.Equal((4000, 6000), (turned.ViewWidth, turned.ViewHeight));
|
||||
Assert.Equal((4000, 6000), (turned.OutputWidth, turned.OutputHeight));
|
||||
Assert.Equal((6000, 4000), (turned.SourceWidth, turned.SourceHeight));
|
||||
|
||||
// 1:1 on the turned view: the box is as wide as the view and 4000/6000 of its height, in normalised units.
|
||||
var square = RenderGeometry.Create(Camera("1:1") with { Rotate = 90 }, Export("png-sequence"), 6000, 4000);
|
||||
Assert.Approximately(1.5, square.LockRatio, 1e-12);
|
||||
var crop = square.EditableCrop;
|
||||
Assert.Approximately(1, crop.Width, 1e-12);
|
||||
Assert.Approximately(2 / 3.0, crop.Height, 1e-12);
|
||||
Assert.Approximately(1 / 6.0, crop.Y, 1e-12);
|
||||
Assert.Equal((4000, 4000), (square.OutputWidth, square.OutputHeight));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TheEditableCropIsTheRenderedCrop()
|
||||
{
|
||||
var camera = Camera("4:5") with { Crop = [0.1, 0.2, 0.5, 0.6], OffsetX = 1, OffsetY = -1, Rotate = 270, Mirror = true };
|
||||
var geometry = RenderGeometry.Create(camera, Export("png-sequence"), 96, 64);
|
||||
// The Export step's footprint of a frame without pose or stabilisation is the Camera step's box.
|
||||
foreach (var (w, h) in new[] { (96, 64), (48, 32) })
|
||||
{
|
||||
var plan = GeometryStage.Plan(w, h, geometry.For(w, h, 0, Affine2D.Identity));
|
||||
var footprint = geometry.FootprintInView(plan, w, h);
|
||||
var box = geometry.EditableCrop;
|
||||
Assert.Approximately(box.X, footprint.X, 1e-9);
|
||||
Assert.Approximately(box.Y, footprint.Y, 1e-9);
|
||||
Assert.Approximately(box.Width, footprint.Width, 1e-9);
|
||||
Assert.Approximately(box.Height, footprint.Height, 1e-9);
|
||||
}
|
||||
Assert.Equal(new RectD(0, 0, 1, 1), geometry.FootprintInView(null, 96, 64));
|
||||
// The box the editor would write for it renders the same.
|
||||
var written = Camera("4:5") with { Crop = CropFrame.ToCrop(geometry.EditableCrop), Rotate = 270, Mirror = true };
|
||||
var again = RenderGeometry.Create(written, Export("png-sequence"), 96, 64);
|
||||
Assert.Equal((geometry.OutputWidth, geometry.OutputHeight), (again.OutputWidth, again.OutputHeight));
|
||||
Assert.Approximately(geometry.EditableCrop.X, again.EditableCrop.X, 1e-12);
|
||||
Assert.Approximately(geometry.EditableCrop.Y, again.EditableCrop.Y, 1e-12);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ViewToFrameMapsTheViewOntoTheDecodedFrame()
|
||||
{
|
||||
var geometry = RenderGeometry.Create(new CameraSettings { Rotate = 180, Flip = true, Straighten = -4 }, Export("png-sequence"), 6000, 4000);
|
||||
var small = geometry.ViewToFrame(600, 400);
|
||||
Assert.Equal(FrameOrientation.Of(180, false, true, -4, 600, 400).ViewToFrame, small);
|
||||
Assert.Equal(geometry.Orientation.ViewToFrame, geometry.ViewToFrame(6000, 4000));
|
||||
Assert.Equal(Affine2D.Identity, RenderGeometry.Create(new CameraSettings(), Export("png-sequence"), 60, 40).ViewToFrame(60, 40));
|
||||
|
||||
// The Source framing shows the whole view at the size asked, and only an identity camera skips the warp.
|
||||
var view = geometry.ViewFor(600, 400, 300, 200);
|
||||
var plan = GeometryStage.Plan(600, 400, view);
|
||||
Assert.Equal((300, 200), (plan.OutputWidth, plan.OutputHeight));
|
||||
Assert.Equal(new RectD(0, 0, 600, 400), plan.Crop);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TheDecodeGrowsWithTheCoverZoom()
|
||||
{
|
||||
var plain = RenderGeometry.Create(new CameraSettings(), Export("png-sequence", 96, 64), 480, 320);
|
||||
Assert.Equal(96, plain.WorkingLongEdge);
|
||||
foreach (var degrees in new[] { 2.0, 10.0, -15.0 })
|
||||
{
|
||||
var straightened = RenderGeometry.Create(new CameraSettings { Straighten = degrees }, Export("png-sequence", 96, 64), 480, 320);
|
||||
var zoom = straightened.Orientation.CoverZoom;
|
||||
Assert.Greater(zoom, 1);
|
||||
Assert.Equal((int)double.Ceiling(96 * zoom), straightened.WorkingLongEdge);
|
||||
}
|
||||
// Quarter turns do not change the long edge.
|
||||
Assert.Equal(96, RenderGeometry.Create(new CameraSettings { Rotate = 90 }, Export("png-sequence", 64, 96), 480, 320).WorkingLongEdge);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void StabilisationComposesInRawFrameSpace()
|
||||
{
|
||||
// stab⁻¹ · O⁻¹ · V: the correction is undone in the decoded frame, after the view is mapped back into it.
|
||||
var geometry = RenderGeometry.Create(new CameraSettings { Rotate = 90 }, Export("png-sequence"), 96, 64);
|
||||
var shift = Affine2D.Translation(3, -2);
|
||||
var settings = geometry.For(96, 64, 0, shift);
|
||||
var plain = geometry.For(96, 64, 0, Affine2D.Identity);
|
||||
var (x, y) = settings.Warp.Apply(10, 20);
|
||||
var (px, py) = plain.Warp.Apply(10, 20);
|
||||
Assert.Within(px - 3, py + 2, x, y, 1e-12);
|
||||
|
||||
using var frame = FrameOrientationTests.Texture(96, 64, seed: 11);
|
||||
using var view = GeometryStage.Apply(frame, plain);
|
||||
Assert.Equal((64, 96), (view.Width, view.Height));
|
||||
}
|
||||
|
||||
private static CameraSettings Camera(string aspect) => new() { Aspect = new Auto<string>().WithManual(aspect) };
|
||||
|
||||
private static ResolvedExport Export(string preset, int width = -1, int height = -1)
|
||||
{
|
||||
var export = ExportResolver.Resolve(new ExportSettings { Preset = new Auto<string>().WithManual(preset) });
|
||||
return width < 0 ? export : export with { Width = width, Height = height };
|
||||
}
|
||||
}
|
||||
@@ -74,6 +74,33 @@ public sealed class RenderPipelineTests
|
||||
Assert.BytesEqual(File.ReadAllBytes(apngA.OutputPath), File.ReadAllBytes(apngB.OutputPath));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TransformedOutputsAreByteIdenticalAcrossRuns()
|
||||
{
|
||||
var dir = _context.CreateTempDirectory();
|
||||
var project = Plain with
|
||||
{
|
||||
Camera = new CameraSettings
|
||||
{
|
||||
Rotate = 90,
|
||||
Mirror = true,
|
||||
Straighten = 3.5,
|
||||
Aspect = new Auto<string>().WithManual("4:5"),
|
||||
Crop = [0.1, 0.05, 0.8, 0.8],
|
||||
Keyframes = [new CameraKey(0, 0.45, 0.5, 1.2, 2), new CameraKey(11, 0.55, 0.5, 1.4, -2)],
|
||||
},
|
||||
};
|
||||
var export = Preset("png-sequence", 64, 64);
|
||||
var first = await Pipeline().RenderAsync(Input(Source(), project), Target(dir, export, "a"), cancellationToken: _context.Cancellation);
|
||||
var second = await Pipeline().RenderAsync(Input(Source(), project), Target(dir, export, "b"), cancellationToken: _context.Cancellation);
|
||||
Assert.Equal((51, 64), (first.Width, first.Height));
|
||||
var a = Directory.GetFiles(first.OutputPath).Order(StringComparer.Ordinal).ToArray();
|
||||
var b = Directory.GetFiles(second.OutputPath).Order(StringComparer.Ordinal).ToArray();
|
||||
Assert.Equal(12, a.Length);
|
||||
Assert.Equal(a.Length, b.Length);
|
||||
for (var i = 0; i < a.Length; i++) Assert.BytesEqual(File.ReadAllBytes(a[i]), File.ReadAllBytes(b[i]));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[Case("mjpeg-avi")]
|
||||
[Case("png-sequence")]
|
||||
|
||||
@@ -142,6 +142,61 @@ public sealed class RenderProcessingTests
|
||||
Assert.Greater(Psnr(expected, corner.Frames[2]), 60);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A crop in the view frame keeps its content through every orientation press: the capture after the press is the
|
||||
/// plain capture turned, mirrored or flipped the same way.
|
||||
/// </summary>
|
||||
[Theory]
|
||||
[Case(OrientationOp.RotateRight, 1, false, false)]
|
||||
[Case(OrientationOp.RotateLeft, 3, false, false)]
|
||||
[Case(OrientationOp.Mirror, 0, true, false)]
|
||||
[Case(OrientationOp.Flip, 0, false, true)]
|
||||
public async Task OrientationPressesKeepTheCroppedContent(OrientationOp op, int turns, bool mirror, bool flip)
|
||||
{
|
||||
var source = Source(4);
|
||||
var framed = With(camera: new CameraSettings { Crop = [0.5, 0.25, 0.5, 0.5], Aspect = On("3:2") });
|
||||
var (plain, before) = await CaptureAsync(Input(source, framed), Preset("png-sequence", 0, 0), cancellationToken: _context.Cancellation);
|
||||
Assert.Equal((48, 32), (before.Width, before.Height));
|
||||
var pressed = OrientationEdit.Apply(framed, op);
|
||||
var (moved, after) = await CaptureAsync(Input(source, pressed), Preset("png-sequence", 0, 0), cancellationToken: _context.Cancellation);
|
||||
Assert.Empty(after.Warnings);
|
||||
using var expected = Geometry.FrameOrientationTests.Permuted(plain.Frames[2], turns, mirror, flip);
|
||||
Assert.Equal((expected.Width, expected.Height), (after.Width, after.Height));
|
||||
var psnr = Psnr(expected, moved.Frames[2]);
|
||||
_context.Log($"{op}: {psnr:F1} dB");
|
||||
Assert.Greater(psnr, 60);
|
||||
Assert.True(expected.Data.SequenceEqual(moved.Frames[2].Data), "a quarter turn, a mirror and a flip move whole pixels");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task APortraitAspectTurnsAFixedPresetToPortrait()
|
||||
{
|
||||
var source = Source(4);
|
||||
using var reference = source.Sequence.Render(2);
|
||||
var project = With(camera: new CameraSettings { Aspect = On("9:16") });
|
||||
var (encode, result) = await CaptureAsync(Input(source, project), Preset("png-sequence", 96, 64), cancellationToken: _context.Cancellation);
|
||||
Assert.Equal((54, 96), (result.Width, result.Height));
|
||||
Assert.DoesNotContain("Export.Render.AspectFromPreset", result.Warnings.Select(w => w.Key));
|
||||
using var expected = GeometryStage.Apply(reference, new GeometrySettings { Crop = new RectD(30, 0, 36, 64), OutputWidth = 54, OutputHeight = 96 });
|
||||
Assert.Greater(Psnr(expected, encode.Frames[2]), 60);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task StraightenLevelsTheViewAndDecodesLarger()
|
||||
{
|
||||
var source = Source(4, width: 480, height: 320);
|
||||
var project = With(camera: new CameraSettings { Straighten = -6 });
|
||||
var (encode, result) = await CaptureAsync(Input(source, project), Preset("png-sequence", 96, 64), cancellationToken: _context.Cancellation);
|
||||
var orientation = FrameOrientation.Of(project.Camera, 480, 320);
|
||||
Assert.Equal((int)double.Ceiling(96 * orientation.CoverZoom), source.LargestRenderRequest);
|
||||
Assert.Empty(result.Warnings);
|
||||
using var full = source.Sequence.Render(1);
|
||||
using var expected = GeometryStage.Apply(full, new GeometrySettings { OutputWidth = 96, OutputHeight = 64, Warp = orientation.ViewToFrame });
|
||||
var psnr = Psnr(expected, encode.Frames[1]);
|
||||
_context.Log($"straighten −6° vs a direct warp: {psnr:F1} dB, cover zoom {orientation.CoverZoom:F4}");
|
||||
Assert.Greater(psnr, 28);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task VirtualCameraZoomsAndTheDecodeSizeFollowsTheNeed()
|
||||
{
|
||||
|
||||
@@ -44,6 +44,16 @@ public sealed class SelfTestTests
|
||||
Assert.True(results.Where(r => r.Id != "render.mp4").All(r => r.Outcome == SelfTestOutcome.Passed));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GeometryChecksPass()
|
||||
{
|
||||
string[] ids = ["proc.orient", "proc.straighten", "proc.portrait", "proc.aspect", "proc.crop"];
|
||||
var results = await SelfTest.RunAsync(new SelfTestOptions { WorkDirectory = _context.CreateTempDirectory(), Filters = ids }, cancellationToken: _context.Cancellation);
|
||||
foreach (var r in results) _context.Log($"{r.Id}: {r.Outcome} {r.Detail}");
|
||||
Assert.SequenceEqual(ids.Order(StringComparer.Ordinal), results.Select(r => r.Id).Order(StringComparer.Ordinal));
|
||||
Assert.True(results.All(r => r.Outcome == SelfTestOutcome.Passed));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FailuresAreReportedNotThrown()
|
||||
{
|
||||
|
||||
@@ -39,6 +39,23 @@ public sealed class AnalysisJobTests(TestContext context)
|
||||
Longitude = 12.3,
|
||||
});
|
||||
|
||||
[Test]
|
||||
public async Task LightAnalysisAdvisesTheAspectOfTheTurnedView()
|
||||
{
|
||||
await using var fx = await ProcessingFixture.CreateAsync(context);
|
||||
var sequence = await fx.AddAsync(Flickering());
|
||||
var turned = new ProjectSettings { Camera = new CameraSettings { Rotate = 90 } };
|
||||
await fx.Db.Sequences.EditAsync(sequence.Id, new SequenceEdit { SettingsJson = turned.ToJson() }, null, fx.Ct);
|
||||
await fx.StartEngineAsync();
|
||||
|
||||
var job = await fx.EnqueueAsync(ProcessingJobs.LightAnalysis([sequence.Id], sequence.DisplayTitle));
|
||||
await fx.WaitForAsync(job.Id, JobState.Done);
|
||||
|
||||
var project = ProjectSettings.FromJson(await File.ReadAllTextAsync(fx.Folder(sequence).ProjectPath, fx.Ct));
|
||||
Assert.Equal(90, project.Camera.Rotate);
|
||||
Assert.Equal("9:16", project.Camera.Aspect.AutoValue);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task LightAnalysisFillsAutoValuesAndKeepsManualOnes()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user