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
@@ -12,9 +12,13 @@ namespace Chronos.Core.Codecs.Icc;
|
||||
/// </summary>
|
||||
public sealed class IccCurve
|
||||
{
|
||||
/// <summary>Samples of a function curve its inverse is found among (<see cref="Invert"/>): steps of 1/65536 in code value.</summary>
|
||||
private const int InverseSamples = 65536;
|
||||
|
||||
private readonly double[]? _table;
|
||||
private readonly double[] _parameters;
|
||||
private readonly int _function;
|
||||
private double[]? _forward;
|
||||
|
||||
private IccCurve(int function, double[] parameters, double[]? table)
|
||||
{
|
||||
@@ -23,6 +27,66 @@ public sealed class IccCurve
|
||||
_table = table;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether every linear value has one code value (<see cref="Invert"/>): the curve never falls and ends above where it
|
||||
/// starts. A curve that falls somewhere maps two codes to one value, which no encoding undoes.
|
||||
/// </summary>
|
||||
public bool IsInvertible
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsPower(out var gamma)) return gamma > 0;
|
||||
var forward = Forward();
|
||||
for (var i = 1; i < forward.Length; i++)
|
||||
{
|
||||
if (forward[i] < forward[i - 1]) return false;
|
||||
}
|
||||
return forward[^1] > forward[0];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The code value 0..1 whose linear value is <paramref name="linear"/> (clamped to the curve's range): the inverse of
|
||||
/// <see cref="Evaluate"/> for an invertible curve, exact for a power law and a table, within 1/65536 of code for a
|
||||
/// parametric function.
|
||||
/// </summary>
|
||||
public double Invert(double linear)
|
||||
{
|
||||
if (IsPower(out var gamma)) return gamma > 0 ? Math.Pow(Math.Clamp(double.IsNaN(linear) ? 0 : linear, 0, 1), 1 / gamma) : 0;
|
||||
var forward = Forward();
|
||||
var y = double.IsNaN(linear) ? 0 : linear;
|
||||
if (y <= forward[0]) return 0;
|
||||
if (y >= forward[^1]) return 1;
|
||||
// The last sample at or below y, then the step to the next one interpolated as Evaluate interpolates.
|
||||
int lo = 0, hi = forward.Length - 1;
|
||||
while (hi - lo > 1)
|
||||
{
|
||||
var mid = (lo + hi) >>> 1;
|
||||
if (forward[mid] <= y) lo = mid;
|
||||
else hi = mid;
|
||||
}
|
||||
var step = forward[hi] - forward[lo];
|
||||
var t = step > 0 ? (y - forward[lo]) / step : 0;
|
||||
return (lo + t) / (forward.Length - 1);
|
||||
}
|
||||
|
||||
/// <summary>Whether the curve is a pure power law (a curveType gamma, or parametric function 0), and its exponent.</summary>
|
||||
private bool IsPower(out double gamma)
|
||||
{
|
||||
gamma = _function == 0 && _table is null ? _parameters[0] : 0;
|
||||
return _function == 0 && _table is null;
|
||||
}
|
||||
|
||||
/// <summary>The curve's linear values at evenly spaced code values: its own table, or the function sampled.</summary>
|
||||
private double[] Forward()
|
||||
{
|
||||
if (_table is { } table) return table;
|
||||
if (_forward is { } cached) return cached;
|
||||
var forward = new double[InverseSamples + 1];
|
||||
for (var i = 0; i < forward.Length; i++) forward[i] = Evaluate(i / (double)InverseSamples);
|
||||
return _forward = forward;
|
||||
}
|
||||
|
||||
/// <summary>A pure power law.</summary>
|
||||
public static IccCurve Gamma(double gamma) => new(0, [gamma], null);
|
||||
|
||||
@@ -115,13 +179,24 @@ public sealed class IccProfile
|
||||
/// <summary>The ICC D50 profile connection white (PCS illuminant).</summary>
|
||||
private static readonly Vector3D PcsWhite = new(0.9642, 1.0, 0.8249);
|
||||
|
||||
private IccProfile(byte[] data, uint colorSpace, string? description, Matrix3? colorants, IReadOnlyList<IccCurve>? curves)
|
||||
/// <summary>
|
||||
/// How far a matrix/TRC profile's matrix to linear Rec.709 and its curves may be from sRGB's for it to be sRGB: what
|
||||
/// profile makers' rounding (s15Fixed16 colorants, other chromatic adaptations, 1024-entry tables) leaves, far below a
|
||||
/// gamma 2.2 curve's difference (0.004 in linear value at code 0.2).
|
||||
/// </summary>
|
||||
private const double SrgbTolerance = 2e-3;
|
||||
|
||||
private readonly Matrix3? _toLinearRec709;
|
||||
|
||||
private IccProfile(byte[] data, uint colorSpace, string? description, Matrix3? colorants, Matrix3? toLinearRec709, IReadOnlyList<IccCurve>? curves)
|
||||
{
|
||||
Data = data;
|
||||
ColorSpace = colorSpace;
|
||||
Description = description;
|
||||
Colorants = colorants;
|
||||
_toLinearRec709 = toLinearRec709;
|
||||
Curves = curves;
|
||||
IsSrgb = IsRgb && (IsMatrixShaper ? DescribesSrgb(toLinearRec709!.Value, curves!) : description?.Contains("sRGB", StringComparison.Ordinal) == true);
|
||||
}
|
||||
|
||||
/// <summary>The profile's bytes, as embedded.</summary>
|
||||
@@ -137,10 +212,18 @@ public sealed class IccProfile
|
||||
/// <summary>The profile's description (its 'desc' tag), when it has one.</summary>
|
||||
public string? Description { get; }
|
||||
|
||||
/// <summary>Whether the profile describes sRGB (its description names it: "sRGB IEC61966-2.1", "sRGB built-in"…).</summary>
|
||||
public bool IsSrgb => IsRgb && Description?.Contains("sRGB", StringComparison.Ordinal) == true;
|
||||
/// <summary>
|
||||
/// Whether the profile describes sRGB: a matrix/TRC profile whose matrix to linear Rec.709 and curves are sRGB's (the
|
||||
/// numbers decide, never the name: "sRGB-elle-V4-g10" has a linear curve); a profile without them (lookup tables) whose
|
||||
/// description names it ("sRGB IEC61966-2.1", "sRGB v4 ICC preference"…).
|
||||
/// </summary>
|
||||
public bool IsSrgb { get; }
|
||||
|
||||
/// <summary>RGB → PCS XYZ (D50) of a matrix/TRC profile: rXYZ, gXYZ and bXYZ as columns; null otherwise.</summary>
|
||||
/// <summary>
|
||||
/// RGB → PCS XYZ (D50) of a matrix/TRC profile: rXYZ, gXYZ and bXYZ as columns; null otherwise, and null for colorants
|
||||
/// that convert nothing (<see cref="ToLinearRec709"/> has no meaning for them: a white without positive cone responses,
|
||||
/// a matrix that cannot be inverted).
|
||||
/// </summary>
|
||||
public Matrix3? Colorants { get; }
|
||||
|
||||
/// <summary>The tone curves: red, green and blue of a matrix/TRC profile, or the gray one; null when absent.</summary>
|
||||
@@ -160,14 +243,42 @@ public sealed class IccProfile
|
||||
|
||||
/// <summary>
|
||||
/// Linear RGB of a matrix/TRC profile → linear Rec.709 (D65): its colorants, adapted from the white they add up to
|
||||
/// (the profile's white in the PCS) to D65, so neutrals stay neutral.
|
||||
/// (the profile's white in the PCS) to D65, so neutrals stay neutral. Checked when the profile is parsed: finite and
|
||||
/// invertible.
|
||||
/// </summary>
|
||||
/// <exception cref="InvalidOperationException">The profile is not a matrix/TRC one.</exception>
|
||||
public Matrix3 ToLinearRec709()
|
||||
public Matrix3 ToLinearRec709() => _toLinearRec709 ?? throw new InvalidOperationException("Only a matrix/TRC profile has a matrix.");
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="ToLinearRec709"/> of <paramref name="colorants"/>, or null when they convert nothing: the white they add up
|
||||
/// to has a cone response that is not positive (Bradford cannot adapt it), or the matrix is not finite or not
|
||||
/// invertible (colours would collapse).
|
||||
/// </summary>
|
||||
private static Matrix3? LinearRec709(Matrix3 colorants)
|
||||
{
|
||||
if (Colorants is not { } colorants) throw new InvalidOperationException("Only a matrix/TRC profile has a matrix.");
|
||||
var white = colorants * new Vector3D(1, 1, 1);
|
||||
return Matrix3.XyzToRec709 * Matrix3.BradfordAdaptation(white, Chromaticity.D65.ToXyz()) * colorants;
|
||||
var cones = Matrix3.Bradford * white;
|
||||
if (!(cones.X > 0 && cones.Y > 0 && cones.Z > 0) || !double.IsFinite(cones.X + cones.Y + cones.Z)) return null;
|
||||
var m = Matrix3.XyzToRec709 * Matrix3.BradfordAdaptation(white, Chromaticity.D65.ToXyz()) * colorants;
|
||||
double[] elements = [m.M11, m.M12, m.M13, m.M21, m.M22, m.M23, m.M31, m.M32, m.M33];
|
||||
if (!elements.All(double.IsFinite) || !m.TryInvert(out var inverse)) return null;
|
||||
double[] inverted = [inverse.M11, inverse.M12, inverse.M13, inverse.M21, inverse.M22, inverse.M23, inverse.M31, inverse.M32, inverse.M33];
|
||||
return inverted.All(double.IsFinite) ? m : null;
|
||||
}
|
||||
|
||||
/// <summary>Whether a matrix to linear Rec.709 and three curves are sRGB's, within <see cref="SrgbTolerance"/>.</summary>
|
||||
private static bool DescribesSrgb(Matrix3 toLinearRec709, IReadOnlyList<IccCurve> curves)
|
||||
{
|
||||
var m = toLinearRec709;
|
||||
double[] offIdentity = [m.M11 - 1, m.M12, m.M13, m.M21, m.M22 - 1, m.M23, m.M31, m.M32, m.M33 - 1];
|
||||
if (offIdentity.Any(d => !(Math.Abs(d) <= SrgbTolerance))) return false;
|
||||
for (var i = 0; i <= 64; i++)
|
||||
{
|
||||
var code = i / 64.0;
|
||||
var srgb = Imaging.ColorSpace.SrgbToLinear(code);
|
||||
if (curves.Any(c => !(Math.Abs(c.Evaluate(code) - srgb) <= SrgbTolerance))) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>The profile in <paramref name="data"/>, or null when it is not an ICC profile (no 'acsp' signature, a size
|
||||
@@ -199,6 +310,7 @@ public sealed class IccProfile
|
||||
var pcs = BinaryPrimitives.ReadUInt32BigEndian(span[20..]);
|
||||
var description = Text(TagData(Tag("desc"), span));
|
||||
Matrix3? colorants = null;
|
||||
Matrix3? toLinearRec709 = null;
|
||||
IccCurve[]? curves = null;
|
||||
if (pcs == Xyz && colorSpace == Rgb)
|
||||
{
|
||||
@@ -209,20 +321,24 @@ public sealed class IccProfile
|
||||
if (XyzValue(TagData(RgbColorants[c], span)) is { } column) columns[c] = column;
|
||||
else matrix = false;
|
||||
}
|
||||
if (matrix) colorants = Matrix3.FromColumns(columns[0], columns[1], columns[2]);
|
||||
curves = new IccCurve[3];
|
||||
for (var c = 0; c < 3 && curves is not null; c++)
|
||||
{
|
||||
if (IccCurve.Parse(TagData(RgbCurves[c], span)) is { } curve) curves[c] = curve;
|
||||
else curves = null;
|
||||
}
|
||||
if (colorants is { } m && !(double.IsFinite(m.Determinant) && m.TryInvert(out _))) colorants = null;
|
||||
// Colorants that convert nothing (a damaged or odd profile) leave a profile that can be carried, not converted.
|
||||
if (matrix && LinearRec709(Matrix3.FromColumns(columns[0], columns[1], columns[2])) is { } m)
|
||||
{
|
||||
colorants = Matrix3.FromColumns(columns[0], columns[1], columns[2]);
|
||||
toLinearRec709 = m;
|
||||
}
|
||||
}
|
||||
else if (pcs == Xyz && colorSpace == Gray && IccCurve.Parse(TagData(Tag("kTRC"), span)) is { } gray)
|
||||
{
|
||||
curves = [gray];
|
||||
}
|
||||
return new IccProfile(data, colorSpace, description, colorants, curves);
|
||||
return new IccProfile(data, colorSpace, description, colorants, toLinearRec709, curves);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Buffers.Binary;
|
||||
using System.IO.Compression;
|
||||
using Chronos.Core.Codecs.Tiff;
|
||||
using Chronos.Core.Imaging;
|
||||
using Chronos.Core.Metadata;
|
||||
using Chronos.Core.Metadata.Bmff;
|
||||
@@ -15,17 +16,20 @@ namespace Chronos.Core.Codecs.Icc;
|
||||
/// with Interop index "R03" is Adobe RGB, a HEIF nclx box with P3 primaries is Display P3), or something no profile here
|
||||
/// describes (<see cref="Unknown"/>: a profile that does not parse, Exif uncalibrated without R03, an nclx box with other
|
||||
/// primaries or an HDR transfer). Camera RAWs and other formats say nothing here: their decoders give Rec.709 pixels.
|
||||
/// It also says how the decoders take the samples (<see cref="IsLinear"/>).
|
||||
/// </summary>
|
||||
public sealed class SourceColor
|
||||
{
|
||||
private const int MaxChunks = 4096;
|
||||
private const ushort Uncalibrated = 0xFFFF, AdobeRgbColorSpace = 2;
|
||||
private const uint FloatSamples = 3, LinearRaw = 34892;
|
||||
private static ReadOnlySpan<byte> IccSignature => "ICC_PROFILE\0"u8;
|
||||
|
||||
private SourceColor(IccProfile? profile, bool unknown)
|
||||
private SourceColor(IccProfile? profile, bool unknown, bool linear = false)
|
||||
{
|
||||
Profile = profile;
|
||||
IsUnknown = unknown;
|
||||
IsLinear = linear;
|
||||
}
|
||||
|
||||
/// <summary>No colour space recorded: the pixel values are sRGB.</summary>
|
||||
@@ -39,6 +43,13 @@ public sealed class SourceColor
|
||||
|
||||
public bool IsUnknown { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Whether the samples are linear light, which the decoders take as they are (a TIFF of floating-point samples, or
|
||||
/// linear RAW): values in the profile's primaries, not code values through its curves. Otherwise the samples are code
|
||||
/// values, which the decoders linearise with the sRGB curve.
|
||||
/// </summary>
|
||||
public bool IsLinear { get; }
|
||||
|
||||
/// <summary>The colour space of the photo at <paramref name="path"/>, whose signature says <paramref name="format"/>.</summary>
|
||||
/// <exception cref="IOException">The file cannot be read.</exception>
|
||||
public static SourceColor Read(string path, ImageFileFormat format)
|
||||
@@ -134,12 +145,18 @@ public sealed class SourceColor
|
||||
return Untagged;
|
||||
}
|
||||
|
||||
/// <summary>IFD0's InterColorProfile, else the colour space its Exif IFD records.</summary>
|
||||
/// <summary>
|
||||
/// The InterColorProfile of the image the decoders read (<see cref="TiffImageReader.MainImage"/>), else IFD0's, else
|
||||
/// the colour space IFD0's Exif IFD records; linear when that image holds floating-point samples or linear RAW.
|
||||
/// </summary>
|
||||
private static SourceColor Tiff(Stream stream)
|
||||
{
|
||||
if (TiffReader.Open(stream) is not { } reader || reader.ReadIfd(reader.FirstIfdOffset) is not { } ifd0) return Untagged;
|
||||
if (ifd0.Entries.TryGetValue(TiffTags.InterColorProfile, out var entry)) return Of(reader.ReadBytes(entry, IccProfile.MaxBytes));
|
||||
return Exif(reader, ifd0) ?? Untagged;
|
||||
var main = TiffImageReader.MainImage(reader);
|
||||
SourceColor? Profile(Ifd ifd) => ifd.Entries.TryGetValue(TiffTags.InterColorProfile, out var entry) ? Of(reader.ReadBytes(entry, IccProfile.MaxBytes)) : null;
|
||||
var color = Profile(main) ?? Profile(ifd0) ?? Exif(reader, ifd0) ?? Untagged;
|
||||
var linear = reader.GetUInt(main, TiffTags.SampleFormat) == FloatSamples || reader.GetUInt(main, TiffTags.PhotometricInterpretation) == LinearRaw;
|
||||
return linear ? new SourceColor(color.Profile, color.IsUnknown, linear: true) : color;
|
||||
}
|
||||
|
||||
/// <summary>The colr properties of the primary item: an ICC profile ('prof', 'rICC') first, else its nclx box.</summary>
|
||||
|
||||
@@ -20,6 +20,21 @@ public static class JpegDecoder
|
||||
return parser.Info();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The frame header of a stream <see cref="DecodeToLinear"/> decodes, from the markers before its first scan (all of
|
||||
/// <paramref name="head"/> needs to hold): what it refuses there is refused here too (a coding process or precision it
|
||||
/// does not decode, more pixels than <see cref="JpegDecodeOptions.MaxPixels"/>, components without a colour
|
||||
/// interpretation). Damage in the scans shows only when decoding.
|
||||
/// </summary>
|
||||
/// <exception cref="InvalidDataException">The headers are malformed, or describe what this decoder does not decode.</exception>
|
||||
public static JpegInfo ReadDecodableInfo(ReadOnlySpan<byte> head)
|
||||
{
|
||||
var info = ReadInfo(head);
|
||||
var renders = info.Process == JpegProcess.Lossless ? info.Components is 1 or 3 : info.ColorSpace != JpegColorSpace.Other;
|
||||
if (!renders) throw new InvalidDataException($"JPEG: {info.Components}-component {info.Process} images have no colour interpretation");
|
||||
return info;
|
||||
}
|
||||
|
||||
/// <summary>Decodes to interleaved gray or RGB samples (8-bit, or 16-bit containers for higher precisions).</summary>
|
||||
public static JpegImage Decode(ReadOnlySpan<byte> data, JpegDecodeOptions? options = null)
|
||||
{
|
||||
|
||||
@@ -44,6 +44,9 @@ public sealed class PngImage
|
||||
/// </summary>
|
||||
public static class PngDecoder
|
||||
{
|
||||
/// <summary>Largest width × height <see cref="Decode"/> decodes by default.</summary>
|
||||
public const long MaxPixels = 1L << 28;
|
||||
|
||||
private static ReadOnlySpan<byte> Signature => [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
|
||||
|
||||
private static readonly (int X, int Y, int Dx, int Dy)[] Adam7 =
|
||||
@@ -57,7 +60,7 @@ public static class PngDecoder
|
||||
}
|
||||
|
||||
/// <param name="maxPixels">Upper bound on width × height, checked before allocation.</param>
|
||||
public static PngImage Decode(ReadOnlySpan<byte> data, long maxPixels = 1L << 28)
|
||||
public static PngImage Decode(ReadOnlySpan<byte> data, long maxPixels = MaxPixels)
|
||||
{
|
||||
var info = ReadInfo(data);
|
||||
if ((long)info.Width * info.Height > maxPixels) throw new InvalidDataException($"PNG: {info.Width}x{info.Height} exceeds the pixel limit");
|
||||
|
||||
@@ -74,30 +74,93 @@ public static class TiffImageReader
|
||||
{
|
||||
private const int CompressionNone = 1, CompressionLzw = 5, CompressionJpeg = 7, CompressionDeflate = 8;
|
||||
private const int CompressionAdobeDeflate = 32946, CompressionPackBits = 32773, CompressionLossyJpeg = 34892;
|
||||
private const int PhotometricMinIsWhite = 0, PhotometricRgb = 2, PhotometricPalette = 3, PhotometricYCbCr = 6;
|
||||
private const int PhotometricMinIsWhite = 0, PhotometricMinIsBlack = 1, PhotometricRgb = 2, PhotometricPalette = 3, PhotometricYCbCr = 6;
|
||||
private const int PhotometricCfa = 32803, PhotometricLinearRaw = 34892;
|
||||
private const int MaxChunks = 1 << 24;
|
||||
private const long MaxChunkBytes = int.MaxValue / 2;
|
||||
|
||||
/// <summary>Decodes the first full-resolution image of a TIFF (or a DNG whose main image is not CFA).</summary>
|
||||
/// <summary>
|
||||
/// Decodes the first full-resolution image of a TIFF (or a DNG whose main image is not CFA). An image whose samples are
|
||||
/// not gray, RGB or linear RAW (palette, CMYK, CIELab, a CFA…) is refused before its pixels are read, never decoded as
|
||||
/// something else (<see cref="ReadInfo"/>).
|
||||
/// </summary>
|
||||
/// <exception cref="InvalidDataException">The file is malformed, or holds what this reader does not decode.</exception>
|
||||
public static ImageBuffer Decode(Stream stream, TiffDecodeOptions? options = null)
|
||||
{
|
||||
var reader = TiffReader.Open(stream) ?? throw new InvalidDataException("TIFF: missing header");
|
||||
var chain = reader.ReadChain();
|
||||
if (chain.Count == 0) throw new InvalidDataException("TIFF: no image directory");
|
||||
var ifd = chain.FirstOrDefault(i => ((reader.GetUInt(i, TiffTags.NewSubfileType) ?? 0) & 1) == 0) ?? chain[0];
|
||||
var ifd = MainImage(reader);
|
||||
var o = options ?? TiffDecodeOptions.Default;
|
||||
var g = Geometry.Read(reader, ifd, o);
|
||||
Renders(g.IsJpeg && g.Photometric == PhotometricYCbCr ? PhotometricRgb : g.Photometric, g.Spp, g.Format);
|
||||
return ToImageBuffer(ReadRaster(reader, ifd, o), o);
|
||||
}
|
||||
|
||||
public static ImageBuffer Decode(byte[] data, TiffDecodeOptions? options = null) => Decode(new MemoryStream(data, writable: false), options);
|
||||
|
||||
/// <summary>
|
||||
/// The size of the image <see cref="Decode"/> decodes, from the headers alone: whatever <see cref="Decode"/> refuses
|
||||
/// before reading pixels is refused here too (a layout, compression, sample format or photometric interpretation it does
|
||||
/// not read, more samples than <see cref="TiffDecodeOptions.MaxSamples"/>, more strips or tiles than it reads, none at
|
||||
/// all). Damage in the strips or tiles shows only when decoding.
|
||||
/// </summary>
|
||||
/// <exception cref="InvalidDataException">The file is malformed, or holds what <see cref="Decode"/> does not decode.</exception>
|
||||
public static (int Width, int Height) ReadInfo(Stream stream, TiffDecodeOptions? options = null)
|
||||
{
|
||||
var reader = TiffReader.Open(stream) ?? throw new InvalidDataException("TIFF: missing header");
|
||||
var ifd = MainImage(reader);
|
||||
var g = Geometry.Read(reader, ifd, options ?? TiffDecodeOptions.Default);
|
||||
Renders(g.IsJpeg && g.Photometric == PhotometricYCbCr ? PhotometricRgb : g.Photometric, g.Spp, g.Format);
|
||||
ChunkTables(reader, ifd, g);
|
||||
return (g.Width, g.Height);
|
||||
}
|
||||
|
||||
/// <summary>The offsets and byte counts of the strips or tiles of <paramref name="ifd"/>.</summary>
|
||||
/// <exception cref="InvalidDataException">Either table is missing or unreadable (one uncompressed strip may go without its count).</exception>
|
||||
private static (uint[] Offsets, uint[] Counts) ChunkTables(TiffReader reader, Ifd ifd, Geometry g)
|
||||
{
|
||||
var offsets = reader.GetUInts(ifd, g.Tiled ? TiffTags.TileOffsets : TiffTags.StripOffsets) ?? throw new InvalidDataException("TIFF: missing strip/tile offsets");
|
||||
var counts = reader.GetUInts(ifd, g.Tiled ? TiffTags.TileByteCounts : TiffTags.StripByteCounts);
|
||||
if (counts is null && g.ChunksPerPlane * g.Planes == 1 && g.Compression == CompressionNone) counts = [(uint)Math.Min(int.MaxValue, g.RowBytes * (long)g.ChunkHeight)];
|
||||
return (offsets, counts ?? throw new InvalidDataException("TIFF: missing strip/tile byte counts"));
|
||||
}
|
||||
|
||||
/// <summary>The directory <see cref="Decode"/> reads: the first full-resolution one (NewSubfileType without its
|
||||
/// reduced-resolution bit), else the first.</summary>
|
||||
/// <exception cref="InvalidDataException">The file has no image directory.</exception>
|
||||
internal static Ifd MainImage(TiffReader reader)
|
||||
{
|
||||
var chain = reader.ReadChain();
|
||||
if (chain.Count == 0) throw new InvalidDataException("TIFF: no image directory");
|
||||
return chain.FirstOrDefault(i => ((reader.GetUInt(i, TiffTags.NewSubfileType) ?? 0) & 1) == 0) ?? chain[0];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Refuses samples <see cref="ToImageBuffer"/> would not render as what they are: only gray (min-is-black, or integer
|
||||
/// min-is-white), RGB of at least three samples and linear RAW (one sample, or three and more) are; palette, CFA, CMYK,
|
||||
/// CIELab, LogLuv and the others are not, nor RGB of fewer than three samples.
|
||||
/// </summary>
|
||||
/// <param name="photometric">The photometric interpretation of the samples as decoded (JPEG-coded YCbCr is RGB).</param>
|
||||
private static void Renders(int photometric, int spp, TiffSampleFormat format)
|
||||
{
|
||||
var renders = photometric switch
|
||||
{
|
||||
PhotometricMinIsWhite => format == TiffSampleFormat.UnsignedInteger,
|
||||
PhotometricMinIsBlack => true,
|
||||
PhotometricRgb => spp >= 3,
|
||||
PhotometricLinearRaw => spp is 1 or >= 3,
|
||||
_ => false,
|
||||
};
|
||||
if (!renders) throw new InvalidDataException($"TIFF: photometric {photometric} with {spp} {format} samples per pixel is not decoded here");
|
||||
}
|
||||
|
||||
/// <summary>Converts a gray/RGB raster to linear light (min-is-white inverted; extra samples dropped).</summary>
|
||||
/// <exception cref="InvalidDataException">The samples are not gray, RGB or linear RAW (palette, CFA, CMYK, CIELab…).</exception>
|
||||
public static ImageBuffer ToImageBuffer(TiffRaster raster, TiffDecodeOptions? options = null)
|
||||
{
|
||||
var o = options ?? TiffDecodeOptions.Default;
|
||||
if (raster.Photometric is PhotometricCfa or PhotometricPalette)
|
||||
throw new InvalidDataException($"TIFF: photometric {raster.Photometric} needs colour reconstruction (CFA/palette) not done here");
|
||||
Renders(raster.Photometric, raster.SamplesPerPixel, raster.SampleFormat);
|
||||
int w = raster.Width, h = raster.Height, spp = raster.SamplesPerPixel;
|
||||
var colour = spp >= 3 && raster.Photometric is PhotometricRgb or PhotometricLinearRaw;
|
||||
var channels = colour ? 3 : 1;
|
||||
@@ -140,11 +203,7 @@ public static class TiffImageReader
|
||||
var g = Geometry.Read(reader, ifd, o);
|
||||
var warnings = new List<string>();
|
||||
var count = g.ChunksPerPlane * g.Planes;
|
||||
if (count > MaxChunks) throw new InvalidDataException($"TIFF: {count} strips/tiles");
|
||||
var offsets = reader.GetUInts(ifd, g.Tiled ? TiffTags.TileOffsets : TiffTags.StripOffsets) ?? throw new InvalidDataException("TIFF: missing strip/tile offsets");
|
||||
var counts = reader.GetUInts(ifd, g.Tiled ? TiffTags.TileByteCounts : TiffTags.StripByteCounts);
|
||||
if (counts is null && count == 1 && g.Compression == CompressionNone) counts = [(uint)Math.Min(int.MaxValue, g.RowBytes * (long)g.ChunkHeight)];
|
||||
if (counts is null) throw new InvalidDataException("TIFF: missing strip/tile byte counts");
|
||||
var (offsets, counts) = ChunkTables(reader, ifd, g);
|
||||
if (offsets.Length < count || counts.Length < count) warnings.Add($"TIFF: {count} strips/tiles expected, {Math.Min(offsets.Length, counts.Length)} present");
|
||||
|
||||
// I/O is sequential (one stream); decoding is parallel.
|
||||
@@ -245,16 +304,18 @@ public static class TiffImageReader
|
||||
g.ChunkWidth = (int)Math.Min(r.GetUInt(ifd, TiffTags.TileWidth) ?? 0, 1 << 16);
|
||||
g.ChunkHeight = (int)Math.Min(r.GetUInt(ifd, TiffTags.TileLength) ?? 0, 1 << 16);
|
||||
if (g.ChunkWidth == 0 || g.ChunkHeight == 0) throw new InvalidDataException("TIFF: zero tile size");
|
||||
g.Across = (g.Width + g.ChunkWidth - 1) / g.ChunkWidth;
|
||||
g.ChunksPerPlane = checked(g.Across * ((g.Height + g.ChunkHeight - 1) / g.ChunkHeight));
|
||||
}
|
||||
else
|
||||
{
|
||||
g.ChunkWidth = g.Width;
|
||||
g.ChunkHeight = (int)Math.Clamp(r.GetUInt(ifd, TiffTags.RowsPerStrip) ?? (uint)g.Height, 1, (uint)g.Height);
|
||||
g.Across = 1;
|
||||
g.ChunksPerPlane = (g.Height + g.ChunkHeight - 1) / g.ChunkHeight;
|
||||
}
|
||||
// Counted in 64 bits: more strips or tiles than this reader takes is refused as data, never as an arithmetic error.
|
||||
var across = ((long)g.Width + g.ChunkWidth - 1) / g.ChunkWidth;
|
||||
var perPlane = across * (((long)g.Height + g.ChunkHeight - 1) / g.ChunkHeight);
|
||||
if (perPlane * g.Planes > MaxChunks) throw new InvalidDataException($"TIFF: {perPlane * g.Planes} strips/tiles");
|
||||
g.Across = (int)across;
|
||||
g.ChunksPerPlane = (int)perPlane;
|
||||
g.RowBytes = ((long)g.ChunkWidth * g.ChunkSpp * g.Bps + 7) / 8;
|
||||
if (g.RowBytes * g.ChunkHeight > MaxChunkBytes) throw new InvalidDataException("TIFF: strip/tile too large");
|
||||
return g;
|
||||
|
||||
@@ -3,6 +3,8 @@ using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Core.Codecs.Icc;
|
||||
using Chronos.Core.Codecs.Jpeg;
|
||||
using Chronos.Core.Codecs.Tiff;
|
||||
using Chronos.Core.Export;
|
||||
using Chronos.Core.Imaging;
|
||||
using Chronos.Core.Model;
|
||||
@@ -15,7 +17,11 @@ public enum ConvertSkip
|
||||
/// <summary>The photo is in the target's format already (a TIFF of 8 bits included).</summary>
|
||||
SameFormat,
|
||||
|
||||
/// <summary>This host has no full-resolution decoder for the photo's format (a server and a CR3 or HEIC): a PC may have one.</summary>
|
||||
/// <summary>
|
||||
/// This host has no full-resolution decoder for the photo's format (a server and a CR3 or HEIC), or its decoders do not
|
||||
/// read this file at full resolution (a CMYK, CIELab or palette TIFF, a compression or size they do not take, a RAW body
|
||||
/// they do not develop and without a preview): a PC may have one.
|
||||
/// </summary>
|
||||
NoNativeDecoder,
|
||||
|
||||
/// <summary>This host reads the photo only from its embedded preview (a camera body its RAW decoder does not support).</summary>
|
||||
@@ -36,7 +42,10 @@ public enum ConvertSkip
|
||||
/// <summary>The photo's colour profile can be neither carried into the target nor converted to it (CMYK, an unknown space…).</summary>
|
||||
ColorProfile,
|
||||
|
||||
/// <summary>The photo is larger than the target holds (a JPEG side above 65535) or one image in memory can.</summary>
|
||||
/// <summary>
|
||||
/// The photo is larger than the target holds (a JPEG side above 65535) or than the server's decoders read back, which
|
||||
/// every converted file must (<see cref="ConvertPlanner.TooLarge"/>).
|
||||
/// </summary>
|
||||
TooLarge,
|
||||
}
|
||||
|
||||
@@ -149,10 +158,11 @@ public sealed record ConvertPlan(Guid ArchiveId, ConvertOptions Options, Convert
|
||||
|
||||
/// <summary>
|
||||
/// Plans the conversion of a sequence's frames (owner review 2, R7): a frame is converted when it is not in the target's
|
||||
/// format already, its file is there, it was hashed, it is not flagged unreadable, the target holds its size, this host
|
||||
/// decodes it at full resolution with the decoders the conversion uses (<see cref="FrameConverter.Decoders"/>: its format
|
||||
/// has a native decoder here and the file itself probes native now, whatever tier another host or an earlier pass
|
||||
/// recorded; for a 16-bit target, not through a RAW development at 8 bits), and its colour profile can be kept
|
||||
/// format already, its file is there, it was hashed, it is not flagged unreadable, its size is one the target holds and the
|
||||
/// server's decoders read back (<see cref="TooLarge"/>), this host decodes it at full resolution with the decoders the
|
||||
/// conversion uses (<see cref="FrameConverter.Decoders"/>: its format has a native decoder here and the file itself probes
|
||||
/// native now, whatever tier another host or an earlier pass recorded, its headers saying what those decoders take; for a
|
||||
/// 16-bit target, not through a RAW development at 8 bits), and its colour profile can be kept
|
||||
/// (<see cref="FrameConverter.Treatment"/>). The plan asks the files what the conversion will ask them, reading headers
|
||||
/// only, several at once. Sizes after are estimated from the pixels (<see cref="EstimateBytes"/>).
|
||||
/// </summary>
|
||||
@@ -176,6 +186,22 @@ public static class ConvertPlanner
|
||||
/// </summary>
|
||||
private const double FullChroma = 1.25;
|
||||
|
||||
/// <summary>
|
||||
/// Bits per pixel of pixels that do not compress at all (noise) in the costliest target: three 16-bit samples, 48 bits
|
||||
/// as Deflate stores them, and less than a bit more each as lossless JPEG codes them (an optimal Huffman code exceeds
|
||||
/// the entropy of what it codes by less than one bit); a 4:4:4 JPEG of noise at quality 100 takes about 22.
|
||||
/// </summary>
|
||||
public const int WorstBitsPerPixel = 51;
|
||||
|
||||
/// <summary>
|
||||
/// Margin on <see cref="WorstBitsPerPixel"/>: Deflate's stored blocks and zlib framing per strip, lossless JPEG's byte
|
||||
/// stuffing (a 0xFF byte in 256 of noise) and headers per tile stay below 1 %.
|
||||
/// </summary>
|
||||
private const double WorstMargin = 1.01;
|
||||
|
||||
/// <summary>What a converted file holds beyond its pixels at most: an ICC profile carried whole, the Exif block, the directory and its strip or tile tables.</summary>
|
||||
private const long WorstOverhead = IccProfile.MaxBytes + (4L << 20);
|
||||
|
||||
/// <summary>The plan of one sequence from its sidecar.</summary>
|
||||
/// <param name="present">Every file in the folders of the sequence's frames, relative to the sequence folder
|
||||
/// (<see cref="FileRenames.ListPresent"/>): the frames whose file is not among them are missing.</param>
|
||||
@@ -231,9 +257,11 @@ public static class ConvertPlanner
|
||||
var path = folder.File(frame.File);
|
||||
try
|
||||
{
|
||||
// Not read at full resolution here, although the format is: what the file holds (its encoding, its size, a RAW
|
||||
// body) is beyond these decoders, and only a preview, or nothing, is left.
|
||||
var probe = decoders.Probe(path, frame.Format);
|
||||
if (probe.Tier != DecodeTier.Native)
|
||||
return probe.Tier == DecodeTier.EmbeddedPreview ? eightBit ? ConvertSkip.EightBit : ConvertSkip.PreviewOnly : ConvertSkip.Undecodable;
|
||||
return eightBit ? ConvertSkip.EightBit : probe.Tier == DecodeTier.EmbeddedPreview ? ConvertSkip.PreviewOnly : ConvertSkip.NoNativeDecoder;
|
||||
if (TooLarge(options, probe.Width, probe.Height)) return ConvertSkip.TooLarge;
|
||||
return FrameConverter.Treatment(SourceColor.Read(path, frame.Format), options.Target) == ColorTreatment.Refuse ? ConvertSkip.ColorProfile : null;
|
||||
}
|
||||
@@ -245,17 +273,32 @@ public static class ConvertPlanner
|
||||
|
||||
/// <summary>
|
||||
/// Whether a frame of <paramref name="width"/> × <paramref name="height"/> (unknown: 0) cannot be converted to
|
||||
/// <paramref name="options"/>: a JPEG side above <see cref="FrameConverter.MaxJpegSide"/>, more samples than one image
|
||||
/// buffer indexes, or an estimated file larger than the array it is made in.
|
||||
/// <paramref name="options"/>, a bound and never an estimate: the target does not hold it (a JPEG side above
|
||||
/// <see cref="FrameConverter.MaxJpegSide"/>), the server's decoders, which every converted file must decode with
|
||||
/// (<see cref="FrameConverter.Verify"/>), do not read it back (a JPEG of more pixels than
|
||||
/// <see cref="JpegDecodeOptions.MaxPixels"/>, a TIFF-16 or DNG of more samples than <see cref="TiffDecodeOptions.MaxSamples"/>),
|
||||
/// or its file could outgrow the array it is made in (<see cref="WorstBytes"/>, which those sizes keep below it). What it
|
||||
/// lets through fits one image buffer, its file the array it is made in, and the server's decoders read it back.
|
||||
/// </summary>
|
||||
public static bool TooLarge(ConvertOptions options, int width, int height)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
if (width <= 0 || height <= 0) return false;
|
||||
if (options.Target == ConvertTarget.Jpeg && Math.Max(width, height) > FrameConverter.MaxJpegSide) return true;
|
||||
return (long)width * height * 3 > Array.MaxLength || EstimateBytes(options, width, height) > Array.MaxLength;
|
||||
var pixels = (long)width * height;
|
||||
var readBack = options.Target == ConvertTarget.Jpeg
|
||||
? Math.Max(width, height) <= FrameConverter.MaxJpegSide && pixels <= JpegDecodeOptions.Default.MaxPixels
|
||||
: pixels * 3 <= TiffDecodeOptions.Default.MaxSamples;
|
||||
return !readBack || WorstBytes(width, height) > Array.MaxLength;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The largest a converted file of <paramref name="width"/> × <paramref name="height"/> pixels can be when its pixels do
|
||||
/// not compress at all, in any target: <see cref="WorstBitsPerPixel"/> with its margin, and the most a file holds beyond
|
||||
/// its pixels.
|
||||
/// </summary>
|
||||
public static long WorstBytes(int width, int height) =>
|
||||
(long)Math.Ceiling((double)Math.Max(0, width) * Math.Max(0, height) * WorstBitsPerPixel / 8 * WorstMargin) + WorstOverhead;
|
||||
|
||||
/// <summary>
|
||||
/// Estimated size of a converted file of <paramref name="width"/> × <paramref name="height"/> pixels (unknown:
|
||||
/// <see cref="UnknownPixels"/>), from the bits per pixel the Export tab estimates with (<see cref="ExportEstimator"/>):
|
||||
|
||||
@@ -36,7 +36,10 @@ public enum ConversionFailure
|
||||
/// <summary>The photo's colour profile cannot be carried into the target or converted to it.</summary>
|
||||
ColorProfile,
|
||||
|
||||
/// <summary>The photo is larger than the target or the memory of one image holds.</summary>
|
||||
/// <summary>
|
||||
/// The photo is larger than the target holds, than the server's decoders read back (<see cref="ConvertPlanner.TooLarge"/>)
|
||||
/// or than one image in memory can.
|
||||
/// </summary>
|
||||
TooLarge,
|
||||
|
||||
/// <summary>The converted file did not verify: read back, metadata, or a decode by the server's decoders.</summary>
|
||||
@@ -63,11 +66,13 @@ public sealed class ConversionException(ConversionFailure reason, string message
|
||||
/// (<see cref="ExifWriter"/>) whose capture time, offset and exposure come from the sidecar (to the tick) and whose camera,
|
||||
/// lens, modes and GPS come from the original's metadata, the sequence's camera and lens filling what it lacks. The colour
|
||||
/// space the original's values are in (<see cref="SourceColor"/>) is kept: a JPEG or TIFF carries the original's ICC profile
|
||||
/// (its values pass through unchanged), a DNG gets them converted through the profile's matrix and curves, a gray profile
|
||||
/// is converted to sRGB; a profile none of that applies to refuses the photo (<see cref="Treatment"/>). The bytes are hashed
|
||||
/// as they are made and written atomically; then the file is read back (its SHA-256 must match), its metadata read again
|
||||
/// (the same capture time and offset) and it is decoded at full-resolution tier by the server's decoders, so every host can
|
||||
/// read it. MakerNote and XMP are not carried; the orientation is 1, the pixels being upright.
|
||||
/// (code values pass through unchanged; a float TIFF's linear values are encoded with the profile's curves), a DNG gets
|
||||
/// them converted through the profile's matrix (and its curves, for code values), a gray profile is converted to sRGB; a
|
||||
/// profile none of that applies to refuses the photo (<see cref="Treatment"/>). A photo larger than the plan allows is
|
||||
/// refused at the size decoded (<see cref="ConvertPlanner.TooLarge"/>). The bytes are hashed as they are made and written
|
||||
/// atomically; then the file is read back (its SHA-256 must match), its metadata read again (the same capture time and
|
||||
/// offset) and it is decoded at full-resolution tier by the server's decoders, so every host can read it. MakerNote and XMP
|
||||
/// are not carried; the orientation is 1, the pixels being upright.
|
||||
/// </summary>
|
||||
public static class FrameConverter
|
||||
{
|
||||
@@ -85,6 +90,9 @@ public static class FrameConverter
|
||||
|
||||
private static readonly Lazy<ImageDecoder> ServerDecoders = new(DecoderFactory.CreateServer);
|
||||
|
||||
/// <summary>The managed provider as a conversion uses it: its probes read the headers of JPEG, PNG and TIFF files.</summary>
|
||||
private static readonly ManagedDecoderProvider HeaderReading = new() { ReadsHeaders = true };
|
||||
|
||||
/// <summary>The decoders a converted file must decode with at full-resolution tier: the server's (managed only).</summary>
|
||||
public static ImageDecoder Verifier => ServerDecoders.Value;
|
||||
|
||||
@@ -93,13 +101,18 @@ public static class FrameConverter
|
||||
/// <paramref name="decoder"/>: the managed provider first, so every format it reads natively is decoded in-house (a RAW
|
||||
/// developed at float precision, as the server does), the others (WIC) after it, for what it cannot read; for a 16-bit
|
||||
/// target, a RAW never through a provider that develops RAWs at 8 bits (<see cref="IImageDecoderProvider.DevelopsRawAt8Bits"/>).
|
||||
/// The managed provider's probes read the headers of JPEG, PNG and TIFF files (<see cref="ManagedDecoderProvider.ReadsHeaders"/>),
|
||||
/// so what its decoders refuse (a CMYK or palette TIFF, a size above their limits) is asked of the others, or is not
|
||||
/// planned where there are none.
|
||||
/// </summary>
|
||||
public static ImageDecoder Decoders(ImageDecoder decoder, ConvertOptions options, ImageFileFormat format)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(decoder);
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
var eightBit = options.IsSixteenBit && ImageFormatInfo.Of(format).IsRaw;
|
||||
var providers = decoder.Providers.Where(p => !(eightBit && p.DevelopsRawAt8Bits)).OrderBy(p => p is ManagedDecoderProvider ? 0 : 1).ToList();
|
||||
var providers = decoder.Providers.Where(p => !(eightBit && p.DevelopsRawAt8Bits))
|
||||
.Select(p => p is ManagedDecoderProvider { ReadsHeaders: false } ? HeaderReading : p)
|
||||
.OrderBy(p => p is ManagedDecoderProvider ? 0 : 1).ToList();
|
||||
return providers.SequenceEqual(decoder.Providers) ? decoder : new ImageDecoder(providers);
|
||||
}
|
||||
|
||||
@@ -115,17 +128,26 @@ public static class FrameConverter
|
||||
&& decoder.Providers.Any(p => p.DevelopsRawAt8Bits && p.TierFor(format) == DecodeTier.Native);
|
||||
}
|
||||
|
||||
/// <summary>What a conversion to <paramref name="target"/> does with a photo whose values are in <paramref name="color"/>.</summary>
|
||||
/// <summary>
|
||||
/// What a conversion to <paramref name="target"/> does with a photo whose values are in <paramref name="color"/>.
|
||||
/// Code values (the decoders linearise them with the sRGB curve) pass to a JPEG or TIFF unchanged with their profile
|
||||
/// (<see cref="ColorTreatment.Carry"/>). Linear values (a float TIFF's, <see cref="SourceColor.IsLinear"/>) are in the
|
||||
/// profile's primaries: a JPEG or TIFF gets them encoded with the profile's own curves (<see cref="ColorTreatment.Encode"/>),
|
||||
/// or with sRGB's when the profile is sRGB. A DNG gets both converted through any matrix/TRC profile that is not sRGB,
|
||||
/// its curves applied to code values only (<see cref="ColorTreatment.Convert"/>), and takes a lookup-table profile only
|
||||
/// when it names sRGB. A gray profile's code values convert to sRGB; its linear values are linear gray already.
|
||||
/// </summary>
|
||||
public static ColorTreatment Treatment(SourceColor color, ConvertTarget target)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(color);
|
||||
if (color.IsUnknown) return ColorTreatment.Refuse;
|
||||
if (color.Profile is not { } profile) return ColorTreatment.None;
|
||||
if (profile.IsGray) return profile.IsGrayCurve ? ColorTreatment.Convert : ColorTreatment.Refuse;
|
||||
if (profile.IsGray) return color.IsLinear ? ColorTreatment.None : profile.IsGrayCurve ? ColorTreatment.Convert : ColorTreatment.Refuse;
|
||||
if (!profile.IsRgb) return ColorTreatment.Refuse;
|
||||
if (target != ConvertTarget.LinearDng) return ColorTreatment.Carry;
|
||||
if (profile.IsSrgb) return ColorTreatment.None;
|
||||
return profile.IsMatrixShaper ? ColorTreatment.Convert : ColorTreatment.Refuse;
|
||||
if (profile.IsSrgb) return target == ConvertTarget.LinearDng ? ColorTreatment.None : ColorTreatment.Carry;
|
||||
if (target == ConvertTarget.LinearDng) return profile.IsMatrixShaper ? ColorTreatment.Convert : ColorTreatment.Refuse;
|
||||
if (!color.IsLinear) return ColorTreatment.Carry;
|
||||
return profile.IsMatrixShaper && profile.Curves!.All(c => c.IsInvertible) ? ColorTreatment.Encode : ColorTreatment.Refuse;
|
||||
}
|
||||
|
||||
/// <summary>Converts <paramref name="source"/> into <paramref name="target"/> (created or replaced atomically) and verifies it.</summary>
|
||||
@@ -166,14 +188,17 @@ public static class FrameConverter
|
||||
$"{name}: decoded from its {decoded.Tier} only ({decoded.Decoder}), not at full resolution");
|
||||
}
|
||||
var pixels = decoded.Pixels;
|
||||
if (options.Target == ConvertTarget.Jpeg && Math.Max(pixels.Width, pixels.Height) > MaxJpegSide)
|
||||
throw new ConversionException(ConversionFailure.TooLarge, $"{name}: {pixels.Width}×{pixels.Height} exceeds a JPEG's {MaxJpegSide} pixels");
|
||||
// What the plan leaves out (ConvertPlanner.TooLarge) is refused here too, at the size decoded.
|
||||
if (ConvertPlanner.TooLarge(options, pixels.Width, pixels.Height))
|
||||
throw new ConversionException(ConversionFailure.TooLarge, $"{name}: {pixels.Width}×{pixels.Height} is too large for {options.Label}");
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
metadata = Metadata(MetadataReader.Read(source.Path, cancellationToken), source);
|
||||
using var rgb = pixels.Channels == 3 ? null : Rgb(pixels);
|
||||
var image = rgb ?? pixels;
|
||||
if (treatment == ColorTreatment.Convert) ToLinearRec709(image, color.Profile!, encoderThreads, cancellationToken);
|
||||
using var output = Encoded(image, metadata, options, treatment == ColorTreatment.Carry ? color.Profile!.Data : null, encoderThreads);
|
||||
if (treatment == ColorTreatment.Convert) ToLinearRec709(image, color.Profile!, color.IsLinear, encoderThreads, cancellationToken);
|
||||
if (treatment == ColorTreatment.Encode) EncodeWithCurves(image, color.Profile!, encoderThreads, cancellationToken);
|
||||
using var output = EncodedWithin(name, image, metadata, options, treatment is ColorTreatment.Carry or ColorTreatment.Encode ? color.Profile!.Data : null,
|
||||
encoderThreads);
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
// Hashed as made, and written from the same bytes (never copied again).
|
||||
var bytes = output.GetBuffer().AsSpan(0, (int)output.Length);
|
||||
@@ -266,6 +291,24 @@ public static class FrameConverter
|
||||
return output.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="Encoded"/>, where a file that outgrows the array it is made in (2 GB: the memory stream says its stream
|
||||
/// was too long) is too large, the photo's failure, never one of the archive's files. The plan leaves out what can
|
||||
/// (<see cref="ConvertPlanner.TooLarge"/>); this is what content beyond its worst case would do.
|
||||
/// </summary>
|
||||
private static MemoryStream EncodedWithin(string name, ImageBuffer image, FrameMetadata metadata, ConvertOptions options, byte[]? iccProfile, int encoderThreads)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Encoded(image, metadata, options, iccProfile, encoderThreads);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
// Encoding writes to memory only: an I/O error there is the stream's length limit.
|
||||
throw new ConversionException(ConversionFailure.TooLarge, $"{name}: its {options.Label} outgrew the memory one file is made in", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary><see cref="Encode"/> into a stream the caller disposes.</summary>
|
||||
private static MemoryStream Encoded(ImageBuffer image, FrameMetadata metadata, ConvertOptions options, byte[]? iccProfile, int encoderThreads)
|
||||
{
|
||||
@@ -315,15 +358,17 @@ public static class FrameConverter
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Values decoded as sRGB (the decoders linearise every code value with the sRGB curve) brought to linear Rec.709
|
||||
/// through the profile they really are in: each back to its code value, through the profile's curve, then its matrix
|
||||
/// (a gray profile's curve gives a neutral). Values outside sRGB come out below 0 or above 1, which a scene-referred
|
||||
/// DNG keeps.
|
||||
/// Values brought to linear Rec.709 through the profile they really are in. Values decoded from code values (the
|
||||
/// decoders linearise them with the sRGB curve) go back to their code value, through the profile's curve, then its
|
||||
/// matrix (a gray profile's curve gives a neutral). <paramref name="linear"/> values (<see cref="SourceColor.IsLinear"/>)
|
||||
/// are linear in the profile's primaries already: through its matrix alone, never a curve, never clamped. Values
|
||||
/// outside sRGB come out below 0 or above 1, which a scene-referred DNG keeps.
|
||||
/// </summary>
|
||||
internal static void ToLinearRec709(ImageBuffer image, IccProfile profile, int threads, CancellationToken cancellationToken)
|
||||
internal static void ToLinearRec709(ImageBuffer image, IccProfile profile, bool linear, int threads, CancellationToken cancellationToken)
|
||||
{
|
||||
var curves = profile.Curves ?? throw new InvalidOperationException("The profile has no tone curve.");
|
||||
var m = profile.IsMatrixShaper ? profile.ToLinearRec709() : Matrix3.Identity;
|
||||
if (linear && !profile.IsMatrixShaper) throw new InvalidOperationException("Linear values convert through a matrix/TRC profile only.");
|
||||
IccCurve red = curves[0], green = curves[Math.Min(1, curves.Count - 1)], blue = curves[Math.Min(2, curves.Count - 1)];
|
||||
Parallel.For(0, image.Height, new ParallelOptions { MaxDegreeOfParallelism = threads, CancellationToken = cancellationToken }, y =>
|
||||
{
|
||||
@@ -332,9 +377,13 @@ public static class FrameConverter
|
||||
var b = image.Row(2, y);
|
||||
for (var x = 0; x < r.Length; x++)
|
||||
{
|
||||
var lr = red.Evaluate(ColorSpace.LinearToSrgb(r[x]));
|
||||
var lg = green.Evaluate(ColorSpace.LinearToSrgb(g[x]));
|
||||
var lb = blue.Evaluate(ColorSpace.LinearToSrgb(b[x]));
|
||||
double lr = r[x], lg = g[x], lb = b[x];
|
||||
if (!linear)
|
||||
{
|
||||
lr = red.Evaluate(ColorSpace.LinearToSrgb(lr));
|
||||
lg = green.Evaluate(ColorSpace.LinearToSrgb(lg));
|
||||
lb = blue.Evaluate(ColorSpace.LinearToSrgb(lb));
|
||||
}
|
||||
r[x] = (float)(m.M11 * lr + m.M12 * lg + m.M13 * lb);
|
||||
g[x] = (float)(m.M21 * lr + m.M22 * lg + m.M23 * lb);
|
||||
b[x] = (float)(m.M31 * lr + m.M32 * lg + m.M33 * lb);
|
||||
@@ -342,6 +391,27 @@ public static class FrameConverter
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Linear values in a matrix/TRC profile's primaries (a float TIFF's) made ready for an encoder that writes code values
|
||||
/// through the sRGB curve, so the codes it writes are the profile's own: each value to its code value through the
|
||||
/// inverse of the profile's curve, then back through the sRGB curve, which the encoder undoes. The file carries the
|
||||
/// profile; what a code value cannot hold (above 1, below 0) is clipped as any such file clips it.
|
||||
/// </summary>
|
||||
internal static void EncodeWithCurves(ImageBuffer image, IccProfile profile, int threads, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!profile.IsMatrixShaper) throw new InvalidOperationException("Only a matrix/TRC profile has curves to encode with.");
|
||||
var curves = profile.Curves!;
|
||||
Parallel.For(0, image.Height, new ParallelOptions { MaxDegreeOfParallelism = threads, CancellationToken = cancellationToken }, y =>
|
||||
{
|
||||
for (var c = 0; c < 3; c++)
|
||||
{
|
||||
var curve = curves[c];
|
||||
var row = image.Row(c, y);
|
||||
for (var x = 0; x < row.Length; x++) row[x] = (float)ColorSpace.SrgbToLinear(curve.Invert(row[x]));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks a converted file: read back whole, its size and SHA-256 are those it was written with; its metadata names its
|
||||
/// format and the capture time and offset written into it; and the server's decoders read it at full-resolution tier,
|
||||
@@ -405,7 +475,7 @@ public static class FrameConverter
|
||||
/// <summary>What a conversion does with the colour space a photo's values are in (<see cref="FrameConverter.Treatment"/>).</summary>
|
||||
public enum ColorTreatment
|
||||
{
|
||||
/// <summary>sRGB (untagged, or an sRGB profile into a DNG): nothing to do.</summary>
|
||||
/// <summary>sRGB (untagged, or an sRGB profile into a DNG) or linear gray: nothing to do.</summary>
|
||||
None,
|
||||
|
||||
/// <summary>A JPEG or TIFF carries the original's ICC profile; its values pass through unchanged.</summary>
|
||||
@@ -414,6 +484,15 @@ public enum ColorTreatment
|
||||
/// <summary>The values are converted to linear Rec.709 through the profile (a DNG from a matrix/TRC profile, or a gray profile).</summary>
|
||||
Convert,
|
||||
|
||||
/// <summary>A profile none of that applies to (CMYK, Lab, a lookup-table RGB profile into a DNG, an unknown space): not converted.</summary>
|
||||
/// <summary>
|
||||
/// A JPEG or TIFF carries the original's matrix/TRC profile, its linear values (a float TIFF's) encoded with the
|
||||
/// profile's own curves (<see cref="FrameConverter.EncodeWithCurves"/>).
|
||||
/// </summary>
|
||||
Encode,
|
||||
|
||||
/// <summary>
|
||||
/// A profile none of that applies to (CMYK, Lab, a lookup-table RGB profile into a DNG or holding linear values, curves
|
||||
/// that cannot be inverted, an unknown space): not converted.
|
||||
/// </summary>
|
||||
Refuse,
|
||||
}
|
||||
@@ -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