CI / Build and test (pull_request) Failing after 2m3s
CI / PostgreSQL 16 integration (pull_request) Skipped
CI / PostgreSQL 17 integration (pull_request) Skipped
CI / Docker image (pull_request) Skipped
CI / Windows tests and installer (pull_request) Skipped
CI / YAML check (pull_request) Successful in 4s
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. 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) and pe-check. 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>
195 lines
7.1 KiB
C#
195 lines
7.1 KiB
C#
using Chronos.Core.Imaging;
|
||
using Chronos.Core.Motion;
|
||
using Chronos.Testing;
|
||
|
||
namespace Chronos.Core.Tests.Motion;
|
||
|
||
public sealed class TemporalStackerTests
|
||
{
|
||
private const int Size = 96;
|
||
|
||
private readonly TestContext _context;
|
||
|
||
public TemporalStackerTests(TestContext context) => _context = context;
|
||
|
||
[Test]
|
||
public void StarTrailLuminanceNeverDecreasesWithUnitDecay()
|
||
{
|
||
var random = new Random(5);
|
||
using var stacker = new StarTrailStacker(1.0);
|
||
using var frame = new ImageBuffer(Size, Size);
|
||
var previous = new float[Size * Size];
|
||
var current = new float[Size * Size];
|
||
long steps = 0, nonDecreasing = 0;
|
||
for (var k = 0; k < 40; k++)
|
||
{
|
||
NightFrame(frame, random, k);
|
||
stacker.Add(frame);
|
||
ColorSpace.Luminance(stacker.Result!, current);
|
||
if (k > 0)
|
||
{
|
||
for (var i = 0; i < current.Length; i++)
|
||
{
|
||
steps++;
|
||
if (current[i] >= previous[i]) nonDecreasing++;
|
||
}
|
||
}
|
||
(previous, current) = (current, previous);
|
||
}
|
||
var fraction = nonDecreasing / (double)steps;
|
||
_context.Log($"non-decreasing on {fraction:P3} of {steps} pixel steps");
|
||
Assert.GreaterOrEqual(fraction, 0.99);
|
||
Assert.Equal(40, stacker.Count);
|
||
// The trail of the first star position is still lit after 40 frames.
|
||
Assert.Greater(stacker.Result![1, StarX(0), StarY(0)], 0.5f);
|
||
}
|
||
|
||
[Test]
|
||
public void FiniteTrailsFadeToOnePercentOverTheirLength()
|
||
{
|
||
Assert.Equal(1.0, StarTrailStacker.DecayForTrailLength(double.PositiveInfinity));
|
||
Assert.Equal(1.0, StarTrailStacker.DecayForTrailLength(0));
|
||
var decay = StarTrailStacker.DecayForTrailLength(20);
|
||
Assert.Approximately(0.01, double.Pow(decay, 20), 1e-12);
|
||
|
||
using var stacker = new StarTrailStacker(decay);
|
||
using var lit = new ImageBuffer(8, 8, 1);
|
||
using var dark = new ImageBuffer(8, 8, 1);
|
||
lit[0, 3, 3] = 1f;
|
||
stacker.Add(lit);
|
||
for (var k = 0; k < 20; k++) stacker.Add(dark);
|
||
Assert.Approximately(0.01, stacker.Result![0, 3, 3], 1e-5);
|
||
}
|
||
|
||
[Test]
|
||
public void MedianRemovesATransientOutlierFrame()
|
||
{
|
||
var plane = SyntheticImages.Crop(SyntheticImages.Natural(128, seed: 12), 128, 0, 0, Size, Size);
|
||
var random = new Random(8);
|
||
using var stacker = new TemporalStacker(7, StackMode.Median);
|
||
for (var k = 0; k < 7; k++)
|
||
{
|
||
using var frame = SyntheticImages.ToRgb(plane, Size, Size);
|
||
var data = frame.Data;
|
||
for (var i = 0; i < data.Length; i++) data[i] += 0.01f * (float)(random.NextDouble() - 0.5);
|
||
if (k == 3)
|
||
{
|
||
// An aircraft light streak crossing one frame.
|
||
for (var c = 0; c < 3; c++)
|
||
for (var x = 10; x < 80; x++)
|
||
frame[c, x, 40 + x / 8] = 5f;
|
||
}
|
||
stacker.Push(frame);
|
||
}
|
||
Assert.True(stacker.IsFull);
|
||
using var output = new ImageBuffer(Size, Size);
|
||
stacker.Render(output);
|
||
using var reference = SyntheticImages.ToRgb(plane, Size, Size);
|
||
double maxError = 0;
|
||
for (var i = 0; i < output.Data.Length; i++) maxError = double.Max(maxError, double.Abs(output.Data[i] - reference.Data[i]));
|
||
_context.Log($"max deviation from the clean frame {maxError:F4}");
|
||
Assert.Less(maxError, 0.01);
|
||
}
|
||
|
||
[Theory]
|
||
[Case(1)]
|
||
[Case(5)]
|
||
[Case(6)]
|
||
[Case(32)]
|
||
[Case(33)]
|
||
public void MedianIsExact(int count)
|
||
{
|
||
var random = new Random(count);
|
||
var frames = new List<ImageBuffer>();
|
||
try
|
||
{
|
||
for (var k = 0; k < count; k++)
|
||
{
|
||
var frame = new ImageBuffer(37, 5, 3);
|
||
var data = frame.Data;
|
||
for (var i = 0; i < data.Length; i++) data[i] = (float)random.NextDouble();
|
||
frames.Add(frame);
|
||
}
|
||
using var output = new ImageBuffer(37, 5, 3);
|
||
TemporalStacker.Median(frames, output);
|
||
for (var i = 0; i < output.Data.Length; i++)
|
||
{
|
||
var sorted = frames.Select(f => f.Data[i]).Order().ToArray();
|
||
var expected = count % 2 == 1 ? sorted[count / 2] : 0.5f * (sorted[count / 2 - 1] + sorted[count / 2]);
|
||
Assert.Equal(expected, output.Data[i]);
|
||
}
|
||
}
|
||
finally
|
||
{
|
||
foreach (var f in frames) f.Dispose();
|
||
}
|
||
}
|
||
|
||
[Test]
|
||
public void MeanReducesNoiseBySquareRootOfN()
|
||
{
|
||
const int n = 16;
|
||
const double sigma = 0.05;
|
||
var random = new Random(3);
|
||
using var stacker = new TemporalStacker(n, StackMode.Mean);
|
||
using var frame = new ImageBuffer(Size, Size, 1);
|
||
for (var k = 0; k < n; k++)
|
||
{
|
||
var data = frame.Data;
|
||
for (var i = 0; i < data.Length; i++) data[i] = 0.4f + (float)(sigma * Gaussian(random));
|
||
stacker.Push(frame);
|
||
}
|
||
using var output = new ImageBuffer(Size, Size, 1);
|
||
stacker.Render(output);
|
||
var values = output.Data.ToArray();
|
||
var mean = values.Average(v => (double)v);
|
||
var std = double.Sqrt(values.Average(v => (v - mean) * (v - mean)));
|
||
_context.Log($"residual σ {std:F5} (expected {sigma / double.Sqrt(n):F5})");
|
||
Assert.Approximately(0.4, mean, 0.002);
|
||
Assert.Approximately(sigma / double.Sqrt(n), std, 0.15 * sigma / double.Sqrt(n));
|
||
}
|
||
|
||
[Test]
|
||
public void WindowSlidesOverTheNewestFrames()
|
||
{
|
||
using var stacker = new TemporalStacker(3, StackMode.Median);
|
||
using var frame = new ImageBuffer(4, 4, 1);
|
||
for (var k = 0; k < 10; k++)
|
||
{
|
||
frame.Data.Fill(k);
|
||
stacker.Push(frame);
|
||
}
|
||
Assert.Equal(3, stacker.Count);
|
||
using var output = new ImageBuffer(4, 4, 1);
|
||
stacker.Render(output);
|
||
Assert.True(output.Data.ToArray().All(v => v == 8f));
|
||
using var other = new ImageBuffer(5, 4, 1);
|
||
Assert.Throws<ArgumentException>(() => stacker.Push(other));
|
||
stacker.Clear();
|
||
Assert.Throws<InvalidOperationException>(() => stacker.Render(output));
|
||
}
|
||
|
||
private static int StarX(int k) => 20 + k;
|
||
|
||
private static int StarY(int k) => 30 + k / 3;
|
||
|
||
/// <summary>Dark noisy sky with one star drifting one pixel per frame and a few twinkling faint ones.</summary>
|
||
private static void NightFrame(ImageBuffer frame, Random random, int k)
|
||
{
|
||
var data = frame.Data;
|
||
for (var i = 0; i < data.Length; i++) data[i] = 0.002f + 0.001f * (float)random.NextDouble();
|
||
for (var c = 0; c < 3; c++)
|
||
{
|
||
frame[c, StarX(k), StarY(k)] = 0.9f;
|
||
frame[c, 60, 70] = 0.1f + 0.05f * (k % 3);
|
||
}
|
||
}
|
||
|
||
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);
|
||
}
|
||
}
|