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:
Alby96andClaude Opus 5.5 committed 2026-11-04 19:35:05 +01:00
1 parent e4364c4f25
commit bee66da63a
18 files changed
+1052 -67

No files matched your search

+14 -10
View File
@@ -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);
+16
View File
@@ -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";
});
}
}
+60 -49
View File
@@ -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)