Files
Chronos/tests/Chronos.Core.Tests/Codecs/LinearDngWriterTests.cs
T
Alby96 f86a151a5d fix(convert): keep what the original holds and plan only what converts
Phase 4b review, codec findings. A Replace makes the converted file the
photo, so a conversion must not lose what the original held, and the dry
run must promise only what the job will do.

- Linear DNG from a conversion is scene-referred: the pixels are stored in
  linear ProPhoto primaries (colours outside sRGB stay positive) and
  divided by the power of two that brings their peak under the white
  level (at most 4 stops), with BaselineExposure giving it back and
  ColorMatrix1/ForwardMatrix1 describing that space. Highlights a RAW
  develops above 1 and its wide-gamut colours now read back as written.
  Exports keep the clipped linear-sRGB DNG.
- A conversion reads with the managed decoders first for every format they
  read natively (RAWs developed at float precision, as on the server) and
  with WIC only for what they cannot. A RAW only WIC's Raw Image Extension
  develops (8 bits) is refused for TIFF-16 and linear DNG and planned out
  as "eight_bit"; JPEG still converts it. Providers say so through
  IImageDecoderProvider.DevelopsRawAt8Bits.
- The original's colour space is kept: a JPEG (APP2) or TIFF (tag 34675)
  carries its ICC profile, with the values unchanged; a linear DNG gets the
  values converted through a matrix/TRC profile; a gray profile converts
  to sRGB. Exif Adobe RGB (uncalibrated + R03) and HEIF nclx P3 get an
  in-house profile. A space nothing here can keep (CMYK, a lookup-table
  profile into a DNG, uncalibrated without R03, HDR nclx) is planned out
  as "color_profile". New in-house ICC parser/writer and header reader
  for JPEG, PNG, TIFF, HEIF/AVIF and WebP.
- The planner asks every file, whatever tier was recorded (unknown before
  the light analysis, or by another host), with the conversion's decoders,
  headers only and eight at a time; a RAW the server cannot develop is
  "convert_on_pc" in the plan instead of failing its sequence. Photos a
  target cannot hold (a JPEG side above 65535, more samples than one
  buffer) are planned out as "too_large", and an overflow fails its photo
  rather than the job.
- A photo that fails reads in the job log as one catalog sentence by cause
  (Archive.Convert.Failed.*), the file named once; the exception's English
  text goes to the diagnostics log only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 0ca1e91ea09946d7a55f45695ee58e1a0ff5f779)
2026-10-27 22:04:34 +01:00

187 lines
9.2 KiB
C#

using Chronos.Core.Codecs.Dng;
using Chronos.Core.Codecs.Raw;
using Chronos.Core.Codecs.Tiff;
using Chronos.Core.Imaging;
using Chronos.Core.Metadata.Tiff;
using Chronos.Testing;
namespace Chronos.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("Chronos", 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);
}
/// <summary>
/// A conversion's archival DNG (owner review 2, R7): highlights above white and colours outside sRGB (negative in
/// Rec.709) read back through the in-house DNG reader and developer as they were written, to the 16-bit step.
/// </summary>
[Test]
public void ASceneReferredDngKeepsHighlightsAndColoursOutsideSrgb()
{
using var image = new ImageBuffer(64, 48);
for (var y = 0; y < 48; y++)
{
for (var x = 0; x < 64; x++)
{
image[0, x, y] = 0.02f + 0.9f * x / 63;
image[1, x, y] = 0.05f + 0.7f * y / 47;
image[2, x, y] = 0.1f + 0.3f * ((x + y) % 7) / 6;
}
}
(int X, int Y, float R, float G, float B)[] special =
[
(1, 1, 2.6f, 2.4f, 2.2f), // a highlight above white, as a RAW's reconstruction gives it
(2, 1, 3.5f, 1.0f, 0.5f), // a bright saturated sunset
(3, 1, -0.2246f, 1.0421f, -0.0786f), // Display P3 green, outside sRGB
(4, 1, -0.15f, 0.8f, 0.9f), // a cyan outside sRGB
(5, 1, 0.6f, -0.05f, 0.7f), // a magenta outside sRGB
];
foreach (var (x, y, r, g, b) in special) (image[0, x, y], image[1, x, y], image[2, x, y]) = (r, g, b);
var dng = LinearDngWriter.Write(image, new LinearDngOptions { SceneReferred = true, TileSize = 32 });
var reader = TiffReader.Open(new MemoryStream(dng))!;
var ifd = reader.ReadChain()[0];
// The peak, about 2.5 in ProPhoto, fits under white two stops down.
Assert.Equal(2.0, reader.GetDouble(ifd, TiffTags.BaselineExposure));
var fm = reader.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);
using var developed = RawDecoder.Decode(new MemoryStream(dng), "scene.dng", new RawDecodeOptions { ApplyOrientation = false });
var back = developed.Image;
Assert.Equal((64, 48, 3), (back.Width, back.Height, back.Channels));
var worst = 0.0;
for (var c = 0; c < 3; c++)
{
for (var i = 0; i < image.PlaneLength; i++) worst = Math.Max(worst, Math.Abs(image.Plane(c)[i] - back.Plane(c)[i]));
}
Assert.Less(worst, 4e-4, "largest difference after the round trip");
foreach (var (x, y, r, g, b) in special)
{
Assert.Approximately(r, back[0, x, y], 4e-4);
Assert.Approximately(g, back[1, x, y], 4e-4);
Assert.Approximately(b, back[2, x, y], 4e-4);
}
// The export mode is unchanged: linear sRGB primaries, clipped to 0..1, no headroom.
var export = LinearDngWriter.Write(image, new LinearDngOptions { TileSize = 32 });
using var clipped = RawDecoder.Decode(new MemoryStream(export), "export.dng", new RawDecodeOptions { ApplyOrientation = false });
Assert.Less(clipped.Image[0, 2, 1], 1.01, "the export clips the highlight");
Assert.Greater(clipped.Image[0, 3, 1], -0.01, "the export clips the colour outside sRGB");
var exportReader = TiffReader.Open(new MemoryStream(export))!;
Assert.Equal(0.0, exportReader.GetDouble(exportReader.ReadChain()[0], TiffTags.BaselineExposure));
}
[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();
}
}