Files
Chronos/tests/Titano.Core.Tests/Raw/NativeRawTests.cs
T
Claude 7e157cbf72 feat(raw): native CR2, NEF, ARW, PEF and RAF decoders
Each container reader builds a RawFrame for the shared pipeline: CR2
sliced lossless JPEG with SensorInfo crop, masked-column black and
ColorData WB; NEF uncompressed and Nikon Huffman (lossy type 1,
lossless, lossy type 2 incl. split); ARW uncompressed and ARW2 cRAW
with tone curve; PEF uncompressed and Pentax Huffman; RAF uncompressed
Bayer and X-Trans with FujiIFD. Colour comes from a dcraw adobe_coeff
matrix table; bodies without an entry are reported unsupported so the
caller uses the embedded preview. Native RAWs detect the sensor's
saturation level for clipping only, never rescaling per frame.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 11:13:18 +00:00

184 lines
8.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 Titano.Core.Codecs.Raw;
using Titano.Core.Imaging;
using Titano.Testing;
using static Titano.Core.Tests.Raw.NativeRawWriters;
namespace Titano.Core.Tests.Raw;
/// <summary>Native RAW decoders on synthetic files written in each container's layout: the decoded image must match the
/// scene the sensor data was generated from (camera matrix, as-shot white balance, black/white levels, crop).</summary>
public sealed class NativeRawTests(TestContext context)
{
private const int W = 96, H = 64;
private RawDecodeResult Decode(byte[] file, string name, RawDecodeOptions? options = null)
{
var result = RawDecoder.Decode(new MemoryStream(file), name, options);
context.Log($"{name}: {result.Info} warnings: {string.Join("; ", result.Warnings)}");
return result;
}
private static void AssertScene(ImageBuffer scene, ImageBuffer image, double minimum, TestContext context)
{
Assert.Equal(scene.Width, image.Width);
Assert.Equal(scene.Height, image.Height);
var psnr = SyntheticRaw.Psnr(scene, image, 4);
context.Log($"PSNR {psnr:F1} dB");
Assert.GreaterOrEqual(psnr, minimum);
}
[Test]
public void Cr2SlicedLosslessJpegWithMaskedBlack()
{
using var scene = SyntheticRaw.Smooth(W, H);
// 24 masked columns left and 4 rows on top; Canon's CFA is RGGB at the raster origin.
var active = new RawRect(24, 4, 24 + W, 4 + H);
var sensor = Sensor(scene, "Canon EOS 5D Mark III", CfaPattern.Bayer("RGGB"), 128, 72, active.Left, active.Top, 2048, 16383);
using var result = Decode(Cr2(sensor, active), "IMG_0001.CR2");
Assert.Equal("managed-cr2", result.Info.Decoder);
Assert.Equal("RGGB", result.Info.Layout);
AssertScene(scene, result.Image, 38, context);
}
[Theory]
[Case(NefEncoding.Lossless, 14)]
[Case(NefEncoding.Lossless, 12)]
[Case(NefEncoding.Unpacked, 14)]
[Case(NefEncoding.Packed, 12)]
public void NefEncodings(NefEncoding encoding, int bits)
{
using var scene = SyntheticRaw.Smooth(W, H);
var sensor = Sensor(scene, "Nikon D850", CfaPattern.Bayer("RGGB"), W, H, 0, 0, 600 >> (14 - bits), (1 << bits) - 1);
using var result = Decode(Nef(sensor, bits, encoding), "DSC_0001.NEF");
Assert.Equal("managed-nef", result.Info.Decoder);
AssertScene(scene, result.Image, 38, context);
}
[Theory]
[Case(NefEncoding.LossyType1, 12, 0)]
[Case(NefEncoding.LossyType2, 14, 0)]
[Case(NefEncoding.LossyType2, 14, 40)]
public void NefLossyCurves(NefEncoding encoding, int bits, int split)
{
// A square-law curve: dense codes in the shadows, coarse in the highlights.
var size = encoding == NefEncoding.LossyType1 ? 683 : 1 << bits;
var curve = Enumerable.Range(0, size).Select(i => (ushort)double.Round(((1 << bits) - 1) * (i / (double)(size - 1)) * (i / (double)(size - 1)))).ToArray();
var white = encoding == NefEncoding.LossyType1 ? curve[^1] : InterpolatedCurve(curve, bits).Max();
// After the split row the tree has no unshifted codes for 9–12-bit differences: keep that scene gentle.
using var scene = split > 0 ? Gentle() : SyntheticRaw.Smooth(W, H);
var sensor = Sensor(scene, "Nikon D850", CfaPattern.Bayer("RGGB"), W, H, 0, 0, 600 >> (14 - bits), white);
using var result = Decode(Nef(sensor, bits, encoding, curve: curve, split: split), "DSC_0002.NEF");
AssertScene(scene, result.Image, 36, context);
}
[Theory]
[Case(true)]
[Case(false)]
public void ArwCompressedAndUncompressed(bool compressed)
{
ushort[] knots = [8000, 10400, 12900, 14100];
var curve = ToneCurve(knots);
double white = compressed ? curve[0x7FF << 1] : 16383;
using var scene = SyntheticRaw.Smooth(W, H);
var sensor = Sensor(scene, "Sony ILCE-7M3", CfaPattern.Bayer("RGGB"), W, H, 0, 0, 512, white);
using var result = Decode(Arw(sensor, knots, compressed), "DSC00001.ARW");
Assert.Equal("managed-arw", result.Info.Decoder);
Assert.Contains(result.Warnings, w => w.Contains("daylight"));
AssertScene(scene, result.Image, compressed ? 36 : 38, context);
}
[Theory]
[Case(true)]
[Case(false)]
public void PefHuffmanAndUncompressed(bool compressed)
{
using var scene = SyntheticRaw.Smooth(W, H);
var sensor = Sensor(scene, "Pentax K-1", CfaPattern.Bayer("BGGR"), W, H, 0, 0, 512, 16383);
using var result = Decode(Pef(sensor, 14, compressed), "IMGP0001.PEF");
Assert.Equal("managed-pef", result.Info.Decoder);
Assert.Equal("BGGR", result.Info.Layout);
AssertScene(scene, result.Image, 38, context);
}
[Theory]
[Case(true)]
[Case(false)]
public void RafXTransAndBayerLayouts(bool xtrans)
{
using var scene = SyntheticRaw.Smooth(W, H);
var rasterCfa = xtrans ? DemosaicTests.XTrans : CfaPattern.Bayer("GRBG");
// Crop starts at an odd offset so the pattern must be re-anchored.
var crop = new RawRect(5, 3, 5 + W, 3 + H);
var sensor = Sensor(scene, "Fujifilm X-T2", rasterCfa, W + 8, H + 6, crop.Left, crop.Top, 1024, 16383);
using var result = Decode(Raf(sensor, Expand(rasterCfa), crop, orientation: 1), "DSCF0001.RAF");
Assert.Equal("managed-raf", result.Info.Decoder);
Assert.Equal("X-T2", result.Info.Model);
AssertScene(scene, result.Image, xtrans ? 34 : 38, context);
using var binned = Decode(Raf(sensor, Expand(rasterCfa), crop), "DSCF0001.RAF", new RawDecodeOptions { Binning = true });
Assert.Equal(W / (xtrans ? 3 : 2), binned.Image.Width);
}
[Test]
public void OrientationComesFromTheContainer()
{
using var scene = SyntheticRaw.Smooth(W, H);
var sensor = Sensor(scene, "Nikon D850", CfaPattern.Bayer("RGGB"), W, H, 0, 0, 600, 16383);
using var result = Decode(Nef(sensor, 14, NefEncoding.Lossless, orientation: 8), "DSC_0003.NEF");
Assert.Equal(H, result.Image.Width);
Assert.Equal(W, result.Image.Height);
Assert.Equal(H, result.Info.Width);
}
[Test]
public void UnknownBodyIsUnsupported()
{
using var scene = SyntheticRaw.Smooth(W, H);
var active = new RawRect(24, 4, 24 + W, 4 + H);
var sensor = Sensor(scene, "Canon EOS 5D Mark III", CfaPattern.Bayer("RGGB"), 128, 72, active.Left, active.Top, 2048, 16383);
var probe = RawDecoder.Probe(new MemoryStream(Cr2(sensor, active, model: "Canon EOS R99")), "x.cr2");
Assert.False(probe.IsSupported);
Assert.Contains("no colour matrix", probe.Reason);
Assert.Throws<InvalidDataException>(() => RawDecoder.Decode(new MemoryStream(Cr2(sensor, active, model: "Canon EOS R99")), "x.cr2"));
}
[Test]
public void CameraNamesNormalise()
{
Assert.Equal("Nikon D850", NativeRaw.CameraName("NIKON CORPORATION", "NIKON D850"));
Assert.Equal("Canon EOS 5D Mark III", NativeRaw.CameraName("Canon", "Canon EOS 5D Mark III"));
Assert.Equal("Sony ILCE-7M3", NativeRaw.CameraName("SONY", "ILCE-7M3"));
Assert.Equal("Pentax K-1", NativeRaw.CameraName("RICOH IMAGING COMPANY, LTD.", "PENTAX K-1"));
Assert.Equal("Fujifilm X-T2", NativeRaw.CameraName("FUJIFILM", "X-T2"));
// Every tabulated matrix maps D65 to a positive camera neutral with green the strongest channel.
foreach (var name in CameraMatrices.Names)
{
var m = CameraMatrices.Find(name)!.Value;
var n = m * Titano.Core.Mathematics.Chromaticity.D65.ToXyz();
Assert.True(n.X > 0 && n.Z > 0 && n.Y > n.X && n.Y > n.Z, name);
}
}
private static ImageBuffer Gentle()
{
var scene = new ImageBuffer(W, H);
for (var y = 0; y < H; y++)
{
for (var x = 0; x < W; x++)
{
scene[0, x, y] = 0.3f + 0.03f * x / W;
scene[1, x, y] = 0.32f + 0.02f * y / H;
scene[2, x, y] = 0.25f + 0.01f * (x + y) / (W + H);
}
}
return scene;
}
/// <summary>The pattern written into the RAF layout record, expanded to 6×6.</summary>
private static CfaPattern Expand(CfaPattern cfa)
{
var colors = new byte[36];
for (var i = 0; i < 36; i++) colors[i] = (byte)cfa.ColorAt(i % 6, i / 6);
return new CfaPattern(6, 6, colors);
}
}