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:
Alby96 committed 2026-10-27 22:04:34 +01:00
1 parent 341fb77507
commit f86a151a5d
26 files changed
+1755 -82

No files matched your search

@@ -1,10 +1,15 @@
using System.Globalization;
using Chronos.Core.Archiving;
using Chronos.Core.Codecs.Dng;
using Chronos.Core.Codecs.Icc;
using Chronos.Core.Codecs.Jpeg;
using Chronos.Core.Codecs.Png;
using Chronos.Core.Codecs.Tiff;
using Chronos.Core.Conversion;
using Chronos.Core.Imaging;
using Chronos.Core.Imaging.Decoders;
using Chronos.Core.Localization;
using Chronos.Core.Mathematics;
using Chronos.Core.Metadata;
using Chronos.Core.Model;
using Chronos.Core.Naming;
@@ -227,11 +232,13 @@ public sealed class FrameConverterTests(TestContext context)
var frame = Frame("frame.dng", ImageFileFormat.Dng, 32, 24);
var options = new ConvertOptions(ConvertTarget.Jpeg);
var target = Target(options);
Assert.Throws<ConversionException>(() =>
var preview = Assert.Throws<ConversionException>(() =>
FrameConverter.Convert(new ConvertSource(path, frame), options, new ImageDecoder([new PreviewOnlyProvider()]), target, cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.PreviewOnly, preview.Reason);
Assert.FileMissing(target);
Assert.Throws<ConversionException>(() =>
var none = Assert.Throws<ConversionException>(() =>
FrameConverter.Convert(new ConvertSource(path, frame), options, new ImageDecoder([]), target, cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.NoDecoder, none.Reason);
}
[Test]
@@ -247,7 +254,7 @@ public sealed class FrameConverterTests(TestContext context)
var bytes = File.ReadAllBytes(file.Path);
bytes[^10] ^= 0x40;
File.WriteAllBytes(file.Path, bytes);
Assert.Throws<ConversionException>(() => FrameConverter.Verify(file, written));
Assert.Equal(ConversionFailure.NotVerified, Assert.Throws<ConversionException>(() => FrameConverter.Verify(file, written)).Reason);
// A capture time that is not the one written.
File.WriteAllBytes(file.Path, File.ReadAllBytes(source));
Assert.Throws<ConversionException>(() => FrameConverter.Verify(file with { Sha256 = ContentHash.Sha256Async(file.Path).GetAwaiter().GetResult(), Size = new FileInfo(file.Path).Length }, written));
@@ -289,6 +296,224 @@ public sealed class FrameConverterTests(TestContext context)
Assert.Equal(new ConvertOptions(ConvertTarget.LinearDng, 10).Key, new ConvertOptions(ConvertTarget.LinearDng, 90).Key);
}
/// <summary>
/// The managed decoder develops a RAW wherever it reads it natively, ahead of WIC (owner review 2, R7): a 16-bit file is
/// never filled from WIC's 8-bit RAW development, which a JPEG may still use.
/// </summary>
[Test]
public void ASixteenBitFileNeverComesFromAnEightBitRawDevelopment()
{
var path = Path.Combine(context.CreateTempDirectory("source"), "frame.dng");
using (var image = Scene(32, 24)) File.WriteAllBytes(path, LinearDngWriter.Write(image));
var frame = Frame("frame.dng", ImageFileFormat.Dng, 32, 24);
var wic = new EightBitRaw();
foreach (var target in (ConvertTarget[])[ConvertTarget.Tiff16, ConvertTarget.LinearDng, ConvertTarget.Jpeg])
{
var options = new ConvertOptions(target);
FrameConverter.Convert(new ConvertSource(path, frame), options, new ImageDecoder([wic, new ManagedDecoderProvider()]), Target(options),
cancellationToken: context.Cancellation);
}
Assert.Equal(0, wic.Calls);
// Where only the 8-bit development reads it: refused for 16 bits, converted to JPEG.
foreach (var target in (ConvertTarget[])[ConvertTarget.Tiff16, ConvertTarget.LinearDng])
{
var options = new ConvertOptions(target);
var refused = Assert.Throws<ConversionException>(() =>
FrameConverter.Convert(new ConvertSource(path, frame), options, new ImageDecoder([wic]), Target(options), cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.EightBit, refused.Reason);
}
var jpeg = new ConvertOptions(ConvertTarget.Jpeg);
FrameConverter.Convert(new ConvertSource(path, frame), jpeg, new ImageDecoder([wic]), Target(jpeg), cancellationToken: context.Cancellation);
Assert.Equal(1, wic.Calls);
}
/// <summary>A RAW developed above white and outside sRGB converts to a linear DNG that reads back the same.</summary>
[Test]
public void ALinearDngKeepsWhatTheRawHasAboveWhiteAndOutsideSrgb()
{
var path = Path.Combine(context.CreateTempDirectory("source"), "IMG_0001.CR2");
using (var image = Scene(40, 30)) File.WriteAllBytes(path, JpegEncoder.Encode(image));
var developed = Scene(40, 30);
(developed[0, 1, 1], developed[1, 1, 1], developed[2, 1, 1]) = (2.4f, 1.9f, 1.2f);
(developed[0, 2, 1], developed[1, 2, 1], developed[2, 2, 1]) = (-0.2f, 1.0f, -0.07f);
var options = new ConvertOptions(ConvertTarget.LinearDng);
var file = FrameConverter.Convert(new ConvertSource(path, Frame("IMG_0001.CR2", ImageFileFormat.Cr2, 40, 30)), options,
new ImageDecoder([new Developed(developed)]), Target(options), cancellationToken: context.Cancellation);
using var back = Assert.NotNull(DecoderFactory.CreateServer().Decode(new DecodeRequest(file.Path, AllowPreview: false)));
for (var c = 0; c < 3; c++)
{
for (var i = 0; i < developed.PlaneLength; i++) Assert.Approximately(developed.Plane(c)[i], back.Pixels.Plane(c)[i], 3e-4);
}
developed.Dispose();
}
/// <summary>
/// The colour space the original's values are in is kept: a JPEG or TIFF carries its ICC profile (or the Adobe RGB one
/// its Exif names) with the values unchanged; a linear DNG gets the values converted through the profile.
/// </summary>
[Test]
public void TheOriginalsColourSpaceIsKept()
{
var folder = context.CreateTempDirectory("source");
var p3 = IccProfile.DisplayP3;
using var scene = ColorFixtures.Scene();
var jpegPath = Path.Combine(folder, "p3.jpg");
File.WriteAllBytes(jpegPath, ColorFixtures.Jpeg(scene, p3.Data, ExifWriter.BuildTiff(Camera)));
var source = new ConvertSource(jpegPath, Frame("p3.jpg", ImageFileFormat.Jpeg, 48, 32));
var server = DecoderFactory.CreateServer();
using var original = server.Decode(new DecodeRequest(jpegPath, AllowPreview: false))!;
foreach (var target in (ConvertTarget[])[ConvertTarget.Jpeg, ConvertTarget.Tiff16])
{
var options = new ConvertOptions(target);
var file = FrameConverter.Convert(source, options, server, Target(options), cancellationToken: context.Cancellation);
Assert.BytesEqual(p3.Data, SourceColor.Read(file.Path, file.Format).Profile!.Data);
using var converted = server.Decode(new DecodeRequest(file.Path, AllowPreview: false))!;
var error = 0.0;
for (var c = 0; c < 3; c++)
{
for (var i = 0; i < scene.PlaneLength; i++) error += Math.Abs(original.Pixels.Plane(c)[i] - converted.Pixels.Plane(c)[i]);
}
Assert.Less(error / (3.0 * scene.PlaneLength), target == ConvertTarget.Jpeg ? 0.02 : 1e-3, $"the values of {target} pass unchanged");
}
var dng = new ConvertOptions(ConvertTarget.LinearDng);
var linear = FrameConverter.Convert(source, dng, server, Target(dng), cancellationToken: context.Cancellation);
using var developed = server.Decode(new DecodeRequest(linear.Path, AllowPreview: false))!;
var matrix = p3.ToLinearRec709();
for (var i = 0; i < scene.PlaneLength; i += 7)
{
var code = new Vector3D(
p3.Curves![0].Evaluate(ColorSpace.LinearToSrgb(original.Pixels.Plane(0)[i])),
p3.Curves[1].Evaluate(ColorSpace.LinearToSrgb(original.Pixels.Plane(1)[i])),
p3.Curves[2].Evaluate(ColorSpace.LinearToSrgb(original.Pixels.Plane(2)[i])));
var expected = matrix * code;
Assert.Approximately(expected.X, developed.Pixels.Plane(0)[i], 5e-4);
Assert.Approximately(expected.Y, developed.Pixels.Plane(1)[i], 5e-4);
Assert.Approximately(expected.Z, developed.Pixels.Plane(2)[i], 5e-4);
}
// The P3 green patch is outside sRGB: its red is negative in the DNG, not clipped to 0.
Assert.Less(developed.Pixels[0, 2, 2], -0.05);
// A camera JPEG in Adobe RGB (Exif uncalibrated, Interop R03) carries the Adobe RGB profile; a PNG its iCCP.
var adobePath = Path.Combine(folder, "adobe.jpg");
File.WriteAllBytes(adobePath, ColorFixtures.Jpeg(scene, exifTiff: ColorFixtures.Exif(0xFFFF, "R03")));
var jpeg = new ConvertOptions(ConvertTarget.Jpeg);
var adobe = FrameConverter.Convert(new ConvertSource(adobePath, Frame("adobe.jpg", ImageFileFormat.Jpeg, 48, 32)), jpeg, server, Target(jpeg),
cancellationToken: context.Cancellation);
Assert.Equal("Compatible with Adobe RGB (1998)", SourceColor.Read(adobe.Path, adobe.Format).Profile!.Description);
var pngPath = Path.Combine(folder, "p3.png");
File.WriteAllBytes(pngPath, ColorFixtures.Png(scene, p3.Data));
var tiff = new ConvertOptions(ConvertTarget.Tiff16);
var fromPng = FrameConverter.Convert(new ConvertSource(pngPath, Frame("p3.png", ImageFileFormat.Png, 48, 32)), tiff, server, Target(tiff),
cancellationToken: context.Cancellation);
Assert.BytesEqual(p3.Data, SourceColor.Read(fromPng.Path, fromPng.Format).Profile!.Data);
}
/// <summary>A colour space nothing here can keep (uncalibrated without R03, a CMYK profile) refuses the photo, whatever the target.</summary>
[Test]
public void APhotoWhoseColourSpaceCannotBeKeptIsRefused()
{
var folder = context.CreateTempDirectory("source");
using var scene = ColorFixtures.Scene();
var unknown = Path.Combine(folder, "unknown.jpg");
File.WriteAllBytes(unknown, ColorFixtures.Jpeg(scene, exifTiff: ColorFixtures.Exif(0xFFFF)));
var cmyk = Path.Combine(folder, "cmyk.jpg");
File.WriteAllBytes(cmyk, ColorFixtures.Jpeg(scene, ColorFixtures.Cmyk()));
foreach (var target in Enum.GetValues<ConvertTarget>())
{
var options = new ConvertOptions(target);
foreach (var path in (string[])[unknown, cmyk])
{
var output = Target(options);
var refused = Assert.Throws<ConversionException>(() => FrameConverter.Convert(new ConvertSource(path, Frame(Path.GetFileName(path), ImageFileFormat.Jpeg, 48, 32)),
options, DecoderFactory.CreateServer(), output, cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.ColorProfile, refused.Reason);
Assert.FileMissing(output);
}
}
}
/// <summary>A photo larger than the target or one buffer holds fails as too large, not with the encoder's own exception.</summary>
[Test]
public void APhotoTooLargeForTheTargetFailsAsTooLarge()
{
var path = Path.Combine(context.CreateTempDirectory("source"), "panorama.png");
using (var image = Scene(8, 8)) File.WriteAllBytes(path, PngEncoder.Encode(image));
var frame = Frame("panorama.png", ImageFileFormat.Png, 70_000, 1);
var jpeg = new ConvertOptions(ConvertTarget.Jpeg);
using var wide = new ImageBuffer(65_536, 1);
var refused = Assert.Throws<ConversionException>(() => FrameConverter.Convert(new ConvertSource(path, frame), jpeg,
new ImageDecoder([new Developed(wide, ImageFileFormat.Png)]), Target(jpeg), cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.TooLarge, refused.Reason);
var overflow = Assert.Throws<ConversionException>(() => FrameConverter.Convert(new ConvertSource(path, frame), new ConvertOptions(ConvertTarget.Tiff16),
new ImageDecoder([new Overflowing()]), Target(jpeg), cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.TooLarge, overflow.Reason);
}
/// <summary>Why a photo failed reads as one sentence by cause in the job log, never the exception's English text.</summary>
[Test]
public void FailuresReadAsOneSentenceByCause()
{
foreach (var reason in Enum.GetValues<ConversionFailure>())
{
var text = FrameConverter.FailureText(new ConversionException(reason, "technical detail"));
Assert.Equal(Loc.L("Archive.Convert.Failed." + reason), text);
Assert.NotNull(Loc.Get("Archive.Convert.Failed." + reason, CultureInfo.GetCultureInfo("it")));
Assert.False(text.Contains("technical", StringComparison.Ordinal), text);
}
Assert.Equal(Loc.L("Archive.Convert.Failed.Full"), FrameConverter.FailureText(new IOException("full", unchecked((int)0x80070070))));
Assert.Equal(Loc.L("Archive.Failed.Access"), FrameConverter.FailureText(new UnauthorizedAccessException("denied")));
Assert.Equal(Loc.L("Archive.Convert.Failed.Unreadable"), FrameConverter.FailureText(new InvalidDataException("JPEG: bad Huffman")));
Assert.Equal(Loc.L("Archive.Failed.Files"), FrameConverter.FailureText(new IOException("network")));
Assert.Equal(Loc.L("Archive.Convert.Failed.Other"), FrameConverter.FailureText(new ArgumentOutOfRangeException("width")));
}
/// <summary>WIC with the Raw Image Extension, as a conversion sees it: every RAW native, developed at 8 bits (counted).</summary>
private sealed class EightBitRaw : IImageDecoderProvider
{
private readonly ManagedDecoderProvider _managed = new();
public int Calls { get; private set; }
public string Name => "wic";
public bool DevelopsRawAt8Bits => true;
public DecodeTier? TierFor(ImageFileFormat format) => ImageFormatInfo.Of(format).IsRaw ? DecodeTier.Native : null;
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default)
{
Calls++;
var decoded = _managed.Decode(request, cancellationToken)!;
foreach (ref var v in decoded.Pixels.Data) v = ColorSpace.Linear8(ColorSpace.Encode8(v));
return new DecodedImage(decoded.Pixels, DecodeTier.Native, Name, decoded.SourceWidth, decoded.SourceHeight);
}
}
/// <summary>A RAW decoder that develops every file of <paramref name="format"/> into a copy of <paramref name="image"/>.</summary>
private sealed class Developed(ImageBuffer image, ImageFileFormat format = ImageFileFormat.Cr2) : IImageDecoderProvider
{
public string Name => "developed";
public DecodeTier? TierFor(ImageFileFormat f) => f == format ? DecodeTier.Native : null;
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default) =>
new(image.Clone(), DecodeTier.Native, Name, image.Width, image.Height);
}
/// <summary>A decoder whose image is too large for one buffer.</summary>
private sealed class Overflowing : IImageDecoderProvider
{
public string Name => "overflowing";
public DecodeTier? TierFor(ImageFileFormat format) => DecodeTier.Native;
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default) => throw new OverflowException();
}
/// <summary>A provider that claims DNG natively but only ever gives the embedded-preview tier.</summary>
private sealed class PreviewOnlyProvider : IImageDecoderProvider
{