Files
Chronos/tests/Chronos.Core.Tests/Codecs/CodecTestData.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

168 lines
6.4 KiB
C#

using System.Text;
using Chronos.Testing;
namespace Chronos.Core.Tests.Codecs;
/// <summary>Shared fixtures, synthetic images and image metrics for codec tests.</summary>
internal static class CodecTestData
{
public static byte[] Fixture(string relativePath) =>
File.ReadAllBytes(Path.Combine(TestContext.FixturesDirectory, "codecs", relativePath));
/// <summary>Netpbm P5/P6/P7 (PAM) reader for reference images; samples are big-endian when maxval &gt; 255.</summary>
public static Pnm ReadPnm(byte[] data)
{
var pos = 0;
string Token()
{
while (pos < data.Length && (char.IsWhiteSpace((char)data[pos]) || data[pos] == '#'))
{
if (data[pos] == '#') while (pos < data.Length && data[pos] != '\n') pos++;
else pos++;
}
var start = pos;
while (pos < data.Length && !char.IsWhiteSpace((char)data[pos])) pos++;
return Encoding.ASCII.GetString(data, start, pos - start);
}
var magic = Token();
int width, height, channels, maxVal;
if (magic == "P7")
{
width = height = channels = maxVal = 0;
for (var t = Token(); t != "ENDHDR"; t = Token())
{
switch (t)
{
case "WIDTH": width = int.Parse(Token()); break;
case "HEIGHT": height = int.Parse(Token()); break;
case "DEPTH": channels = int.Parse(Token()); break;
case "MAXVAL": maxVal = int.Parse(Token()); break;
case "TUPLTYPE": Token(); break;
}
}
}
else
{
channels = magic switch { "P5" => 1, "P6" => 3, _ => throw new InvalidDataException(magic) };
width = int.Parse(Token());
height = int.Parse(Token());
maxVal = int.Parse(Token());
}
pos++;
var samples = new int[width * height * channels];
for (var i = 0; i < samples.Length; i++)
{
samples[i] = maxVal > 255 ? data[pos + 2 * i] << 8 | data[pos + 2 * i + 1] : data[pos + i];
}
return new Pnm(width, height, channels, maxVal, samples);
}
public sealed record Pnm(int Width, int Height, int Channels, int MaxVal, int[] Samples)
{
public byte[] Bytes => Samples.Select(s => (byte)s).ToArray();
public ushort[] Words => Samples.Select(s => (ushort)s).ToArray();
}
/// <summary>Deterministic RGB test card: smooth gradients, saturated colour bars, seeded noise and
/// text-like one-pixel strokes (the four content classes codecs struggle with).</summary>
public static byte[] TestCard(int width, int height, int seed = 1)
{
var rng = new Random(seed);
var rgb = new byte[width * height * 3];
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
var band = y * 4 / height;
int r, g, b;
switch (band)
{
case 0:
r = x * 255 / Math.Max(1, width - 1);
g = y * 4 * 255 / Math.Max(1, height - 1) / 4 * 4 % 256;
b = 255 - r;
break;
case 1:
var bar = x * 8 / width;
r = (bar & 4) != 0 ? 230 : 25;
g = (bar & 2) != 0 ? 230 : 25;
b = (bar & 1) != 0 ? 230 : 25;
break;
case 2:
r = rng.Next(256);
g = rng.Next(256);
b = rng.Next(256);
break;
default:
var stroke = x % 7 == 0 || y % 9 == 0 || (x + y) % 13 == 0;
r = g = b = stroke ? 20 : 235;
break;
}
var i = (y * width + x) * 3;
rgb[i] = (byte)r;
rgb[i + 1] = (byte)g;
rgb[i + 2] = (byte)b;
}
}
return rgb;
}
/// <summary>Smooth photographic-like content (low-frequency gradients); compresses like real photos.</summary>
public static byte[] SmoothImage(int width, int height)
{
var rgb = new byte[width * height * 3];
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
double u = x / (double)width, v = y / (double)height;
var i = (y * width + x) * 3;
rgb[i] = (byte)(127.5 + 120 * Math.Sin(6.1 * u + 2.3 * v));
rgb[i + 1] = (byte)(127.5 + 120 * Math.Cos(4.7 * u * v + 3.1 * v));
rgb[i + 2] = (byte)(127.5 + 120 * Math.Sin(9.3 * v - 1.7 * u + 0.5));
}
}
return rgb;
}
public static double Psnr(ReadOnlySpan<byte> a, ReadOnlySpan<byte> b) => Psnr(a, b, 255);
public static double Psnr<T>(ReadOnlySpan<T> a, ReadOnlySpan<T> b, double peak) where T : struct, IConvertible
{
Assert.Equal(a.Length, b.Length, "sample count");
double sum = 0;
for (var i = 0; i < a.Length; i++)
{
var d = a[i].ToDouble(null) - b[i].ToDouble(null);
sum += d * d;
}
var mse = sum / a.Length;
return mse == 0 ? 99 : 10 * Math.Log10(peak * peak / mse);
}
public static int MaxAbsDiff(ReadOnlySpan<byte> a, ReadOnlySpan<int> b)
{
Assert.Equal(a.Length, b.Length, "sample count");
var max = 0;
for (var i = 0; i < a.Length; i++) max = Math.Max(max, Math.Abs(a[i] - b[i]));
return max;
}
/// <summary>Seeded corruption: the input truncated at <paramref name="cases"/> points plus single-byte flips.</summary>
public static IEnumerable<byte[]> Corruptions(byte[] valid, int cases, int seed)
{
var rng = new Random(seed);
for (var i = 0; i < cases; i++)
{
yield return valid.AsSpan(0, rng.Next(valid.Length)).ToArray();
var flipped = (byte[])valid.Clone();
var flips = 1 + rng.Next(4);
for (var f = 0; f < flips; f++) flipped[rng.Next(flipped.Length)] ^= (byte)(1 << rng.Next(8));
yield return flipped;
var garbage = (byte[])valid.Clone();
garbage[rng.Next(garbage.Length)] = (byte)rng.Next(256);
yield return garbage;
}
}
}