diff --git a/src/Chronos.Core/Codecs/Icc/IccProfile.cs b/src/Chronos.Core/Codecs/Icc/IccProfile.cs
index fba7027d..b8b1ca63 100644
--- a/src/Chronos.Core/Codecs/Icc/IccProfile.cs
+++ b/src/Chronos.Core/Codecs/Icc/IccProfile.cs
@@ -12,9 +12,13 @@ namespace Chronos.Core.Codecs.Icc;
///
public sealed class IccCurve
{
+ /// Samples of a function curve its inverse is found among (): steps of 1/65536 in code value.
+ 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;
}
+ ///
+ /// Whether every linear value has one code value (): 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.
+ ///
+ 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];
+ }
+ }
+
+ ///
+ /// The code value 0..1 whose linear value is (clamped to the curve's range): the inverse of
+ /// for an invertible curve, exact for a power law and a table, within 1/65536 of code for a
+ /// parametric function.
+ ///
+ 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);
+ }
+
+ /// Whether the curve is a pure power law (a curveType gamma, or parametric function 0), and its exponent.
+ private bool IsPower(out double gamma)
+ {
+ gamma = _function == 0 && _table is null ? _parameters[0] : 0;
+ return _function == 0 && _table is null;
+ }
+
+ /// The curve's linear values at evenly spaced code values: its own table, or the function sampled.
+ 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;
+ }
+
/// A pure power law.
public static IccCurve Gamma(double gamma) => new(0, [gamma], null);
@@ -115,13 +179,24 @@ public sealed class IccProfile
/// The ICC D50 profile connection white (PCS illuminant).
private static readonly Vector3D PcsWhite = new(0.9642, 1.0, 0.8249);
- private IccProfile(byte[] data, uint colorSpace, string? description, Matrix3? colorants, IReadOnlyList? curves)
+ ///
+ /// 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).
+ ///
+ private const double SrgbTolerance = 2e-3;
+
+ private readonly Matrix3? _toLinearRec709;
+
+ private IccProfile(byte[] data, uint colorSpace, string? description, Matrix3? colorants, Matrix3? toLinearRec709, IReadOnlyList? 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);
}
/// The profile's bytes, as embedded.
@@ -137,10 +212,18 @@ public sealed class IccProfile
/// The profile's description (its 'desc' tag), when it has one.
public string? Description { get; }
- /// Whether the profile describes sRGB (its description names it: "sRGB IEC61966-2.1", "sRGB built-in"…).
- public bool IsSrgb => IsRgb && Description?.Contains("sRGB", StringComparison.Ordinal) == true;
+ ///
+ /// 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"…).
+ ///
+ public bool IsSrgb { get; }
- /// RGB → PCS XYZ (D50) of a matrix/TRC profile: rXYZ, gXYZ and bXYZ as columns; null otherwise.
+ ///
+ /// RGB → PCS XYZ (D50) of a matrix/TRC profile: rXYZ, gXYZ and bXYZ as columns; null otherwise, and null for colorants
+ /// that convert nothing ( has no meaning for them: a white without positive cone responses,
+ /// a matrix that cannot be inverted).
+ ///
public Matrix3? Colorants { get; }
/// The tone curves: red, green and blue of a matrix/TRC profile, or the gray one; null when absent.
@@ -160,14 +243,42 @@ public sealed class IccProfile
///
/// 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.
///
/// The profile is not a matrix/TRC one.
- public Matrix3 ToLinearRec709()
+ public Matrix3 ToLinearRec709() => _toLinearRec709 ?? throw new InvalidOperationException("Only a matrix/TRC profile has a matrix.");
+
+ ///
+ /// of , 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).
+ ///
+ 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;
+ }
+
+ /// Whether a matrix to linear Rec.709 and three curves are sRGB's, within .
+ private static bool DescribesSrgb(Matrix3 toLinearRec709, IReadOnlyList 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;
}
/// The profile in , 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);
}
///
diff --git a/src/Chronos.Core/Codecs/Icc/SourceColor.cs b/src/Chronos.Core/Codecs/Icc/SourceColor.cs
index 19afd573..1f05c431 100644
--- a/src/Chronos.Core/Codecs/Icc/SourceColor.cs
+++ b/src/Chronos.Core/Codecs/Icc/SourceColor.cs
@@ -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 (: 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 ().
///
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 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;
}
/// No colour space recorded: the pixel values are sRGB.
@@ -39,6 +43,13 @@ public sealed class SourceColor
public bool IsUnknown { get; }
+ ///
+ /// 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.
+ ///
+ public bool IsLinear { get; }
+
/// The colour space of the photo at , whose signature says .
/// The file cannot be read.
public static SourceColor Read(string path, ImageFileFormat format)
@@ -134,12 +145,18 @@ public sealed class SourceColor
return Untagged;
}
- /// IFD0's InterColorProfile, else the colour space its Exif IFD records.
+ ///
+ /// The InterColorProfile of the image the decoders read (), else IFD0's, else
+ /// the colour space IFD0's Exif IFD records; linear when that image holds floating-point samples or linear RAW.
+ ///
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;
}
/// The colr properties of the primary item: an ICC profile ('prof', 'rICC') first, else its nclx box.
diff --git a/src/Chronos.Core/Codecs/Jpeg/JpegDecoder.cs b/src/Chronos.Core/Codecs/Jpeg/JpegDecoder.cs
index 94361ad3..0e111861 100644
--- a/src/Chronos.Core/Codecs/Jpeg/JpegDecoder.cs
+++ b/src/Chronos.Core/Codecs/Jpeg/JpegDecoder.cs
@@ -20,6 +20,21 @@ public static class JpegDecoder
return parser.Info();
}
+ ///
+ /// The frame header of a stream decodes, from the markers before its first scan (all of
+ /// needs to hold): what it refuses there is refused here too (a coding process or precision it
+ /// does not decode, more pixels than , components without a colour
+ /// interpretation). Damage in the scans shows only when decoding.
+ ///
+ /// The headers are malformed, or describe what this decoder does not decode.
+ public static JpegInfo ReadDecodableInfo(ReadOnlySpan 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;
+ }
+
/// Decodes to interleaved gray or RGB samples (8-bit, or 16-bit containers for higher precisions).
public static JpegImage Decode(ReadOnlySpan data, JpegDecodeOptions? options = null)
{
diff --git a/src/Chronos.Core/Codecs/Png/PngDecoder.cs b/src/Chronos.Core/Codecs/Png/PngDecoder.cs
index f395ebe7..eaf75a84 100644
--- a/src/Chronos.Core/Codecs/Png/PngDecoder.cs
+++ b/src/Chronos.Core/Codecs/Png/PngDecoder.cs
@@ -44,6 +44,9 @@ public sealed class PngImage
///
public static class PngDecoder
{
+ /// Largest width × height decodes by default.
+ public const long MaxPixels = 1L << 28;
+
private static ReadOnlySpan 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
}
/// Upper bound on width × height, checked before allocation.
- public static PngImage Decode(ReadOnlySpan data, long maxPixels = 1L << 28)
+ public static PngImage Decode(ReadOnlySpan 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");
diff --git a/src/Chronos.Core/Codecs/Tiff/TiffImageReader.cs b/src/Chronos.Core/Codecs/Tiff/TiffImageReader.cs
index ffa4fffd..9e81786b 100644
--- a/src/Chronos.Core/Codecs/Tiff/TiffImageReader.cs
+++ b/src/Chronos.Core/Codecs/Tiff/TiffImageReader.cs
@@ -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;
- /// Decodes the first full-resolution image of a TIFF (or a DNG whose main image is not CFA).
+ ///
+ /// 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 ().
+ ///
+ /// The file is malformed, or holds what this reader does not decode.
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);
+ ///
+ /// The size of the image decodes, from the headers alone: whatever refuses
+ /// before reading pixels is refused here too (a layout, compression, sample format or photometric interpretation it does
+ /// not read, more samples than , more strips or tiles than it reads, none at
+ /// all). Damage in the strips or tiles shows only when decoding.
+ ///
+ /// The file is malformed, or holds what does not decode.
+ 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);
+ }
+
+ /// The offsets and byte counts of the strips or tiles of .
+ /// Either table is missing or unreadable (one uncompressed strip may go without its count).
+ 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"));
+ }
+
+ /// The directory reads: the first full-resolution one (NewSubfileType without its
+ /// reduced-resolution bit), else the first.
+ /// The file has no image directory.
+ 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];
+ }
+
+ ///
+ /// Refuses samples 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.
+ ///
+ /// The photometric interpretation of the samples as decoded (JPEG-coded YCbCr is RGB).
+ 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");
+ }
+
/// Converts a gray/RGB raster to linear light (min-is-white inverted; extra samples dropped).
+ /// The samples are not gray, RGB or linear RAW (palette, CFA, CMYK, CIELab…).
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();
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;
diff --git a/src/Chronos.Core/Conversion/ConvertPlanner.cs b/src/Chronos.Core/Conversion/ConvertPlanner.cs
index 29e6d432..496bbc14 100644
--- a/src/Chronos.Core/Conversion/ConvertPlanner.cs
+++ b/src/Chronos.Core/Conversion/ConvertPlanner.cs
@@ -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
/// The photo is in the target's format already (a TIFF of 8 bits included).
SameFormat,
- /// This host has no full-resolution decoder for the photo's format (a server and a CR3 or HEIC): a PC may have one.
+ ///
+ /// 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.
+ ///
NoNativeDecoder,
/// This host reads the photo only from its embedded preview (a camera body its RAW decoder does not support).
@@ -36,7 +42,10 @@ public enum ConvertSkip
/// The photo's colour profile can be neither carried into the target nor converted to it (CMYK, an unknown space…).
ColorProfile,
- /// The photo is larger than the target holds (a JPEG side above 65535) or one image in memory can.
+ ///
+ /// 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 ().
+ ///
TooLarge,
}
@@ -149,10 +158,11 @@ public sealed record ConvertPlan(Guid ArchiveId, ConvertOptions Options, Convert
///
/// 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 (: 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 (), this host decodes it at full resolution with the decoders the
+/// conversion uses (: 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
/// (). The plan asks the files what the conversion will ask them, reading headers
/// only, several at once. Sizes after are estimated from the pixels ().
///
@@ -176,6 +186,22 @@ public static class ConvertPlanner
///
private const double FullChroma = 1.25;
+ ///
+ /// 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.
+ ///
+ public const int WorstBitsPerPixel = 51;
+
+ ///
+ /// Margin on : 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 %.
+ ///
+ private const double WorstMargin = 1.01;
+
+ /// 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.
+ private const long WorstOverhead = IccProfile.MaxBytes + (4L << 20);
+
/// The plan of one sequence from its sidecar.
/// Every file in the folders of the sequence's frames, relative to the sequence folder
/// (): the frames whose file is not among them are missing.
@@ -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
///
/// Whether a frame of × (unknown: 0) cannot be converted to
- /// : a JPEG side above , more samples than one image
- /// buffer indexes, or an estimated file larger than the array it is made in.
+ /// , a bound and never an estimate: the target does not hold it (a JPEG side above
+ /// ), the server's decoders, which every converted file must decode with
+ /// (), do not read it back (a JPEG of more pixels than
+ /// , a TIFF-16 or DNG of more samples than ),
+ /// or its file could outgrow the array it is made in (, 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.
///
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;
}
+ ///
+ /// The largest a converted file of × pixels can be when its pixels do
+ /// not compress at all, in any target: with its margin, and the most a file holds beyond
+ /// its pixels.
+ ///
+ public static long WorstBytes(int width, int height) =>
+ (long)Math.Ceiling((double)Math.Max(0, width) * Math.Max(0, height) * WorstBitsPerPixel / 8 * WorstMargin) + WorstOverhead;
+
///
/// Estimated size of a converted file of × pixels (unknown:
/// ), from the bits per pixel the Export tab estimates with ():
diff --git a/src/Chronos.Core/Conversion/FrameConverter.cs b/src/Chronos.Core/Conversion/FrameConverter.cs
index 3ddc7a50..3215a5f3 100644
--- a/src/Chronos.Core/Conversion/FrameConverter.cs
+++ b/src/Chronos.Core/Conversion/FrameConverter.cs
@@ -36,7 +36,10 @@ public enum ConversionFailure
/// The photo's colour profile cannot be carried into the target or converted to it.
ColorProfile,
- /// The photo is larger than the target or the memory of one image holds.
+ ///
+ /// The photo is larger than the target holds, than the server's decoders read back ()
+ /// or than one image in memory can.
+ ///
TooLarge,
/// The converted file did not verify: read back, metadata, or a decode by the server's decoders.
@@ -63,11 +66,13 @@ public sealed class ConversionException(ConversionFailure reason, string message
/// () 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 () 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 (). 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 (). A photo larger than the plan allows is
+/// refused at the size decoded (). 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.
///
public static class FrameConverter
{
@@ -85,6 +90,9 @@ public static class FrameConverter
private static readonly Lazy ServerDecoders = new(DecoderFactory.CreateServer);
+ /// The managed provider as a conversion uses it: its probes read the headers of JPEG, PNG and TIFF files.
+ private static readonly ManagedDecoderProvider HeaderReading = new() { ReadsHeaders = true };
+
/// The decoders a converted file must decode with at full-resolution tier: the server's (managed only).
public static ImageDecoder Verifier => ServerDecoders.Value;
@@ -93,13 +101,18 @@ public static class FrameConverter
/// : 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 ().
+ /// The managed provider's probes read the headers of JPEG, PNG and TIFF files (),
+ /// 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.
///
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);
}
- /// What a conversion to does with a photo whose values are in .
+ ///
+ /// What a conversion to does with a photo whose values are in .
+ /// Code values (the decoders linearise them with the sRGB curve) pass to a JPEG or TIFF unchanged with their profile
+ /// (). Linear values (a float TIFF's, ) are in the
+ /// profile's primaries: a JPEG or TIFF gets them encoded with the profile's own curves (),
+ /// 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 (), 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.
+ ///
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;
}
/// Converts into (created or replaced atomically) and verifies it.
@@ -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();
}
+ ///
+ /// , 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
+ /// (); this is what content beyond its worst case would do.
+ ///
+ 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);
+ }
+ }
+
/// into a stream the caller disposes.
private static MemoryStream Encoded(ImageBuffer image, FrameMetadata metadata, ConvertOptions options, byte[]? iccProfile, int encoderThreads)
{
@@ -315,15 +358,17 @@ public static class FrameConverter
}
///
- /// 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). values ()
+ /// 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.
///
- 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
});
}
+ ///
+ /// 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.
+ ///
+ 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]));
+ }
+ });
+ }
+
///
/// 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
/// What a conversion does with the colour space a photo's values are in ().
public enum ColorTreatment
{
- /// sRGB (untagged, or an sRGB profile into a DNG): nothing to do.
+ /// sRGB (untagged, or an sRGB profile into a DNG) or linear gray: nothing to do.
None,
/// A JPEG or TIFF carries the original's ICC profile; its values pass through unchanged.
@@ -414,6 +484,15 @@ public enum ColorTreatment
/// The values are converted to linear Rec.709 through the profile (a DNG from a matrix/TRC profile, or a gray profile).
Convert,
- /// A profile none of that applies to (CMYK, Lab, a lookup-table RGB profile into a DNG, an unknown space): not converted.
+ ///
+ /// 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 ().
+ ///
+ Encode,
+
+ ///
+ /// 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.
+ ///
Refuse,
}
diff --git a/src/Chronos.Core/Imaging/Decoders/ManagedDecoderProvider.cs b/src/Chronos.Core/Imaging/Decoders/ManagedDecoderProvider.cs
index f97a159d..49fa74fe 100644
--- a/src/Chronos.Core/Imaging/Decoders/ManagedDecoderProvider.cs
+++ b/src/Chronos.Core/Imaging/Decoders/ManagedDecoderProvider.cs
@@ -26,6 +26,15 @@ public sealed class ManagedDecoderProvider : IImageDecoderProvider
public string Name => "managed";
+ ///
+ /// Whether probes read the headers of JPEG, PNG and TIFF files too ()
+ /// and find unreadable what 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 ():
+ /// its dry run promises what it lists. Off, those files are not even opened.
+ ///
+ public bool ReadsHeaders { get; init; }
+
public DecodeTier? TierFor(ImageFileFormat format)
{
var info = ImageFormatInfo.Of(format);
@@ -57,9 +66,11 @@ public sealed class ManagedDecoderProvider : IImageDecoderProvider
}
///
- /// 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 .
///
+ /// With : a JPEG, PNG or TIFF that
+ /// refuses from its headers ().
public FileTier Probe(string path, ImageFileFormat format) => Probe(path, format, null);
///
@@ -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);
+ }
+
+ ///
+ /// The tier of the file at from its headers, as 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
+ /// (, , )
+ /// and a JPEG or PNG is no larger than one array holds (it is read whole); any other file is probed as
+ /// probes it.
+ ///
+ /// The file is malformed, or holds what does not decode.
+ /// The file cannot be read.
+ 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" };
+
+ /// The size of a JPEG its decoder takes, from the markers before its first scan.
+ 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);
+ }
+
+ /// The size of a PNG its decoder takes, from its header chunk.
+ 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);
}
/// The tier this provider would use for the file at , without decoding pixels.
@@ -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);
diff --git a/tests/Chronos.Core.Tests/Codecs/IccProfileTests.cs b/tests/Chronos.Core.Tests/Codecs/IccProfileTests.cs
index b3810337..60c93bf9 100644
--- a/tests/Chronos.Core.Tests/Codecs/IccProfileTests.cs
+++ b/tests/Chronos.Core.Tests/Codecs/IccProfileTests.cs
@@ -1,5 +1,6 @@
using Chronos.Core.Codecs.Icc;
using Chronos.Core.Codecs.Tiff;
+using Chronos.Core.Conversion;
using Chronos.Core.Imaging;
using Chronos.Core.Mathematics;
using Chronos.Core.Tests.Conversion;
@@ -8,7 +9,7 @@ using Chronos.Testing;
namespace Chronos.Core.Tests.Codecs;
/// ICC profiles (owner review 2, R7): parsed, synthesized for the spaces a marker names, and read from each container.
-public sealed class IccProfileTests
+public sealed class IccProfileTests(TestContext context)
{
[Test]
public void TheSynthesizedProfilesAreMatrixShapersOfTheirSpace()
@@ -69,6 +70,142 @@ public sealed class IccProfileTests
}
}
+ ///
+ /// Colorants that convert nothing (their white has a cone response that is not positive, as a damaged profile's may) are
+ /// dropped when the profile is parsed: it stays an RGB profile to carry, never one to convert through, and asking its
+ /// matrix fails as for any profile without one, not inside the chromatic adaptation.
+ ///
+ [Test]
+ public void ColorantsThatConvertNothingAreDroppedWhenParsed()
+ {
+ var degenerate = IccProfile.Parse(ColorFixtures.Degenerate())!;
+ Assert.True(degenerate.IsRgb);
+ Assert.Null(degenerate.Colorants);
+ Assert.False(degenerate.IsMatrixShaper);
+ Assert.False(degenerate.IsSrgb);
+ Assert.Throws(() => degenerate.ToLinearRec709());
+ // The same colorants inside a white with positive cone responses convert.
+ Assert.NotNull(IccProfile.AdobeRgb.Colorants);
+ }
+
+ ///
+ /// A damaged profile never fails a conversion's colour work midway (owner review 2, R7): bytes flipped in matrix/TRC
+ /// profiles, as in a damaged file, give a profile that does not parse, one only to carry, or one whose matrix is finite
+ /// and invertible, and whose conversion of a JPEG's code values or a float TIFF's linear values, wherever the treatment
+ /// asks for one, runs to finite values; anything else the treatment refuses before any pixel is decoded.
+ ///
+ [Test]
+ public void DamagedProfilesConvertOrAreRefusedNeverFailMidway()
+ {
+ var random = new Random(27);
+ using var pixels = new ImageBuffer(4, 2);
+ float[] values = [0, 0.18f, 0.5f, 1, 3, -0.2f, 0.02f, 0.9f];
+ var data = pixels.Data;
+ for (var i = 0; i < data.Length; i++) data[i] = values[i % values.Length];
+ int parsed = 0, worked = 0;
+ foreach (var original in (byte[][])[IccProfile.AdobeRgb.Data, IccProfile.DisplayP3.Data, ColorFixtures.Profile("Wide gamma 1.8", IccCurve.Gamma(1.8), wide: true).Data])
+ {
+ for (var n = 0; n < 1000; n++)
+ {
+ var damaged = (byte[])original.Clone();
+ // The tag table and the tags: colorants, curves, description.
+ for (var k = random.Next(1, 4); k > 0; k--) damaged[random.Next(128, damaged.Length)] ^= (byte)random.Next(1, 256);
+ if (IccProfile.Parse(damaged) is not { } profile) continue;
+ parsed++;
+ if (profile.IsMatrixShaper)
+ {
+ var m = profile.ToLinearRec709();
+ Assert.True(Finite(m) && m.TryInvert(out var inverse) && Finite(inverse), "a matrix to convert through is finite and invertible");
+ }
+ SourceColor[] carriers =
+ [
+ SourceColor.Read(new MemoryStream(JpegCarrying(damaged)), ImageFileFormat.Jpeg),
+ SourceColor.Read(new MemoryStream(ColorFixtures.FloatTiff(pixels, damaged)), ImageFileFormat.Tiff),
+ ];
+ foreach (var color in carriers)
+ {
+ foreach (var target in Enum.GetValues())
+ {
+ using var image = pixels.Clone();
+ var treatment = FrameConverter.Treatment(color, target);
+ if (treatment == ColorTreatment.Convert) FrameConverter.ToLinearRec709(image, color.Profile!, color.IsLinear, 1, CancellationToken.None);
+ else if (treatment == ColorTreatment.Encode) FrameConverter.EncodeWithCurves(image, color.Profile!, 1, CancellationToken.None);
+ else continue;
+ worked++;
+ // Code values convert within the matrix's reach; linear ones as large as the file holds may overflow.
+ if (treatment == ColorTreatment.Encode || !color.IsLinear)
+ {
+ foreach (var v in image.Data) Assert.True(float.IsFinite(v), $"{treatment} of {target} gives finite values");
+ }
+ }
+ }
+ }
+ }
+ context.Log($"{parsed} damaged profiles parsed, {worked} conversions run");
+ Assert.Greater(parsed, 1000);
+ Assert.Greater(worked, 1000);
+
+ static bool Finite(Matrix3 m) => new[] { m.M11, m.M12, m.M13, m.M21, m.M22, m.M23, m.M31, m.M32, m.M33 }.All(double.IsFinite);
+
+ // The markers of a JPEG carrying the profile in one APP2 chunk, no image: all its colour is read from.
+ static byte[] JpegCarrying(byte[] icc)
+ {
+ var length = 2 + 14 + icc.Length;
+ return [0xFF, 0xD8, 0xFF, 0xE2, (byte)(length >> 8), (byte)length, .. "ICC_PROFILE\0"u8, 1, 1, .. icc, 0xFF, 0xD9];
+ }
+ }
+
+ /// A matrix/TRC profile is sRGB when its numbers are sRGB's, whatever its name says; one without them by its name.
+ [Test]
+ public void SrgbIsWhatTheNumbersSay()
+ {
+ Assert.True(ColorFixtures.Profile("sRGB IEC61966-2.1", IccCurve.Srgb).IsSrgb);
+ Assert.True(ColorFixtures.Profile("Camera RGB", IccCurve.Srgb).IsSrgb);
+ // sRGB's primaries with a linear curve, or a gamma 2.2 one, named sRGB.
+ Assert.False(ColorFixtures.Profile("sRGB-elle-V4-g10", IccCurve.Gamma(1)).IsSrgb);
+ Assert.False(ColorFixtures.Profile("sRGB gamma 2.2", IccCurve.Gamma(2.2)).IsSrgb);
+ Assert.False(ColorFixtures.Profile("sRGB wide", IccCurve.Srgb, wide: true).IsSrgb);
+ Assert.False(IccProfile.DisplayP3.IsSrgb);
+ }
+
+ /// Curves invert: exactly for a power law and a table, within a 16-bit step for a parametric function.
+ [Test]
+ public void CurvesInvert()
+ {
+ var gamma = IccCurve.Gamma(2.2);
+ var table = ColorFixtures.Profile("Gamma 1.8", IccCurve.Gamma(1.8)).Curves![0];
+ var srgb = IccCurve.Srgb;
+ foreach (var curve in (IccCurve[])[gamma, table, srgb]) Assert.True(curve.IsInvertible);
+ for (var code = 0.0; code <= 1; code += 1 / 128.0)
+ {
+ Assert.Approximately(code, gamma.Invert(gamma.Evaluate(code)), 1e-9);
+ Assert.Approximately(code, table.Invert(table.Evaluate(code)), 1e-6);
+ Assert.Approximately(code, srgb.Invert(ColorSpace.SrgbToLinear(code)), 1 / 65536.0);
+ }
+ // Linear values beyond the curve's range take its ends.
+ Assert.Equal((0.0, 1.0), (gamma.Invert(-0.5), gamma.Invert(3)));
+ Assert.Equal((0.0, 1.0), (table.Invert(-0.5), table.Invert(3)));
+ // A curve that falls somewhere maps two codes to one value: no encoding undoes it.
+ Assert.False(IccCurve.Parse([.. "curv"u8, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0xFF, 0xFF, 0x80, 0])!.IsInvertible);
+ Assert.False(IccCurve.Parse([.. "curv"u8, 0, 0, 0, 0, 0, 0, 0, 2, 0x80, 0, 0x80, 0])!.IsInvertible);
+ }
+
+ /// A TIFF of floating-point samples or linear RAW holds linear values; integer samples are code values.
+ [Test]
+ public void ATiffSaysWhetherItsSamplesAreLinear()
+ {
+ using var scene = ColorFixtures.Scene();
+ SourceColor Read(byte[] file) => SourceColor.Read(new MemoryStream(file), ImageFileFormat.Tiff);
+ var adobe = IccProfile.AdobeRgb.Data;
+ var linear = Read(ColorFixtures.FloatTiff(scene, adobe));
+ Assert.True(linear.IsLinear);
+ Assert.BytesEqual(adobe, linear.Profile!.Data);
+ Assert.True(Read(ColorFixtures.FloatTiff(scene)) is { IsLinear: true, Profile: null, IsUnknown: false });
+ Assert.True(Read(Chronos.Core.Codecs.Dng.LinearDngWriter.Write(scene)).IsLinear);
+ Assert.False(Read(TiffWriter.Write(scene, new TiffWriteOptions { BitsPerSample = 16, IccProfile = adobe })).IsLinear);
+ Assert.False(Read(TiffWriter.Write(scene)).IsLinear);
+ }
+
[Test]
public void EachContainerSaysItsColourSpace()
{
diff --git a/tests/Chronos.Core.Tests/Codecs/TiffTests.cs b/tests/Chronos.Core.Tests/Codecs/TiffTests.cs
index f429c44d..706c8b3c 100644
--- a/tests/Chronos.Core.Tests/Codecs/TiffTests.cs
+++ b/tests/Chronos.Core.Tests/Codecs/TiffTests.cs
@@ -194,6 +194,78 @@ public sealed class TiffTests(TestContext context)
Assert.Throws(() => TiffImageReader.ToImageBuffer(raster));
}
+ ///
+ /// Samples that are not gray, RGB or linear RAW are refused before their pixels are read, from the headers alone too,
+ /// never decoded as gray from their first plane (a CMYK red read as black, owner review 2, R7).
+ ///
+ [Theory]
+ [Case(5, 4)] // CMYK
+ [Case(8, 3)] // CIELab
+ [Case(3, 1)] // palette
+ [Case(2, 1)] // RGB of one sample
+ [Case(32844, 1)] // LogL
+ public void SamplesItDoesNotRenderAreRefusedNotReadAsGray(int photometric, int samples)
+ {
+ // A CMYK red: no cyan, full magenta and yellow, no black.
+ var pixel = Enumerable.Range(0, samples).Select(c => (byte)(c is 1 or 2 ? 255 : 0)).ToArray();
+ var tiff = Conversion.ColorFixtures.Tiff(6, 4, (ushort)photometric, pixel);
+ Assert.Throws(() => TiffImageReader.Decode(tiff));
+ Assert.Throws(() => TiffImageReader.ReadInfo(new MemoryStream(tiff)));
+ Assert.Throws(() => TiffImageReader.ToImageBuffer(Raster(tiff)));
+ }
+
+ /// The headers say what reads, and refuse what it refuses.
+ [Test]
+ public void ReadInfoSaysWhatDecodeReads()
+ {
+ foreach (var name in (string[])["tiff/lzw-pred.tif", "tiff/lzw-tiled.tif", "tiff/planar-zip.tif", "tiff/jpeg-ycbcr.tif", "tiff/float32-pred3.tif", "tiff/gray-miniswhite.tif"])
+ {
+ using var image = TiffImageReader.Decode(Fixture(name));
+ Assert.Equal((image.Width, image.Height), TiffImageReader.ReadInfo(new MemoryStream(Fixture(name))), name);
+ }
+ var rgb = Conversion.ColorFixtures.Tiff(6, 4, 2, [10, 20, 30]);
+ Assert.Equal((6, 4), TiffImageReader.ReadInfo(new MemoryStream(rgb)));
+ // More samples than the decoder takes, a compression it lacks (ZSTD), float samples whose white is 0, no strips.
+ Assert.Throws(() => TiffImageReader.ReadInfo(new MemoryStream(rgb), new TiffDecodeOptions { MaxSamples = 6 * 4 * 3 - 1 }));
+ Assert.Throws(() => TiffImageReader.ReadInfo(new MemoryStream(Conversion.ColorFixtures.Tiff(6, 4, 2, [1, 2, 3], d => d.Short(TiffTags.Compression, 50000)))));
+ var floatWhiteIsZero = TiffTestEncoders.Build(d =>
+ {
+ Layout(d, 2, 2, 1, 32, compression: 1);
+ d.Short(TiffTags.PhotometricInterpretation, 0);
+ d.Short(TiffTags.SampleFormat, 3);
+ }, [new byte[16]], tiled: false);
+ Assert.Throws(() => TiffImageReader.ReadInfo(new MemoryStream(floatWhiteIsZero)));
+ Assert.Throws(() => TiffImageReader.Decode(floatWhiteIsZero));
+ using var noStrips = new MemoryStream();
+ noStrips.Write("II*\0\0\0\0\0"u8);
+ var directory = new TiffDirectory();
+ Layout(directory, 6, 4, 3, 8, compression: 1);
+ TiffWriter.WriteDirectory(noStrips, 0, directory);
+ Assert.Throws(() => TiffImageReader.ReadInfo(new MemoryStream(noStrips.ToArray())));
+ // Tiles that no count holds are a malformed file, not an arithmetic error, to a decode as to the headers: a chain
+ // that finds them refused asks its next decoder.
+ var tiles = Conversion.ColorFixtures.Tiff(6, 4, 2, [1, 2, 3], d =>
+ {
+ d.Long(TiffTags.ImageWidth, int.MaxValue);
+ d.Long(TiffTags.ImageLength, int.MaxValue);
+ d.Long(TiffTags.TileWidth, 1);
+ d.Long(TiffTags.TileLength, 1);
+ d.Long(TiffTags.TileOffsets, 8);
+ });
+ var unbounded = new TiffDecodeOptions { MaxSamples = long.MaxValue };
+ Assert.Throws(() => TiffImageReader.ReadInfo(new MemoryStream(tiles), unbounded));
+ Assert.Throws(() => TiffImageReader.Decode(tiles, unbounded));
+ // More strips than the reader takes (one row each), refused before any is read.
+ var strips = Conversion.ColorFixtures.Tiff(6, 4, 1, [1], d =>
+ {
+ d.Long(TiffTags.ImageWidth, 1);
+ d.Long(TiffTags.ImageLength, (1 << 24) + 1);
+ d.Long(TiffTags.RowsPerStrip, 1);
+ });
+ Assert.Throws(() => TiffImageReader.ReadInfo(new MemoryStream(strips)));
+ Assert.Throws(() => TiffImageReader.Decode(strips));
+ }
+
[Theory]
[Case(8, 1, TiffCompression.None, false)]
[Case(8, 3, TiffCompression.Deflate, true)]
diff --git a/tests/Chronos.Core.Tests/Conversion/ColorFixtures.cs b/tests/Chronos.Core.Tests/Conversion/ColorFixtures.cs
index 7cd51102..d15b8664 100644
--- a/tests/Chronos.Core.Tests/Conversion/ColorFixtures.cs
+++ b/tests/Chronos.Core.Tests/Conversion/ColorFixtures.cs
@@ -1,9 +1,12 @@
+using System.Buffers.Binary;
using System.IO.Compression;
using Chronos.Core.Codecs.Icc;
using Chronos.Core.Codecs.Jpeg;
using Chronos.Core.Codecs.Png;
+using Chronos.Core.Codecs.Tiff;
using Chronos.Core.Imaging;
using Chronos.Core.Metadata.Tiff;
+using Chronos.Core.Tests.Codecs;
using Chronos.Core.Tests.Metadata.Builders;
namespace Chronos.Core.Tests.Conversion;
@@ -94,6 +97,101 @@ internal static class ColorFixtures
return data;
}
+ /// A matrix/TRC profile of , with sRGB's primaries or Adobe RGB's (D65) and .
+ public static IccProfile Profile(string description, IccCurve curve, bool wide = false) => wide
+ ? IccProfile.Synthesize(description, new(0.64, 0.33), new(0.21, 0.71), new(0.15, 0.06), curve)
+ : IccProfile.Synthesize(description, new(0.64, 0.33), new(0.30, 0.60), new(0.15, 0.06), curve);
+
+ ///
+ /// The Adobe RGB profile with colorants that convert nothing: they add up to a white whose cone responses are not all
+ /// positive, although the matrix they make can be inverted (as a damaged profile's can).
+ ///
+ public static byte[] Degenerate()
+ {
+ var data = (byte[])IccProfile.AdobeRgb.Data.Clone();
+ var count = BinaryPrimitives.ReadInt32BigEndian(data.AsSpan(128));
+ (string Tag, double X, double Y, double Z)[] colorants = [("rXYZ", -1.0, 0.2, 0.1), ("gXYZ", 0.1, 0.5, 0.1), ("bXYZ", 0.1, 0.3, 0.6)];
+ for (var i = 0; i < count; i++)
+ {
+ var entry = data.AsSpan(132 + 12 * i, 12);
+ foreach (var (tag, x, y, z) in colorants)
+ {
+ if (!entry[..4].SequenceEqual(System.Text.Encoding.ASCII.GetBytes(tag))) continue;
+ var at = BinaryPrimitives.ReadInt32BigEndian(entry[4..]) + 8;
+ BinaryPrimitives.WriteInt32BigEndian(data.AsSpan(at), (int)Math.Round(x * 65536));
+ BinaryPrimitives.WriteInt32BigEndian(data.AsSpan(at + 4), (int)Math.Round(y * 65536));
+ BinaryPrimitives.WriteInt32BigEndian(data.AsSpan(at + 8), (int)Math.Round(z * 65536));
+ }
+ }
+ return data;
+ }
+
+ ///
+ /// An uncompressed TIFF of × 8-bit samples,
+ /// per pixel in interpretation, each pixel .
+ ///
+ public static byte[] Tiff(int width, int height, ushort photometric, byte[] pixel, Action? tags = null)
+ {
+ var strip = new byte[width * height * pixel.Length];
+ for (var i = 0; i < width * height; i++) pixel.CopyTo(strip, i * pixel.Length);
+ return TiffTestEncoders.Build(d =>
+ {
+ TiffTests.Layout(d, width, height, pixel.Length, 8, compression: 1);
+ d.Short(TiffTags.PhotometricInterpretation, photometric);
+ tags?.Invoke(d);
+ }, [strip], tiled: false);
+ }
+
+ ///
+ /// The headers of a TIFF of × pixels of three 8-bit samples in
+ /// interpretation, in strips of 16 rows, of which only the first is there: what a
+ /// header read takes, without its pixels.
+ ///
+ public static byte[] TiffHeader(int width, int height, ushort photometric = 2) => Tiff(6, 4, photometric, [1, 2, 3], d =>
+ {
+ d.Long(TiffTags.ImageWidth, (uint)width);
+ d.Long(TiffTags.ImageLength, (uint)height);
+ d.Long(TiffTags.RowsPerStrip, 16);
+ });
+
+ /// A PNG signature and header chunk of × 8-bit RGB, nothing after it.
+ public static byte[] PngHeader(int width, int height)
+ {
+ var ihdr = new byte[13];
+ BinaryPrimitives.WriteInt32BigEndian(ihdr, width);
+ BinaryPrimitives.WriteInt32BigEndian(ihdr.AsSpan(4), height);
+ (ihdr[8], ihdr[9]) = (8, 2);
+ return [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, .. Box.Png("IHDR", ihdr)];
+ }
+
+ ///
+ /// The markers of a JPEG of × three-component pixels coded by
+ /// (0xC0 baseline, 0xC9 arithmetic…), up to its first scan.
+ ///
+ public static byte[] JpegHeader(int width, int height, byte sof = 0xC0) =>
+ [
+ 0xFF, 0xD8,
+ 0xFF, sof, 0x00, 0x11, 0x08, (byte)(height >> 8), (byte)height, (byte)(width >> 8), (byte)width, 0x03, 0x01, 0x11, 0x00, 0x02, 0x11, 0x00, 0x03, 0x11, 0x00,
+ 0xFF, 0xDA, 0x00, 0x0C, 0x03, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00, 0x3F, 0x00,
+ 0x12, 0x34, 0xFF, 0xD9,
+ ];
+
+ /// An uncompressed TIFF of 32-bit floating-point RGB (linear light), with an ICC profile when one is given.
+ public static byte[] FloatTiff(ImageBuffer image, byte[]? icc = null)
+ {
+ var strip = new byte[image.PlaneLength * 3 * 4];
+ for (var i = 0; i < image.PlaneLength; i++)
+ {
+ for (var c = 0; c < 3; c++) BinaryPrimitives.WriteSingleLittleEndian(strip.AsSpan((i * 3 + c) * 4), image.Plane(c)[i]);
+ }
+ return TiffTestEncoders.Build(d =>
+ {
+ TiffTests.Layout(d, image.Width, image.Height, 3, 32, compression: 1);
+ d.Short(TiffTags.SampleFormat, 3, 3, 3);
+ if (icc is not null) d.Add(TiffTags.InterColorProfile, TiffType.Undefined, icc.Length, icc);
+ }, [strip], tiled: false);
+ }
+
/// A valid profile larger than one JPEG APP2 chunk holds (the Adobe RGB one padded with a private tag's room).
public static byte[] Large()
{
diff --git a/tests/Chronos.Core.Tests/Conversion/ConvertPlannerTests.cs b/tests/Chronos.Core.Tests/Conversion/ConvertPlannerTests.cs
index d400d547..195b4bb8 100644
--- a/tests/Chronos.Core.Tests/Conversion/ConvertPlannerTests.cs
+++ b/tests/Chronos.Core.Tests/Conversion/ConvertPlannerTests.cs
@@ -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)
///
/// 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.
///
[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));
}
+ ///
+ /// 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.
+ ///
+ [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(() => 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(() => 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())
+ {
+ 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())
+ {
+ 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));
+ }
+ }
+
+ ///
+ /// 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.
+ ///
+ [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().Single().ReadsHeaders);
+ Assert.Equal(DecodeTier.Native, DecoderFactory.CreateServer().Probe(folder.File(frames[1].File), ImageFileFormat.Tiff).Tier);
+ }
+
+ /// A photo whose profile's colorants convert nothing is planned out of a DNG, which converts through them, and carried into a TIFF.
+ [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;
}
+ /// WIC as a PC with every codec has it: JPEG, PNG and TIFF native, whatever they hold.
+ 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;
+ }
+
/// WIC with the Raw Image Extension, as a conversion sees it: every camera RAW native, developed at 8 bits.
internal sealed class EightBitRaw : IImageDecoderProvider
{
diff --git a/tests/Chronos.Core.Tests/Conversion/FrameConverterTests.cs b/tests/Chronos.Core.Tests/Conversion/FrameConverterTests.cs
index 3451f5b9..13f5f5d7 100644
--- a/tests/Chronos.Core.Tests/Conversion/FrameConverterTests.cs
+++ b/tests/Chronos.Core.Tests/Conversion/FrameConverterTests.cs
@@ -436,6 +436,135 @@ public sealed class FrameConverterTests(TestContext context)
}
}
+ ///
+ /// A CMYK TIFF without a profile is decoded by what reads CMYK (WIC on a PC), never by the managed reader as the gray of
+ /// its first ink; where nothing reads it (the server), it is not converted, and nothing is written.
+ ///
+ [Test]
+ public void ACmykTiffIsDecodedByWhatReadsItNeverAsGray()
+ {
+ var path = Path.Combine(context.CreateTempDirectory("source"), "cmyk.tif");
+ File.WriteAllBytes(path, ColorFixtures.Tiff(6, 4, 5, [0, 255, 255, 0]));
+ var frame = Frame("cmyk.tif", ImageFileFormat.Tiff, 6, 4);
+ using var red = new ImageBuffer(6, 4);
+ red.Plane(0).Fill(0.6f);
+ red.Plane(1).Fill(0.003f);
+ red.Plane(2).Fill(0.002f);
+ var jpeg = new ConvertOptions(ConvertTarget.Jpeg);
+ var file = FrameConverter.Convert(new ConvertSource(path, frame), jpeg, new ImageDecoder([new Developed(red, ImageFileFormat.Tiff), new ManagedDecoderProvider()]),
+ Target(jpeg), cancellationToken: context.Cancellation);
+ using var converted = DecoderFactory.CreateServer().Decode(new DecodeRequest(file.Path, AllowPreview: false))!;
+ Assert.Greater(converted.Pixels[0, 3, 2], 0.5);
+ Assert.Less(converted.Pixels[1, 3, 2], 0.02);
+
+ var output = Target(jpeg);
+ var refused = Assert.Throws(() =>
+ FrameConverter.Convert(new ConvertSource(path, frame), jpeg, DecoderFactory.CreateServer(), output, cancellationToken: context.Cancellation));
+ Assert.Equal(ConversionFailure.NoDecoder, refused.Reason);
+ Assert.FileMissing(output);
+ }
+
+ ///
+ /// A float TIFF's values are linear light in its profile's primaries (owner review 2, R7): a JPEG or TIFF carries the
+ /// profile with codes its own curves give them, so a reader that applies the profile reads the original values; a DNG
+ /// gets them through the profile's matrix alone, no curve and no clip, so values above white stay.
+ ///
+ [Theory]
+ [Case(1.0)]
+ [Case(1.8)]
+ public void AFloatTiffKeepsItsLinearValuesInItsColourSpace(double gamma)
+ {
+ var profile = ColorFixtures.Profile($"Wide gamma {gamma}", IccCurve.Gamma(gamma), wide: true);
+ using var scene = new ImageBuffer(8, 4);
+ scene.Data.Fill(0.18f);
+ (scene[0, 1, 1], scene[1, 1, 1], scene[2, 1, 1]) = (0.5f, 0.1f, 0.05f);
+ (scene[0, 2, 1], scene[1, 2, 1], scene[2, 2, 1]) = (3f, 3f, 3f);
+ var path = Path.Combine(context.CreateTempDirectory("source"), "stack.tif");
+ File.WriteAllBytes(path, ColorFixtures.FloatTiff(scene, profile.Data));
+ var source = new ConvertSource(path, Frame("stack.tif", ImageFileFormat.Tiff, 8, 4));
+ var server = DecoderFactory.CreateServer();
+ Assert.Equal(ColorTreatment.Encode, FrameConverter.Treatment(SourceColor.Read(path, ImageFileFormat.Tiff), ConvertTarget.Tiff16));
+
+ foreach (var target in (ConvertTarget[])[ConvertTarget.Tiff16, ConvertTarget.Jpeg])
+ {
+ var options = new ConvertOptions(target, 100);
+ var file = FrameConverter.Convert(source, options, server, Target(options), cancellationToken: context.Cancellation);
+ Assert.BytesEqual(profile.Data, SourceColor.Read(file.Path, file.Format).Profile!.Data);
+ using var converted = server.Decode(new DecodeRequest(file.Path, AllowPreview: false))!;
+ // The codes written, read through the profile carried.
+ double Read(int c, int x, int y) => profile.Curves![c].Evaluate(ColorSpace.LinearToSrgb(converted.Pixels[c, x, y]));
+ var tolerance = target == ConvertTarget.Jpeg ? 0.012 : 1e-3;
+ Assert.Approximately(0.18, Read(1, 5, 2), tolerance, $"{target}, gamma {gamma}");
+ Assert.Approximately(0.5, Read(0, 1, 1), tolerance * 2, $"{target}, gamma {gamma}");
+ Assert.Approximately(0.05, Read(2, 1, 1), tolerance, $"{target}, gamma {gamma}");
+ // Above white is clipped there, as in any file of code values.
+ Assert.Approximately(1, Read(0, 2, 1), tolerance * 2);
+ }
+
+ 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 = profile.ToLinearRec709();
+ foreach (var (x, y) in ((int, int)[])[(5, 2), (1, 1), (2, 1)])
+ {
+ var expected = matrix * new Vector3D(scene[0, x, y], scene[1, x, y], scene[2, x, y]);
+ Assert.Approximately(expected.X, developed.Pixels[0, x, y], 2e-3 * Math.Max(1, expected.X));
+ Assert.Approximately(expected.Y, developed.Pixels[1, x, y], 2e-3 * Math.Max(1, expected.Y));
+ Assert.Approximately(expected.Z, developed.Pixels[2, x, y], 2e-3 * Math.Max(1, expected.Z));
+ }
+ Assert.Greater(developed.Pixels[1, 2, 1], 2.9);
+ }
+
+ ///
+ /// A DNG converts through any matrix/TRC profile that is not sRGB by its numbers, whatever its name: one named sRGB with
+ /// a linear curve gets its code values as linear light, not as sRGB codes.
+ ///
+ [Test]
+ public void AProfileNamedSrgbWithAnotherCurveIsConvertedIntoADng()
+ {
+ var folder = context.CreateTempDirectory("source");
+ using var scene = ColorFixtures.Scene();
+ var server = DecoderFactory.CreateServer();
+ var dng = new ConvertOptions(ConvertTarget.LinearDng);
+ var linearCurve = ColorFixtures.Profile("sRGB-elle-V4-g10", IccCurve.Gamma(1));
+ var real = ColorFixtures.Profile("sRGB IEC61966-2.1", IccCurve.Srgb);
+ Assert.Equal(ColorTreatment.Convert, FrameConverter.Treatment(SourceColor.Read(new MemoryStream(ColorFixtures.Jpeg(scene, linearCurve.Data)), ImageFileFormat.Jpeg), ConvertTarget.LinearDng));
+ Assert.Equal(ColorTreatment.None, FrameConverter.Treatment(SourceColor.Read(new MemoryStream(ColorFixtures.Jpeg(scene, real.Data)), ImageFileFormat.Jpeg), ConvertTarget.LinearDng));
+
+ var path = Path.Combine(folder, "g10.jpg");
+ File.WriteAllBytes(path, ColorFixtures.Jpeg(scene, linearCurve.Data));
+ using var original = server.Decode(new DecodeRequest(path, AllowPreview: false))!;
+ var file = FrameConverter.Convert(new ConvertSource(path, Frame("g10.jpg", ImageFileFormat.Jpeg, 48, 32)), dng, server, Target(dng), cancellationToken: context.Cancellation);
+ using var developed = server.Decode(new DecodeRequest(file.Path, AllowPreview: false))!;
+ for (var i = 0; i < scene.PlaneLength; i += 5)
+ {
+ var code = ColorSpace.LinearToSrgb(original.Pixels.Plane(1)[i]);
+ Assert.Approximately(code, developed.Pixels.Plane(1)[i], 3e-3);
+ }
+ }
+
+ ///
+ /// A profile whose colorants convert nothing (a damaged one) refuses its photo for a DNG, which would convert through
+ /// them, at once and as a colour profile, and is carried unchanged into a JPEG or TIFF.
+ ///
+ [Test]
+ public void AProfileThatConvertsNothingIsRefusedForADngAndCarriedElsewhere()
+ {
+ var path = Path.Combine(context.CreateTempDirectory("source"), "odd.jpg");
+ using var scene = ColorFixtures.Scene();
+ var degenerate = ColorFixtures.Degenerate();
+ File.WriteAllBytes(path, ColorFixtures.Jpeg(scene, degenerate));
+ var source = new ConvertSource(path, Frame("odd.jpg", ImageFileFormat.Jpeg, 48, 32));
+ var dng = new ConvertOptions(ConvertTarget.LinearDng);
+ var output = Target(dng);
+ var refused = Assert.Throws(() => FrameConverter.Convert(source, dng, DecoderFactory.CreateServer(), output, cancellationToken: context.Cancellation));
+ Assert.Equal(ConversionFailure.ColorProfile, refused.Reason);
+ Assert.FileMissing(output);
+ var tiff = new ConvertOptions(ConvertTarget.Tiff16);
+ var carried = FrameConverter.Convert(source, tiff, DecoderFactory.CreateServer(), Target(tiff), cancellationToken: context.Cancellation);
+ Assert.BytesEqual(degenerate, SourceColor.Read(carried.Path, carried.Format).Profile!.Data);
+ }
+
/// A photo larger than the target or one buffer holds fails as too large, not with the encoder's own exception.
[Test]
public void APhotoTooLargeForTheTargetFailsAsTooLarge()
diff --git a/tests/Chronos.Core.Tests/Decoding/ManagedDecoderProviderTests.cs b/tests/Chronos.Core.Tests/Decoding/ManagedDecoderProviderTests.cs
index d03912a6..64fc057d 100644
--- a/tests/Chronos.Core.Tests/Decoding/ManagedDecoderProviderTests.cs
+++ b/tests/Chronos.Core.Tests/Decoding/ManagedDecoderProviderTests.cs
@@ -232,6 +232,44 @@ public sealed class ManagedDecoderProviderTests(TestContext context)
Assert.Equal(DecodeTier.Unavailable, client.Probe(Write("y.webp", Fixtures.WebP()), ImageFileFormat.WebP).Tier);
}
+ ///
+ /// A provider that reads headers (a conversion's) finds a JPEG, PNG or TIFF native, at its oriented size, only when its
+ /// decoder takes what the headers describe; the others probe as before. Damage past the headers shows only when decoding.
+ ///
+ [Test]
+ public void HeaderProbesSayWhatTheDecodersTake()
+ {
+ var headers = new ManagedDecoderProvider { ReadsHeaders = true };
+ using var scene = SyntheticRaw.Smooth(W, H);
+ var jpeg = headers.Probe(Write("portrait.jpg", JpegFile(W, H, 6)), ImageFileFormat.Jpeg);
+ Assert.Equal((DecodeTier.Native, "managed-jpeg", H, W), (jpeg.Tier, jpeg.Decoder, jpeg.Width, jpeg.Height));
+ var png = headers.Probe(Write("a.png", PngEncoder.Encode(scene)), ImageFileFormat.Png);
+ Assert.Equal((DecodeTier.Native, "managed-png", W, H), (png.Tier, png.Decoder, png.Width, png.Height));
+ var tiff = headers.Probe(Write("a.tif", TiffWriter.Write(scene)), ImageFileFormat.Tiff);
+ Assert.Equal((DecodeTier.Native, "managed-tiff", W, H), (tiff.Tier, tiff.Decoder, tiff.Width, tiff.Height));
+ Assert.Equal(DecodeTier.Native, headers.Probe(Write("IMG_5.CR2", Cr2File("Canon EOS 5D Mark III")), ImageFileFormat.Cr2).Tier);
+ // Headers alone: what follows them is not read.
+ var header = headers.Probe(Write("header.jpg", Conversion.ColorFixtures.JpegHeader(4_000, 3_000)), ImageFileFormat.Jpeg);
+ Assert.Equal((DecodeTier.Native, 4_000, 3_000), (header.Tier, header.Width, header.Height));
+
+ // A CMYK TIFF, a JPEG cut before its first scan, a file that is not what its format says, an arithmetic-coded JPEG,
+ // and a JPEG, PNG or TIFF larger than its decoder takes: refused, like a decode.
+ var cmyk = Write("cmyk.tif", Conversion.ColorFixtures.Tiff(6, 4, 5, [0, 255, 255, 0]));
+ Assert.Throws(() => headers.Probe(cmyk, ImageFileFormat.Tiff));
+ Assert.Throws(() => headers.Probe(Write("cut.jpg", JpegFile(W, H, 1)[..200]), ImageFileFormat.Jpeg));
+ Assert.Throws(() => headers.Probe(Write("short.png", [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]), ImageFileFormat.Png));
+ Assert.Throws(() => headers.Probe(Write("arithmetic.jpg", Conversion.ColorFixtures.JpegHeader(6, 4, sof: 0xC9)), ImageFileFormat.Jpeg));
+ Assert.Throws(() => headers.Probe(Write("huge.jpg", Conversion.ColorFixtures.JpegHeader(16_384, 16_385)), ImageFileFormat.Jpeg));
+ Assert.Throws(() => headers.Probe(Write("huge.png", Conversion.ColorFixtures.PngHeader(16_384, 16_385)), ImageFileFormat.Png));
+ Assert.Throws(() => headers.Probe(Write("huge.tif", Conversion.ColorFixtures.TiffHeader(13_377, 13_378)), ImageFileFormat.Tiff));
+ // Through a chain: unavailable with the decoder's reason; a provider without header reads still finds it native.
+ var chain = new ImageDecoder([headers]).Probe(cmyk, ImageFileFormat.Tiff);
+ Assert.Equal(DecodeTier.Unavailable, chain.Tier);
+ Assert.Contains("photometric 5", chain.Reason);
+ Assert.Equal(DecodeTier.Native, new ManagedDecoderProvider().Probe(cmyk, ImageFileFormat.Tiff).Tier);
+ Assert.Throws(() => new ManagedDecoderProvider().Decode(new DecodeRequest(cmyk)));
+ }
+
[Test]
public void GoProRawDecodesThroughTheJpegBesideIt()
{
diff --git a/tests/Chronos.Core.Windows.Tests/Wic/WicDecoderProviderTests.cs b/tests/Chronos.Core.Windows.Tests/Wic/WicDecoderProviderTests.cs
index 76789d58..a3f5e928 100644
--- a/tests/Chronos.Core.Windows.Tests/Wic/WicDecoderProviderTests.cs
+++ b/tests/Chronos.Core.Windows.Tests/Wic/WicDecoderProviderTests.cs
@@ -39,6 +39,76 @@ public sealed class WicDecoderProviderTests(TestContext context)
Assert.SequenceEqual(["managed", "wic"], heif.Providers.Select(p => p.Name));
}
+ ///
+ /// An untagged CMYK TIFF, which the managed reader refuses rather than read as the gray of its first ink, is planned and
+ /// converted through WIC on the desktop (owner review 2, R7): its red stays red in the converted file.
+ ///
+ [Test]
+ public void ACmykTiffConvertsThroughWicInItsColours()
+ {
+ var path = Path.Combine(context.CreateTempDirectory(), "cmyk.tif");
+ File.WriteAllBytes(path, CmykTiff(6, 4, [0, 255, 255, 0]));
+ var desktop = Chronos.Core.Imaging.Decoders.DecoderFactory.CreateClient(Provider);
+ var jpeg = new Chronos.Core.Conversion.ConvertOptions(Chronos.Core.Conversion.ConvertTarget.Jpeg);
+ var probe = Chronos.Core.Conversion.FrameConverter.Decoders(desktop, jpeg, ImageFileFormat.Tiff).Probe(path, ImageFileFormat.Tiff);
+ Assert.Equal((DecodeTier.Native, "wic"), (probe.Tier, probe.Decoder));
+
+ var frame = new Chronos.Core.Archiving.SidecarFrame
+ {
+ Id = Guid.NewGuid(),
+ File = "originals/cmyk.tif",
+ Sha256 = new string('a', 64),
+ W = 6,
+ H = 4,
+ Format = ImageFileFormat.Tiff,
+ };
+ var target = Path.Combine(context.CreateTempDirectory(), "converted.jpg");
+ var file = Chronos.Core.Conversion.FrameConverter.Convert(new Chronos.Core.Conversion.ConvertSource(path, frame), jpeg, desktop, target,
+ cancellationToken: context.Cancellation);
+ using var converted = Assert.NotNull(Chronos.Core.Conversion.FrameConverter.Verifier.Decode(new DecodeRequest(file.Path, AllowPreview: false)));
+ Assert.Greater(converted.Pixels[0, 3, 2], 0.5);
+ Assert.Less(converted.Pixels[1, 3, 2], 0.1);
+ Assert.Less(converted.Pixels[2, 3, 2], 0.1);
+ }
+
+ /// An uncompressed little-endian TIFF of × CMYK pixels, each , without a profile.
+ private static byte[] CmykTiff(int width, int height, byte[] inks)
+ {
+ var strip = new byte[width * height * 4];
+ for (var i = 0; i < width * height; i++) inks.CopyTo(strip, i * 4);
+ const int ifd = 8;
+ (ushort Tag, ushort Type, uint Count, uint Value)[] entries =
+ [
+ (256, 4, 1, (uint)width),
+ (257, 4, 1, (uint)height),
+ (258, 3, 4, 0), // four 8-bit samples, stored after the directory
+ (259, 3, 1, 1),
+ (262, 3, 1, 5), // separated: CMYK
+ (273, 4, 1, 0), // the strip, after the samples' bits
+ (277, 3, 1, 4),
+ (278, 4, 1, (uint)height),
+ (279, 4, 1, (uint)strip.Length),
+ ];
+ var bits = ifd + 2 + entries.Length * 12 + 4;
+ var data = bits + 8;
+ using var stream = new MemoryStream();
+ using var writer = new BinaryWriter(stream);
+ writer.Write("II*\0"u8);
+ writer.Write((uint)ifd);
+ writer.Write((ushort)entries.Length);
+ foreach (var (tag, type, count, value) in entries)
+ {
+ writer.Write(tag);
+ writer.Write(type);
+ writer.Write(count);
+ writer.Write(tag switch { 258 => (uint)bits, 273 => (uint)data, _ => value });
+ }
+ writer.Write(0u);
+ for (var i = 0; i < 4; i++) writer.Write((ushort)8);
+ writer.Write(strip);
+ return stream.ToArray();
+ }
+
[Test]
public void PngMidGreyDecodesToLinearLight()
{