diff --git a/tests/Titano.Core.Tests/Raw/DngDecoderTests.cs b/tests/Titano.Core.Tests/Raw/DngDecoderTests.cs
index 463093c6..9fd8f8e0 100644
--- a/tests/Titano.Core.Tests/Raw/DngDecoderTests.cs
+++ b/tests/Titano.Core.Tests/Raw/DngDecoderTests.cs
@@ -49,6 +49,59 @@ public sealed class DngDecoderTests(TestContext context)
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()
{
diff --git a/tests/Titano.Core.Tests/Raw/SyntheticRaw.cs b/tests/Titano.Core.Tests/Raw/SyntheticRaw.cs
index 0a47f74f..b8c16155 100644
--- a/tests/Titano.Core.Tests/Raw/SyntheticRaw.cs
+++ b/tests/Titano.Core.Tests/Raw/SyntheticRaw.cs
@@ -148,9 +148,16 @@ internal sealed record DngSpec
/// Forward linearisation table (stored code → linear value).
public ushort[]? Linearization { get; init; }
- /// 1 (uncompressed), 7 (lossless JPEG) or 8 (Deflate with horizontal predictor).
+ /// 1 (uncompressed), 7 (lossless JPEG), 8 (Deflate with horizontal predictor) or 34892 (lossy JPEG tiles of
+ /// 8-bit codes; pair it with a 256-entry ).
public int Compression { get; init; } = 7;
+ /// Store 32-bit floats (raw camera values, white 1.0) with Deflate and the floating-point predictor.
+ public bool FloatingPoint { get; init; }
+
+ public Matrix3? CameraCalibration1 { get; init; }
+ public Vector3D? AnalogBalance { get; init; }
+
/// Lossless JPEG tiles encode CFA rows as two interleaved components at half width (as the DNG SDK does).
public bool TwoComponentTiles { get; init; } = true;
@@ -193,6 +200,7 @@ internal static class SyntheticDng
int w = sw + spec.MarginLeft + spec.MarginRight, h = sh + spec.MarginTop + spec.MarginBottom;
var spp = spec.Cfa is null ? 3 : 1;
var samples = new ushort[w * h * spp];
+ var floats = spec.FloatingPoint ? new float[w * h * spp] : null;
for (var y = 0; y < h; y++)
{
for (var x = 0; x < w; x++)
@@ -208,6 +216,7 @@ internal static class SyntheticDng
var c = spec.Cfa?.ColorAt(ax, ay) ?? s;
var raw = camera[c][ay * sw + ax];
if (spec.RawTransform is { } t) raw = t(ax, ay, raw);
+ if (floats is not null) floats[(y * w + x) * spp + s] = raw;
linear = black + raw * (spec.White - black);
}
linear = double.Clamp(linear, 0, spec.White);
@@ -215,7 +224,7 @@ internal static class SyntheticDng
}
}
}
- return Assemble(spec, samples, w, h, spp, neutral);
+ return Assemble(spec, samples, floats, w, h, spp, neutral);
}
/// Black level of the spec at active-area coordinates (outside the area: the pattern without deltas).
@@ -239,7 +248,7 @@ internal static class SyntheticDng
return (ushort)best;
}
- private static byte[] Assemble(DngSpec spec, ushort[] samples, int w, int h, int spp, Vector3D neutral)
+ private static byte[] Assemble(DngSpec spec, ushort[] samples, float[]? floats, int w, int h, int spp, Vector3D neutral)
{
using var output = new MemoryStream();
output.Write("II*\0\0\0\0\0"u8);
@@ -247,13 +256,24 @@ internal static class SyntheticDng
raw.Long(TiffTags.NewSubfileType, 0);
raw.Long(TiffTags.ImageWidth, (uint)w);
raw.Long(TiffTags.ImageLength, (uint)h);
- raw.Short(TiffTags.BitsPerSample, [.. Enumerable.Repeat((ushort)16, spp)]);
+ var bits = floats is not null ? 32 : spec.Compression == 34892 ? 8 : 16;
+ raw.Short(TiffTags.BitsPerSample, [.. Enumerable.Repeat((ushort)bits, spp)]);
+ if (floats is not null) raw.Short(TiffTags.SampleFormat, [.. Enumerable.Repeat((ushort)3, spp)]);
raw.Short(TiffTags.Compression, (ushort)spec.Compression);
raw.Short(TiffTags.PhotometricInterpretation, (ushort)(spec.Cfa is null ? 34892 : 32803));
raw.Short(TiffTags.SamplesPerPixel, (ushort)spp);
raw.Short(TiffTags.PlanarConfiguration, 1);
- if (spec.Compression == 8) raw.Short(TiffTags.Predictor, 2);
- WriteData(spec, raw, output, samples, w, h, spp);
+ if (floats is not null)
+ {
+ raw.Short(TiffTags.Compression, 8);
+ raw.Short(TiffTags.Predictor, 3);
+ WriteFloats(raw, output, floats, w, h, spp);
+ }
+ else
+ {
+ if (spec.Compression == 8) raw.Short(TiffTags.Predictor, 2);
+ WriteData(spec, raw, output, samples, w, h, spp);
+ }
if (spec.Cfa is { } cfa)
{
raw.Short(TiffTags.CfaRepeatPatternDim, (ushort)cfa.Height, (ushort)cfa.Width);
@@ -272,10 +292,10 @@ internal static class SyntheticDng
}
if (spec.Linearization is { } table) raw.Short(TiffTags.LinearizationTable, table);
if (spec.BlackRows * spec.BlackColumns > 1) raw.Short(TiffTags.BlackLevelRepeatDim, (ushort)spec.BlackRows, (ushort)spec.BlackColumns);
- raw.Rational(TiffTags.BlackLevel, [.. spec.Black.Select(b => ((uint)double.Round(b * 100), 100u))]);
+ if (floats is null) raw.Rational(TiffTags.BlackLevel, [.. spec.Black.Select(b => ((uint)double.Round(b * 100), 100u))]);
if (spec.BlackDeltaH is { } dh) raw.SRational(TiffTags.BlackLevelDeltaH, [.. dh.Select(d => ((int)double.Round(d * 100), 100))]);
if (spec.BlackDeltaV is { } dv) raw.SRational(TiffTags.BlackLevelDeltaV, [.. dv.Select(d => ((int)double.Round(d * 100), 100))]);
- raw.Long(TiffTags.WhiteLevel, [.. Enumerable.Repeat((uint)spec.White, spp)]);
+ if (floats is null) raw.Long(TiffTags.WhiteLevel, [.. Enumerable.Repeat((uint)spec.White, spp)]);
if (spec.OpcodeList1 is { } o1) raw.Add(TiffTags.OpcodeList1, TiffType.Undefined, o1.Length, o1);
if (spec.OpcodeList2 is { } o2) raw.Add(TiffTags.OpcodeList2, TiffType.Undefined, o2.Length, o2);
if (spec.OpcodeList3 is { } o3) raw.Add(TiffTags.OpcodeList3, TiffType.Undefined, o3.Length, o3);
@@ -309,6 +329,8 @@ internal static class SyntheticDng
ifd0.Short(TiffTags.CalibrationIlluminant2, (ushort)spec.Illuminant2);
}
if (spec.ForwardMatrix1 is { } fm) ifd0.SRational(TiffTags.ForwardMatrix1, Rationals(fm));
+ if (spec.CameraCalibration1 is { } cc) ifd0.SRational(TiffTags.CameraCalibration1, Rationals(cc));
+ if (spec.AnalogBalance is { } ab) ifd0.Rational(TiffTags.AnalogBalance, R(ab.X), R(ab.Y), R(ab.Z));
if (spec.WriteWhiteXy)
{
ifd0.Rational(TiffTags.AsShotWhiteXy, ((uint)double.Round(spec.SceneWhite.X * 1e6), 1_000_000u), ((uint)double.Round(spec.SceneWhite.Y * 1e6), 1_000_000u));
@@ -327,9 +349,34 @@ internal static class SyntheticDng
private static (int, int)[] Rationals(Matrix3 m) =>
[.. new[] { m.M11, m.M12, m.M13, m.M21, m.M22, m.M23, m.M31, m.M32, m.M33 }.Select(v => ((int)double.Round(v * 1_000_000), 1_000_000))];
+ /// 32-bit float rows with the TIFF floating-point predictor (bytes split into MSB-first planes, then
+ /// differenced with a stride of one pixel), Deflate-compressed in one strip.
+ private static void WriteFloats(TiffDirectory raw, Stream output, float[] floats, int w, int h, int spp)
+ {
+ var count = w * spp;
+ var rows = new byte[floats.Length * 4];
+ for (var y = 0; y < h; y++)
+ {
+ var row = rows.AsSpan(y * count * 4, count * 4);
+ for (var i = 0; i < count; i++)
+ {
+ var bitsValue = BitConverter.SingleToUInt32Bits(floats[y * count + i]);
+ for (var k = 0; k < 4; k++) row[k * count + i] = (byte)(bitsValue >> (24 - 8 * k));
+ }
+ for (var i = row.Length - 1; i >= spp; i--) row[i] -= row[i - spp];
+ }
+ using var ms = new MemoryStream();
+ using (var z = new ZLibStream(ms, CompressionLevel.Fastest, leaveOpen: true)) z.Write(rows);
+ var bytes = ms.ToArray();
+ raw.Long(TiffTags.StripOffsets, (uint)output.Position);
+ raw.Long(TiffTags.StripByteCounts, (uint)bytes.Length);
+ raw.Long(TiffTags.RowsPerStrip, (uint)h);
+ output.Write(bytes);
+ }
+
private static void WriteData(DngSpec spec, TiffDirectory raw, Stream output, ushort[] samples, int w, int h, int spp)
{
- if (spec.Compression == 7)
+ if (spec.Compression is 7 or 34892)
{
var t = spec.TileSize;
int across = (w + t - 1) / t, down = (h + t - 1) / t;
@@ -349,7 +396,9 @@ internal static class SyntheticDng
}
}
var twoComponents = spec.TwoComponentTiles && spp == 1;
- var jpeg = twoComponents ? JpegLosslessEncoder.Encode(tile, t / 2, t, 2, 16) : JpegLosslessEncoder.Encode(tile, t, t, spp, 16);
+ var jpeg = spec.Compression == 34892
+ ? JpegEncoder.Encode([.. tile.Select(v => (byte)v)], t, t, spp, new JpegEncodeOptions { Quality = 95, Subsampling = JpegSubsampling.Yuv444 })
+ : twoComponents ? JpegLosslessEncoder.Encode(tile, t / 2, t, 2, 16) : JpegLosslessEncoder.Encode(tile, t, t, spp, 16);
offsets[ty * across + tx] = (uint)output.Position;
counts[ty * across + tx] = (uint)jpeg.Length;
output.Write(jpeg);