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
125 lines
5.8 KiB
C#
125 lines
5.8 KiB
C#
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();
|
|
}
|
|
}
|