fix(convert): a conversion never changes what Chronos reads from a photo

Owner review 2, phase 4b, fix round 3 (codecs).

Chronos's decoders never read an ICC profile: they take code values through
the sRGB curve and float samples as they are. Round 2 wrote a float TIFF's
values with its profile's own curves, so after a Replace the renders,
thumbnails, analyses and deflicker of an Adobe RGB, ProPhoto or gamma-1.0
float TIFF saw other pixels (up to 2.7 EV darker, shadows lifted). Every
converted file now holds the values the decoders read from the original, as
they read them back, and says truthfully what they are:

- A JPEG or TIFF-16 keeps the codes of code values and carries the original's
  profile. Float values are written as sRGB codes with a profile made for
  them: the original's colorants and media white with the sRGB curve. A gray
  profile's codes are kept, its curve carried as an RGB profile (they were
  converted to sRGB before, which changed them too).
- A Linear DNG, whose matrices Chronos develops it with as well, takes only
  values that are sRGB's as the decoders read them; any other profile is
  refused as color_profile instead of converted to colours Chronos would then
  show differently.
- A profile with colorant tags that convert nothing is never sRGB by its name.
- A float file with NaN or infinite samples fails as damaged data instead of
  being written black (and spending a DNG's headroom).

The plan and the decoders behind it:

- TooLarge and WorstBytes answer any size, int.MaxValue sides included,
  without a product that wraps around; the worst case saturates.
- A frame another host flagged unreadable (the server, for a CMYK or CIELab
  TIFF its reader refuses) is asked again with a small decode where the
  conversion has other decoders (WIC on a PC), so a PC still converts it.
  The too_large sentences say "or Chronos reads back" (en, it).
- WIC's TIFF decoder decodes an unknown compression as zeros, does not undo
  the floating-point predictor of Photoshop's 32-bit files and reads 32-bit
  integers as floats, all without an error: such TIFFs are left to the
  managed reader, on the desktop as in conversions, never black or noise.
- A conversion's WIC probe reads the headers (decoder, first frame's oriented
  size, pixel format), so the size bound applies to what only WIC reads; a
  read error stays a read error (inaccessible), never a verdict.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-03 09:36:00 +01:00
1 parent 4dd3050334
commit b471a33bfc
16 files changed
+1341 -330

No files matched your search

