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
@@ -26,6 +26,15 @@ public sealed class ManagedDecoderProvider : IImageDecoderProvider
|
||||
|
||||
public string Name => "managed";
|
||||
|
||||
/// <summary>
|
||||
/// Whether probes read the headers of JPEG, PNG and TIFF files too (<see cref="Probe(string, ImageFileFormat, string?)"/>)
|
||||
/// and find unreadable what <see cref="Decode"/> refuses before decoding pixels: an encoding, layout or colour model its
|
||||
/// decoders do not read (a CMYK, CIELab or palette TIFF, a compression it lacks, arithmetic coding), more pixels than
|
||||
/// they take, a file larger than it reads whole. A conversion's decoders do (<see cref="Conversion.FrameConverter.Decoders"/>):
|
||||
/// its dry run promises what it lists. Off, those files are not even opened.
|
||||
/// </summary>
|
||||
public bool ReadsHeaders { get; init; }
|
||||
|
||||
public DecodeTier? TierFor(ImageFileFormat format)
|
||||
{
|
||||
var info = ImageFormatInfo.Of(format);
|
||||
@@ -57,9 +66,11 @@ public sealed class ManagedDecoderProvider : IImageDecoderProvider
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The tier for a file whose signature format is already known (the index's): JPEG, PNG and TIFF decode natively
|
||||
/// whatever they hold (damage shows only when decoding), so only RAW and HEIF files are opened.
|
||||
/// The tier for a file whose signature format is already known (the index's): JPEG, PNG and TIFF are taken to decode
|
||||
/// natively, so only RAW and HEIF files are opened, unless the provider <see cref="ReadsHeaders"/>.
|
||||
/// </summary>
|
||||
/// <exception cref="InvalidDataException">With <see cref="ReadsHeaders"/>: a JPEG, PNG or TIFF that <see cref="Decode"/>
|
||||
/// refuses from its headers (<see cref="ReadHeaders"/>).</exception>
|
||||
public FileTier Probe(string path, ImageFileFormat format) => Probe(path, format, null);
|
||||
|
||||
/// <inheritdoc cref="Probe(string, ImageFileFormat)"/>
|
||||
@@ -68,8 +79,75 @@ public sealed class ManagedDecoderProvider : IImageDecoderProvider
|
||||
{
|
||||
var family = ImageFormatInfo.Of(format).Family;
|
||||
if (family is not (ImageFamily.Jpeg or ImageFamily.Png or ImageFamily.Tiff)) return Probe(path, companion);
|
||||
var decoder = family switch { ImageFamily.Jpeg => "managed-jpeg", ImageFamily.Png => "managed-png", _ => "managed-tiff" };
|
||||
return new FileTier(format, DecodeTier.Native, decoder, null, 0, 0);
|
||||
if (ReadsHeaders) return ReadHeaders(path, companion);
|
||||
return new FileTier(format, DecodeTier.Native, DecoderName(family), null, 0, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The tier of the file at <paramref name="path"/> from its headers, as <see cref="Decode"/> would reach it: a JPEG, PNG
|
||||
/// or TIFF (by its signature) is native, at its oriented size, only when its decoder takes what its headers describe
|
||||
/// (<see cref="JpegDecoder.ReadDecodableInfo"/>, <see cref="PngDecoder.MaxPixels"/>, <see cref="TiffImageReader.ReadInfo"/>)
|
||||
/// and a JPEG or PNG is no larger than one array holds (it is read whole); any other file is probed as
|
||||
/// <see cref="Probe(string, string?)"/> probes it.
|
||||
/// </summary>
|
||||
/// <exception cref="InvalidDataException">The file is malformed, or holds what <see cref="Decode"/> does not decode.</exception>
|
||||
/// <exception cref="IOException">The file cannot be read.</exception>
|
||||
private FileTier ReadHeaders(string path, string? companion)
|
||||
{
|
||||
using (var stream = Open(path))
|
||||
{
|
||||
var format = ImageSignature.Detect(stream, path);
|
||||
var family = ImageFormatInfo.Of(format).Family;
|
||||
if (family is ImageFamily.Jpeg or ImageFamily.Png or ImageFamily.Tiff)
|
||||
{
|
||||
// JPEG and PNG files are decoded from one array holding them whole.
|
||||
if (family != ImageFamily.Tiff && stream.Length > Array.MaxLength) throw new InvalidDataException($"{format}: the file is larger than one array holds");
|
||||
var (width, height) = family switch
|
||||
{
|
||||
ImageFamily.Jpeg => JpegSize(stream),
|
||||
ImageFamily.Png => PngSize(stream),
|
||||
_ => TiffImageReader.ReadInfo(stream),
|
||||
};
|
||||
(width, height) = PixelConvert.OrientedSize(width, height, MetadataReader.Read(stream, path).Orientation);
|
||||
return new FileTier(format, DecodeTier.Native, DecoderName(family), null, width, height);
|
||||
}
|
||||
}
|
||||
// The signature says another format than the index: probed as any file.
|
||||
return Probe(path, companion);
|
||||
}
|
||||
|
||||
private static string DecoderName(ImageFamily family) => family switch { ImageFamily.Jpeg => "managed-jpeg", ImageFamily.Png => "managed-png", _ => "managed-tiff" };
|
||||
|
||||
/// <summary>The size of a JPEG its decoder takes, from the markers before its first scan.</summary>
|
||||
private static (int Width, int Height) JpegSize(Stream stream)
|
||||
{
|
||||
var scanner = new Metadata.Jpeg.JpegParser.SegmentScanner(stream, 0, stream.Length, long.MaxValue);
|
||||
if (!scanner.StartsWithSoi()) throw new InvalidDataException("JPEG: missing SOI marker");
|
||||
var end = 0L;
|
||||
while (scanner.Next(out var segment))
|
||||
{
|
||||
if (segment.Marker is 0xD9) break;
|
||||
if (segment.Marker != 0xDA) continue;
|
||||
end = segment.Payload + segment.Length;
|
||||
break;
|
||||
}
|
||||
if (end == 0) throw new InvalidDataException("JPEG: no scan");
|
||||
var head = new byte[end];
|
||||
stream.Position = 0;
|
||||
stream.ReadExactly(head);
|
||||
var info = JpegDecoder.ReadDecodableInfo(head);
|
||||
return (info.Width, info.Height);
|
||||
}
|
||||
|
||||
/// <summary>The size of a PNG its decoder takes, from its header chunk.</summary>
|
||||
private static (int Width, int Height) PngSize(Stream stream)
|
||||
{
|
||||
var head = new byte[33];
|
||||
stream.Position = 0;
|
||||
if (stream.ReadAtLeast(head, head.Length, throwOnEndOfStream: false) < head.Length) throw new InvalidDataException("PNG: missing signature or IHDR");
|
||||
var info = PngDecoder.ReadInfo(head);
|
||||
if ((long)info.Width * info.Height > PngDecoder.MaxPixels) throw new InvalidDataException($"PNG: {info.Width}x{info.Height} exceeds the pixel limit");
|
||||
return (info.Width, info.Height);
|
||||
}
|
||||
|
||||
/// <summary>The tier this provider would use for the file at <paramref name="path"/>, without decoding pixels.</summary>
|
||||
@@ -82,8 +160,7 @@ public sealed class ManagedDecoderProvider : IImageDecoderProvider
|
||||
if (family is ImageFamily.Jpeg or ImageFamily.Png or ImageFamily.Tiff)
|
||||
{
|
||||
var (w, h) = MetadataReader.Read(stream, path).OrientedSize;
|
||||
var decoder = family switch { ImageFamily.Jpeg => "managed-jpeg", ImageFamily.Png => "managed-png", _ => "managed-tiff" };
|
||||
return new FileTier(format, DecodeTier.Native, decoder, null, w, h);
|
||||
return new FileTier(format, DecodeTier.Native, DecoderName(family), null, w, h);
|
||||
}
|
||||
if (family is not (ImageFamily.Dng or ImageFamily.CameraRaw or ImageFamily.Heif))
|
||||
return new FileTier(format, DecodeTier.Unavailable, null, $"no managed decoder for {format}", 0, 0);
|
||||
|
||||
Reference in new issue
Block a user