Files
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

172 lines
7.2 KiB
C#

using Chronos.Core.Imaging;
using Chronos.Core.Imaging.Geometry;
using Chronos.Core.Motion;
using Chronos.Testing;
namespace Chronos.Core.Tests.Motion;
public sealed class StabilizerTests
{
private const int Width = 256;
private const int Height = 192;
private const int Frames = 48;
private readonly TestContext _context;
public StabilizerTests(TestContext context) => _context = context;
[Test]
public void TexturelessFramesReportNoShakeAndNoCorrection()
{
var random = new Random(17);
using var stabilizer = new Stabilizer(128, 128);
using var frame = new ImageBuffer(128, 128);
for (var k = 0; k < 20; k++)
{
var data = frame.Data;
for (var i = 0; i < data.Length; i++) data[i] = 0.3f + 0.004f * (float)Gaussian(random);
Assert.False(stabilizer.AddFrame(frame).Estimated);
}
var result = stabilizer.Solve();
_context.Log($"textureless shake {result.ShakeRms:F4} px, crop scale {result.CropScale:F4}");
Assert.LessOrEqual(result.ShakeRms, 0.1);
Assert.False(result.Recommended);
Assert.Equal(1.0, result.CropScale);
Assert.True(result.Corrections.All(c => c == Affine2D.Identity));
}
[Test]
public void JitterIsReducedByAtLeastEightyPercent()
{
var random = new Random(23);
var cameras = new Affine2D[Frames];
for (var k = 0; k < Frames; k++) cameras[k] = Camera(2.0 * Gaussian(random), 2.0 * Gaussian(random), 0.3 * Gaussian(random), pan: 0, k);
var result = Stabilize(cameras, out var motionError);
var before = Jitter(cameras, null);
var after = Jitter(cameras, result.Corrections);
var reduction = 1 - after / before;
_context.Log($"frame-to-frame jitter {before:F3} → {after:F3} px ({reduction:P1}); motion error {motionError:F4} px; shake {result.ShakeRms:F3} px; crop cost {result.CropCost:P1}");
Assert.GreaterOrEqual(reduction, 0.8);
Assert.Less(motionError, 0.1);
Assert.True(result.Recommended);
Assert.Greater(result.ShakeRms, 1.0);
Assert.InRange(result.CropCost, 0.01, 0.3);
}
[Test]
public void IntentionalPanSurvivesStabilisation()
{
const double pan = 1.5;
var random = new Random(29);
var cameras = new Affine2D[Frames];
for (var k = 0; k < Frames; k++) cameras[k] = Camera(1.5 * Gaussian(random), 1.5 * Gaussian(random), 0.2 * Gaussian(random), pan, k);
var result = Stabilize(cameras, out _);
// The stabilised view still follows the pan: content drifts left by `pan` px per frame.
var (bx, by) = cameras[0].Apply(Width / 2.0, Height / 2.0);
(double X, double Y) Track(int k)
{
var (x, y) = cameras[k].Invert().Apply(bx, by);
return result.Corrections[k].Apply(x, y);
}
var (sx, sy) = Track(0);
var (ex, ey) = Track(Frames - 1);
var velocity = (ex - sx) / (Frames - 1);
_context.Log($"stabilised velocity {velocity:F3} px/frame (pan {-pan}), vertical drift {ey - sy:F3} px");
Assert.Approximately(-pan, velocity, 0.1);
var reduction = 1 - Jitter(cameras, result.Corrections, removeMean: true) / Jitter(cameras, null, removeMean: true);
Assert.GreaterOrEqual(reduction, 0.8);
}
[Test]
public void SmootherKeepsLinesAndEnds()
{
var line = Enumerable.Range(0, 30).Select(i => 3 + 0.5 * i).ToArray();
var smoothed = PathSmoother.LocalLinear(line, 5);
for (var i = 0; i < line.Length; i++) Assert.Approximately(line[i], smoothed[i], 1e-9);
// Frame-rate jitter (alternating ±1) is removed in the interior and strongly attenuated at the ends.
var noisy = Enumerable.Range(0, 200).Select(i => i % 2 == 0 ? 1.0 : -1.0).ToArray();
var flat = PathSmoother.LocalLinear(noisy, 8);
var interior = flat.Skip(24).SkipLast(24).Max(double.Abs);
var ends = flat.Max(double.Abs);
_context.Log($"interior {interior:E2}, ends {ends:E2}");
Assert.Less(interior, 0.01);
Assert.Less(ends, 0.2);
}
/// <summary>Frame pixel → base-texture coordinate: jitter translation and rotation about the frame centre, plus a pan.</summary>
private static Affine2D Camera(double tx, double ty, double degrees, double pan, int k) =>
Affine2D.Translation(100 + pan * k, 150)
* Affine2D.About(Affine2D.Rotation(degrees * double.Pi / 180), (Width - 1) / 2.0, (Height - 1) / 2.0)
* Affine2D.Translation(tx, ty);
private static StabilizationResult Stabilize(Affine2D[] cameras, out double motionError)
{
const int Base = 512;
using var texture = SyntheticImages.ToRgb(SyntheticImages.Crop(SyntheticImages.Natural(Base, seed: 41), Base, 0, 0, Base, Base), Base, Base);
using var frame = new ImageBuffer(Width, Height);
using var stabilizer = new Stabilizer(Width, Height);
double error = 0;
var samples = 0;
for (var k = 0; k < cameras.Length; k++)
{
GeometryStage.Warp(texture, frame, cameras[k]);
var motion = stabilizer.AddFrame(frame);
if (k == 0) continue;
// True inter-frame map: previous frame → base → current frame.
var truth = cameras[k].Invert() * cameras[k - 1];
foreach (var (x, y) in GridPoints())
{
var (tx, ty) = truth.Apply(x, y);
var (mx, my) = motion.Transform.Apply(x, y);
error += double.Hypot(tx - mx, ty - my);
samples++;
}
}
motionError = error / samples;
return stabilizer.Solve();
}
/// <summary>RMS frame-to-frame displacement of fixed content points in the (corrected) frames.</summary>
private static double Jitter(Affine2D[] cameras, IReadOnlyList<Affine2D>? corrections, bool removeMean = false)
{
var points = GridPoints().Select(p => cameras[0].Apply(p.X, p.Y)).ToArray();
var steps = new List<(double X, double Y)>();
for (var k = 1; k < cameras.Length; k++)
{
foreach (var (bx, by) in points)
{
var (x0, y0) = cameras[k - 1].Invert().Apply(bx, by);
var (x1, y1) = cameras[k].Invert().Apply(bx, by);
if (corrections is not null)
{
(x0, y0) = corrections[k - 1].Apply(x0, y0);
(x1, y1) = corrections[k].Apply(x1, y1);
}
steps.Add((x1 - x0, y1 - y0));
}
}
double mx = 0, my = 0;
if (removeMean)
{
mx = steps.Average(s => s.X);
my = steps.Average(s => s.Y);
}
return double.Sqrt(steps.Average(s => (s.X - mx) * (s.X - mx) + (s.Y - my) * (s.Y - my)));
}
private static IEnumerable<(double X, double Y)> GridPoints()
{
for (var j = 1; j <= 3; j++)
for (var i = 1; i <= 3; i++)
yield return (Width * i / 4.0, Height * j / 4.0);
}
private static double Gaussian(Random random)
{
var u1 = 1.0 - random.NextDouble();
var u2 = random.NextDouble();
return double.Sqrt(-2 * double.Log(u1)) * double.Cos(2 * double.Pi * u2);
}
}