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)
This commit is contained in:
1 parent
341fb77507
commit
f86a151a5d
26 files changed
+1755
-82
No files matched your search
@@ -0,0 +1,103 @@
|
||||
using Chronos.Core.Codecs.Icc;
|
||||
using Chronos.Core.Codecs.Tiff;
|
||||
using Chronos.Core.Imaging;
|
||||
using Chronos.Core.Mathematics;
|
||||
using Chronos.Core.Tests.Conversion;
|
||||
using Chronos.Testing;
|
||||
|
||||
namespace Chronos.Core.Tests.Codecs;
|
||||
|
||||
/// <summary>ICC profiles (owner review 2, R7): parsed, synthesized for the spaces a marker names, and read from each container.</summary>
|
||||
public sealed class IccProfileTests
|
||||
{
|
||||
[Test]
|
||||
public void TheSynthesizedProfilesAreMatrixShapersOfTheirSpace()
|
||||
{
|
||||
var adobe = IccProfile.AdobeRgb;
|
||||
Assert.True(adobe.IsMatrixShaper && adobe.IsRgb && !adobe.IsSrgb);
|
||||
Assert.Equal("Compatible with Adobe RGB (1998)", adobe.Description);
|
||||
// Colorants add up to the D50 PCS white.
|
||||
var white = adobe.Colorants!.Value * new Vector3D(1, 1, 1);
|
||||
Assert.Approximately(0.9642, white.X, 2e-4);
|
||||
Assert.Approximately(1.0, white.Y, 2e-4);
|
||||
Assert.Approximately(0.8249, white.Z, 2e-4);
|
||||
// Adobe RGB → linear sRGB: red reaches 1.398 with green −0.398, blue stays put but borrows from green.
|
||||
var m = adobe.ToLinearRec709();
|
||||
Assert.Approximately(1.3982, m.M11, 2e-3);
|
||||
Assert.Approximately(-0.3982, m.M12, 2e-3);
|
||||
Assert.Approximately(1.0, m.M22, 2e-3);
|
||||
Assert.Approximately(-0.0429, m.M32, 2e-3);
|
||||
Assert.Approximately(1.0429, m.M33, 2e-3);
|
||||
var neutral = m * new Vector3D(1, 1, 1);
|
||||
Assert.Approximately(1, neutral.X, 1e-9);
|
||||
Assert.Approximately(1, neutral.Y, 1e-9);
|
||||
Assert.Approximately(1, neutral.Z, 1e-9);
|
||||
// Its gamma 563/256, written as a u8Fixed8 curve.
|
||||
Assert.Approximately(Math.Pow(0.5, 563 / 256.0), adobe.Curves![0].Evaluate(0.5), 1e-12);
|
||||
|
||||
var p3 = IccProfile.DisplayP3;
|
||||
Assert.True(p3.IsMatrixShaper);
|
||||
// The sRGB curve, tabulated in 1024 entries.
|
||||
for (var v = 0.0; v <= 1; v += 0.05) Assert.Approximately(ColorSpace.SrgbToLinear(v), p3.Curves![1].Evaluate(v), 2e-5);
|
||||
// P3's own green is outside sRGB: negative red and blue in Rec.709.
|
||||
var green = p3.ToLinearRec709() * new Vector3D(0, 1, 0);
|
||||
Assert.Approximately(-0.2246, green.X, 3e-3);
|
||||
Assert.Approximately(1.0421, green.Y, 3e-3);
|
||||
Assert.Approximately(-0.0786, green.Z, 3e-3);
|
||||
|
||||
Assert.Null(IccProfile.Parse(new byte[200]));
|
||||
Assert.Null(IccProfile.Parse(adobe.Data[..131]));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ParametricCurvesFollowTheirFunctions()
|
||||
{
|
||||
// para function 3 with the sRGB parameters (as Apple's Display P3 profile writes it).
|
||||
byte[] tag = [.. "para"u8, 0, 0, 0, 0, 0, 3, 0, 0, .. Fixed(2.4), .. Fixed(1 / 1.055), .. Fixed(0.055 / 1.055), .. Fixed(1 / 12.92), .. Fixed(0.04045)];
|
||||
var curve = IccCurve.Parse(tag)!;
|
||||
foreach (var v in new[] { 0.0, 0.02, 0.04045, 0.3, 0.75, 1.0 }) Assert.Approximately(ColorSpace.SrgbToLinear(v), curve.Evaluate(v), 2e-5);
|
||||
Assert.Approximately(Math.Pow(0.5, 1.8), IccCurve.Parse([.. "para"u8, 0, 0, 0, 0, 0, 0, 0, 0, .. Fixed(1.8)])!.Evaluate(0.5), 1e-4);
|
||||
Assert.Approximately(0.25, IccCurve.Parse([.. "curv"u8, 0, 0, 0, 0, 0, 0, 0, 0])!.Evaluate(0.25), 1e-12);
|
||||
Assert.Approximately(0.5, IccCurve.Parse([.. "curv"u8, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0x80, 0, 0xFF, 0xFF])!.Evaluate(0.5), 1e-4);
|
||||
Assert.Null(IccCurve.Parse([.. "para"u8, 0, 0, 0, 0, 0, 9, 0, 0]));
|
||||
|
||||
static byte[] Fixed(double v)
|
||||
{
|
||||
var b = new byte[4];
|
||||
System.Buffers.Binary.BinaryPrimitives.WriteInt32BigEndian(b, (int)Math.Round(v * 65536));
|
||||
return b;
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EachContainerSaysItsColourSpace()
|
||||
{
|
||||
using var scene = ColorFixtures.Scene();
|
||||
var p3 = IccProfile.DisplayP3.Data;
|
||||
SourceColor Read(byte[] file, ImageFileFormat format) => SourceColor.Read(new MemoryStream(file), format);
|
||||
|
||||
// Untagged and sRGB files are sRGB.
|
||||
Assert.Same(SourceColor.Untagged, Read(ColorFixtures.Jpeg(scene), ImageFileFormat.Jpeg));
|
||||
Assert.Same(SourceColor.Untagged, Read(ColorFixtures.Jpeg(scene, exifTiff: ColorFixtures.Exif(1, "R98")), ImageFileFormat.Jpeg));
|
||||
// An embedded profile, byte for byte: JPEG APP2 (one chunk, or several), PNG iCCP, TIFF tag 34675, HEIF prof.
|
||||
Assert.BytesEqual(p3, Read(ColorFixtures.Jpeg(scene, p3), ImageFileFormat.Jpeg).Profile!.Data);
|
||||
var large = ColorFixtures.Large();
|
||||
Assert.BytesEqual(large, Read(ColorFixtures.Jpeg(scene, large), ImageFileFormat.Jpeg).Profile!.Data);
|
||||
Assert.BytesEqual(p3, Read(ColorFixtures.Png(scene, p3), ImageFileFormat.Png).Profile!.Data);
|
||||
Assert.BytesEqual(p3, Read(TiffWriter.Write(scene, new TiffWriteOptions { IccProfile = p3 }), ImageFileFormat.Tiff).Profile!.Data);
|
||||
Assert.BytesEqual(p3, Read(ColorFixtures.Heif(ColorFixtures.Nclx(1, 13), ColorFixtures.Prof(p3)), ImageFileFormat.Heif).Profile!.Data);
|
||||
// A grid whose tiles carry the profile, not the grid item.
|
||||
Assert.BytesEqual(p3, Read(ColorFixtures.Heif(2, ColorFixtures.Prof(p3)), ImageFileFormat.Heif).Profile!.Data);
|
||||
// A camera's Adobe RGB marker (uncalibrated, Interop R03) names Adobe RGB, in a JPEG and in a TIFF.
|
||||
Assert.Same(IccProfile.AdobeRgb, Read(ColorFixtures.Jpeg(scene, exifTiff: ColorFixtures.Exif(0xFFFF, "R03")), ImageFileFormat.Jpeg).Profile);
|
||||
// nclx: BT.709 is sRGB, P3 primaries Display P3, BT.2020 or PQ unknown.
|
||||
Assert.Same(SourceColor.Untagged, Read(ColorFixtures.Heif(ColorFixtures.Nclx(1, 13)), ImageFileFormat.Heif));
|
||||
Assert.Same(IccProfile.DisplayP3, Read(ColorFixtures.Heif(ColorFixtures.Nclx(12, 13)), ImageFileFormat.Heif).Profile);
|
||||
Assert.True(Read(ColorFixtures.Heif(ColorFixtures.Nclx(9, 16)), ImageFileFormat.Heif).IsUnknown);
|
||||
// Uncalibrated without R03, or a profile that does not parse: a space nothing here describes.
|
||||
Assert.True(Read(ColorFixtures.Jpeg(scene, exifTiff: ColorFixtures.Exif(0xFFFF)), ImageFileFormat.Jpeg).IsUnknown);
|
||||
Assert.True(Read(ColorFixtures.Jpeg(scene, new byte[300]), ImageFileFormat.Jpeg).IsUnknown);
|
||||
// RAWs say nothing here: their decoders give Rec.709.
|
||||
Assert.Same(SourceColor.Untagged, Read([1, 2, 3], ImageFileFormat.Cr2));
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Chronos.Core.Codecs.Dng;
|
||||
using Chronos.Core.Codecs.Raw;
|
||||
using Chronos.Core.Codecs.Tiff;
|
||||
using Chronos.Core.Imaging;
|
||||
using Chronos.Core.Metadata.Tiff;
|
||||
@@ -87,6 +88,67 @@ public sealed class LinearDngWriterTests
|
||||
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()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user