Files
Chronos/tests/Chronos.Core.Tests/Raw/DngDecoderTests.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

278 lines
12 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.Codecs.Dng;
using Chronos.Core.Codecs.Raw;
using Chronos.Core.Imaging;
using Chronos.Core.Mathematics;
using Chronos.Testing;
namespace Chronos.Core.Tests.Raw;
/// <summary>Synthetic DNG round trips: a known linear scene is mosaicked through a known camera model, written with the
/// DNG feature under test, decoded, and compared with the scene.</summary>
public sealed class DngDecoderTests(TestContext context)
{
private const int W = 96, H = 72;
private static RawDecodeResult Decode(byte[] dng, RawDecodeOptions? options = null) =>
RawDecoder.Decode(new MemoryStream(dng), "test.dng", options);
[Theory]
[Case("RGGB")]
[Case("BGGR")]
[Case("GRBG")]
[Case("GBRG")]
public void BayerPatternsRoundTrip(string layout)
{
using var scene = SyntheticRaw.Smooth(W, H);
var dng = SyntheticDng.Write(scene, new DngSpec { Cfa = CfaPattern.Bayer(layout) });
using var result = Decode(dng);
Assert.Equal(W, result.Image.Width);
Assert.Equal(H, result.Image.Height);
Assert.Equal(layout, result.Info.Layout);
var psnr = SyntheticRaw.Psnr(scene, result.Image, 4);
var de = SyntheticRaw.MeanDeltaE(scene, result.Image, 4);
context.Log($"{layout}: PSNR {psnr:F1} dB, mean ΔE {de:F2}");
Assert.GreaterOrEqual(psnr, 40);
Assert.Less(de, 1.0);
}
[Theory]
[Case(1, false)]
[Case(7, false)]
[Case(7, true)]
[Case(8, false)]
public void CompressionsDecodeIdentically(int compression, bool twoComponents)
{
using var scene = SyntheticRaw.Smooth(W, H);
using var reference = Decode(SyntheticDng.Write(scene, new DngSpec { Compression = 1 }));
using var result = Decode(SyntheticDng.Write(scene, new DngSpec { Compression = compression, TwoComponentTiles = twoComponents, RawInSubIfd = true }));
Assert.Empty(result.Warnings);
Assert.SequenceEqual(reference.Image.Data.ToArray(), result.Image.Data.ToArray());
}
[Test]
public void LossyJpegTilesWithLinearizationTable()
{
// Compression 34892: 8-bit JPEG tiles of sRGB-shaped codes, mapped back to linear by a 256-entry table.
var table = Enumerable.Range(0, 256).Select(i => (ushort)double.Round(ColorSpace.SrgbToLinear(i / 255.0) * 16383)).ToArray();
using var scene = SyntheticRaw.Smooth(W, H);
using var result = Decode(SyntheticDng.Write(scene, new DngSpec { Cfa = null, Compression = 34892, Linearization = table, Black = [0], TileSize = 32 }));
var psnr = SyntheticRaw.Psnr(scene, result.Image, 0);
context.Log($"lossy JPEG DNG: PSNR {psnr:F1} dB");
Assert.GreaterOrEqual(psnr, 36);
}
[Theory]
[Case(false)]
[Case(true)]
public void FloatingPointPredictorWithDeflate(bool mosaic)
{
using var scene = SyntheticRaw.Smooth(W, H);
using var result = Decode(SyntheticDng.Write(scene, new DngSpec { Cfa = mosaic ? CfaPattern.Bayer("RGGB") : null, FloatingPoint = true }));
Assert.GreaterOrEqual(SyntheticRaw.Psnr(scene, result.Image, 4), mosaic ? 40 : 60);
}
[Test]
public void CameraCalibrationAndAnalogBalanceEnterTheColourTransform()
{
var cc = Matrix3.Diagonal(1.08, 1.0, 0.93);
var ab = new Vector3D(1.05, 1.0, 0.97);
using var scene = SyntheticRaw.Smooth(W, H);
var spec = new DngSpec
{
Cfa = null,
CameraCalibration1 = cc,
AnalogBalance = ab,
CaptureMatrix = Matrix3.Diagonal(ab.X, ab.Y, ab.Z) * cc * SyntheticRaw.CameraMatrix,
};
using var result = Decode(SyntheticDng.Write(scene, spec));
Assert.GreaterOrEqual(SyntheticRaw.Psnr(scene, result.Image, 0), 55);
}
[Test]
public void ValueMappingOpcodesInListThreeSeeUnbalancedValues()
{
// Stored halved; MapPolynomial ×2 in OpcodeList3 restores the scene only if applied before white balance.
var ops = new OpcodeListBuilder().Add(8, 0, p =>
{
OpcodeListBuilder.Area(p, 0, 0, H, W, planes: 3);
p.U32(1).F64(0).F64(2);
}).Build();
using var scene = SyntheticRaw.Smooth(W, H);
using var result = Decode(SyntheticDng.Write(scene, new DngSpec { Cfa = null, OpcodeList3 = ops, RawTransform = (_, _, v) => v / 2 }));
Assert.GreaterOrEqual(SyntheticRaw.Psnr(scene, result.Image, 0), 55);
}
[Test]
public void LinearRawRoundTripsExactly()
{
using var scene = SyntheticRaw.Smooth(W, H);
using var result = Decode(SyntheticDng.Write(scene, new DngSpec { Cfa = null }));
Assert.Equal("linear", result.Info.Layout);
Assert.GreaterOrEqual(SyntheticRaw.Psnr(scene, result.Image, 0), 60);
}
[Test]
public void BlackLevelPatternAndDeltasAreSubtracted()
{
using var scene = SyntheticRaw.Smooth(W, H);
var deltaH = Enumerable.Range(0, W).Select(x => (double)(x % 7)).ToArray();
var deltaV = Enumerable.Range(0, H).Select(y => y * 0.5).ToArray();
var spec = new DngSpec { BlackRows = 2, BlackColumns = 2, Black = [500, 520, 530, 510], BlackDeltaH = deltaH, BlackDeltaV = deltaV, MarginLeft = 4, MarginTop = 2 };
using var result = Decode(SyntheticDng.Write(scene, spec));
Assert.GreaterOrEqual(SyntheticRaw.Psnr(scene, result.Image, 4), 40);
}
[Test]
public void LinearizationTableIsApplied()
{
using var scene = SyntheticRaw.Smooth(W, H);
// 4096 codes covering 0..16383 with a square law: dense in the shadows, sparse in the highlights.
var table = Enumerable.Range(0, 4096).Select(i => (ushort)double.Round(16383.0 * i * i / (4095.0 * 4095.0))).ToArray();
using var result = Decode(SyntheticDng.Write(scene, new DngSpec { Linearization = table, Black = [0] }));
Assert.GreaterOrEqual(SyntheticRaw.Psnr(scene, result.Image, 4), 38);
}
[Test]
public void ActiveAreaAndDefaultCropSelectTheImage()
{
using var scene = SyntheticRaw.Smooth(W, H);
var spec = new DngSpec { MarginLeft = 6, MarginTop = 4, MarginRight = 2, MarginBottom = 3, Crop = new RawRect(8, 6, 8 + 64, 6 + 48) };
using var result = Decode(SyntheticDng.Write(scene, spec));
Assert.Equal(64, result.Image.Width);
Assert.Equal(48, result.Image.Height);
Assert.Equal(64, result.Info.Width);
using var expected = Crop(scene, 8, 6, 64, 48);
Assert.GreaterOrEqual(SyntheticRaw.Psnr(expected, result.Image, 2), 40);
}
[Theory]
[Case(false)]
[Case(true)]
public void WhiteBalanceFromAsShotNeutralOrWhiteXy(bool whiteXy)
{
// Scene lit by tungsten (2850 K): decoding must adapt it back to the D65 scene.
using var scene = SyntheticRaw.Smooth(W, H);
var tungsten = new Chromaticity(0.44757, 0.40745);
using var result = Decode(SyntheticDng.Write(scene, new DngSpec { SceneWhite = tungsten, WriteWhiteXy = whiteXy }));
Assert.GreaterOrEqual(SyntheticRaw.Psnr(scene, result.Image, 4), 38);
Assert.Less(SyntheticRaw.MeanDeltaE(scene, result.Image, 4), 1.5);
}
[Test]
public void DualIlluminantMatricesInterpolateByTemperature()
{
var profile = new DngColorProfile
{
ColorMatrix1 = SyntheticRaw.TungstenMatrix,
CalibrationIlluminant1 = 17,
ColorMatrix2 = SyntheticRaw.CameraMatrix,
CalibrationIlluminant2 = 21,
};
var warm = new Chromaticity(0.44757, 0.40745);
var mid = ColorTemperatureXy(4200);
Assert.Approximately(2850, DngColorProfile.Temperature(warm), 15);
Assert.Approximately(6504, DngColorProfile.Temperature(Chromaticity.D65), 30);
AssertMatrix(SyntheticRaw.TungstenMatrix, profile.XyzToCamera(warm), 1e-3);
AssertMatrix(SyntheticRaw.CameraMatrix, profile.XyzToCamera(Chromaticity.D65), 1e-9);
// Between the calibrations the weight is linear in inverse temperature.
var t = DngColorProfile.Temperature(mid);
var g = (1 / t - 1 / 6500.0) / (1 / 2850.0 - 1 / 6500.0);
Assert.Approximately(g * SyntheticRaw.TungstenMatrix.M11 + (1 - g) * SyntheticRaw.CameraMatrix.M11, profile.XyzToCamera(mid).M11, 1e-9);
// A DNG with both matrices, shot at 4200 K, decodes back to the scene.
using var scene = SyntheticRaw.Smooth(W, H);
var spec = new DngSpec
{
ColorMatrix1 = SyntheticRaw.TungstenMatrix,
Illuminant1 = 17,
ColorMatrix2 = SyntheticRaw.CameraMatrix,
Illuminant2 = 21,
CaptureMatrix = profile.XyzToCamera(mid),
SceneWhite = mid,
};
using var result = Decode(SyntheticDng.Write(scene, spec));
Assert.GreaterOrEqual(SyntheticRaw.Psnr(scene, result.Image, 4), 38);
}
[Test]
public void ForwardMatrixMapsNeutralToWhite()
{
// ForwardMatrix = Rec.709 → XYZ(D50) with camera = linear Rec.709: a gray scene must stay gray.
var fm = Matrix3.BradfordAdaptation(Chromaticity.D65.ToXyz(), Chromaticity.D50.ToXyz()) * Matrix3.Rec709ToXyz;
using var scene = new ImageBuffer(32, 32);
scene.Data.Fill(0.4f);
var spec = new DngSpec { Cfa = null, ColorMatrix1 = Matrix3.XyzToRec709, ForwardMatrix1 = fm };
using var result = Decode(SyntheticDng.Write(scene, spec));
for (var c = 0; c < 3; c++) Assert.Approximately(0.4, result.Image[c, 16, 16], 2e-3);
}
[Test]
public void BaselineExposureScalesOutput()
{
using var scene = SyntheticRaw.Smooth(W, H);
using var plain = Decode(SyntheticDng.Write(scene, new DngSpec { Cfa = null }));
using var bright = Decode(SyntheticDng.Write(scene, new DngSpec { Cfa = null, BaselineExposure = 1 }));
Assert.Approximately(2 * plain.Image[1, 40, 30], bright.Image[1, 40, 30], 1e-4);
}
[Theory]
[Case((ushort)6)]
[Case((ushort)3)]
public void OrientationIsApplied(ushort orientation)
{
using var scene = SyntheticRaw.Smooth(W, H);
var dng = SyntheticDng.Write(scene, new DngSpec { Cfa = null, Orientation = orientation });
using var oriented = Decode(dng);
using var stored = Decode(dng, new RawDecodeOptions { ApplyOrientation = false });
using var expected = PixelConvert.ApplyOrientation(stored.Image, orientation);
Assert.Equal(expected.Width, oriented.Image.Width);
Assert.Equal(expected.Width, oriented.Info.Width);
Assert.SequenceEqual(expected.Data.ToArray(), oriented.Image.Data.ToArray());
}
[Theory]
[Case(DemosaicQuality.Best)]
[Case(DemosaicQuality.Draft)]
public void BinningHalvesTheSize(DemosaicQuality quality)
{
using var scene = SyntheticRaw.Smooth(W, H);
using var result = Decode(SyntheticDng.Write(scene, new DngSpec()), new RawDecodeOptions { MaxLongEdge = W / 2, Quality = quality });
Assert.Equal(W / 2, result.Image.Width);
Assert.Equal(H / 2, result.Image.Height);
using var expected = Resampler.Resize(scene, W / 2, H / 2, ResampleFilter.Box);
Assert.GreaterOrEqual(SyntheticRaw.Psnr(expected, result.Image, 1), 34);
}
[Test]
public void ProbeDescribesWithoutDecoding()
{
using var scene = SyntheticRaw.Smooth(W, H);
var dng = SyntheticDng.Write(scene, new DngSpec { Orientation = 6, Crop = new RawRect(2, 2, 66, 50) });
var probe = RawDecoder.Probe(new MemoryStream(dng), "x.dng");
Assert.True(probe.IsSupported);
Assert.Equal(48, probe.Info!.Width);
Assert.Equal(64, probe.Info.Height);
Assert.Equal("managed-dng", probe.Info.Decoder);
Assert.Equal("Synthetic", probe.Info.Model);
}
internal static ImageBuffer Crop(ImageBuffer source, int x0, int y0, int w, int h)
{
var result = new ImageBuffer(w, h, source.Channels);
for (var c = 0; c < source.Channels; c++)
{
for (var y = 0; y < h; y++) source.Row(c, y0 + y).Slice(x0, w).CopyTo(result.Row(c, y));
}
return result;
}
private static Chromaticity ColorTemperatureXy(double kelvin) => Chronos.Core.Analysis.ColorTemperature.PlanckianChromaticity(kelvin);
private static void AssertMatrix(Matrix3 expected, Matrix3 actual, double tolerance)
{
double[] e = [expected.M11, expected.M12, expected.M13, expected.M21, expected.M22, expected.M23, expected.M31, expected.M32, expected.M33];
double[] a = [actual.M11, actual.M12, actual.M13, actual.M21, actual.M22, actual.M23, actual.M31, actual.M32, actual.M33];
for (var i = 0; i < 9; i++) Assert.Approximately(e[i], a[i], tolerance);
}
}