@@ -1,6 +1,7 @@
using System.Buffers;
using System.Runtime.InteropServices;
using Chronos.Core.Imaging;
using Chronos.Core.Metadata.Tiff;
using Chronos.Core.Windows.Codecs;
using Chronos.Core.Windows.Interop;
@@ -25,6 +26,12 @@ namespace Chronos.Core.Windows.Wic;
/// (the managed decoder develops it at 16 bits, the Raw Image Extension at 8), and a request that prefers the embedded
/// preview is left to the managed decoder, which reads it without developing anything (R4, owner review 1).
/// </para>
/// <para>
/// WIC's TIFF decoder gives no error for what it does not decode: a compression it does not know (ZSTD, JPEG 2000, LERC…)
/// comes out as zeros, a predictor other than the horizontal one of integer samples (the floating-point predictor of
/// Photoshop's 32-bit files) is not undone, and 32-bit integer samples are taken for floats. Such a TIFF is left to the
/// other decoders (<see cref="TiffRefusal"/>; owner review 2, R7), never decoded black or as noise.
/// </para>
/// </remarks>
public sealed class WicDecoderProvider : IImageDecoderProvider
{
@@ -34,10 +41,17 @@ public sealed class WicDecoderProvider : IImageDecoderProvider
private const int WICBitmapPaletteTypeCustom = 0;
private const uint NumericFixedPoint = 4, NumericFloat = 5;
private const int BandBytes = 4 << 20;
private const uint IntegerSamples = 1, PredictorNone = 1, PredictorHorizontal = 2;
/// <summary>The photo-policy query covers every WIC container; the raw paths catch decoders without the policy.</summary>
private static readonly string[] OrientationQueries = ["System.Photo.Orientation", "/app1/ifd/{ushort=274}", "/ifd/{ushort=274}"];
/// <summary>
/// The TIFF compressions WIC's TIFF decoder decodes (checked on Windows 11 against files of each): none, CCITT RLE, T.4
/// and T.6, LZW, JPEG, Deflate (both codes) and PackBits.
/// </summary>
private static readonly HashSet<uint> TiffCompressions = [1, 2, 3, 4, 5, 7, 8, 32773, 32946];
private volatile CodecInventory _inventory;
/// <param name="inventory">Codec snapshot deciding <see cref="TierFor"/>; detected now when omitted.</param>
@@ -45,6 +59,16 @@ public sealed class WicDecoderProvider : IImageDecoderProvider
public string Name => "wic";
/// <summary>
/// Whether probes open each file and read what WIC makes of its headers (<see cref="Probe"/>): the decoder it picks, the
/// first frame's size, orientation and pixel format, and for a TIFF what <see cref="TiffRefusal"/> refuses. A
/// conversion's decoders do (<see cref="WithHeaderProbes"/>); otherwise a probe trusts the format and opens nothing.
/// </summary>
public bool ReadsHeaders { get; init; }
/// <summary>This provider with probes that read headers (<see cref="ReadsHeaders"/>), on the same codec snapshot.</summary>
public IImageDecoderProvider WithHeaderProbes() => ReadsHeaders ? this : new WicDecoderProvider(_inventory) { ReadsHeaders = true };
/// <summary>The codec snapshot in use; <see cref="Refresh"/> replaces it.</summary>
public CodecInventory Inventory => _inventory;
@@ -58,8 +82,9 @@ public sealed class WicDecoderProvider : IImageDecoderProvider
/// <summary>
/// Decodes the first frame. Returns null when WIC cannot natively decode this format here, when the request
/// forces the embedded preview, or when no installed decoder recognises the content. Corrupt files raise the
/// WIC <see cref="COMException"/>; unreadable files the matching <see cref="IOException"/>.
/// forces the embedded preview, when no installed decoder recognises the content, or for a TIFF WIC would not decode
/// as what it holds (<see cref="TiffRefusal"/>). Corrupt files raise the WIC <see cref="COMException"/>; unreadable
/// files the matching <see cref="IOException"/>.
/// </summary>
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default)
{
@@ -81,7 +106,12 @@ public sealed class WicDecoderProvider : IImageDecoderProvider
if (hr == WINCODEC_ERR_COMPONENTNOTFOUND) return null;
Marshal.ThrowExceptionForHR(hr);
if (format.IsRaw && DecoderClsid(decoder!) != WicGuids.RawDecoder) return null;
frame = decoder!.GetFrame(0);
if (decoder!.GetContainerFormat() == WicGuids.ContainerFormatTiff && TiffRefusal(request.Path) is { } refusal)
{
Chronos.Core.Diagnostics.Log.Debug("Decode", $"{Path.GetFileName(request.Path)}: {refusal}, which WIC does not decode; left to the other decoders");
return null;
}
frame = decoder.GetFrame(0);
frame.GetSize(out var width, out var height);
var orientation = request.ApplyOrientation ? ReadOrientation(frame) : 1;
@@ -115,6 +145,87 @@ public sealed class WicDecoderProvider : IImageDecoderProvider
}
}
/// <summary>
/// The tier for the file at <paramref name="path"/>, whose signature says <paramref name="format"/>. Without
/// <see cref="ReadsHeaders"/>, the format's (<see cref="TierFor"/>), nothing opened. With it, what WIC makes of the
/// file's headers, as <see cref="Decode"/> reaches them: native at the first frame's oriented size when the decoder it
/// picks is one this provider decodes with (the Raw Image Extension for a RAW), the TIFF is one it decodes
/// (<see cref="TiffRefusal"/>), and the frame's pixel format converts to what it requests; unavailable, with why,
/// whatever else WIC answers.
/// </summary>
/// <exception cref="IOException">The file cannot be read (missing, held by another program).</exception>
/// <exception cref="UnauthorizedAccessException">Access to the file is denied.</exception>
public FileTier Probe(string path, ImageFileFormat format)
{
ArgumentException.ThrowIfNullOrWhiteSpace(path);
if (TierFor(format) is not { } tier) return Unavailable(format, $"no decoder for {format}");
return ReadsHeaders ? ReadHeaders(path, format) : new FileTier(format, tier, Name, null, 0, 0);
}
private FileTier ReadHeaders(string path, ImageFileFormat format)
{
Win32Com.EnsureInitialized();
var factory = WicFactory.Instance;
// A read error is the file's, never a verdict on its content: it reaches the caller.
var stream = ShellStream.OpenRead(path);
IWICBitmapDecoder? decoder = null;
IWICBitmapFrameDecode? frame = null;
IWICFormatConverter? converter = null;
try
{
var hr = factory.CreateDecoderFromStream(stream, 0, WicDecodeOptions.MetadataCacheOnDemand, out decoder);
if (hr < 0) return Unavailable(format, $"no decoder takes the file (0x{hr:X8})");
if (ImageFormatInfo.Of(format).IsRaw && DecoderClsid(decoder!) != WicGuids.RawDecoder) return Unavailable(format, "only the Raw Image Extension develops a RAW here");
if (decoder!.GetContainerFormat() == WicGuids.ContainerFormatTiff && TiffRefusal(path) is { } refusal) return Unavailable(format, $"{refusal} is not decoded");
frame = decoder.GetFrame(0);
frame.GetSize(out var width, out var height);
var pixelFormat = frame.GetPixelFormat();
converter = factory.CreateFormatConverter();
var converts = IsFloatingPoint(factory, pixelFormat) && CanConvert(converter, pixelFormat, WicGuids.PixelFormat128bppRGBAFloat)
|| CanConvert(converter, pixelFormat, WicGuids.PixelFormat64bppRGBA);
if (!converts) return Unavailable(format, $"pixel format {pixelFormat} does not convert");
var swap = OrientationMap.SwapsAxes(ReadOrientation(frame));
int w = (int)Math.Min(width, int.MaxValue), h = (int)Math.Min(height, int.MaxValue);
return new FileTier(format, DecodeTier.Native, Name, null, swap ? h : w, swap ? w : h);
}
catch (Exception ex) when (ex is not (IOException or UnauthorizedAccessException or OutOfMemoryException))
{
// Whatever else WIC answers (an HRESULT its interop turns into one exception or another), it does not decode the file.
return Unavailable(format, ex.Message);
}
finally
{
Win32Com.Release(converter);
Win32Com.Release(frame);
Win32Com.Release(decoder);
Marshal.Release(stream);
}
}
private FileTier Unavailable(ImageFileFormat format, string why) => new(format, DecodeTier.Unavailable, null, $"{Name}: {why}", 0, 0);
/// <summary>
/// Why WIC's TIFF decoder would not decode the first image of the TIFF at <paramref name="path"/> (its first directory,
/// WIC's first frame) as what it holds, or null: a compression it does not know, which it decodes as zeros; a predictor
/// it does not undo (any but the horizontal one of integer samples); integer samples of more than 16 bits, which it
/// takes for floats. It says so with no error. What it does report (an unknown photometric interpretation, 64-bit
/// floats) it reports itself.
/// </summary>
/// <exception cref="IOException">The file cannot be read.</exception>
internal static string? TiffRefusal(string path)
{
using var file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, 4096, FileOptions.RandomAccess);
// A file this reader finds no directory in is WIC's to make sense of, or to fail on.
if (TiffReader.Open(file) is not { } reader || reader.ReadIfd(reader.FirstIfdOffset) is not { } ifd) return null;
var compression = reader.GetUInt(ifd, TiffTags.Compression) ?? 1;
if (!TiffCompressions.Contains(compression)) return $"TIFF compression {compression}";
var samples = reader.GetUInt(ifd, TiffTags.SampleFormat) ?? IntegerSamples;
var predictor = reader.GetUInt(ifd, TiffTags.Predictor) ?? PredictorNone;
if (predictor != PredictorNone && !(predictor == PredictorHorizontal && samples == IntegerSamples)) return $"TIFF predictor {predictor}";
var bits = reader.GetUInts(ifd, TiffTags.BitsPerSample) is { Length: > 0 } each ? each.Max() : 1;
return samples == IntegerSamples && bits > 16 ? $"TIFF {bits}-bit integer samples" : null;
}
/// <summary>CLSID of the decoder WIC chose for the stream, or <see cref="Guid.Empty"/> when it does not say.</summary>
private static Guid DecoderClsid(IWICBitmapDecoder decoder)
{
+3
View File
@@ -15,6 +15,9 @@ internal static class WicGuids
/// <summary>CLSID_WICWebpDecoder (WebP Image Extensions).</summary>
public static readonly Guid WebpDecoder = new("7693e886-51c9-4070-8419-9f70738ec8fa");
/// <summary>GUID_ContainerFormatTiff: the container of the in-box TIFF decoder.</summary>
public static readonly Guid ContainerFormatTiff = new("163bcc30-e2e9-4f0b-961d-a3e9fdb788a3");
public static readonly Guid VendorMicrosoft = new("f0e749ca-edef-4589-a73a-ee0e626a2a2b");
public static readonly Guid VendorMicrosoftBuiltIn = new("257a30fd-06b6-462b-aea4-63f70b86e533");
+99 -82
View File
@@ -12,13 +12,15 @@ namespace Chronos.Core.Codecs.Icc;
/// </summary>
public sealed class IccCurve
{
/// <summary>Samples of a function curve its inverse is found among (<see cref="Invert"/>): steps of 1/65536 in code value.</summary>
private const int InverseSamples = 65536;
/// <summary>
/// How far a curve may be from sRGB's for it to be sRGB's: what profile makers' rounding (1024-entry tables, s15Fixed16
/// parameters) leaves, far below a gamma 2.2 curve's difference (0.004 in linear value at code 0.2).
/// </summary>
internal const double SrgbTolerance = 2e-3;
private readonly double[]? _table;
private readonly double[] _parameters;
private readonly int _function;
private double[]? _forward;
private IccCurve(int function, double[] parameters, double[]? table)
{
@@ -27,66 +29,20 @@ public sealed class IccCurve
_table = table;
}
/// <summary>
/// Whether every linear value has one code value (<see cref="Invert"/>): the curve never falls and ends above where it
/// starts. A curve that falls somewhere maps two codes to one value, which no encoding undoes.
/// </summary>
public bool IsInvertible
/// <summary>Whether the curve is the sRGB EOTF (<see cref="Srgb"/>), within <see cref="SrgbTolerance"/> at 65 code values.</summary>
public bool IsSrgb
{
get
{
if (IsPower(out var gamma)) return gamma > 0;
var forward = Forward();
for (var i = 1; i < forward.Length; i++)
for (var i = 0; i <= 64; i++)
{
if (forward[i] < forward[i - 1]) return false;
var code = i / 64.0;
if (!(Math.Abs(Evaluate(code) - ColorSpace.SrgbToLinear(code)) <= SrgbTolerance)) return false;
}
return forward[^1] > forward[0];
return true;
}
}
/// <summary>
/// The code value 0..1 whose linear value is <paramref name="linear"/> (clamped to the curve's range): the inverse of
/// <see cref="Evaluate"/> for an invertible curve, exact for a power law and a table, within 1/65536 of code for a
/// parametric function.
/// </summary>
public double Invert(double linear)
{
if (IsPower(out var gamma)) return gamma > 0 ? Math.Pow(Math.Clamp(double.IsNaN(linear) ? 0 : linear, 0, 1), 1 / gamma) : 0;
var forward = Forward();
var y = double.IsNaN(linear) ? 0 : linear;
if (y <= forward[0]) return 0;
if (y >= forward[^1]) return 1;
// The last sample at or below y, then the step to the next one interpolated as Evaluate interpolates.
int lo = 0, hi = forward.Length - 1;
while (hi - lo > 1)
{
var mid = (lo + hi) >>> 1;
if (forward[mid] <= y) lo = mid;
else hi = mid;
}
var step = forward[hi] - forward[lo];
var t = step > 0 ? (y - forward[lo]) / step : 0;
return (lo + t) / (forward.Length - 1);
}
/// <summary>Whether the curve is a pure power law (a curveType gamma, or parametric function 0), and its exponent.</summary>
private bool IsPower(out double gamma)
{
gamma = _function == 0 && _table is null ? _parameters[0] : 0;
return _function == 0 && _table is null;
}
/// <summary>The curve's linear values at evenly spaced code values: its own table, or the function sampled.</summary>
private double[] Forward()
{
if (_table is { } table) return table;
if (_forward is { } cached) return cached;
var forward = new double[InverseSamples + 1];
for (var i = 0; i < forward.Length; i++) forward[i] = Evaluate(i / (double)InverseSamples);
return _forward = forward;
}
/// <summary>A pure power law.</summary>
public static IccCurve Gamma(double gamma) => new(0, [gamma], null);
@@ -163,9 +119,10 @@ public sealed class IccCurve
/// <summary>
/// An ICC colour profile (ICC.1:2010) as a photo embeds it, kept byte for byte so a converted file can carry it, with what a
/// conversion needs to read from it: its colour space, its description, and for a matrix/TRC profile its colorants (RGB →
/// PCS XYZ, D50) and tone curves. Profiles Chronos writes itself (<see cref="AdobeRgb"/>, <see cref="DisplayP3"/>) are
/// version 2.1 display profiles with a matrix and curveType TRCs, read by every colour-managed program.
/// conversion needs to read from it: its colour space, its description, its media white, and for a matrix/TRC profile its
/// colorants (RGB → PCS XYZ, D50) and tone curves. Profiles Chronos writes itself (<see cref="AdobeRgb"/>,
/// <see cref="DisplayP3"/>, <see cref="WithSrgbCurve"/>, <see cref="GrayAsRgb"/>) are version 2.1 display profiles with a
/// matrix and curveType TRCs, read by every colour-managed program.
/// </summary>
public sealed class IccProfile
{
@@ -179,24 +136,32 @@ public sealed class IccProfile
/// <summary>The ICC D50 profile connection white (PCS illuminant).</summary>
private static readonly Vector3D PcsWhite = new(0.9642, 1.0, 0.8249);
/// <summary>Longest description a profile Chronos writes holds.</summary>
private const int MaxDescription = 200;
/// <summary>
/// How far a matrix/TRC profile's matrix to linear Rec.709 and its curves may be from sRGB's for it to be sRGB: what
/// profile makers' rounding (s15Fixed16 colorants, other chromatic adaptations, 1024-entry tables) leaves, far below a
/// gamma 2.2 curve's difference (0.004 in linear value at code 0.2).
/// How far a matrix/TRC profile's matrix to linear Rec.709 may be from the identity for its primaries to be sRGB's: what
/// profile makers' rounding (s15Fixed16 colorants, other chromatic adaptations) leaves.
/// </summary>
private const double SrgbTolerance = 2e-3;
private const double SrgbTolerance = IccCurve.SrgbTolerance;
private readonly Matrix3? _toLinearRec709;
private IccProfile(byte[] data, uint colorSpace, string? description, Matrix3? colorants, Matrix3? toLinearRec709, IReadOnlyList<IccCurve>? curves)
private IccProfile(byte[] data, uint colorSpace, string? description, Vector3D? mediaWhite, bool colorantTags, Matrix3? colorants, Matrix3? toLinearRec709,
IReadOnlyList<IccCurve>? curves)
{
Data = data;
ColorSpace = colorSpace;
Description = description;
MediaWhite = mediaWhite;
Colorants = colorants;
_toLinearRec709 = toLinearRec709;
Curves = curves;
IsSrgb = IsRgb && (IsMatrixShaper ? DescribesSrgb(toLinearRec709!.Value, curves!) : description?.Contains("sRGB", StringComparison.Ordinal) == true);
// Only a profile without colorants at all (lookup tables) is sRGB by its name: one whose colorants were dropped as
// converting nothing, or whose curves are missing, does not say what its values are.
IsSrgb = IsRgb && (IsMatrixShaper
? HasSrgbPrimaries(toLinearRec709!.Value) && curves!.All(c => c.IsSrgb)
: !colorantTags && description?.Contains("sRGB", StringComparison.Ordinal) == true);
}
/// <summary>The profile's bytes, as embedded.</summary>
@@ -212,13 +177,32 @@ public sealed class IccProfile
/// <summary>The profile's description (its 'desc' tag), when it has one.</summary>
public string? Description { get; }
/// <summary>The media white point (its 'wtpt' tag, PCS XYZ), when it has one.</summary>
public Vector3D? MediaWhite { get; }
/// <summary>
/// Whether the profile describes sRGB: a matrix/TRC profile whose matrix to linear Rec.709 and curves are sRGB's (the
/// numbers decide, never the name: "sRGB-elle-V4-g10" has a linear curve); a profile without them (lookup tables) whose
/// description names it ("sRGB IEC61966-2.1", "sRGB v4 ICC preference"…).
/// numbers decide, never the name: "sRGB-elle-V4-g10" has a linear curve); a profile without colorant tags at all
/// (lookup tables) whose description names it ("sRGB IEC61966-2.1", "sRGB v4 ICC preference"…). A profile with colorant
/// tags that convert nothing (<see cref="Colorants"/> null), or without its curves, is not.
/// </summary>
public bool IsSrgb { get; }
/// <summary>
/// Whether values of the kind given in this profile are what Chronos's decoders take every value for (owner review 2,
/// R7): they never read a profile, and take code values through the sRGB curve and linear values as they are, both in
/// sRGB's primaries. <paramref name="linear"/> values (<see cref="SourceColor.IsLinear"/>) need sRGB's primaries only
/// (a matrix/TRC profile whose matrix to linear Rec.709 is the identity, whatever its curves; one <see cref="IsSrgb"/>);
/// code values an sRGB profile (<see cref="IsSrgb"/>). A gray profile has no primaries: its linear values are linear
/// gray, its code values need the sRGB curve. Any other space: no.
/// </summary>
public bool IsSrgbFor(bool linear)
{
if (IsGray) return linear || (IsGrayCurve && Curves![0].IsSrgb);
if (!IsRgb) return false;
return linear && IsMatrixShaper ? HasSrgbPrimaries(_toLinearRec709!.Value) : IsSrgb;
}
/// <summary>
/// RGB → PCS XYZ (D50) of a matrix/TRC profile: rXYZ, gXYZ and bXYZ as columns; null otherwise, and null for colorants
/// that convert nothing (<see cref="ToLinearRec709"/> has no meaning for them: a white without positive cone responses,
@@ -266,19 +250,37 @@ public sealed class IccProfile
return inverted.All(double.IsFinite) ? m : null;
}
/// <summary>Whether a matrix to linear Rec.709 and three curves are sRGB's, within <see cref="SrgbTolerance"/>.</summary>
private static bool DescribesSrgb(Matrix3 toLinearRec709, IReadOnlyList<IccCurve> curves)
/// <summary>Whether a matrix to linear Rec.709 is the identity within <see cref="SrgbTolerance"/>: the colorants are sRGB's primaries.</summary>
private static bool HasSrgbPrimaries(Matrix3 toLinearRec709)
{
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;
return offIdentity.All(d => Math.Abs(d) <= SrgbTolerance);
}
/// <summary>
/// This matrix/TRC profile with the sRGB curve on its three channels (owner review 2, R7): its colorants and media white
/// as they are, so a file whose codes are the sRGB encoding of linear values in this profile's primaries (a float TIFF's,
/// written as Chronos's decoders read them back) says so to every colour-managed program.
/// </summary>
/// <exception cref="InvalidOperationException">The profile is not a matrix/TRC one.</exception>
public IccProfile WithSrgbCurve()
{
if (!IsMatrixShaper) throw new InvalidOperationException("Only a matrix/TRC profile has colorants to keep.");
return Build($"{Description ?? "RGB"} (sRGB curve)", MediaWhite ?? PcsWhite, Colorants!.Value, IccCurve.Srgb);
}
/// <summary>
/// This gray profile's curve as an RGB profile (owner review 2, R7): the curve on the three channels, sRGB's colorants
/// (adapted to the PCS white, so an equal R, G and B is neutral, as the gray profile's values are), its media white; a
/// gray photo converted to RGB keeps its code values and says what they are.
/// </summary>
/// <exception cref="InvalidOperationException">The profile is not a gray one with its curve.</exception>
public IccProfile GrayAsRgb()
{
if (!IsGrayCurve) throw new InvalidOperationException("Only a gray profile with its curve converts to an RGB one.");
var colorants = Matrix3.BradfordAdaptation(Chromaticity.D65.ToXyz(), PcsWhite) * Matrix3.Rec709ToXyz;
return Build($"{Description ?? "Gray"} (RGB)", MediaWhite ?? PcsWhite, colorants, Curves![0]);
}
/// <summary>The profile in <paramref name="data"/>, or null when it is not an ICC profile (no 'acsp' signature, a size
@@ -309,6 +311,8 @@ public sealed class IccProfile
var colorSpace = BinaryPrimitives.ReadUInt32BigEndian(span[16..]);
var pcs = BinaryPrimitives.ReadUInt32BigEndian(span[20..]);
var description = Text(TagData(Tag("desc"), span));
var mediaWhite = XyzValue(TagData(Tag("wtpt"), span));
var colorantTags = RgbColorants.Any(tags.ContainsKey);
Matrix3? colorants = null;
Matrix3? toLinearRec709 = null;
IccCurve[]? curves = null;
@@ -338,18 +342,31 @@ public sealed class IccProfile
{
curves = [gray];
}
return new IccProfile(data, colorSpace, description, colorants, toLinearRec709, curves);
return new IccProfile(data, colorSpace, description, mediaWhite, colorantTags, colorants, toLinearRec709, curves);
}
/// <summary>
/// A version 2.1 RGB display profile (ICC.1:2001-04): its colorants from the primaries and white, adapted to the D50 PCS
/// with Bradford; its media white; the same tone curve for the three channels.
/// A version 2.1 RGB display profile (ICC.1:2001-04) of primaries with the D65 white: its colorants from them, adapted to
/// the D50 PCS with Bradford; its media white; the same tone curve for the three channels.
/// </summary>
internal static IccProfile Synthesize(string description, Chromaticity red, Chromaticity green, Chromaticity blue, IccCurve curve)
public static IccProfile Synthesize(string description, Chromaticity red, Chromaticity green, Chromaticity blue, IccCurve curve)
{
ArgumentNullException.ThrowIfNull(description);
ArgumentNullException.ThrowIfNull(curve);
var white = Chromaticity.D65;
var colorants = Matrix3.BradfordAdaptation(white.ToXyz(), PcsWhite) * Matrix3.RgbToXyz(red, green, blue, white);
var text = Encoding.ASCII.GetBytes(description + "\0");
return Build(description, white.ToXyz(), colorants, curve);
}
/// <summary>
/// A version 2.1 RGB display profile: <paramref name="colorants"/> (RGB → PCS XYZ, columns) as its rXYZ, gXYZ and bXYZ,
/// <paramref name="mediaWhite"/>, and <paramref name="curve"/> for the three channels.
/// </summary>
private static IccProfile Build(string description, Vector3D mediaWhite, Matrix3 colorants, IccCurve curve)
{
// ASCII text, of a length any reader shows: another alphabet's letters read as '?', a damaged profile's endless
// description is cut.
var text = Encoding.ASCII.GetBytes(description[..Math.Min(description.Length, MaxDescription)] + "\0");
var desc = new byte[12 + text.Length + 4 + 4 + 2 + 1 + 67];
"desc"u8.CopyTo(desc);
BinaryPrimitives.WriteUInt32BigEndian(desc.AsSpan(8), (uint)text.Length);
@@ -360,7 +377,7 @@ public sealed class IccProfile
[
(Tag("desc"), desc),
(Tag("cprt"), cprt),
(Tag("wtpt"), XyzTag(white.ToXyz())),
(Tag("wtpt"), XyzTag(mediaWhite)),
(Tag("rXYZ"), XyzTag(new Vector3D(colorants.M11, colorants.M21, colorants.M31))),
(Tag("gXYZ"), XyzTag(new Vector3D(colorants.M12, colorants.M22, colorants.M32))),
(Tag("bXYZ"), XyzTag(new Vector3D(colorants.M13, colorants.M23, colorants.M33))),
+66 -22
View File
@@ -7,6 +7,7 @@ using Chronos.Core.Codecs.Jpeg;
using Chronos.Core.Codecs.Tiff;
using Chronos.Core.Export;
using Chronos.Core.Imaging;
using Chronos.Core.Imaging.Decoders;
using Chronos.Core.Model;
namespace Chronos.Core.Conversion;
@@ -27,7 +28,10 @@ public enum ConvertSkip
/// <summary>This host reads the photo only from its embedded preview (a camera body its RAW decoder does not support).</summary>
PreviewOnly,
/// <summary>The photo is flagged unreadable: no decoder produced its pixels.</summary>
/// <summary>
/// The photo is flagged unreadable, damaged or corrupt: no decoder produced its pixels. A flag set where other decoders
/// read the file (the server's, for a CMYK TIFF that WIC reads on a PC) holds only where a decode says so too.
/// </summary>
Undecodable,
/// <summary>The photo's file is not there.</summary>
@@ -43,8 +47,8 @@ public enum ConvertSkip
ColorProfile,
/// <summary>
/// The photo is larger than the target holds (a JPEG side above 65535) or than the server's decoders read back, which
/// every converted file must (<see cref="ConvertPlanner.TooLarge"/>).
/// The photo is larger than the target holds (a JPEG side above 65535) or than Chronos reads back: the server's decoders,
/// which every converted file must decode with (<see cref="ConvertPlanner.TooLarge"/>).
/// </summary>
TooLarge,
@@ -164,19 +168,24 @@ public sealed record ConvertPlan(Guid ArchiveId, ConvertOptions Options, Convert
/// <summary>
/// Plans the conversion of a sequence's frames (owner review 2, R7): a frame is converted when it is not in the target's
/// format already, its file is there, it was hashed, it is not flagged unreadable, its size is one the target holds and the
/// server's decoders read back (<see cref="TooLarge"/>), this host decodes it at full resolution with the decoders the
/// conversion uses (<see cref="FrameConverter.Decoders"/>: its format has a native decoder here and the file itself probes
/// native now, whatever tier another host or an earlier pass recorded, its headers saying what those decoders take; for a
/// 16-bit target, not through a RAW development at 8 bits), and its colour profile can be kept
/// (<see cref="FrameConverter.Treatment"/>). The plan asks the files what the conversion will ask them, reading headers
/// only, several at once. Sizes after are estimated from the pixels (<see cref="EstimateBytes"/>).
/// format already, its file is there, it was hashed, it is not flagged unreadable (or another host's decoders flagged it and
/// this host's decode it), its size is one the target holds and the server's decoders read back (<see cref="TooLarge"/>),
/// this host decodes it at full resolution with the decoders the conversion uses (<see cref="FrameConverter.Decoders"/>: its
/// format has a native decoder here and the file itself probes native now, whatever tier another host or an earlier pass
/// recorded, its headers saying what those decoders take; for a 16-bit target, not through a RAW development at 8 bits),
/// and its colour profile can be kept (<see cref="FrameConverter.Treatment"/>). The plan asks the files what the conversion
/// will ask them, reading headers only (and decoding small the few frames flagged elsewhere), several at once; a file it
/// cannot read now is inaccessible, never a verdict on its content. Sizes after are estimated from the pixels
/// (<see cref="EstimateBytes"/>).
/// </summary>
public static class ConvertPlanner
{
/// <summary>Files asked at once while a sequence is planned: header reads, bound by the disk more than the cores.</summary>
private const int ProbesAtOnce = 8;
/// <summary>Long edge of the decode that asks a frame flagged unreadable elsewhere whether this host's decoders read it.</summary>
private const int ConfirmEdge = 64;
/// <summary>Margin over the estimate the free space must hold (estimates of compressed files vary with the content).</summary>
public const double SpaceMargin = 1.15;
@@ -252,9 +261,13 @@ public static class ConvertPlanner
if (frame.Format == options.Format) return ConvertSkip.SameFormat;
if ((frame.Flags & FrameFlags.Missing) != 0 || !present.Contains(frame.File)) return ConvertSkip.Missing;
if (frame.Sha256 is null) return ConvertSkip.Unhashed;
if ((frame.Flags & (FrameFlags.Undecodable | FrameFlags.Corrupt)) != 0) return ConvertSkip.Undecodable;
if (TooLarge(options, frame.W, frame.H)) return ConvertSkip.TooLarge;
if ((frame.Flags & FrameFlags.Corrupt) != 0) return ConvertSkip.Undecodable;
var decoders = FrameConverter.Decoders(decoder, options, frame.Format);
// Flagged unreadable by the host that analysed it last: on a server, whose managed decoders refuse what they do not
// read (a CMYK or CIELab TIFF), that holds for a PC only where its own decoders (WIC) do not read it either.
var flagged = (frame.Flags & FrameFlags.Undecodable) != 0;
if (flagged && !OthersRead(decoders, frame.Format)) return ConvertSkip.Undecodable;
if (TooLarge(options, frame.W, frame.H)) return ConvertSkip.TooLarge;
var eightBit = FrameConverter.OnlyAt8Bits(decoder, options, frame.Format);
if (decoders.TierFor(frame.Format) != DecodeTier.Native) return eightBit ? ConvertSkip.EightBit : ConvertSkip.NoNativeDecoder;
// Whatever tier was recorded (unknown before the light analysis, or by another host with other decoders, or for a
@@ -267,8 +280,16 @@ public static class ConvertPlanner
// 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 eightBit ? ConvertSkip.EightBit : probe.Tier == DecodeTier.EmbeddedPreview ? ConvertSkip.PreviewOnly : ConvertSkip.NoNativeDecoder;
{
return flagged ? ConvertSkip.Undecodable
: eightBit ? ConvertSkip.EightBit
: probe.Tier == DecodeTier.EmbeddedPreview ? ConvertSkip.PreviewOnly
: ConvertSkip.NoNativeDecoder;
}
if (TooLarge(options, probe.Width, probe.Height)) return ConvertSkip.TooLarge;
// The flag was another host's verdict, whose decoders failed where the headers say nothing: a decode here says
// whether these read its pixels.
if (flagged && !Decodes(decoders, path)) return ConvertSkip.Undecodable;
return FrameConverter.Treatment(SourceColor.Read(path, frame.Format), options.Target) == ColorTreatment.Refuse ? ConvertSkip.ColorProfile : null;
}
catch (Exception ex) when (ex is FileNotFoundException or DirectoryNotFoundException)
@@ -283,39 +304,62 @@ public static class ConvertPlanner
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
// There, but held by another program or denied: not missing, and not planned (a conversion job converts only the
// photos its plan listed), so the dry run says why.
// There, but held by another program or denied, or a read failed: not missing, and not planned (a conversion job
// converts only the photos its plan listed), so the dry run says why. Nothing is concluded of its content.
return ConvertSkip.Inaccessible;
}
}
/// <summary>
/// Whether <paramref name="decoders"/> have, for <paramref name="format"/>, a native decoder besides the managed ones
/// every host has (WIC on a PC): a frame those flagged unreadable may still decode here.
/// </summary>
private static bool OthersRead(ImageDecoder decoders, ImageFileFormat format) =>
decoders.Providers.Any(p => p is not ManagedDecoderProvider && p.TierFor(format) == DecodeTier.Native);
/// <summary>
/// Whether <paramref name="decoders"/> produce the pixels of the file at <paramref name="path"/> at full-resolution tier,
/// asked with a small decode.
/// </summary>
/// <exception cref="IOException">The file cannot be read.</exception>
private static bool Decodes(ImageDecoder decoders, string path)
{
using var decoded = decoders.Decode(new DecodeRequest(path, ConfirmEdge, DecodePurpose.Thumbnail, ApplyOrientation: false, AllowPreview: false));
return decoded is { Tier: DecodeTier.Native, ByChoice: false };
}
/// <summary>
/// Whether a frame of <paramref name="width"/> × <paramref name="height"/> (unknown: 0) cannot be converted to
/// <paramref name="options"/>, a bound and never an estimate: the target does not hold it (a JPEG side above
/// <see cref="FrameConverter.MaxJpegSide"/>), the server's decoders, which every converted file must decode with
/// (<see cref="FrameConverter.Verify"/>), do not read it back (a JPEG of more pixels than
/// <see cref="FrameConverter.MaxJpegSide"/>), Chronos does not read it back (the server's decoders, which every converted
/// file must decode with, <see cref="FrameConverter.Verify"/>: a JPEG of more pixels than
/// <see cref="JpegDecodeOptions.MaxPixels"/>, a TIFF-16 or DNG of more samples than <see cref="TiffDecodeOptions.MaxSamples"/>),
/// or its file could outgrow the array it is made in (<see cref="WorstBytes"/>, which those sizes keep below it). What it
/// lets through fits one image buffer, its file the array it is made in, and the server's decoders read it back.
/// lets through fits one image buffer, its file the array it is made in, and the server's decoders read it back. Any
/// size is answered, a damaged header's included: nothing is multiplied beyond what a long holds.
/// </summary>
public static bool TooLarge(ConvertOptions options, int width, int height)
{
ArgumentNullException.ThrowIfNull(options);
if (width <= 0 || height <= 0) return false;
// At most (2^31 - 1)^2, below 2^62.
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;
: pixels <= TiffDecodeOptions.Default.MaxSamples / 3;
return !readBack || WorstBytes(width, height) > Array.MaxLength;
}
/// <summary>
/// The largest a converted file of <paramref name="width"/> × <paramref name="height"/> pixels can be when its pixels do
/// not compress at all, in any target: <see cref="WorstBitsPerPixel"/> with its margin, and the most a file holds beyond
/// its pixels.
/// its pixels; <see cref="long.MaxValue"/> for sizes beyond it.
/// </summary>
public static long WorstBytes(int width, int height) =>
(long)Math.Ceiling((double)Math.Max(0, width) * Math.Max(0, height) * WorstBitsPerPixel / 8 * WorstMargin) + WorstOverhead;
Saturated(Math.Ceiling((double)Math.Max(0, width) * Math.Max(0, height) * WorstBitsPerPixel / 8 * WorstMargin) + WorstOverhead);
/// <summary><paramref name="bytes"/> (whole, not negative) as a long, <see cref="long.MaxValue"/> when beyond it.</summary>
private static long Saturated(double bytes) => bytes >= long.MaxValue ? long.MaxValue : (long)bytes;
/// <summary>
/// Estimated size of a converted file of <paramref name="width"/> × <paramref name="height"/> pixels (unknown:
@@ -332,7 +376,7 @@ public static class ConvertPlanner
ConvertTarget.Tiff16 => 30,
_ => 26,
};
return (long)Math.Ceiling(pixels * bits / 8);
return Saturated(Math.Ceiling(pixels * bits / 8));
}
/// <summary>The free space a conversion whose files are estimated at <paramref name="estimated"/> bytes needs.</summary>
+82 -90
View File
@@ -1,3 +1,5 @@
using System.Numerics;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using Chronos.Core.Archiving;
using Chronos.Core.Codecs.Dng;
@@ -8,7 +10,6 @@ using Chronos.Core.Imaging;
using Chronos.Core.Imaging.Decoders;
using Chronos.Core.IO;
using Chronos.Core.Localization;
using Chronos.Core.Mathematics;
using Chronos.Core.Metadata;
using Chronos.Core.Model;
@@ -47,6 +48,9 @@ public enum ConversionFailure
/// <summary>The original no longer matches the SHA-256 it was archived with.</summary>
Changed,
/// <summary>The photo's pixels hold values that are not numbers (NaN, or infinite): its data is damaged.</summary>
Damaged,
}
/// <summary>A photo could not be converted, or its converted file did not verify; nothing of it is kept. The message is the
@@ -64,15 +68,17 @@ public sealed class ConversionException(ConversionFailure reason, string message
/// writers (<see cref="JpegEncoder"/> 4:4:4, <see cref="TiffWriter"/> 16-bit with Deflate and the predictor,
/// <see cref="LinearDngWriter"/> scene-referred: highlights above white and colours outside sRGB kept), with an Exif block
/// (<see cref="ExifWriter"/>) whose capture time, offset and exposure come from the sidecar (to the tick) and whose camera,
/// lens, modes and GPS come from the original's metadata, the sequence's camera and lens filling what it lacks. The colour
/// space the original's values are in (<see cref="SourceColor"/>) is kept: a JPEG or TIFF carries the original's ICC profile
/// (code values pass through unchanged; a float TIFF's linear values are encoded with the profile's curves), a DNG gets
/// them converted through the profile's matrix (and its curves, for code values), a gray profile is converted to sRGB; a
/// profile none of that applies to refuses the photo (<see cref="Treatment"/>). A photo larger than the plan allows is
/// refused at the size decoded (<see cref="ConvertPlanner.TooLarge"/>). The bytes are hashed as they are made and written
/// atomically; then the file is read back (its SHA-256 must match), its metadata read again (the same capture time and
/// offset) and it is decoded at full-resolution tier by the server's decoders, so every host can read it. MakerNote and XMP
/// are not carried; the orientation is 1, the pixels being upright.
/// lens, modes and GPS come from the original's metadata, the sequence's camera and lens filling what it lacks. What
/// Chronos reads from the photo never changes: the converted file decodes, with Chronos's own decoders, to the values the
/// original decoded to, within what the target holds (a JPEG or TIFF-16 holds 0..1 at 8 or 16 bits, a DNG 16-bit linear
/// values up to 4 stops above white), and says truthfully what they are (<see cref="Treatment"/>, from the colour space
/// <see cref="SourceColor"/> reads): a JPEG or TIFF carries the original's ICC profile when it keeps the original's codes,
/// or a profile made for the codes it holds; a DNG, whose matrices Chronos develops it with too, takes only values that are
/// sRGB's as the decoders read them; any other photo is refused, as is one whose pixels are not numbers (damaged data). A
/// photo larger than the plan allows is refused at the size decoded (<see cref="ConvertPlanner.TooLarge"/>). The bytes are
/// hashed as they are made and written atomically; then the file is read back (its SHA-256 must match), its metadata read
/// again (the same capture time and offset) and it is decoded at full-resolution tier by the server's decoders, so every
/// host can read it. MakerNote and XMP are not carried; the orientation is 1, the pixels being upright.
/// </summary>
public static class FrameConverter
{
@@ -90,9 +96,6 @@ public static class FrameConverter
private static readonly Lazy<ImageDecoder> ServerDecoders = new(DecoderFactory.CreateServer);
/// <summary>The managed provider as a conversion uses it: its probes read the headers of JPEG, PNG and TIFF files.</summary>
private static readonly ManagedDecoderProvider HeaderReading = new() { ReadsHeaders = true };
/// <summary>The decoders a converted file must decode with at full-resolution tier: the server's (managed only).</summary>
public static ImageDecoder Verifier => ServerDecoders.Value;
@@ -101,9 +104,9 @@ public static class FrameConverter
/// <paramref name="decoder"/>: the managed provider first, so every format it reads natively is decoded in-house (a RAW
/// developed at float precision, as the server does), the others (WIC) after it, for what it cannot read; for a 16-bit
/// target, a RAW never through a provider that develops RAWs at 8 bits (<see cref="IImageDecoderProvider.DevelopsRawAt8Bits"/>).
/// The managed provider's probes read the headers of JPEG, PNG and TIFF files (<see cref="ManagedDecoderProvider.ReadsHeaders"/>),
/// so what its decoders refuse (a CMYK or palette TIFF, a size above their limits) is asked of the others, or is not
/// planned where there are none.
/// Every provider's probes read each file's headers (<see cref="IImageDecoderProvider.WithHeaderProbes"/>: the managed
/// ones of JPEG, PNG and TIFF files, WIC's of any), so what a provider's decoder refuses (a CMYK or palette TIFF, a
/// compression or size it does not take) is asked of the others, or is not planned where none takes it.
/// </summary>
public static ImageDecoder Decoders(ImageDecoder decoder, ConvertOptions options, ImageFileFormat format)
{
@@ -111,7 +114,7 @@ public static class FrameConverter
ArgumentNullException.ThrowIfNull(options);
var eightBit = options.IsSixteenBit && ImageFormatInfo.Of(format).IsRaw;
var providers = decoder.Providers.Where(p => !(eightBit && p.DevelopsRawAt8Bits))
.Select(p => p is ManagedDecoderProvider { ReadsHeaders: false } ? HeaderReading : p)
.Select(p => p.WithHeaderProbes())
.OrderBy(p => p is ManagedDecoderProvider ? 0 : 1).ToList();
return providers.SequenceEqual(decoder.Providers) ? decoder : new ImageDecoder(providers);
}
@@ -129,32 +132,43 @@ public static class FrameConverter
}
/// <summary>
/// What a conversion to <paramref name="target"/> does with a photo whose values are in <paramref name="color"/>.
/// Code values (the decoders linearise them with the sRGB curve) pass to a JPEG or TIFF unchanged with their profile
/// (<see cref="ColorTreatment.Carry"/>). Linear values (a float TIFF's, <see cref="SourceColor.IsLinear"/>) are in the
/// profile's primaries: a JPEG or TIFF gets them encoded with the profile's own curves (<see cref="ColorTreatment.Encode"/>),
/// or with sRGB's when the profile is sRGB. A DNG gets both converted through any matrix/TRC profile that is not sRGB,
/// its curves applied to code values only (<see cref="ColorTreatment.Convert"/>), and takes a lookup-table profile only
/// when it names sRGB. A gray profile's code values convert to sRGB; its linear values are linear gray already.
/// What a conversion to <paramref name="target"/> does with a photo whose values are in <paramref name="color"/>, so that
/// the converted file decodes, with Chronos's decoders, to the values the original decoded to, and says truthfully what
/// they are. The decoders never read a profile: they take code values through the sRGB curve and linear values (a float
/// TIFF's, <see cref="SourceColor.IsLinear"/>) as they are, and every converted file is written as they read it back. A
/// JPEG or TIFF keeps the codes of code values in an RGB profile and carries that profile (<see cref="ColorTreatment.Carry"/>);
/// it holds linear values as sRGB codes, with the profile's colorants and the sRGB curve (<see cref="ColorTreatment.Describe"/>),
/// or the profile itself when it names sRGB without colorants; it keeps a gray profile's codes with its curve as an RGB
/// profile (<see cref="ColorTreatment.Describe"/>), and takes its linear values as linear gray. A DNG says what its values
/// are through its matrices, which Chronos develops it with too, so it takes only values that are sRGB's as the decoders
/// read them (<see cref="IccProfile.IsSrgbFor"/>): what it would convert to another space would read back changed. A
/// space nothing here describes, CMYK, Lab, or values a profile does not say enough of, is refused.
/// </summary>
public static ColorTreatment Treatment(SourceColor color, ConvertTarget target)
{
ArgumentNullException.ThrowIfNull(color);
if (color.IsUnknown) return ColorTreatment.Refuse;
if (color.Profile is not { } profile) return ColorTreatment.None;
if (profile.IsGray) return color.IsLinear ? ColorTreatment.None : profile.IsGrayCurve ? ColorTreatment.Convert : ColorTreatment.Refuse;
if (target == ConvertTarget.LinearDng) return profile.IsSrgbFor(color.IsLinear) ? ColorTreatment.None : ColorTreatment.Refuse;
if (profile.IsGray) return color.IsLinear ? ColorTreatment.None : profile.IsGrayCurve ? ColorTreatment.Describe : ColorTreatment.Refuse;
if (!profile.IsRgb) return 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;
return profile.IsMatrixShaper ? ColorTreatment.Describe : profile.IsSrgb ? ColorTreatment.Carry : ColorTreatment.Refuse;
}
/// <summary>
/// The profile a JPEG or TIFF carries for <see cref="ColorTreatment.Describe"/>: a gray profile's curve as an RGB profile
/// (<see cref="IccProfile.GrayAsRgb"/>), else a matrix/TRC profile's colorants with the sRGB curve the codes are in
/// (<see cref="IccProfile.WithSrgbCurve"/>).
/// </summary>
internal static IccProfile Described(IccProfile profile) => profile.IsGray ? profile.GrayAsRgb() : profile.WithSrgbCurve();
/// <summary>Converts <paramref name="source"/> into <paramref name="target"/> (created or replaced atomically) and verifies it.</summary>
/// <param name="decoder">This host's decoders (WIC first on a desktop); the conversion orders them itself (<see cref="Decoders"/>).</param>
/// <param name="encoderThreads">Worker limit of the encoder (-1: all cores); 1 when several photos convert at once.</param>
/// <exception cref="ConversionException">The photo does not decode at full resolution here, at 16 bits for a 16-bit target,
/// its colour profile cannot be kept, it is too large, or its converted file did not verify (it is deleted).</exception>
/// its colour profile cannot be kept, it is too large, its pixels are not numbers, or its converted file did not verify
/// (it is deleted).</exception>
/// <exception cref="IOException">The original cannot be read, or the target cannot be written.</exception>
public static ConvertedFile Convert(ConvertSource source, ConvertOptions options, ImageDecoder decoder, string target, int encoderThreads = -1,
CancellationToken cancellationToken = default)
@@ -170,6 +184,13 @@ public static class FrameConverter
var treatment = Treatment(color, options.Target);
if (treatment == ColorTreatment.Refuse)
throw new ConversionException(ConversionFailure.ColorProfile, $"{name}: its colour profile ({color.Profile?.Description ?? "unknown"}) cannot be kept in {options.Label}");
// The codes written are those the decoders read back as the original's values: the profile carried says what they are.
var iccProfile = treatment switch
{
ColorTreatment.Carry => color.Profile!.Data,
ColorTreatment.Describe => Described(color.Profile!).Data,
_ => null,
};
ConvertedFile file;
FrameMetadata metadata;
try
@@ -191,14 +212,14 @@ public static class FrameConverter
// 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}");
// A float file's NaN or infinity is no value any file holds: written, it would be black (and a DNG's headroom all spent).
if (!AllFinite(pixels.Data))
throw new ConversionException(ConversionFailure.Damaged, $"{name}: its pixels hold values that are not numbers (NaN or infinite)");
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!, 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);
using var output = EncodedWithin(name, image, metadata, options, iccProfile, encoderThreads);
cancellationToken.ThrowIfCancellationRequested();
// Hashed as made, and written from the same bytes (never copied again).
var bytes = output.GetBuffer().AsSpan(0, (int)output.Length);
@@ -358,58 +379,28 @@ public static class FrameConverter
}
/// <summary>
/// Values brought to linear Rec.709 through the profile they really are in. Values decoded from code values (the
/// decoders linearise them with the sRGB curve) go back to their code value, through the profile's curve, then its
/// matrix (a gray profile's curve gives a neutral). <paramref name="linear"/> values (<see cref="SourceColor.IsLinear"/>)
/// are linear in the profile's primaries already: through its matrix alone, never a curve, never clamped. Values
/// outside sRGB come out below 0 or above 1, which a scene-referred DNG keeps.
/// Whether every value is a number: neither NaN nor infinite (a float TIFF or DNG whose data is damaged holds such
/// values, which every decoder passes on as they are).
/// </summary>
internal static void ToLinearRec709(ImageBuffer image, IccProfile profile, bool linear, int threads, CancellationToken cancellationToken)
internal static bool AllFinite(ReadOnlySpan<float> values)
{
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 =>
var i = 0;
if (Vector.IsHardwareAccelerated && values.Length >= Vector<float>.Count)
{
var r = image.Row(0, y);
var g = image.Row(1, y);
var b = image.Row(2, y);
for (var x = 0; x < r.Length; x++)
var infinity = new Vector<float>(float.PositiveInfinity);
var vectors = MemoryMarshal.Cast<float, Vector<float>>(values);
// |NaN| < ∞ and |±∞| < ∞ are both false.
foreach (var v in vectors)
{
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);
if (!Vector.LessThanAll(Vector.Abs(v), infinity)) return false;
}
});
}
/// <summary>
/// Linear values in a matrix/TRC profile's primaries (a float TIFF's) made ready for an encoder that writes code values
/// through the sRGB curve, so the codes it writes are the profile's own: each value to its code value through the
/// inverse of the profile's curve, then back through the sRGB curve, which the encoder undoes. The file carries the
/// profile; what a code value cannot hold (above 1, below 0) is clipped as any such file clips it.
/// </summary>
internal static void EncodeWithCurves(ImageBuffer image, IccProfile profile, int threads, CancellationToken cancellationToken)
{
if (!profile.IsMatrixShaper) throw new InvalidOperationException("Only a matrix/TRC profile has curves to encode with.");
var curves = profile.Curves!;
Parallel.For(0, image.Height, new ParallelOptions { MaxDegreeOfParallelism = threads, CancellationToken = cancellationToken }, y =>
i = vectors.Length * Vector<float>.Count;
}
for (; i < values.Length; i++)
{
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]));
}
});
if (!float.IsFinite(values[i])) return false;
}
return true;
}
/// <summary>
@@ -472,27 +463,28 @@ public static class FrameConverter
}
}
/// <summary>What a conversion does with the colour space a photo's values are in (<see cref="FrameConverter.Treatment"/>).</summary>
/// <summary>
/// What a conversion does with the colour space a photo's values are in (<see cref="FrameConverter.Treatment"/>). Whatever it
/// does, the converted file holds the values Chronos's decoders read from the original, as they read them back.
/// </summary>
public enum ColorTreatment
{
/// <summary>sRGB (untagged, or an sRGB profile into a DNG) or linear gray: nothing to do.</summary>
/// <summary>The values are sRGB's as the decoders read them (untagged; an sRGB photo or linear gray into a DNG): no profile.</summary>
None,
/// <summary>A JPEG or TIFF carries the original's ICC profile; its values pass through unchanged.</summary>
/// <summary>A JPEG or TIFF keeps the original's codes and carries its ICC profile, which says what they are.</summary>
Carry,
/// <summary>The values are converted to linear Rec.709 through the profile (a DNG from a matrix/TRC profile, or a gray profile).</summary>
Convert,
/// <summary>
/// A JPEG or TIFF carries the original's matrix/TRC profile, its linear values (a float TIFF's) encoded with the
/// profile's own curves (<see cref="FrameConverter.EncodeWithCurves"/>).
/// A JPEG or TIFF holds the codes the decoders read back as the original's values, and carries a profile made for them
/// (<see cref="FrameConverter.Described"/>): the original's colorants with the sRGB curve (linear values, a float
/// TIFF's), or a gray profile's curve as an RGB profile.
/// </summary>
Encode,
Describe,
/// <summary>
/// A profile none of that applies to (CMYK, Lab, a lookup-table RGB profile into a DNG or holding linear values, curves
/// that cannot be inverted, an unknown space): not converted.
/// A space nothing here can keep: CMYK, Lab, an unknown space; linear values in a profile without colorants that is not
/// sRGB; into a DNG, values that are not sRGB's as the decoders read them (another profile's primaries or curve).
/// </summary>
Refuse,
}
@@ -24,6 +24,9 @@ public sealed class ManagedDecoderProvider : IImageDecoderProvider
private const long MaxPreviewBytes = 256L << 20;
private static readonly string[] CompanionExtensions = [".JPG", ".jpg", ".JPEG", ".jpeg"];
/// <summary>The provider whose probes read headers (<see cref="WithHeaderProbes"/>); it holds nothing else.</summary>
private static readonly ManagedDecoderProvider HeaderProbes = new() { ReadsHeaders = true };
public string Name => "managed";
/// <summary>
@@ -35,6 +38,9 @@ public sealed class ManagedDecoderProvider : IImageDecoderProvider
/// </summary>
public bool ReadsHeaders { get; init; }
/// <summary>A provider whose probes read the headers of JPEG, PNG and TIFF files (<see cref="ReadsHeaders"/>).</summary>
public IImageDecoderProvider WithHeaderProbes() => ReadsHeaders ? this : HeaderProbes;
public DecodeTier? TierFor(ImageFileFormat format)
{
var info = ImageFormatInfo.Of(format);
+8
View File
@@ -117,6 +117,14 @@ public interface IImageDecoderProvider
/// </summary>
FileTier Probe(string path, ImageFileFormat format, string? companion) => Probe(path, format);
/// <summary>
/// This provider with probes that read each file's headers and find unavailable what <see cref="Decode"/> would refuse
/// from them, at the size it would decode (owner review 2, R7): a conversion's dry run promises only what its decoders
/// take. Probes that trust the format are cheap, and enough wherever a decode that fails later costs nothing (imports,
/// analyses). The default is the provider itself, for one whose probes say already what it decodes.
/// </summary>
IImageDecoderProvider WithHeaderProbes() => this;
/// <summary>Decodes or returns null when this provider cannot handle the specific file.</summary>
DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default);
}