ManagedDecoderProvider decodes JPEG/PNG/TIFF natively, DNG and the supported RAWs through RawDecoder, and falls back to the largest suitable embedded JPEG (oriented like the main image) for other RAWs, CR3, HEIF and unsupported bodies. Requests use JPEG DCT scaling, CFA binning when MaxLongEdge allows, bilinear demosaic for thumbnails and analysis, and ForcePreview. DecoderFactory builds the client (WIC then managed) and server chains and the per-format tier table; Probe gives the per-file tier without decoding. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
225 lines
10 KiB
C#
225 lines
10 KiB
C#
using Titano.Core.Codecs.Dng;
|
|
using Titano.Core.Codecs.Raw;
|
|
using Titano.Core.Imaging;
|
|
using Titano.Core.Mathematics;
|
|
using Titano.Testing;
|
|
|
|
namespace Titano.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 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) => Titano.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);
|
|
}
|
|
}
|