Files
Chronos/tests/Chronos.Core.Tests/Imaging/ResamplerTests.cs
T
Alby96andClaude Opus 5.5 5f27a86406
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
refactor!: rename Titano to Chronos and move CI and releases to Gitea
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>
2026-10-23 11:34:48 +02:00

157 lines
6.3 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 System.Diagnostics;
using Chronos.Core.Imaging;
using Chronos.Testing;
namespace Chronos.Core.Tests.Imaging;
public sealed class ResamplerTests(TestContext context)
{
private static ImageBuffer Filled(int w, int h, Func<int, int, int, float> value, int channels = 3)
{
var image = new ImageBuffer(w, h, channels);
for (var c = 0; c < channels; c++)
{
for (var y = 0; y < h; y++)
{
for (var x = 0; x < w; x++) image[c, x, y] = value(c, x, y);
}
}
return image;
}
[Theory]
[Case(ResampleFilter.Lanczos3, 97, 31)]
[Case(ResampleFilter.Lanczos3, 400, 260)]
[Case(ResampleFilter.CatmullRom, 400, 260)]
[Case(ResampleFilter.CatmullRom, 20, 15)]
[Case(ResampleFilter.Box, 37, 23)]
[Case(ResampleFilter.Box, 333, 211)]
[Case(ResampleFilter.Auto, 300, 40)]
public void ConstantImageStaysConstant(ResampleFilter filter, int width, int height)
{
using var source = Filled(150, 100, (c, _, _) => 0.25f + 0.25f * c);
using var result = Resampler.Resize(source, width, height, filter);
Assert.Equal(width, result.Width);
Assert.Equal(height, result.Height);
for (var c = 0; c < 3; c++)
{
foreach (var v in result.Plane(c)) Assert.Approximately(0.25 + 0.25 * c, v, 1e-5);
}
}
[Theory]
[Case(ResampleFilter.Lanczos3)]
[Case(ResampleFilter.Box)]
[Case(ResampleFilter.CatmullRom)]
public void DownscalePreservesEnergy(ResampleFilter filter)
{
var rng = new Random(4);
using var source = Filled(640, 480, (_, x, y) => (float)(rng.NextDouble() * 0.5 + 0.4 * Math.Sin(x * 0.05) * Math.Cos(y * 0.07) + 0.4), 1);
using var result = Resampler.Resize(source, 213, 157, filter);
double Mean(ImageBuffer b) => b.Plane(0).ToArray().Average(v => (double)v);
var ratio = Mean(result) / Mean(source);
context.Log($"{filter}: mean ratio {ratio:F5}");
Assert.Approximately(1, ratio, 0.005);
}
[Theory]
[Case(ResampleFilter.Lanczos3, 0.3)]
[Case(ResampleFilter.Lanczos3, 2.7)]
[Case(ResampleFilter.CatmullRom, 3.0)]
[Case(ResampleFilter.CatmullRom, 0.45)]
[Case(ResampleFilter.Box, 0.3)]
public void StepRingingStaysWithinKernelBounds(ResampleFilter filter, double scale)
{
const int w = 200;
using var step = Filled(w, 4, (_, x, _) => x < w / 2 + 0.3 ? 0f : 1f, 1);
using var result = Resampler.Resize(step, (int)(w * scale), 4, filter);
var values = result.Row(0, 1).ToArray();
var bound = StepBound(filter, scale);
context.Log($"{filter} ×{scale}: range [{values.Min():F4}, {values.Max():F4}], bound {bound:F4}");
Assert.GreaterOrEqual(values.Min(), -bound - 1e-4);
Assert.LessOrEqual(values.Max(), 1 + bound + 1e-4);
if (filter == ResampleFilter.Box)
{
Assert.GreaterOrEqual(values.Min(), 0);
Assert.LessOrEqual(values.Max(), 1 + 1e-6);
}
Assert.Approximately(0, values[0], 1e-5);
Assert.Approximately(1, values[^1], 1e-5);
}
/// <summary>
/// The "Lanczos bound" for a step at this scale: every output sample is a normalised sum of kernel taps spaced
/// 1/stretch apart, so on a step it equals a partial sum of those taps. The bound is the largest excursion
/// outside [0, 1] of any partial sum, over all sub-pixel phases.
/// </summary>
private static double StepBound(ResampleFilter filter, double scale)
{
if (filter == ResampleFilter.Box) return 0;
var radius = filter == ResampleFilter.Lanczos3 ? 3.0 : 2.0;
var stretch = Math.Max(1, 1 / scale);
var reach = (int)Math.Ceiling(radius * stretch) + 1;
var worst = 0.0;
for (var phase = 0.0; phase < 1; phase += 1 / 512.0)
{
var taps = new List<double>();
for (var k = -reach; k <= reach; k++) taps.Add(Kernel(filter, (k + phase) / stretch));
var total = taps.Sum();
var running = 0.0;
foreach (var t in taps)
{
running += t / total;
worst = Math.Max(worst, Math.Max(running - 1, -running));
}
}
return worst;
}
private static double Kernel(ResampleFilter filter, double x)
{
var a = Math.Abs(x);
if (filter == ResampleFilter.Lanczos3) return a < 1e-12 ? 1 : a >= 3 ? 0 : 3 * Math.Sin(Math.PI * a) * Math.Sin(Math.PI * a / 3) / (Math.PI * Math.PI * a * a);
return a < 1 ? (1.5 * a - 2.5) * a * a + 1 : a < 2 ? ((-0.5 * a + 2.5) * a - 4) * a + 2 : 0;
}
[Test]
public void ThumbnailFitsAndAveragesAreas()
{
using var source = Filled(1200, 800, (c, x, _) => x % 2 == 0 ? 1f : 0f);
using var thumb = Resampler.Thumbnail(source, 300);
Assert.Equal(300, thumb.Width);
Assert.Equal(200, thumb.Height);
foreach (var v in thumb.Plane(1)) Assert.Approximately(0.5, v, 1e-5);
Assert.Equal((300, 200), Resampler.FitWithin(1200, 800, 300));
Assert.Equal((10, 20), Resampler.FitWithin(10, 20, 300));
using var small = Resampler.Thumbnail(thumb, 1000);
Assert.Equal(300, small.Width);
}
[Test]
public void UpscaleInterpolatesThroughSamples()
{
using var source = Filled(16, 16, (_, x, y) => x * 0.05f + y * 0.01f, 1);
using var up = Resampler.Resize(source, 64, 64, ResampleFilter.CatmullRom);
// Catmull-Rom reproduces linear ramps exactly away from the edges.
for (var x = 8; x < 56; x++)
{
var sx = (x + 0.5) / 4 - 0.5;
Assert.Approximately(sx * 0.05 + ((20 + 0.5) / 4 - 0.5) * 0.01, up[0, x, 20], 1e-4);
}
}
[Test]
public void TwentyFourMegapixelThumbnailIsFast()
{
using var source = new ImageBuffer(6000, 4000, 3, clear: true);
var sw = Stopwatch.StartNew();
using var thumb = Resampler.Thumbnail(source, 320);
var box = sw.Elapsed;
sw.Restart();
using var lanczos = Resampler.Resize(source, 1920, 1280);
context.Log($"24 MP → 320 px box {box.TotalMilliseconds:F0} ms; → 1920 px Lanczos3 {sw.Elapsed.TotalMilliseconds:F0} ms");
Assert.Equal(320, thumb.Width);
Assert.Less(box.TotalSeconds, 2.5);
}
}