Files
Chronos/tests/Titano.Core.Tests/Raw/DngOpcodeTests.cs
T
Claude 1b3a691f76 feat(raw): DNG development pipeline with AHD demosaic, opcodes and DNG colour
Reads CFA and LinearRaw DNGs (compression 1/7/8/34892), linearises with
LinearizationTable, BlackLevel (repeat + deltas) and WhiteLevel over the
ActiveArea, applies OpcodeList1-3 (GainMap, WarpRectilinear,
FixVignetteRadial, FixBadPixels*, TrimBounds, MapTable/MapPolynomial,
Delta/ScalePer*), solves white balance per DNG 1.6 chapter 6 (CCT
interpolation, ForwardMatrix, Bradford to D65), demosaics with AHD
(Hirakawa & Parks 2005), bilinear, a neighbourhood interpolation for
non-Bayer CFAs or binning, and reconstructs clipped highlights.

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

222 lines
9.7 KiB
C#

using Titano.Core.Codecs.Raw;
using Titano.Core.Imaging;
using Titano.Testing;
namespace Titano.Core.Tests.Raw;
/// <summary>DNG opcode lists: raw data is distorted by the inverse of an opcode, the decoder must undo it.</summary>
public sealed class DngOpcodeTests(TestContext context)
{
private const int W = 96, H = 72;
private static RawDecodeResult Decode(byte[] dng) => RawDecoder.Decode(new MemoryStream(dng), "op.dng");
/// <summary>Bilinear grid interpolation as DNG §7 GainMap defines it (independent of the decoder's implementation).</summary>
private static double GainAt(double[,] grid, double spacingV, double spacingH, int x, int y)
{
int rows = grid.GetLength(0), cols = grid.GetLength(1);
double fr = double.Clamp(y / (double)H / spacingV, 0, rows - 1), fc = double.Clamp(x / (double)W / spacingH, 0, cols - 1);
int r0 = (int)fr, c0 = (int)fc, r1 = int.Min(r0 + 1, rows - 1), c1 = int.Min(c0 + 1, cols - 1);
double a = fr - r0, b = fc - c0;
return (grid[r0, c0] * (1 - b) + grid[r0, c1] * b) * (1 - a) + (grid[r1, c0] * (1 - b) + grid[r1, c1] * b) * a;
}
[Test]
public void GainMapRestoresShading()
{
double[,] grid = { { 1.6, 1.3, 1.6 }, { 1.2, 1.0, 1.25 }, { 1.7, 1.35, 1.5 } };
var ops = new OpcodeListBuilder().Add(9, 0, p =>
{
OpcodeListBuilder.Area(p, 0, 0, H, W);
p.U32(3).U32(3).F64(0.5).F64(0.5).F64(0).F64(0).U32(1);
for (var r = 0; r < 3; r++)
{
for (var c = 0; c < 3; c++) p.F32((float)grid[r, c]);
}
}).Build();
using var scene = SyntheticRaw.Smooth(W, H);
var spec = new DngSpec { OpcodeList2 = ops, RawTransform = (x, y, v) => (float)(v / GainAt(grid, 0.5, 0.5, x, y)) };
using var result = Decode(SyntheticDng.Write(scene, spec));
Assert.Empty(result.Warnings);
Assert.GreaterOrEqual(SyntheticRaw.Psnr(scene, result.Image, 4), 38);
}
[Test]
public void GainMapWithPitchTargetsOneCfaColour()
{
// Red sites of RGGB (even rows and columns) were attenuated by 1/1.5.
var ops = new OpcodeListBuilder().Add(9, 0, p =>
{
OpcodeListBuilder.Area(p, 0, 0, H, W, rowPitch: 2, colPitch: 2);
p.U32(1).U32(1).F64(1).F64(1).F64(0).F64(0).U32(1).F32(1.5f);
}).Build();
using var scene = SyntheticRaw.Smooth(W, H);
var spec = new DngSpec { OpcodeList2 = ops, RawTransform = (x, y, v) => x % 2 == 0 && y % 2 == 0 ? v / 1.5f : v };
using var result = Decode(SyntheticDng.Write(scene, spec));
Assert.GreaterOrEqual(SyntheticRaw.Psnr(scene, result.Image, 4), 38);
}
[Test]
public void FixVignetteRadialRestoresFalloff()
{
double k0 = 0.4, k1 = 0.2;
var ops = new OpcodeListBuilder().Add(3, 0, p => p.F64(k0).F64(k1).F64(0).F64(0).F64(0).F64(0.5).F64(0.5)).Build();
var m = double.Hypot(W / 2.0, H / 2.0);
using var scene = SyntheticRaw.Smooth(W, H);
var spec = new DngSpec
{
OpcodeList2 = ops,
RawTransform = (x, y, v) =>
{
var r2 = ((x - W / 2.0) * (x - W / 2.0) + (y - H / 2.0) * (y - H / 2.0)) / (m * m);
return (float)(v / (1 + k0 * r2 + k1 * r2 * r2));
},
};
using var result = Decode(SyntheticDng.Write(scene, spec));
Assert.GreaterOrEqual(SyntheticRaw.Psnr(scene, result.Image, 4), 38);
}
[Theory]
[Case(false)]
[Case(true)]
public void WarpRectilinearMovesPointsToTheirCorrectedPositions(bool mosaic)
{
// Corrected position p maps to source position f(p) (radial + tangential model, DNG §7 WarpRectilinear).
const int w = 160, h = 120;
double[] k = [1.0, 0.06, -0.01, 0.0, 0.002, -0.0015];
double cx = 0.48, cy = 0.53;
var ops = new OpcodeListBuilder().Add(1, 0, p =>
{
p.U32(1);
foreach (var v in k) p.F64(v);
p.F64(cx).F64(cy);
}).Build();
double ox = cx * w, oy = cy * h;
var m = new[] { (0.0, 0.0), (w, 0.0), (0.0, h), (w, h) }.Max(c => double.Hypot(c.Item1 - ox, c.Item2 - oy));
(double X, double Y) Source(double x, double y)
{
double dx = (x - ox) / m, dy = (y - oy) / m, r2 = dx * dx + dy * dy;
var f = k[0] + r2 * (k[1] + r2 * (k[2] + r2 * k[3]));
return (ox + m * (f * dx + k[4] * 2 * dx * dy + k[5] * (r2 + 2 * dx * dx)), oy + m * (f * dy + k[5] * 2 * dx * dy + k[4] * (r2 + 2 * dy * dy)));
}
(double X, double Y)[] targets = [(20, 18), (130, 25), (80, 60), (30, 100), (140, 104), (60, 40)];
using var distorted = new ImageBuffer(w, h);
foreach (var (tx, ty) in targets)
{
var (sx, sy) = Source(tx, ty);
for (var y = 0; y < h; y++)
{
for (var x = 0; x < w; x++)
{
var g = (float)(0.6 * double.Exp(-((x - sx) * (x - sx) + (y - sy) * (y - sy)) / (2 * 2.0 * 2.0)));
for (var c = 0; c < 3; c++) distorted[c, x, y] += g;
}
}
}
for (var i = 0; i < distorted.Data.Length; i++) distorted.Data[i] += 0.02f;
var spec = new DngSpec { Cfa = mosaic ? CfaPattern.Bayer("RGGB") : null, OpcodeList3 = ops };
using var result = Decode(SyntheticDng.Write(distorted, spec));
Assert.Empty(result.Warnings);
foreach (var (tx, ty) in targets)
{
var (x, y) = Centroid(result.Image, (int)tx, (int)ty, 6);
context.Log($"target ({tx}, {ty}) → ({x:F3}, {y:F3})");
Assert.Within(tx, ty, x, y, mosaic ? 0.2 : 0.1);
}
}
[Test]
public void TrimBoundsAndDefaultCropCompose()
{
var ops = new OpcodeListBuilder().Add(6, 0, p => p.U32(4).U32(10).U32(60).U32(90)).Build();
using var scene = SyntheticRaw.Smooth(W, H);
using var result = Decode(SyntheticDng.Write(scene, new DngSpec { OpcodeList3 = ops, Crop = new RawRect(0, 0, 80, 64) }));
Assert.Equal(70, result.Image.Width);
Assert.Equal(56, result.Image.Height);
using var expected = DngDecoderTests.Crop(scene, 10, 4, 70, 56);
Assert.GreaterOrEqual(SyntheticRaw.Psnr(expected, result.Image, 2), 38);
}
[Test]
public void FixBadPixelsConstantAndListRepairMosaic()
{
(int X, int Y)[] dead = [(10, 10), (41, 20), (70, 33)];
(int X, int Y)[] hot = [(30, 50), (55, 12)];
var list1 = new OpcodeListBuilder().Add(4, 0, p => p.U32(0).U32(0)).Build();
var list2 = new OpcodeListBuilder().Add(5, 0, p =>
{
p.U32(0).U32((uint)hot.Length).U32(1);
foreach (var (x, y) in hot) p.U32((uint)y).U32((uint)x);
p.U32(60).U32(80).U32(62).U32(83);
}).Build();
using var scene = SyntheticRaw.Smooth(W, H);
var spec = new DngSpec
{
OpcodeList1 = list1,
OpcodeList2 = list2,
RawTransform = (x, y, v) => dead.Contains((x, y)) ? -1f : hot.Contains((x, y)) || (y is >= 60 and < 62 && x is >= 80 and < 83) ? 1f : v,
};
using var result = Decode(SyntheticDng.Write(scene, spec));
Assert.GreaterOrEqual(SyntheticRaw.Psnr(scene, result.Image, 4), 38);
}
[Test]
public void MapTableAndMapPolynomialInvertEncodings()
{
// Raw stored as v² is mapped back by a square-root table; then halved values are doubled by a polynomial.
var table = Enumerable.Range(0, 65536).Select(i => (ushort)double.Round(double.Sqrt(i / 65535.0) * 65535)).ToArray();
var ops = new OpcodeListBuilder()
.Add(7, 0, p =>
{
OpcodeListBuilder.Area(p, 0, 0, H, W);
p.U32((uint)table.Length);
foreach (var t in table) p.U16(t);
})
.Add(8, 0, p =>
{
OpcodeListBuilder.Area(p, 0, 0, H, W);
p.U32(1).F64(0).F64(2);
})
.Build();
using var scene = SyntheticRaw.Smooth(W, H);
var spec = new DngSpec { Black = [0], OpcodeList2 = ops, RawTransform = (_, _, v) => float.Max(0, v / 2) * float.Max(0, v / 2) };
using var result = Decode(SyntheticDng.Write(scene, spec));
var psnr = SyntheticRaw.Psnr(scene, result.Image, 4);
context.Log($"MapTable+MapPolynomial PSNR {psnr:F1} dB");
Assert.GreaterOrEqual(psnr, 36);
}
[Test]
public void UnknownAndMalformedOpcodesWarnAndContinue()
{
var ops = new OpcodeListBuilder()
.Add(99, 1, p => p.U32(1))
.Add(98, 0, p => p.U32(1))
.Add(9, 0, p => p.U32(1))
.Build();
using var scene = SyntheticRaw.Smooth(W, H);
using var result = Decode(SyntheticDng.Write(scene, new DngSpec { OpcodeList3 = ops }));
Assert.Contains(result.Warnings, w => w.Contains("optional opcode 99"));
Assert.Contains(result.Warnings, w => w.Contains("required opcode 98"));
Assert.Contains(result.Warnings, w => w.Contains("GainMap") && w.Contains("malformed"));
Assert.GreaterOrEqual(SyntheticRaw.Psnr(scene, result.Image, 4), 40);
}
/// <summary>Intensity-weighted centroid above the background in a window around (x, y).</summary>
private static (double X, double Y) Centroid(ImageBuffer image, int cx, int cy, int radius)
{
double sum = 0, sx = 0, sy = 0;
for (var y = cy - radius; y <= cy + radius; y++)
{
for (var x = cx - radius; x <= cx + radius; x++)
{
var v = double.Max(0, image[1, x, y] - 0.05);
sum += v;
sx += v * x;
sy += v * y;
}
}
return (sx / sum, sy / sum);
}
}