fix(convert): plan, decode and colour only what a conversion really does
Round-2 fixes for the codecs part of owner review 2, phase 4b (mass conversion), so the dry run never promises a conversion that then fails or writes wrong pixels. - The managed TIFF reader refuses samples it does not render (CMYK, CIELab, palette, LogL, RGB of fewer than three samples, float min-is-white) before reading pixels, instead of decoding them as the gray of their first plane. The conversion chain then asks WIC on a PC; the server leaves them to a PC. Impossible strip/tile counts are refused as data, not as an OverflowException that escaped the chain. - A conversion's managed provider reads JPEG, PNG and TIFF headers in its probe (ReadsHeaders): what its decoders refuse there (compression, sample format, photometric, MaxSamples, MaxPixels, arithmetic coding) is not planned as convertible on the server, and is asked of WIC on a PC. The probe gives the real size, so TooLarge sees it. - Float TIFFs are linear light (SourceColor.IsLinear): a JPEG or TIFF carrying their profile gets codes from the profile's own curves, a DNG gets them through the matrix only (no curve, no clip, highlights kept). IsSrgb is decided by the numbers for matrix/TRC profiles, so a linear profile named sRGB is converted, not taken as sRGB. The TIFF profile is read from the image the decoders read. - TooLarge is a bound: the target's limits and what the server's decoders read back, which every converted file must (JPEG 2^28 pixels, TIFF-16 and DNG 2^29 samples), and the incompressible worst case against the 2 GB array. A MemoryStream overflow is TooLarge, never a generic file failure. - Colorants that convert nothing (a white without positive cone responses, a non-finite or singular matrix) are dropped when a profile is parsed: the photo is carried into a JPEG or TIFF and planned out of a DNG as colour_profile, instead of throwing in BradfordAdaptation at every retry. Tests: header probes, refused photometrics, the size bound against the verifier's header reads, float TIFF colour, a fuzz of damaged profiles, and a Windows test converting a CMYK TIFF through WIC. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 18cf9133087aada2db06c8310d2c69ff804c0115)
This commit is contained in:
1 parent
e3cbe30b29
commit
5b5acaf307
15 files changed
+1160
-78
No files matched your search
@@ -1,10 +1,13 @@
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Core.Codecs.Icc;
|
||||
using Chronos.Core.Codecs.Jpeg;
|
||||
using Chronos.Core.Codecs.Raw;
|
||||
using Chronos.Core.Codecs.Tiff;
|
||||
using Chronos.Core.Conversion;
|
||||
using Chronos.Core.Export;
|
||||
using Chronos.Core.Imaging;
|
||||
using Chronos.Core.Imaging.Decoders;
|
||||
using Chronos.Core.Metadata.Tiff;
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Core.Tests.Raw;
|
||||
using Chronos.Testing;
|
||||
@@ -78,7 +81,8 @@ public sealed class ConvertPlannerTests(TestContext context)
|
||||
|
||||
/// <summary>
|
||||
/// A frame whose tier is not known yet (imported, not analysed) or was recorded native by another host with other
|
||||
/// decoders is asked again: the dry run promises only what the file says here now.
|
||||
/// decoders is asked again: the dry run promises only what the file says here now. A file the decoders here do not read
|
||||
/// at all, although they read its format, is left to a PC; only a frame flagged unreadable is unreadable.
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void AFrameIsAskedWhateverTierWasRecorded()
|
||||
@@ -93,7 +97,7 @@ public sealed class ConvertPlannerTests(TestContext context)
|
||||
Assert.Empty(preview.Frames);
|
||||
Assert.SequenceEqual([(ConvertSkip.PreviewOnly, 2)], preview.Skipped.Select(s => (s.Reason, s.Frames)));
|
||||
var unreadable = ConvertPlanner.Plan(manifest, folder, present, dng, new ImageDecoder([new ProbeAs(DecodeTier.Unavailable)]), "Roof");
|
||||
Assert.SequenceEqual([(ConvertSkip.Undecodable, 2)], unreadable.Skipped.Select(s => (s.Reason, s.Frames)));
|
||||
Assert.SequenceEqual([(ConvertSkip.NoNativeDecoder, 2)], unreadable.Skipped.Select(s => (s.Reason, s.Frames)));
|
||||
var native = ConvertPlanner.Plan(manifest, folder, present, dng, new ImageDecoder([new ProbeAs(DecodeTier.Native)]), "Roof");
|
||||
Assert.SequenceEqual([0, 1], native.Frames.Select(f => f.Index));
|
||||
}
|
||||
@@ -151,14 +155,15 @@ public sealed class ConvertPlannerTests(TestContext context)
|
||||
Frame(1, "p3.jpg", ImageFileFormat.Jpeg),
|
||||
Frame(2, "unknown.jpg", ImageFileFormat.Jpeg),
|
||||
Frame(3, "cmyk.jpg", ImageFileFormat.Jpeg),
|
||||
Frame(4, "panorama.png", ImageFileFormat.Png) with { W = 70_000, H = 3_800 },
|
||||
Frame(4, "panorama.png", ImageFileFormat.Png) with { W = 70_000, H = 2_500 },
|
||||
Frame(5, "huge.png", ImageFileFormat.Png) with { W = 70_000, H = 12_000 },
|
||||
];
|
||||
File.WriteAllBytes(folder.File(frames[0].File), ColorFixtures.Jpeg(scene));
|
||||
File.WriteAllBytes(folder.File(frames[1].File), ColorFixtures.Jpeg(scene, IccProfile.DisplayP3.Data));
|
||||
File.WriteAllBytes(folder.File(frames[2].File), ColorFixtures.Jpeg(scene, exifTiff: ColorFixtures.Exif(0xFFFF)));
|
||||
File.WriteAllBytes(folder.File(frames[3].File), ColorFixtures.Jpeg(scene, ColorFixtures.Cmyk()));
|
||||
File.WriteAllBytes(folder.File(frames[4].File), [0]);
|
||||
// A panorama's header (the dry run reads no further), and a file the sidecar's size already rules out.
|
||||
File.WriteAllBytes(folder.File(frames[4].File), ColorFixtures.PngHeader(70_000, 2_500));
|
||||
File.WriteAllBytes(folder.File(frames[5].File), [0]);
|
||||
var manifest = new SequenceManifest { Id = Guid.NewGuid(), Frames = frames };
|
||||
var present = frames.Select(f => f.File).ToList();
|
||||
@@ -179,6 +184,118 @@ public sealed class ConvertPlannerTests(TestContext context)
|
||||
Assert.False(ConvertPlanner.TooLarge(new ConvertOptions(ConvertTarget.Tiff16), 0, 0));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The size bound is what the server's decoders read back, which every converted file must (owner review 2, R7), never
|
||||
/// an estimate: a JPEG of 2^28 pixels, a TIFF-16 or DNG (whose raster the same TIFF reader reads) of 2^29 samples, whose
|
||||
/// headers are read while one row more is refused. A file that size stays within the array it is made in (2 GB) even
|
||||
/// when its pixels do not compress at all (16-bit noise takes 48 bits a pixel, up to 51 as lossless JPEG codes them),
|
||||
/// and noise encodes within that bound in every target.
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TheSizeBoundIsWhatTheServersDecodersReadBack()
|
||||
{
|
||||
var jpeg = new ConvertOptions(ConvertTarget.Jpeg, 100);
|
||||
Assert.False(ConvertPlanner.TooLarge(jpeg, 16_384, 16_384));
|
||||
Assert.True(ConvertPlanner.TooLarge(jpeg, 16_384, 16_385));
|
||||
Assert.False(ConvertPlanner.TooLarge(jpeg, 4_096, 65_535));
|
||||
Assert.True(ConvertPlanner.TooLarge(jpeg, 4_097, 65_535));
|
||||
var largest = JpegDecoder.ReadInfo(ColorFixtures.JpegHeader(16_384, 16_384));
|
||||
Assert.Equal((16_384, 16_384), (largest.Width, largest.Height));
|
||||
Assert.Throws<InvalidDataException>(() => JpegDecoder.ReadInfo(ColorFixtures.JpegHeader(16_384, 16_385)));
|
||||
foreach (var target in (ConvertTarget[])[ConvertTarget.Tiff16, ConvertTarget.LinearDng])
|
||||
{
|
||||
Assert.False(ConvertPlanner.TooLarge(new ConvertOptions(target), 13_377, 13_377), $"{target} of 2^29 samples at most");
|
||||
Assert.True(ConvertPlanner.TooLarge(new ConvertOptions(target), 13_377, 13_378), $"{target} of one row more");
|
||||
}
|
||||
foreach (ushort photometric in (ushort[])[2, 34892])
|
||||
{
|
||||
Assert.Equal((13_377, 13_377), TiffImageReader.ReadInfo(new MemoryStream(ColorFixtures.TiffHeader(13_377, 13_377, photometric))));
|
||||
Assert.Throws<InvalidDataException>(() => TiffImageReader.ReadInfo(new MemoryStream(ColorFixtures.TiffHeader(13_377, 13_378, photometric))));
|
||||
}
|
||||
Assert.LessOrEqual(ConvertPlanner.WorstBytes(16_384, 16_384), Array.MaxLength);
|
||||
Assert.LessOrEqual(ConvertPlanner.WorstBytes(13_377, 13_377), Array.MaxLength);
|
||||
// 342 MP: its estimate (30 or 26 bits a pixel) would fit, the worst case would not, and nothing reads it back.
|
||||
foreach (var target in Enum.GetValues<ConvertTarget>())
|
||||
{
|
||||
Assert.True(ConvertPlanner.TooLarge(new ConvertOptions(target, 100), 18_500, 18_500), $"{target} at 342 MP");
|
||||
Assert.Less(ConvertPlanner.EstimateBytes(new ConvertOptions(target, 100), 18_500, 18_500), Array.MaxLength);
|
||||
}
|
||||
Assert.Greater(ConvertPlanner.WorstBytes(18_500, 18_500), Array.MaxLength);
|
||||
using var noise = new ImageBuffer(512, 512);
|
||||
var random = new Random(7);
|
||||
foreach (ref var v in noise.Data) v = random.NextSingle();
|
||||
var metadata = new Chronos.Core.Metadata.FrameMetadata { CapturedAt = new DateTime(2026, 10, 27, 12, 0, 0) };
|
||||
foreach (var target in Enum.GetValues<ConvertTarget>())
|
||||
{
|
||||
var bytes = FrameConverter.Encode(noise, metadata, new ConvertOptions(target, 100));
|
||||
var bits = bytes.Length * 8.0 / noise.PlaneLength;
|
||||
context.Log($"{target}: {bits:0.00} bits a pixel of noise");
|
||||
Assert.LessOrEqual(bits, ConvertPlanner.WorstBitsPerPixel, $"{target} encodes noise within the bound, its headers included");
|
||||
Assert.LessOrEqual(bytes.Length, ConvertPlanner.WorstBytes(512, 512));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The dry run reads the headers of TIFF, PNG and JPEG files before it promises them (owner review 2, R7): on the server,
|
||||
/// a CMYK, CIELab, palette or ZSTD TIFF, one with more samples than its decoder takes and an arithmetic-coded JPEG are
|
||||
/// left to a PC, never planned to fail at every retry; where another decoder reads them (WIC on a PC), they convert. A
|
||||
/// photo larger than a converted file may be stays as it is everywhere.
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void WhatTheServersDecodersRefuseIsLeftToAPc()
|
||||
{
|
||||
var folder = new ArchivePaths(context.CreateTempDirectory("archive")).Sequence("roof");
|
||||
System.IO.Directory.CreateDirectory(Path.GetDirectoryName(folder.File("originals/x"))!);
|
||||
(string File, ImageFileFormat Format, byte[] Bytes, int W, int H)[] files =
|
||||
[
|
||||
("rgb.tif", ImageFileFormat.Tiff, ColorFixtures.Tiff(6, 4, 2, [200, 30, 10]), 6, 4),
|
||||
("cmyk.tif", ImageFileFormat.Tiff, ColorFixtures.Tiff(6, 4, 5, [0, 255, 255, 0]), 6, 4),
|
||||
("lab.tif", ImageFileFormat.Tiff, ColorFixtures.Tiff(6, 4, 8, [50, 60, 70]), 6, 4),
|
||||
("palette.tif", ImageFileFormat.Tiff, ColorFixtures.Tiff(6, 4, 3, [1]), 6, 4),
|
||||
("zstd.tif", ImageFileFormat.Tiff, ColorFixtures.Tiff(6, 4, 2, [1, 2, 3], d => d.Short(TiffTags.Compression, 50000)), 6, 4),
|
||||
// RGBA of 576 M samples: beyond the managed reader, while its DNG (432 M) is not.
|
||||
("stitched.tif", ImageFileFormat.Tiff, ColorFixtures.Tiff(6, 4, 2, [1, 2, 3, 4], d =>
|
||||
{
|
||||
d.Long(TiffTags.ImageWidth, 12_000);
|
||||
d.Long(TiffTags.ImageLength, 12_000);
|
||||
}), 12_000, 12_000),
|
||||
("arithmetic.jpg", ImageFileFormat.Jpeg, ColorFixtures.JpegHeader(6, 4, sof: 0xC9), 6, 4),
|
||||
("stitched.png", ImageFileFormat.Png, ColorFixtures.PngHeader(17_000, 17_000), 17_000, 17_000),
|
||||
];
|
||||
var frames = files.Select((f, i) => Frame(i, f.File, f.Format) with { W = f.W, H = f.H }).ToArray();
|
||||
for (var i = 0; i < files.Length; i++) File.WriteAllBytes(folder.File(frames[i].File), files[i].Bytes);
|
||||
var manifest = new SequenceManifest { Id = Guid.NewGuid(), Frames = frames };
|
||||
var present = frames.Select(f => f.File).ToList();
|
||||
var dng = new ConvertOptions(ConvertTarget.LinearDng);
|
||||
|
||||
var server = ConvertPlanner.Plan(manifest, folder, present, dng, DecoderFactory.CreateServer(), "Roof");
|
||||
Assert.SequenceEqual([0], server.Frames.Select(f => f.Index));
|
||||
Assert.SequenceEqual([(ConvertSkip.NoNativeDecoder, 6), (ConvertSkip.TooLarge, 1)], server.Skipped.Select(s => (s.Reason, s.Frames)));
|
||||
// A PC whose WIC reads them: the managed decoders first, WIC for what they refuse.
|
||||
var desktop = ConvertPlanner.Plan(manifest, folder, present, dng, new ImageDecoder([new ReadsEverything(), new ManagedDecoderProvider()]), "Roof");
|
||||
Assert.SequenceEqual(Enumerable.Range(0, 7), desktop.Frames.Select(f => f.Index));
|
||||
Assert.SequenceEqual([(ConvertSkip.TooLarge, 1)], desktop.Skipped.Select(s => (s.Reason, s.Frames)));
|
||||
// The managed provider of a conversion reads headers; the import's does not open the files.
|
||||
Assert.True(FrameConverter.Decoders(DecoderFactory.CreateServer(), dng, ImageFileFormat.Tiff).Providers.OfType<ManagedDecoderProvider>().Single().ReadsHeaders);
|
||||
Assert.Equal(DecodeTier.Native, DecoderFactory.CreateServer().Probe(folder.File(frames[1].File), ImageFileFormat.Tiff).Tier);
|
||||
}
|
||||
|
||||
/// <summary>A photo whose profile's colorants convert nothing is planned out of a DNG, which converts through them, and carried into a TIFF.</summary>
|
||||
[Test]
|
||||
public void AProfileThatConvertsNothingKeepsItsPhotoOutOfADng()
|
||||
{
|
||||
var folder = new ArchivePaths(context.CreateTempDirectory("archive")).Sequence("roof");
|
||||
System.IO.Directory.CreateDirectory(Path.GetDirectoryName(folder.File("originals/x"))!);
|
||||
using var scene = ColorFixtures.Scene();
|
||||
var frame = Frame(0, "odd.jpg", ImageFileFormat.Jpeg);
|
||||
File.WriteAllBytes(folder.File(frame.File), ColorFixtures.Jpeg(scene, ColorFixtures.Degenerate()));
|
||||
var manifest = new SequenceManifest { Id = Guid.NewGuid(), Frames = [frame] };
|
||||
var dng = ConvertPlanner.Plan(manifest, folder, [frame.File], new ConvertOptions(ConvertTarget.LinearDng), DecoderFactory.CreateServer(), "Roof");
|
||||
Assert.SequenceEqual([(ConvertSkip.ColorProfile, 1)], dng.Skipped.Select(s => (s.Reason, s.Frames)));
|
||||
var tiff = ConvertPlanner.Plan(manifest, folder, [frame.File], new ConvertOptions(ConvertTarget.Tiff16), DecoderFactory.CreateServer(), "Roof");
|
||||
Assert.Count(1, tiff.Frames);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SizesAreEstimatedFromThePixels()
|
||||
{
|
||||
@@ -255,6 +372,16 @@ public sealed class ConvertPlannerTests(TestContext context)
|
||||
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default) => null;
|
||||
}
|
||||
|
||||
/// <summary>WIC as a PC with every codec has it: JPEG, PNG and TIFF native, whatever they hold.</summary>
|
||||
private sealed class ReadsEverything : IImageDecoderProvider
|
||||
{
|
||||
public string Name => "wic";
|
||||
|
||||
public DecodeTier? TierFor(ImageFileFormat format) => format is ImageFileFormat.Jpeg or ImageFileFormat.Png or ImageFileFormat.Tiff ? DecodeTier.Native : null;
|
||||
|
||||
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default) => null;
|
||||
}
|
||||
|
||||
/// <summary>WIC with the Raw Image Extension, as a conversion sees it: every camera RAW native, developed at 8 bits.</summary>
|
||||
internal sealed class EightBitRaw : IImageDecoderProvider
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user