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:
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();
|
||||
}
|
||||
Reference in new issue
Block a user