feat(codecs): add linear DNG writer

16-bit LinearRaw DNG 1.4 with tiles (lossless JPEG or uncompressed),
ColorMatrix1 = XYZ(D65) to linear sRGB, ForwardMatrix1 to XYZ(D50),
D65 illuminant, neutral AsShotNeutral, WhiteLevel 65535 and optional
Exif. LibRaw renders test patches back to the exact source values.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 10:02:27 +00:00
1 parent 6d9ede0c47
commit c7d8dcd0bd
2 files changed
+279

No files matched your search

@@ -0,0 +1,155 @@
using System.Buffers.Binary;
using Titano.Core.Codecs.Jpeg;
using Titano.Core.Codecs.Tiff;
using Titano.Core.Imaging;
using Titano.Core.Metadata.Tiff;
namespace Titano.Core.Codecs.Dng;
/// <summary>Tile compression of a linear DNG.</summary>
public enum DngCompression
{
Uncompressed = 1,
/// <summary>Lossless JPEG (SOF3, predictor 1) per tile, DNG Compression = 7.</summary>
LosslessJpeg = 7,
}
/// <summary>Options for <see cref="LinearDngWriter"/>.</summary>
public sealed record LinearDngOptions
{
public static LinearDngOptions Default { get; } = new();
public DngCompression Compression { get; init; } = DngCompression.LosslessJpeg;
/// <summary>UniqueCameraModel (and Model): raw editors key their profiles on it.</summary>
public string UniqueCameraModel { get; init; } = "Titano";
/// <summary>TIFF-structured Exif block (JPEG APP1 payload) whose capture metadata is embedded.</summary>
public byte[]? Exif { get; init; }
/// <summary>Tile edge in pixels (multiple of 16).</summary>
public int TileSize { get; init; } = 256;
/// <summary>Worker limit for tile encoding (-1 = all cores).</summary>
public int MaxDegreeOfParallelism { get; init; } = -1;
}
/// <summary>
/// Writes linear Rec.709/sRGB-primaries RGB as a 16-bit LinearRaw DNG 1.4 that raw editors render with correct
/// colours: ColorMatrix1 = XYZ(D65) → linear sRGB, ForwardMatrix1 = linear sRGB → XYZ(D50), illuminant D65,
/// AsShotNeutral 1/1/1, WhiteLevel 65535, BaselineExposure 0. Values above 1.0 are clipped.
/// </summary>
public static class LinearDngWriter
{
/// <summary>XYZ (D65) → linear sRGB, i.e. the inverse of the sRGB → XYZ matrix (IEC 61966-2-1).</summary>
internal static readonly double[] XyzToLinearSrgb =
[
3.2404542, -1.5371385, -0.4985314,
-0.9692660, 1.8760108, 0.0415560,
0.0556434, -0.2040259, 1.0572252,
];
/// <summary>Linear sRGB → XYZ (D50), Bradford-adapted: neutral (1,1,1) maps to the D50 white point.</summary>
internal static readonly double[] LinearSrgbToXyzD50 =
[
0.4360747, 0.3850649, 0.1430804,
0.2225045, 0.7168786, 0.0606169,
0.0139322, 0.0971045, 0.7141733,
];
public static byte[] Write(ImageBuffer image, LinearDngOptions? options = null)
{
using var ms = new MemoryStream();
Write(ms, image, options);
return ms.ToArray();
}
/// <summary>Writes a linear-light buffer (gray is stored as neutral RGB; alpha is dropped).</summary>
public static void Write(Stream output, ImageBuffer image, LinearDngOptions? options = null)
{
var o = options ?? LinearDngOptions.Default;
if (o.TileSize is < 16 or > 4096 || o.TileSize % 16 != 0) throw new ArgumentOutOfRangeException(nameof(options), "Tile size must be a multiple of 16 in 16..4096");
if (!output.CanSeek)
{
using var buffer = new MemoryStream();
Write(buffer, image, o);
buffer.Position = 0;
buffer.CopyTo(output);
return;
}
int w = image.Width, h = image.Height, t = o.TileSize;
int across = (w + t - 1) / t, down = (h + t - 1) / t;
var offsets = new uint[across * down];
var counts = new uint[across * down];
var start = output.Position;
output.Write("II*\0\0\0\0\0"u8);
var parallel = new ParallelOptions { MaxDegreeOfParallelism = o.MaxDegreeOfParallelism };
var band = new ushort[w * t * 3];
var tiles = new byte[across][];
for (var ty = 0; ty < down; ty++)
{
var y0 = ty * t;
var rows = Math.Min(t, h - y0);
Parallel.For(0, rows, parallel, r => PixelConvert.EncodeRow16(image, y0 + r, band.AsSpan(r * w * 3, w * 3), 3, TransferFunction.Linear));
Parallel.For(0, across, parallel, tx => tiles[tx] = EncodeTile(band, w, rows, tx * t, t, o.Compression));
for (var tx = 0; tx < across; tx++)
{
if (output.Position - start > uint.MaxValue - (long)tiles[tx].Length) throw new InvalidOperationException("DNG exceeds 4 GB");
offsets[ty * across + tx] = (uint)(output.Position - start);
counts[ty * across + tx] = (uint)tiles[tx].Length;
output.Write(tiles[tx]);
}
}
var ifd = new TiffDirectory();
// Capture metadata first, so the source camera's Make/Model win; UniqueCameraModel still keys the profile.
if (o.Exif is { } exif) ifd.AttachExif(exif);
if (!ifd.Has(TiffTags.Make)) ifd.Ascii(TiffTags.Make, "Titano");
if (!ifd.Has(TiffTags.Model)) ifd.Ascii(TiffTags.Model, o.UniqueCameraModel);
ifd.Long(TiffTags.NewSubfileType, 0);
ifd.Long(TiffTags.ImageWidth, (uint)w);
ifd.Long(TiffTags.ImageLength, (uint)h);
ifd.Short(TiffTags.BitsPerSample, 16, 16, 16);
ifd.Short(TiffTags.Compression, (ushort)o.Compression);
ifd.Short(TiffTags.PhotometricInterpretation, 34892);
ifd.Short(TiffTags.Orientation, 1);
ifd.Short(TiffTags.SamplesPerPixel, 3);
ifd.Short(TiffTags.PlanarConfiguration, 1);
ifd.Ascii(TiffTags.Software, "Titano");
ifd.Long(TiffTags.TileWidth, (uint)t);
ifd.Long(TiffTags.TileLength, (uint)t);
ifd.Long(TiffTags.TileOffsets, offsets);
ifd.Long(TiffTags.TileByteCounts, counts);
ifd.Bytes(TiffTags.DngVersion, 1, 4, 0, 0);
ifd.Bytes(TiffTags.DngBackwardVersion, 1, 1, 0, 0);
ifd.Ascii(TiffTags.UniqueCameraModel, o.UniqueCameraModel);
ifd.SRational(TiffTags.ColorMatrix1, Rationals(XyzToLinearSrgb));
ifd.SRational(TiffTags.ForwardMatrix1, Rationals(LinearSrgbToXyzD50));
ifd.Short(TiffTags.CalibrationIlluminant1, 21); // D65
ifd.Rational(TiffTags.AsShotNeutral, (1u, 1u), (1u, 1u), (1u, 1u));
ifd.SRational(TiffTags.BaselineExposure, (0, 1));
ifd.Long(TiffTags.WhiteLevel, 65535, 65535, 65535);
TiffWriter.WriteDirectory(output, start, ifd);
}
/// <summary>One t×t tile from a band of rows; pixels beyond the image repeat the edge (better compression).</summary>
private static byte[] EncodeTile(ushort[] band, int width, int rows, int x0, int t, DngCompression compression)
{
var tile = new ushort[t * t * 3];
for (var y = 0; y < t; y++)
{
var src = band.AsSpan(Math.Min(y, rows - 1) * width * 3, width * 3);
var dst = tile.AsSpan(y * t * 3, t * 3);
var valid = Math.Min(t, width - x0);
src.Slice(x0 * 3, valid * 3).CopyTo(dst);
for (var x = valid; x < t; x++) src.Slice((width - 1) * 3, 3).CopyTo(dst[(x * 3)..]);
}
if (compression == DngCompression.LosslessJpeg) return JpegLosslessEncoder.Encode(tile, t, t, 3, 16);
var bytes = new byte[tile.Length * 2];
for (var i = 0; i < tile.Length; i++) BinaryPrimitives.WriteUInt16LittleEndian(bytes.AsSpan(2 * i), tile[i]);
return bytes;
}
private static (int, int)[] Rationals(double[] matrix) => matrix.Select(v => ((int)Math.Round(v * 10000), 10000)).ToArray();
}
@@ -0,0 +1,124 @@
using Titano.Core.Codecs.Dng;
using Titano.Core.Codecs.Tiff;
using Titano.Core.Imaging;
using Titano.Core.Metadata.Tiff;
using Titano.Testing;
namespace Titano.Core.Tests.Codecs;
public sealed class LinearDngWriterTests
{
private static ImageBuffer Scene(int w, int h)
{
var image = new ImageBuffer(w, h);
for (var y = 0; y < h; y++)
{
for (var x = 0; x < w; x++)
{
image[0, x, y] = x / (float)(w - 1);
image[1, x, y] = y / (float)(h - 1) * 0.6f;
image[2, x, y] = (x * y % 97) / 96f;
}
}
image[0, 0, 0] = 1.7f; // highlight above white: clipped
return image;
}
[Theory]
[Case(DngCompression.LosslessJpeg)]
[Case(DngCompression.Uncompressed)]
public void PixelsRoundTripThroughTheTiffReader(DngCompression compression)
{
using var image = Scene(300, 70);
var dng = LinearDngWriter.Write(image, new LinearDngOptions { Compression = compression, TileSize = 128 });
var reader = TiffReader.Open(new MemoryStream(dng))!;
var raster = TiffImageReader.ReadRaster(reader, reader.ReadChain()[0]);
Assert.Empty(raster.Warnings);
Assert.Equal(34892, raster.Photometric);
Assert.SequenceEqual(PixelConvert.ToInterleaved16(image, 3, TransferFunction.Linear), raster.Samples!);
using var back = TiffImageReader.Decode(dng);
Assert.Approximately(1.0, back[0, 0, 0], 1e-6);
for (var i = 1; i < image.Data.Length; i += 37)
{
Assert.Approximately(Math.Clamp(image.Data[i], 0, 1), back.Data[i], 1.0 / 65535);
}
}
[Test]
public void TagsDescribeLinearRec709()
{
using var image = Scene(40, 30);
var dng = LinearDngWriter.Write(image);
var r = TiffReader.Open(new MemoryStream(dng))!;
Assert.True(r.LittleEndian);
var ifd = r.ReadChain()[0];
Assert.SequenceEqual(new uint[] { 1, 4, 0, 0 }, r.GetUInts(ifd, TiffTags.DngVersion)!);
Assert.Equal(0u, r.GetUInt(ifd, TiffTags.NewSubfileType));
Assert.Equal(34892u, r.GetUInt(ifd, TiffTags.PhotometricInterpretation));
Assert.Equal(3u, r.GetUInt(ifd, TiffTags.SamplesPerPixel));
Assert.SequenceEqual(new uint[] { 16, 16, 16 }, r.GetUInts(ifd, TiffTags.BitsPerSample)!);
Assert.Equal(7u, r.GetUInt(ifd, TiffTags.Compression));
Assert.Equal("Titano", r.GetString(ifd, TiffTags.UniqueCameraModel));
Assert.Equal(21u, r.GetUInt(ifd, TiffTags.CalibrationIlluminant1));
Assert.SequenceEqual(new[] { 1.0, 1.0, 1.0 }, r.GetDoubles(ifd, TiffTags.AsShotNeutral)!);
Assert.Equal(0.0, r.GetDouble(ifd, TiffTags.BaselineExposure));
Assert.SequenceEqual(new uint[] { 65535, 65535, 65535 }, r.GetUInts(ifd, TiffTags.WhiteLevel)!);
Assert.True(ifd.Has(TiffTags.TileWidth) && ifd.Has(TiffTags.TileOffsets) && ifd.Has(TiffTags.TileByteCounts));
// ColorMatrix1 maps XYZ to camera space: D65 white must land on the camera neutral (1, 1, 1),
// and its product with sRGB → XYZ(D65) must be the identity (camera space = linear sRGB).
var cm = r.GetDoubles(ifd, TiffTags.ColorMatrix1)!;
Assert.Equal(9, cm.Length);
double[] d65 = [0.95047, 1.0, 1.08883];
double[] srgbToXyz = [0.4124564, 0.3575761, 0.1804375, 0.2126729, 0.7151522, 0.0721750, 0.0193339, 0.1191920, 0.9503041];
for (var row = 0; row < 3; row++)
{
Assert.Approximately(1.0, cm[row * 3] * d65[0] + cm[row * 3 + 1] * d65[1] + cm[row * 3 + 2] * d65[2], 2e-3);
for (var col = 0; col < 3; col++)
{
var p = 0.0;
for (var k = 0; k < 3; k++) p += cm[row * 3 + k] * srgbToXyz[k * 3 + col];
Assert.Approximately(row == col ? 1 : 0, p, 2e-4);
}
}
// ForwardMatrix1 maps the white-balanced neutral to D50 (the DNG SDK's xy 0.3457, 0.3585).
var fm = r.GetDoubles(ifd, TiffTags.ForwardMatrix1)!;
double[] d50 = [0.3457 / 0.3585, 1.0, (1 - 0.3457 - 0.3585) / 0.3585];
for (var row = 0; row < 3; row++) Assert.Approximately(d50[row], fm[row * 3] + fm[row * 3 + 1] + fm[row * 3 + 2], 3e-4);
}
[Test]
public void GrayInputExifAndNonSeekableOutput()
{
using var gray = new ImageBuffer(33, 20, 1);
for (var i = 0; i < gray.PlaneLength; i++) gray.Plane(0)[i] = i / (float)gray.PlaneLength;
using var sink = new MemoryStream();
LinearDngWriter.Write(new ForwardOnlyStream(sink), gray, new LinearDngOptions { Exif = TiffTests.ExifBlock() });
var dng = sink.ToArray();
TiffTests.AssertExif(dng);
using var back = TiffImageReader.Decode(dng);
Assert.Equal(3, back.Channels);
Assert.Approximately(gray[0, 20, 10], back[2, 20, 10], 1.0 / 65535);
}
/// <summary>A write-only, non-seekable stream (pipes, network) to exercise the buffering path.</summary>
private sealed class ForwardOnlyStream(Stream inner) : Stream
{
public override bool CanRead => false;
public override bool CanSeek => false;
public override bool CanWrite => true;
public override long Length => throw new NotSupportedException();
public override long Position
{
get => throw new NotSupportedException();
set => throw new NotSupportedException();
}
public override void Write(byte[] buffer, int offset, int count) => inner.Write(buffer, offset, count);
public override void Flush() => inner.Flush();
public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException();
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
public override void SetLength(long value) => throw new NotSupportedException();
}
}