Files
Chronos/src/Chronos.Core/Motion/MotionBlurRenderer.cs
T
Alby96andClaude Opus 5.5 f2b58522b9
CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
refactor!: rename Titano to Chronos and move CI and releases to Gitea (#6)
The owner renamed the product and moved the repository to the self-hosted
Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control,
and asked for Actions and a first automatic release with the server package
and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md).

- Chronos everywhere: a byte-exact rename of contents and paths, then a
  per-language review of the forms the old name's ending had dictated.
- Gitea instead of GitHub: the update check, licence, issue and support
  links read ChronosReleasesUrl; the update check takes the highest stable
  version, offers a pre-release build its final release and reports a
  repository it cannot see as a failed check. EULA, security policy and
  Gitea issue forms live at the repository root.
- Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release,
  nightly image and performance gates in the .NET SDK container with git
  checkout and the system SQLite. The release cross-compiles the Windows
  programs, checks their icon, version and DPI manifest, packs installers
  and zips, publishes the Gitea release and pushes the server image
  (REGISTRY_TOKEN). Windows-only tests run in an opt-in job on a Windows
  runner (WINDOWS_RUNNER).
- New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the
  docker-exec script, which Gitea's service containers do not allow; the
  throw-away server runs without durability) and pe-check.
- Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR,
  3 on the shared runner; the weekly performance gates keep theirs).

BREAKING CHANGE: programs, settings, data folders, file types, environment
variables and the Docker image use the new name; nothing is migrated from
Titano, which never had a release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-10-23 12:45:18 +02:00

144 lines
6.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Chronos.Core.Imaging;
namespace Chronos.Core.Motion;
/// <summary>Shutter-angle arithmetic for synthetic motion blur.</summary>
public static class ShutterAngle
{
/// <summary>Shutter angle actually used by the camera, 360° × exposure / interval.</summary>
public static double FromExposure(double exposureSeconds, double intervalSeconds)
{
ArgumentOutOfRangeException.ThrowIfNegative(exposureSeconds);
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(intervalSeconds);
return 360.0 * exposureSeconds / intervalSeconds;
}
/// <summary>
/// Fraction of the frame-to-frame motion to smear so that the real exposure composed with the synthetic
/// blur behaves like the target shutter: √(target² − real²) / 360, and 0 when the real angle already
/// reaches the target (the blur would only be added on top of blur that is already there).
/// </summary>
public static double BlurFraction(double targetDegrees, double realDegrees)
{
ArgumentOutOfRangeException.ThrowIfNegative(targetDegrees);
ArgumentOutOfRangeException.ThrowIfNegative(realDegrees);
if (realDegrees >= targetDegrees) return 0;
return double.Sqrt(targetDegrees * targetDegrees - realDegrees * realDegrees) / 360.0;
}
}
/// <summary>Tuning for <see cref="MotionBlurRenderer"/>.</summary>
public sealed record MotionBlurOptions
{
/// <summary>Samples per pixel of blur length (≥ 1 keeps the bilinear taps overlapping).</summary>
public double TapsPerPixel { get; init; } = 1.0;
/// <summary>Upper bound on samples per pixel.</summary>
public int MaxTaps { get; init; } = 64;
}
/// <summary>
/// Motion blur by a gather filter: every output pixel averages bilinear samples of the source along a
/// segment centred on it, oriented by its motion vector and as long as the vector times the blur fraction
/// (a box shutter: uniform weights, tap count adaptive to the length). Equal weights and bilinear taps
/// preserve energy; zero vectors copy the pixel. Rows run in parallel.
/// </summary>
public sealed class MotionBlurRenderer
{
private const double MinimumLength = 1e-3;
public MotionBlurRenderer(MotionBlurOptions? options = null)
{
Options = options ?? new MotionBlurOptions();
ArgumentOutOfRangeException.ThrowIfLessThan(Options.TapsPerPixel, 0.25);
ArgumentOutOfRangeException.ThrowIfLessThan(Options.MaxTaps, 2);
}
public MotionBlurOptions Options { get; }
/// <summary>
/// Renders <paramref name="source"/> blurred along <paramref name="motion"/> (frame-to-frame vectors)
/// scaled by <paramref name="blurFraction"/> (see <see cref="ShutterAngle.BlurFraction"/>).
/// </summary>
public void Render(ImageBuffer source, MotionField motion, double blurFraction, ImageBuffer destination, CancellationToken cancellationToken = default)
{
if (destination.Width != source.Width || destination.Height != source.Height || destination.Channels != source.Channels)
throw new ArgumentException("Destination must match the source.", nameof(destination));
if (motion.ImageWidth != source.Width || motion.ImageHeight != source.Height)
throw new ArgumentException("Motion field describes a different image size.", nameof(motion));
ArgumentOutOfRangeException.ThrowIfNegative(blurFraction);
if (blurFraction == 0)
{
source.CopyTo(destination);
return;
}
var w = source.Width;
var h = source.Height;
var channels = source.Channels;
var plane = source.PlaneLength;
var src = source.Storage;
var dst = destination.Storage;
var tapsPerPixel = Options.TapsPerPixel;
var maxTaps = Options.MaxTaps;
void Row(int y)
{
Span<float> sum = stackalloc float[4];
for (var x = 0; x < w; x++)
{
var o = y * w + x;
var (mu, mv) = motion.Sample(x, y);
var du = mu * blurFraction;
var dv = mv * blurFraction;
var length = double.Sqrt(du * du + dv * dv);
if (length < MinimumLength)
{
for (var c = 0; c < channels; c++) dst[c * plane + o] = src[c * plane + o];
continue;
}
var taps = int.Clamp((int)double.Ceiling(length * tapsPerPixel) + 1, 2, maxTaps);
sum.Clear();
for (var k = 0; k < taps; k++)
{
var t = k / (double)(taps - 1) - 0.5;
var sx = double.Clamp(x + t * du, 0, w - 1);
var sy = double.Clamp(y + t * dv, 0, h - 1);
var x0 = int.Min((int)sx, int.Max(w - 2, 0));
var y0 = int.Min((int)sy, int.Max(h - 2, 0));
var x1 = int.Min(x0 + 1, w - 1);
var y1 = int.Min(y0 + 1, h - 1);
var fx = (float)(sx - x0);
var fy = (float)(sy - y0);
var w00 = (1 - fx) * (1 - fy);
var w10 = fx * (1 - fy);
var w01 = (1 - fx) * fy;
var w11 = fx * fy;
int i00 = y0 * w + x0, i10 = y0 * w + x1, i01 = y1 * w + x0, i11 = y1 * w + x1;
for (var c = 0; c < channels; c++)
{
var p = c * plane;
sum[c] += w00 * src[p + i00] + w10 * src[p + i10] + w01 * src[p + i01] + w11 * src[p + i11];
}
}
var inv = 1f / taps;
for (var c = 0; c < channels; c++) dst[c * plane + o] = sum[c] * inv;
}
}
if ((long)w * h < PlaneOps.ParallelThreshold)
{
for (var y = 0; y < h; y++)
{
cancellationToken.ThrowIfCancellationRequested();
Row(y);
}
}
else
{
Parallel.For(0, h, new ParallelOptions { CancellationToken = cancellationToken }, Row);
}
}
}