feat: owner review 2 phase 4b: mass conversion after import #13

Merged
Alby96 merged 82 commits from claude/owner-review-2-p4b-rc7 into main 2026-11-03 10:41:59 +01:00
16 changed files with 1341 additions and 330 deletions
Showing only changes of commit b471a33bfc - Show all commits

No files matched your search

+4 -3
View File
@@ -47,13 +47,14 @@
<data name="Archive.Convert.Done.other" xml:space="preserve"><value>{0:N0} sequences converted</value></data>
<data name="Archive.Convert.Failed.Changed" xml:space="preserve"><value>The photo no longer matches the copy that was archived; verify the sequence.</value><comment>long: why a photo was not converted, in the job log</comment></data>
<data name="Archive.Convert.Failed.ColorProfile" xml:space="preserve"><value>Its colour profile can be neither kept nor converted in this format.</value><comment>long: why a photo was not converted, in the job log</comment></data>
<data name="Archive.Convert.Failed.Damaged" xml:space="preserve"><value>Its pixels hold values that are not numbers: the file is damaged.</value><comment>long: why a photo was not converted, in the job log</comment></data>
<data name="Archive.Convert.Failed.EightBit" xml:space="preserve"><value>This computer develops this RAW at 8 bits only, too few for a 16-bit file; JPEG converts it.</value><comment>long: why a photo was not converted, in the job log</comment></data>
<data name="Archive.Convert.Failed.Full" xml:space="preserve"><value>The archive's disk is full.</value><comment>long: why a photo was not converted, in the job log</comment></data>
<data name="Archive.Convert.Failed.NoDecoder" xml:space="preserve"><value>No decoder on this computer reads it at full resolution.</value><comment>long: why a photo was not converted, in the job log</comment></data>
<data name="Archive.Convert.Failed.NotVerified" xml:space="preserve"><value>The converted file did not read back as it was written.</value><comment>long: why a photo was not converted, in the job log</comment></data>
<data name="Archive.Convert.Failed.Other" xml:space="preserve"><value>The converted file could not be made.</value><comment>long: why a photo was not converted, in the job log</comment></data>
<data name="Archive.Convert.Failed.PreviewOnly" xml:space="preserve"><value>This computer reads it only from its embedded preview: convert it on a PC that reads it at full resolution.</value><comment>long: why a photo was not converted, in the job log</comment></data>
<data name="Archive.Convert.Failed.TooLarge" xml:space="preserve"><value>It is larger than this format or the memory for one photo can hold.</value><comment>long: why a photo was not converted, in the job log</comment></data>
<data name="Archive.Convert.Failed.TooLarge" xml:space="preserve"><value>It is larger than this format holds, than Chronos reads back or than the memory for one photo can hold.</value><comment>long: why a photo was not converted, in the job log</comment></data>
<data name="Archive.Convert.Failed.Unreadable" xml:space="preserve"><value>The photo could not be decoded.</value><comment>long: why a photo was not converted, in the job log</comment></data>
<data name="Archive.Convert.Format" xml:space="preserve"><value>Format</value></data>
<data name="Archive.Convert.Heading.one" xml:space="preserve"><value>Convert {0:N0} sequence</value></data>
@@ -73,7 +74,7 @@
<data name="Archive.Convert.NotCopied.NoNativeDecoder" xml:space="preserve"><value>Photos of a format this computer cannot read at full resolution are not copied into the new sequence.</value><comment>long: why photos are not in the new sequence a conversion makes, in its dry run</comment></data>
<data name="Archive.Convert.NotCopied.PreviewOnly" xml:space="preserve"><value>Photos read only from their embedded preview here are not copied into the new sequence.</value><comment>long: why photos are not in the new sequence a conversion makes, in its dry run</comment></data>
<data name="Archive.Convert.NotCopied.SameFormat" xml:space="preserve"><value>Photos already in this format are not copied into the new sequence.</value><comment>long: why photos are not in the new sequence a conversion makes, in its dry run</comment></data>
<data name="Archive.Convert.NotCopied.TooLarge" xml:space="preserve"><value>Photos larger than this format can hold are not copied into the new sequence.</value><comment>long: why photos are not in the new sequence a conversion makes, in its dry run</comment></data>
<data name="Archive.Convert.NotCopied.TooLarge" xml:space="preserve"><value>Photos larger than this format holds or Chronos reads back are not copied into the new sequence.</value><comment>long: why photos are not in the new sequence a conversion makes, in its dry run</comment></data>
<data name="Archive.Convert.NotCopied.Undecodable" xml:space="preserve"><value>Unreadable photos are not copied into the new sequence.</value><comment>long: why photos are not in the new sequence a conversion makes, in its dry run</comment></data>
<data name="Archive.Convert.NotCopied.Unhashed" xml:space="preserve"><value>Photos never verified are not copied into the new sequence; verify the sequence first.</value><comment>long: why photos are not in the new sequence a conversion makes, in its dry run</comment></data>
<data name="Archive.Convert.NotCopiedPhotos.one" xml:space="preserve"><value>{0:N0} not copied</value><comment>photos a conversion into a new sequence leaves out of it</comment></data>
@@ -94,7 +95,7 @@
<data name="Archive.Convert.Skipped.NoNativeDecoder" xml:space="preserve"><value>Photos of a format this computer cannot read at full resolution stay as they are.</value><comment>long: why photos are not converted in a conversion dry run</comment></data>
<data name="Archive.Convert.Skipped.PreviewOnly" xml:space="preserve"><value>Photos read only from their embedded preview here stay as they are.</value><comment>long: why photos are not converted in a conversion dry run</comment></data>
<data name="Archive.Convert.Skipped.SameFormat" xml:space="preserve"><value>Photos already in this format stay as they are.</value><comment>long: why photos are not converted in a conversion dry run</comment></data>
<data name="Archive.Convert.Skipped.TooLarge" xml:space="preserve"><value>Photos larger than this format can hold stay as they are.</value><comment>long: why photos are not converted in a conversion dry run</comment></data>
<data name="Archive.Convert.Skipped.TooLarge" xml:space="preserve"><value>Photos larger than this format holds or Chronos reads back stay as they are.</value><comment>long: why photos are not converted in a conversion dry run</comment></data>
<data name="Archive.Convert.Skipped.Undecodable" xml:space="preserve"><value>Unreadable photos stay as they are.</value><comment>long: why photos are not converted in a conversion dry run</comment></data>
<data name="Archive.Convert.Skipped.Unhashed" xml:space="preserve"><value>Photos never verified stay as they are; verify the sequence first.</value><comment>long: why photos are not converted in a conversion dry run</comment></data>
<data name="Archive.Convert.SkippedPhotos.one" xml:space="preserve"><value>{0:N0} skipped</value><comment>photos a conversion leaves as they are</comment></data>
+4 -3
View File
@@ -44,13 +44,14 @@
<data name="Archive.Convert.Done.other" xml:space="preserve"><value>{0:N0} sequenze convertite</value></data>
<data name="Archive.Convert.Failed.Changed" xml:space="preserve"><value>La foto non corrisponde più alla copia archiviata; verifica la sequenza.</value></data>
<data name="Archive.Convert.Failed.ColorProfile" xml:space="preserve"><value>Il suo profilo colore non si può né conservare né convertire in questo formato.</value></data>
<data name="Archive.Convert.Failed.Damaged" xml:space="preserve"><value>I suoi pixel contengono valori che non sono numeri: il file è danneggiato.</value></data>
<data name="Archive.Convert.Failed.EightBit" xml:space="preserve"><value>Questo computer sviluppa questo RAW solo a 8 bit, troppo pochi per un file a 16 bit; JPEG lo converte.</value></data>
<data name="Archive.Convert.Failed.Full" xml:space="preserve"><value>Il disco dell'archivio è pieno.</value></data>
<data name="Archive.Convert.Failed.NoDecoder" xml:space="preserve"><value>Nessun decodificatore di questo computer la legge a piena risoluzione.</value></data>
<data name="Archive.Convert.Failed.NotVerified" xml:space="preserve"><value>Il file convertito non si è riletto come era stato scritto.</value></data>
<data name="Archive.Convert.Failed.Other" xml:space="preserve"><value>Non è stato possibile creare il file convertito.</value></data>
<data name="Archive.Convert.Failed.PreviewOnly" xml:space="preserve"><value>Questo computer la legge solo dalla sua anteprima incorporata: convertila su un PC che la legga a piena risoluzione.</value></data>
<data name="Archive.Convert.Failed.TooLarge" xml:space="preserve"><value>È più grande di quanto questo formato o la memoria per una foto possano contenere.</value></data>
<data name="Archive.Convert.Failed.TooLarge" xml:space="preserve"><value>È più grande di quanto questo formato possa contenere, Chronos possa rileggere o la memoria per una foto possa contenere.</value></data>
<data name="Archive.Convert.Failed.Unreadable" xml:space="preserve"><value>Non è stato possibile decodificare la foto.</value></data>
<data name="Archive.Convert.Format" xml:space="preserve"><value>Formato</value></data>
<data name="Archive.Convert.Heading.one" xml:space="preserve"><value>Converti {0:N0} sequenza</value></data>
@@ -70,7 +71,7 @@
<data name="Archive.Convert.NotCopied.NoNativeDecoder" xml:space="preserve"><value>Le foto di un formato che questo computer non legge a piena risoluzione non vengono copiate nella nuova sequenza.</value></data>
<data name="Archive.Convert.NotCopied.PreviewOnly" xml:space="preserve"><value>Le foto lette qui solo dalla loro anteprima incorporata non vengono copiate nella nuova sequenza.</value></data>
<data name="Archive.Convert.NotCopied.SameFormat" xml:space="preserve"><value>Le foto già in questo formato non vengono copiate nella nuova sequenza.</value></data>
<data name="Archive.Convert.NotCopied.TooLarge" xml:space="preserve"><value>Le foto più grandi di quanto questo formato possa contenere non vengono copiate nella nuova sequenza.</value></data>
<data name="Archive.Convert.NotCopied.TooLarge" xml:space="preserve"><value>Le foto più grandi di quanto questo formato possa contenere o Chronos possa rileggere non vengono copiate nella nuova sequenza.</value></data>
<data name="Archive.Convert.NotCopied.Undecodable" xml:space="preserve"><value>Le foto illeggibili non vengono copiate nella nuova sequenza.</value></data>
<data name="Archive.Convert.NotCopied.Unhashed" xml:space="preserve"><value>Le foto mai verificate non vengono copiate nella nuova sequenza; verifica prima la sequenza.</value></data>
<data name="Archive.Convert.NotCopiedPhotos.one" xml:space="preserve"><value>{0:N0} non copiata</value></data>
@@ -91,7 +92,7 @@
<data name="Archive.Convert.Skipped.NoNativeDecoder" xml:space="preserve"><value>Le foto di un formato che questo computer non legge a piena risoluzione restano come sono.</value></data>
<data name="Archive.Convert.Skipped.PreviewOnly" xml:space="preserve"><value>Le foto lette qui solo dalla loro anteprima incorporata restano come sono.</value></data>
<data name="Archive.Convert.Skipped.SameFormat" xml:space="preserve"><value>Le foto già in questo formato restano come sono.</value></data>
<data name="Archive.Convert.Skipped.TooLarge" xml:space="preserve"><value>Le foto più grandi di quanto questo formato possa contenere restano come sono.</value></data>
<data name="Archive.Convert.Skipped.TooLarge" xml:space="preserve"><value>Le foto più grandi di quanto questo formato possa contenere o Chronos possa rileggere restano come sono.</value></data>
<data name="Archive.Convert.Skipped.Undecodable" xml:space="preserve"><value>Le foto illeggibili restano come sono.</value></data>
<data name="Archive.Convert.Skipped.Unhashed" xml:space="preserve"><value>Le foto mai verificate restano come sono; verifica prima la sequenza.</value></data>
<data name="Archive.Convert.SkippedPhotos.one" xml:space="preserve"><value>{0:N0} esclusa</value></data>
@@ -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);
}
@@ -91,8 +91,9 @@ public sealed class IccProfileTests(TestContext context)
/// <summary>
/// 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.
/// and invertible; wherever the treatment asks for a profile made for the codes written (a float TIFF's linear values
/// into a JPEG or TIFF), that profile is made and parses, its matrix finite and invertible; a DNG takes the values as
/// they are or refuses them. Anything else the treatment refuses before any pixel is decoded.
/// </summary>
[Test]
public void DamagedProfilesConvertOrAreRefusedNeverFailMidway()
@@ -102,7 +103,7 @@ public sealed class IccProfileTests(TestContext context)
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;
int parsed = 0, described = 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++)
@@ -126,24 +127,21 @@ public sealed class IccProfileTests(TestContext context)
{
foreach (var target in Enum.GetValues<ConvertTarget>())
{
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");
}
if (target == ConvertTarget.LinearDng) Assert.True(treatment is ColorTreatment.None or ColorTreatment.Refuse, $"a DNG of {treatment}");
if (treatment != ColorTreatment.Describe) continue;
var made = IccProfile.Parse(FrameConverter.Described(color.Profile!).Data);
Assert.True(made is { IsMatrixShaper: true }, "the profile made for the codes parses as a matrix/TRC one");
var m = made!.ToLinearRec709();
Assert.True(Finite(m) && m.TryInvert(out var inverse) && Finite(inverse), "and converts with finite numbers");
described++;
}
}
}
}
context.Log($"{parsed} damaged profiles parsed, {worked} conversions run");
context.Log($"{parsed} damaged profiles parsed, {described} described");
Assert.Greater(parsed, 1000);
Assert.Greater(worked, 1000);
Assert.Greater(described, 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);
@@ -155,7 +153,11 @@ public sealed class IccProfileTests(TestContext context)
}
}
/// <summary>A matrix/TRC profile is sRGB when its numbers are sRGB's, whatever its name says; one without them by its name.</summary>
/// <summary>
/// A matrix/TRC profile is sRGB when its numbers are sRGB's, whatever its name says; one without colorant tags at all (a
/// lookup-table profile) by its name. One whose colorants convert nothing, or whose curves are missing, is not, whatever
/// its name (owner review 2, R7).
/// </summary>
[Test]
public void SrgbIsWhatTheNumbersSay()
{
@@ -166,28 +168,77 @@ public sealed class IccProfileTests(TestContext context)
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);
// Without colorant tags, the name decides; with colorants that convert nothing, or no curves, nothing says sRGB.
Assert.True(IccProfile.Parse(ColorFixtures.WithoutColorants(ColorFixtures.Profile("sRGB IEC61966-2.1", IccCurve.Gamma(1)).Data))!.IsSrgb);
Assert.False(IccProfile.Parse(ColorFixtures.WithoutColorants(IccProfile.AdobeRgb.Data))!.IsSrgb);
var degenerate = IccProfile.Parse(ColorFixtures.Degenerate(ColorFixtures.Profile("sRGB-elle-V4-g10", IccCurve.Srgb).Data))!;
Assert.True(degenerate is { IsRgb: true, Colorants: null, IsSrgb: false });
var curveless = ColorFixtures.Renamed(ColorFixtures.Profile("sRGB IEC61966-2.1", IccCurve.Srgb).Data, "rTRC", "xTRC");
Assert.True(IccProfile.Parse(curveless) is { IsMatrixShaper: false, IsSrgb: false });
// The curves themselves.
Assert.True(IccCurve.Srgb.IsSrgb && IccProfile.DisplayP3.Curves![0].IsSrgb);
Assert.False(IccCurve.Gamma(2.2).IsSrgb || IccCurve.Gamma(1).IsSrgb);
}
/// <summary>Curves invert: exactly for a power law and a table, within a 16-bit step for a parametric function.</summary>
/// <summary>
/// Values are sRGB's as Chronos's decoders take them (owner review 2, R7) when they are in sRGB's primaries and, for code
/// values, through sRGB's curve: a linear-curve profile in sRGB's primaries holds sRGB's linear values but not its code
/// values; a gray profile's linear values are linear gray, its code values need the sRGB curve.
/// </summary>
[Test]
public void CurvesInvert()
public void ValuesAreSrgbsWhereThePrimariesAndTheCurveSaySo()
{
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)
(IccProfile Profile, bool Linear, bool Codes)[] cases =
[
(ColorFixtures.Profile("sRGB IEC61966-2.1", IccCurve.Srgb), true, true),
(ColorFixtures.Profile("sRGB-elle-V4-g10", IccCurve.Gamma(1)), true, false),
(ColorFixtures.Profile("sRGB gamma 2.2", IccCurve.Gamma(2.2)), true, false),
(IccProfile.AdobeRgb, false, false),
(IccProfile.DisplayP3, false, false),
(ColorFixtures.ProPhoto(), false, false),
(IccProfile.Parse(ColorFixtures.Gray("Gray Gamma 2.2", IccCurve.Gamma(2.2)))!, true, false),
(IccProfile.Parse(ColorFixtures.Gray("sGray", IccCurve.Srgb))!, true, true),
(IccProfile.Parse(ColorFixtures.Cmyk())!, false, false),
(IccProfile.Parse(ColorFixtures.Degenerate())!, false, false),
];
foreach (var (profile, linear, codes) in cases)
Assert.Equal((linear, codes), (profile.IsSrgbFor(linear: true), profile.IsSrgbFor(linear: false)), $"{profile.Description} ({profile.ColorSpace:X8})");
}
/// <summary>
/// The profiles a converted file carries for codes that are not the original's (owner review 2, R7): a matrix/TRC
/// profile's colorants and media white with the sRGB curve, to the bit; a gray profile's curve on three channels whose
/// equal values are neutral.
/// </summary>
[Test]
public void ProfilesMadeForTheCodesKeepWhatTheyDescribe()
{
foreach (var source in (IccProfile[])[IccProfile.AdobeRgb, ColorFixtures.ProPhoto(), ColorFixtures.Profile("Wide gamma 1.0", IccCurve.Gamma(1), wide: true)])
{
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);
var made = IccProfile.Parse(source.WithSrgbCurve().Data)!;
Assert.True(made.IsMatrixShaper && made.IsRgb, source.Description);
Assert.Equal(source.Colorants, made.Colorants);
Assert.Equal(source.MediaWhite, made.MediaWhite);
Assert.True(made.Curves!.All(c => c.IsSrgb), source.Description);
Assert.Equal($"{source.Description} (sRGB curve)", made.Description);
}
// 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);
var gray = IccProfile.Parse(ColorFixtures.Gray("Gray Gamma 2.2", IccCurve.Gamma(2.2)))!;
var rgb = IccProfile.Parse(gray.GrayAsRgb().Data)!;
Assert.True(rgb is { IsMatrixShaper: true, IsRgb: true });
Assert.Equal("Gray Gamma 2.2 (RGB)", rgb.Description);
for (var code = 0.0; code <= 1; code += 0.125)
{
var light = rgb.ToLinearRec709() * new Vector3D(rgb.Curves![0].Evaluate(code), rgb.Curves[1].Evaluate(code), rgb.Curves[2].Evaluate(code));
var expected = gray.Curves![0].Evaluate(code);
Assert.Approximately(expected, light.X, 1e-3);
Assert.Approximately(expected, light.Y, 1e-3);
Assert.Approximately(expected, light.Z, 1e-3);
}
Assert.Throws<InvalidOperationException>(() => gray.WithSrgbCurve());
Assert.Throws<InvalidOperationException>(() => IccProfile.AdobeRgb.GrayAsRgb());
// A damaged profile's endless description is cut.
var long1 = IccProfile.Synthesize(new string('x', 5000), new(0.64, 0.33), new(0.30, 0.60), new(0.15, 0.06), IccCurve.Srgb);
Assert.Less(long1.Description!.Length, 300);
}
/// <summary>A TIFF of floating-point samples or linear RAW holds linear values; integer samples are code values.</summary>
@@ -102,13 +102,47 @@ internal static class ColorFixtures
? 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);
/// <summary>
/// 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).
/// </summary>
public static byte[] Degenerate()
/// <summary>ProPhoto (ROMM) primaries with its gamma 1.8 (and the D65 white the synthesized profiles have).</summary>
public static IccProfile ProPhoto() =>
IccProfile.Synthesize("ProPhoto RGB", new(0.7347, 0.2653), new(0.1596, 0.8404), new(0.0366, 0.0001), IccCurve.Gamma(1.8));
/// <summary>A gray profile (GRAY, a kTRC of <paramref name="curve"/>) named <paramref name="description"/>.</summary>
public static byte[] Gray(string description, IccCurve curve)
{
var data = (byte[])IccProfile.AdobeRgb.Data.Clone();
var data = Renamed(Profile(description, curve).Data, "rTRC", "kTRC");
"GRAY"u8.CopyTo(data.AsSpan(16));
return data;
}
/// <summary><paramref name="profile"/> with its rXYZ, gXYZ and bXYZ tags renamed: a profile without colorant tags, as a lookup-table one is.</summary>
public static byte[] WithoutColorants(byte[] profile) => Renamed(Renamed(Renamed(profile, "rXYZ", "rLUT"), "gXYZ", "gLUT"), "bXYZ", "bLUT");
/// <summary>A copy of <paramref name="profile"/> whose tag <paramref name="from"/> is named <paramref name="to"/>: a reader no longer finds it.</summary>
public static byte[] Renamed(byte[] profile, string from, string to)
{
var data = (byte[])profile.Clone();
var count = BinaryPrimitives.ReadInt32BigEndian(data.AsSpan(128));
for (var i = 0; i < count; i++)
{
var entry = data.AsSpan(132 + 12 * i, 12);
if (entry[..4].SequenceEqual(System.Text.Encoding.ASCII.GetBytes(from))) System.Text.Encoding.ASCII.GetBytes(to).CopyTo(entry);
}
return data;
}
/// <summary>A gray JPEG whose codes are the sRGB encoding of <paramref name="gray"/> (one channel), with an ICC profile.</summary>
public static byte[] GrayJpeg(ImageBuffer gray, byte[]? icc = null) => JpegEncoder.Encode(gray, new JpegEncodeOptions { Quality = 98, IccProfile = icc });
/// <summary>A 16-bit TIFF whose codes are the sRGB encoding of <paramref name="scene"/>, with an ICC profile.</summary>
public static byte[] Tiff16(ImageBuffer scene, byte[]? icc = null) => TiffWriter.Write(scene, new TiffWriteOptions { BitsPerSample = 16, IccProfile = icc });
/// <summary>
/// <paramref name="profile"/> (the Adobe RGB one by default) 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).
/// </summary>
public static byte[] Degenerate(byte[]? profile = null)
{
var data = (byte[])(profile ?? 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++)
@@ -176,18 +210,22 @@ internal static class ColorFixtures
0x12, 0x34, 0xFF, 0xD9,
];
/// <summary>An uncompressed TIFF of 32-bit floating-point RGB (linear light), with an ICC profile when one is given.</summary>
/// <summary>
/// An uncompressed TIFF of 32-bit floating-point RGB, or gray for a one-channel image (linear light), with an ICC
/// profile when one is given.
/// </summary>
public static byte[] FloatTiff(ImageBuffer image, byte[]? icc = null)
{
var strip = new byte[image.PlaneLength * 3 * 4];
var channels = image.Channels == 1 ? 1 : 3;
var strip = new byte[image.PlaneLength * channels * 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]);
for (var c = 0; c < channels; c++) BinaryPrimitives.WriteSingleLittleEndian(strip.AsSpan((i * channels + 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);
TiffTests.Layout(d, image.Width, image.Height, channels, 32, compression: 1);
d.Short(TiffTags.SampleFormat, [.. Enumerable.Repeat((ushort)3, channels)]);
if (icc is not null) d.Add(TiffTags.InterColorProfile, TiffType.Undefined, icc.Length, icc);
}, [strip], tiled: false);
}
@@ -205,13 +205,14 @@ public sealed class ConvertPlannerTests(TestContext context)
var present = frames.Select(f => f.File).ToList();
var decoder = DecoderFactory.CreateServer();
// A TIFF and a DNG keep the P3 photo (carried, or converted), not the unknown and CMYK ones, nor 2.5 G samples.
foreach (var target in (ConvertTarget[])[ConvertTarget.Tiff16, ConvertTarget.LinearDng])
{
var plan = ConvertPlanner.Plan(manifest, folder, present, new ConvertOptions(target), decoder, "Roof");
Assert.SequenceEqual([0, 1, 4], plan.Frames.Select(f => f.Index));
Assert.SequenceEqual([(ConvertSkip.ColorProfile, 2), (ConvertSkip.TooLarge, 1)], plan.Skipped.Select(s => (s.Reason, s.Frames)));
}
// A TIFF carries the P3 photo's profile; a DNG, which holds sRGB as Chronos reads it, does not take it. Neither takes
// the unknown and CMYK ones, nor 2.5 G samples.
var tiff = ConvertPlanner.Plan(manifest, folder, present, new ConvertOptions(ConvertTarget.Tiff16), decoder, "Roof");
Assert.SequenceEqual([0, 1, 4], tiff.Frames.Select(f => f.Index));
Assert.SequenceEqual([(ConvertSkip.ColorProfile, 2), (ConvertSkip.TooLarge, 1)], tiff.Skipped.Select(s => (s.Reason, s.Frames)));
var dng = ConvertPlanner.Plan(manifest, folder, present, new ConvertOptions(ConvertTarget.LinearDng), decoder, "Roof");
Assert.SequenceEqual([0, 4], dng.Frames.Select(f => f.Index));
Assert.SequenceEqual([(ConvertSkip.ColorProfile, 3), (ConvertSkip.TooLarge, 1)], dng.Skipped.Select(s => (s.Reason, s.Frames)));
// A JPEG side holds 65535 pixels.
var jpeg = ConvertPlanner.Plan(manifest, folder, present, new ConvertOptions(ConvertTarget.Jpeg), decoder, "Roof");
Assert.SequenceEqual([(ConvertSkip.SameFormat, 4), (ConvertSkip.TooLarge, 2)], jpeg.Skipped.Select(s => (s.Reason, s.Frames)));
@@ -271,6 +272,88 @@ public sealed class ConvertPlannerTests(TestContext context)
}
}
/// <summary>
/// Any size is answered, a damaged header's included (owner review 2, R7): sides up to <see cref="int.MaxValue"/> are too
/// large for every target, never let through by a product that wrapped around, and the worst case saturates instead of
/// turning negative.
/// </summary>
[Test]
public void AnySizeIsAnsweredWithoutOverflow()
{
(int W, int H)[] absurd =
[
(int.MaxValue, int.MaxValue), (2_000_000_000, 2_000_000_000), (1_800_000_000, 1_800_000_000), (1_000_000_000, 1_000_000_000),
(int.MaxValue, 1), (1, int.MaxValue),
];
foreach (var target in Enum.GetValues<ConvertTarget>())
{
foreach (var (w, h) in absurd) Assert.True(ConvertPlanner.TooLarge(new ConvertOptions(target), w, h), $"{target} at {w}×{h}");
Assert.Greater(ConvertPlanner.EstimateBytes(new ConvertOptions(target), int.MaxValue, int.MaxValue), 0L);
}
Assert.Equal(long.MaxValue, ConvertPlanner.WorstBytes(int.MaxValue, int.MaxValue));
Assert.Greater(ConvertPlanner.WorstBytes(1_800_000_000, 1_800_000_000), Array.MaxLength);
Assert.Greater(ConvertPlanner.WorstBytes(65_535, 65_535), 0L);
Assert.False(ConvertPlanner.TooLarge(new ConvertOptions(ConvertTarget.Tiff16), -5, 10));
}
/// <summary>
/// A frame flagged unreadable by the host that analysed it (the server, whose decoders refuse a CMYK TIFF) is asked again
/// where the conversion's decoders have others for its format (WIC on a PC), with a small decode (owner review 2, R7):
/// converted where they read it, unreadable where they do not. A corrupt frame stays unreadable, and so does a flagged one
/// where only the managed decoders read the format. A file that cannot be read while it is asked is inaccessible, nothing
/// concluded of its content.
/// </summary>
[Test]
public void AFrameFlaggedUnreadableElsewhereIsAskedWhereOtherDecodersReadIt()
{
var folder = new ArchivePaths(context.CreateTempDirectory("archive")).Sequence("roof");
System.IO.Directory.CreateDirectory(Path.GetDirectoryName(folder.File("originals/x"))!);
SidecarFrame[] frames =
[
Frame(0, "cmyk.tif", ImageFileFormat.Tiff, flags: FrameFlags.Undecodable) with { W = 6, H = 4 },
Frame(1, "corrupt.tif", ImageFileFormat.Tiff, flags: FrameFlags.Corrupt) with { W = 6, H = 4 },
];
File.WriteAllBytes(folder.File(frames[0].File), ColorFixtures.Tiff(6, 4, 5, [0, 255, 255, 0]));
File.WriteAllBytes(folder.File(frames[1].File), ColorFixtures.Tiff(6, 4, 2, [1, 2, 3]));
var manifest = new SequenceManifest { Id = Guid.NewGuid(), Frames = frames };
var present = frames.Select(f => f.File).ToList();
var jpeg = new ConvertOptions(ConvertTarget.Jpeg);
IEnumerable<(ConvertSkip, int)> Skipped(ConvertSequencePlan plan) => plan.Skipped.Select(s => (s.Reason, s.Frames));
var server = ConvertPlanner.Plan(manifest, folder, present, jpeg, DecoderFactory.CreateServer(), "Roof");
Assert.Empty(server.Frames);
Assert.SequenceEqual([(ConvertSkip.Undecodable, 2)], Skipped(server));
var reads = ConvertPlanner.Plan(manifest, folder, present, jpeg, new ImageDecoder([new DecodesTiffs(decodes: true), new ManagedDecoderProvider()]), "Roof");
Assert.SequenceEqual([0], reads.Frames.Select(f => f.Index));
Assert.SequenceEqual([(ConvertSkip.Undecodable, 1)], Skipped(reads));
var fails = ConvertPlanner.Plan(manifest, folder, present, jpeg, new ImageDecoder([new DecodesTiffs(decodes: false), new ManagedDecoderProvider()]), "Roof");
Assert.SequenceEqual([(ConvertSkip.Undecodable, 2)], Skipped(fails));
var held = ConvertPlanner.Plan(manifest, folder, present, jpeg,
new ImageDecoder([new DecodesTiffs(decodes: true, failure: new IOException("held by another process")), new ManagedDecoderProvider()]), "Roof");
Assert.Empty(held.Frames);
Assert.SequenceEqual([(ConvertSkip.Undecodable, 1), (ConvertSkip.Inaccessible, 1)], Skipped(held));
}
/// <summary>
/// WIC as a PC has it, for TIFFs: native by format (its probe trusts it), and a decode that reads a CMYK TIFF, finds
/// nothing in it, or fails as <paramref name="failure"/> says.
/// </summary>
private sealed class DecodesTiffs(bool decodes, Exception? failure = null) : IImageDecoderProvider
{
public string Name => "wic";
public DecodeTier? TierFor(ImageFileFormat format) => format == ImageFileFormat.Tiff ? DecodeTier.Native : null;
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default)
{
if (failure is not null) throw failure;
if (!decodes) return null;
var red = new ImageBuffer(6, 4);
red.Plane(0).Fill(0.6f);
return new DecodedImage(red, DecodeTier.Native, Name, 6, 4);
}
}
/// <summary>
/// The dry run reads the headers of TIFF, PNG and JPEG files before it promises them (owner review 2, R7): on the server,
/// a CMYK, CIELab, palette or ZSTD TIFF, one with more samples than its decoder takes and an arithmetic-coded JPEG are
@@ -351,7 +351,8 @@ public sealed class FrameConverterTests(TestContext context)
/// <summary>
/// The colour space the original's values are in is kept: a JPEG or TIFF carries its ICC profile (or the Adobe RGB one
/// its Exif names) with the values unchanged; a linear DNG gets the values converted through the profile.
/// its Exif names) with the values unchanged. A linear DNG, whose matrices Chronos develops it with, cannot hold P3 codes
/// as Chronos reads them and say they are P3: it refuses the photo, which converting would show otherwise.
/// </summary>
[Test]
public void TheOriginalsColourSpaceIsKept()
@@ -380,22 +381,10 @@ public sealed class FrameConverterTests(TestContext context)
}
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 = p3.ToLinearRec709();
for (var i = 0; i < scene.PlaneLength; i += 7)
{
var code = new Vector3D(
p3.Curves![0].Evaluate(ColorSpace.LinearToSrgb(original.Pixels.Plane(0)[i])),
p3.Curves[1].Evaluate(ColorSpace.LinearToSrgb(original.Pixels.Plane(1)[i])),
p3.Curves[2].Evaluate(ColorSpace.LinearToSrgb(original.Pixels.Plane(2)[i])));
var expected = matrix * code;
Assert.Approximately(expected.X, developed.Pixels.Plane(0)[i], 5e-4);
Assert.Approximately(expected.Y, developed.Pixels.Plane(1)[i], 5e-4);
Assert.Approximately(expected.Z, developed.Pixels.Plane(2)[i], 5e-4);
}
// The P3 green patch is outside sRGB: its red is negative in the DNG, not clipped to 0.
Assert.Less(developed.Pixels[0, 2, 2], -0.05);
var output = Target(dng);
var refused = Assert.Throws<ConversionException>(() => FrameConverter.Convert(source, dng, server, output, cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.ColorProfile, refused.Reason);
Assert.FileMissing(output);
// A camera JPEG in Adobe RGB (Exif uncalibrated, Interop R03) carries the Adobe RGB profile; a PNG its iCCP.
var adobePath = Path.Combine(folder, "adobe.jpg");
@@ -465,87 +454,235 @@ public sealed class FrameConverterTests(TestContext context)
}
/// <summary>
/// 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.
/// What Chronos reads from a photo never changes with its conversion (owner review 2, R7): Chronos's decoders, which
/// never read a profile, read the converted file as they read the original, within what the target holds, whatever the
/// original's colour space (sRGB, Adobe RGB, ProPhoto, a linear-curve profile, gray) and samples (8-bit, 16-bit, float).
/// The file says truthfully what the values are: the original's profile where its codes are the original's, else one
/// made for them (the original's colorants with the sRGB curve, or a gray curve as RGB). A DNG takes only values that are
/// sRGB's as the decoders read them, and reads back the same; it refuses the others, which its matrices would show
/// otherwise.
/// </summary>
[Test]
public void WhatChronosReadsIsWhatTheConvertedFileReadsBack()
{
var folder = context.CreateTempDirectory("source");
using var scene = ColorFixtures.Scene();
using var grays = new ImageBuffer(48, 32, 1);
for (var i = 0; i < grays.PlaneLength; i++) grays.Plane(0)[i] = 0.002f + 0.95f * i / grays.PlaneLength;
var srgb = ColorFixtures.Profile("sRGB IEC61966-2.1", IccCurve.Srgb);
var g10 = ColorFixtures.Profile("sRGB-elle-V4-g10", IccCurve.Gamma(1));
var wideG10 = ColorFixtures.Profile("Wide gamma 1.0", IccCurve.Gamma(1), wide: true);
var prophoto = ColorFixtures.ProPhoto();
var gamma22 = ColorFixtures.Gray("Gray Gamma 2.2", IccCurve.Gamma(2.2));
var sGray = ColorFixtures.Gray("sGray", IccCurve.Srgb);
(string Name, byte[] File, IccProfile? Profile, bool DngKeeps)[] sources =
[
("untagged.jpg", ColorFixtures.Jpeg(scene), null, true),
("srgb.jpg", ColorFixtures.Jpeg(scene, srgb.Data), srgb, true),
("adobe.jpg", ColorFixtures.Jpeg(scene, IccProfile.AdobeRgb.Data), IccProfile.AdobeRgb, false),
("g10.jpg", ColorFixtures.Jpeg(scene, g10.Data), g10, false),
("adobe16.tif", ColorFixtures.Tiff16(scene, IccProfile.AdobeRgb.Data), IccProfile.AdobeRgb, false),
("prophoto16.tif", ColorFixtures.Tiff16(scene, prophoto.Data), prophoto, false),
("float.tif", ColorFixtures.FloatTiff(scene), null, true),
("float-srgb.tif", ColorFixtures.FloatTiff(scene, srgb.Data), srgb, true),
("float-adobe.tif", ColorFixtures.FloatTiff(scene, IccProfile.AdobeRgb.Data), IccProfile.AdobeRgb, false),
("float-prophoto.tif", ColorFixtures.FloatTiff(scene, prophoto.Data), prophoto, false),
("float-g10.tif", ColorFixtures.FloatTiff(scene, g10.Data), g10, true),
("float-wide-g10.tif", ColorFixtures.FloatTiff(scene, wideG10.Data), wideG10, false),
("gray22.jpg", ColorFixtures.GrayJpeg(grays, gamma22), IccProfile.Parse(gamma22), false),
("sgray.jpg", ColorFixtures.GrayJpeg(grays, sGray), IccProfile.Parse(sGray), true),
("float-gray22.tif", ColorFixtures.FloatTiff(grays, gamma22), IccProfile.Parse(gamma22), true),
];
var server = DecoderFactory.CreateServer();
foreach (var (name, bytes, profile, dngKeeps) in sources)
{
var path = Path.Combine(folder, name);
File.WriteAllBytes(path, bytes);
var format = name.EndsWith(".jpg", StringComparison.Ordinal) ? ImageFileFormat.Jpeg : ImageFileFormat.Tiff;
var color = SourceColor.Read(path, format);
var source = new ConvertSource(path, Frame(name, format, 48, 32));
using var original = server.Decode(new DecodeRequest(path, AllowPreview: false))!;
foreach (var target in (ConvertTarget[])[ConvertTarget.Jpeg, ConvertTarget.Tiff16])
{
var options = new ConvertOptions(target, 100);
var file = FrameConverter.Convert(source, options, server, Target(options), cancellationToken: context.Cancellation);
using var converted = server.Decode(new DecodeRequest(file.Path, AllowPreview: false))!;
var (max, mean) = CodeDifference(original.Pixels, converted.Pixels);
var what = $"{name} → {target}";
Assert.LessOrEqual(max, target == ConvertTarget.Jpeg ? 4 / 255.0 : 2 / 65535.0, what);
Assert.Less(mean, target == ConvertTarget.Jpeg ? 1 / 255.0 : 1 / 65535.0, what);
AssertTruthful(color, SourceColor.Read(file.Path, file.Format), original.Pixels, converted.Pixels, target == ConvertTarget.Jpeg ? 0.05 : 2e-4, what);
}
var dng = new ConvertOptions(ConvertTarget.LinearDng);
var output = Target(dng);
Assert.Equal(dngKeeps ? ColorTreatment.None : ColorTreatment.Refuse, FrameConverter.Treatment(color, ConvertTarget.LinearDng), name);
if (!dngKeeps)
{
var refused = Assert.Throws<ConversionException>(() => FrameConverter.Convert(source, dng, server, output, cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.ColorProfile, refused.Reason, name);
Assert.FileMissing(output);
continue;
}
FrameConverter.Convert(source, dng, server, output, cancellationToken: context.Cancellation);
using var developed = server.Decode(new DecodeRequest(output, AllowPreview: false))!;
for (var c = 0; c < 3; c++)
{
for (var i = 0; i < scene.PlaneLength; i++) Assert.Approximately(original.Pixels.Plane(c)[i], developed.Pixels.Plane(c)[i], 3e-4, $"{name} → DNG");
}
}
}
/// <summary>The largest and the mean difference between two decodes of the same size, in sRGB code values (what the files hold).</summary>
private static (double Max, double Mean) CodeDifference(ImageBuffer a, ImageBuffer b)
{
Assert.Equal((a.Width, a.Height, a.Channels), (b.Width, b.Height, b.Channels));
double max = 0, sum = 0;
for (var c = 0; c < a.Channels; c++)
{
var pa = a.Plane(c);
var pb = b.Plane(c);
for (var i = 0; i < pa.Length; i++)
{
var d = Math.Abs(ColorSpace.LinearToSrgb(pa[i]) - ColorSpace.LinearToSrgb(pb[i]));
max = Math.Max(max, d);
sum += d;
}
}
return (max, sum / ((double)a.Channels * a.PlaneLength));
}
/// <summary>
/// The converted file's profile says what its values are: read through it, its codes give the light the original's
/// profile gives the original's values (linear Rec.709, to <paramref name="tolerance"/>); no profile where the original
/// had none, or where its values are linear gray.
/// </summary>
private static void AssertTruthful(SourceColor original, SourceColor converted, ImageBuffer originalPixels, ImageBuffer convertedPixels, double tolerance,
string what)
{
Assert.False(converted.IsLinear || converted.IsUnknown, what);
if (original.Profile is not { } source || (source.IsGray && original.IsLinear))
{
Assert.Null(converted.Profile, what);
return;
}
var carried = Assert.NotNull(converted.Profile, what);
if (!original.IsLinear && source.IsRgb)
{
Assert.BytesEqual(source.Data, carried.Data);
return;
}
Assert.True(carried.IsMatrixShaper, what);
var to709 = carried.ToLinearRec709();
for (var i = 0; i < originalPixels.PlaneLength; i += 11)
{
var (x, y) = (i % originalPixels.Width, i / originalPixels.Width);
Vector3D light;
if (source.IsGray)
{
// A gray profile's code: its curve's light, neutral.
var gray = source.Curves![0].Evaluate(ColorSpace.LinearToSrgb(originalPixels[0, x, y]));
light = new Vector3D(gray, gray, gray);
}
else
{
light = source.ToLinearRec709() * new Vector3D(originalPixels[0, x, y], originalPixels[1, x, y], originalPixels[2, x, y]);
}
var read = to709 * new Vector3D(
carried.Curves![0].Evaluate(ColorSpace.LinearToSrgb(convertedPixels[0, x, y])),
carried.Curves[1].Evaluate(ColorSpace.LinearToSrgb(convertedPixels[1, x, y])),
carried.Curves[2].Evaluate(ColorSpace.LinearToSrgb(convertedPixels[2, x, y])));
Assert.Approximately(light.X, read.X, tolerance, what);
Assert.Approximately(light.Y, read.Y, tolerance, what);
Assert.Approximately(light.Z, read.Z, tolerance, what);
}
}
/// <summary>
/// A float TIFF's values are linear light in its profile's primaries (owner review 2, R7): a JPEG or TIFF holds them as
/// the decoders read them back, sRGB codes, and carries the profile's colorants with the sRGB curve, so colour-managed
/// programs read the same light; above white is clipped there, as in any file of code values. A DNG keeps above white,
/// for values in sRGB's primaries only: in other primaries it refuses them.
/// </summary>
[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])
foreach (var wide in (bool[])[true, false])
{
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 profile = ColorFixtures.Profile($"Gamma {gamma}", IccCurve.Gamma(gamma), wide);
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);
(scene[0, 3, 1], scene[1, 3, 1], scene[2, 3, 1]) = (0.001f, 0.01f, 0.0005f);
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.Describe, FrameConverter.Treatment(SourceColor.Read(path, ImageFileFormat.Tiff), ConvertTarget.Tiff16));
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));
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);
var carried = SourceColor.Read(file.Path, file.Format).Profile!;
Assert.Equal(profile.Colorants, carried.Colorants);
Assert.True(carried.Curves!.All(c => c.IsSrgb), "the codes are sRGB's");
using var converted = server.Decode(new DecodeRequest(file.Path, AllowPreview: false))!;
var tolerance = target == ConvertTarget.Jpeg ? 0.012 : 1e-4;
foreach (var (x, y) in ((int, int)[])[(5, 2), (1, 1), (3, 1)])
{
for (var c = 0; c < 3; c++)
Assert.Approximately(ColorSpace.LinearToSrgb(scene[c, x, y]), ColorSpace.LinearToSrgb(converted.Pixels[c, x, y]), tolerance, $"{target}, gamma {gamma}");
}
Assert.Approximately(1, converted.Pixels[0, 2, 1], target == ConvertTarget.Jpeg ? 0.02 : 1e-3, "above white is clipped");
}
var dng = new ConvertOptions(ConvertTarget.LinearDng);
var output = Target(dng);
if (wide)
{
Assert.Equal(ConversionFailure.ColorProfile,
Assert.Throws<ConversionException>(() => FrameConverter.Convert(source, dng, server, output, cancellationToken: context.Cancellation)).Reason);
continue;
}
FrameConverter.Convert(source, dng, server, output, cancellationToken: context.Cancellation);
using var developed = server.Decode(new DecodeRequest(output, AllowPreview: false))!;
foreach (var (x, y) in ((int, int)[])[(5, 2), (1, 1), (2, 1), (3, 1)])
{
for (var c = 0; c < 3; c++) Assert.Approximately(scene[c, x, y], developed.Pixels[c, x, y], 2e-3 * Math.Max(1, scene[c, x, y]));
}
Assert.Greater(developed.Pixels[1, 2, 1], 2.9);
}
Assert.Greater(developed.Pixels[1, 2, 1], 2.9);
}
/// <summary>
/// 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.
/// A DNG takes values only where they are sRGB's as the decoders read them, whatever the profile's name: codes in one
/// named sRGB with a linear curve are not (the decoders read them through the sRGB curve), the same profile's linear
/// values are (sRGB's primaries), a real sRGB profile's codes are.
/// </summary>
[Test]
public void AProfileNamedSrgbWithAnotherCurveIsConvertedIntoADng()
public void ADngTakesOnlyValuesThatAreSrgbsAsTheDecodersReadThem()
{
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);
}
ColorTreatment Dng(byte[] file, ImageFileFormat format) => FrameConverter.Treatment(SourceColor.Read(new MemoryStream(file), format), ConvertTarget.LinearDng);
Assert.Equal(ColorTreatment.Refuse, Dng(ColorFixtures.Jpeg(scene, linearCurve.Data), ImageFileFormat.Jpeg));
Assert.Equal(ColorTreatment.None, Dng(ColorFixtures.FloatTiff(scene, linearCurve.Data), ImageFileFormat.Tiff));
Assert.Equal(ColorTreatment.None, Dng(ColorFixtures.Jpeg(scene, real.Data), ImageFileFormat.Jpeg));
Assert.Equal(ColorTreatment.None, Dng(ColorFixtures.Jpeg(scene), ImageFileFormat.Jpeg));
Assert.Equal(ColorTreatment.Refuse, Dng(ColorFixtures.Jpeg(scene, IccProfile.AdobeRgb.Data), ImageFileFormat.Jpeg));
// Into a JPEG or TIFF the same codes are carried with their profile.
Assert.Equal(ColorTreatment.Carry, FrameConverter.Treatment(SourceColor.Read(new MemoryStream(ColorFixtures.Jpeg(scene, linearCurve.Data)), ImageFileFormat.Jpeg), ConvertTarget.Tiff16));
}
/// <summary>
/// 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.
/// A profile whose colorants convert nothing (a damaged one) refuses its photo for a DNG, at once and as a colour
/// profile, and is carried unchanged into a JPEG or TIFF with the codes it came with. One named sRGB is no sRGB: a float
/// TIFF's linear values in it, which a JPEG or TIFF would need its colorants to describe, are refused there too.
/// </summary>
[Test]
public void AProfileThatConvertsNothingIsRefusedForADngAndCarriedElsewhere()
@@ -563,6 +700,71 @@ public sealed class FrameConverterTests(TestContext context)
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);
var namedSrgb = ColorFixtures.Degenerate(ColorFixtures.Profile("sRGB-elle-V4-g10", IccCurve.Gamma(1)).Data);
var floatPath = Path.Combine(context.CreateTempDirectory("source"), "stack.tif");
File.WriteAllBytes(floatPath, ColorFixtures.FloatTiff(scene, namedSrgb));
foreach (var target in Enum.GetValues<ConvertTarget>())
{
var options = new ConvertOptions(target);
var floatOutput = Target(options);
Assert.Equal(ConversionFailure.ColorProfile, Assert.Throws<ConversionException>(() =>
FrameConverter.Convert(new ConvertSource(floatPath, Frame("stack.tif", ImageFileFormat.Tiff, 48, 32)), options, DecoderFactory.CreateServer(), floatOutput,
cancellationToken: context.Cancellation)).Reason, target.ToString());
Assert.FileMissing(floatOutput);
}
}
/// <summary>
/// A float file whose samples are not numbers (NaN, ±infinity: damaged data) fails its conversion as damaged in every
/// target, nothing written, rather than written black (with a DNG's headroom spent on it).
/// </summary>
[Test]
public void SamplesThatAreNotNumbersFailAsDamagedData()
{
foreach (var bad in (float[])[float.NaN, float.PositiveInfinity, float.NegativeInfinity])
{
using var scene = new ImageBuffer(8, 4);
scene.Data.Fill(0.18f);
scene[1, 6, 3] = bad;
var path = Path.Combine(context.CreateTempDirectory("source"), "stack.tif");
File.WriteAllBytes(path, ColorFixtures.FloatTiff(scene, IccProfile.AdobeRgb.Data));
File.WriteAllBytes(Path.ChangeExtension(path, ".untagged.tif"), ColorFixtures.FloatTiff(scene));
foreach (var file in (string[])[path, Path.ChangeExtension(path, ".untagged.tif")])
{
foreach (var target in Enum.GetValues<ConvertTarget>())
{
var options = new ConvertOptions(target);
if (FrameConverter.Treatment(SourceColor.Read(file, ImageFileFormat.Tiff), target) == ColorTreatment.Refuse) continue;
var output = Target(options);
var failed = Assert.Throws<ConversionException>(() => FrameConverter.Convert(new ConvertSource(file, Frame("stack.tif", ImageFileFormat.Tiff, 8, 4)), options,
DecoderFactory.CreateServer(), output, cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.Damaged, failed.Reason, $"{bad} → {target}");
Assert.FileMissing(output);
}
}
}
}
/// <summary>Every NaN and infinity is found, wherever it is (in a vector or in the tail), and finite values pass, however large.</summary>
[Test]
public void NumbersAreToldFromWhatIsNot()
{
foreach (var length in (int[])[0, 1, 7, 8, 9, 31, 64, 1001])
{
var values = new float[length];
for (var i = 0; i < length; i++) values[i] = (i % 3 - 1) * (i % 2 == 0 ? float.MaxValue : float.Epsilon);
Assert.True(FrameConverter.AllFinite(values), $"{length} finite values");
for (var at = 0; at < length; at += Math.Max(1, length / 5))
{
foreach (var bad in (float[])[float.NaN, float.PositiveInfinity, float.NegativeInfinity])
{
var copy = (float[])values.Clone();
copy[at] = bad;
Assert.False(FrameConverter.AllFinite(copy), $"{bad} at {at} of {length}");
}
}
}
}
/// <summary>A photo larger than the target or one buffer holds fails as too large, not with the encoder's own exception.</summary>
@@ -0,0 +1,169 @@
using Chronos.Core.Archiving;
using Chronos.Core.Codecs.Icc;
using Chronos.Core.Codecs.Jpeg;
using Chronos.Core.Codecs.Tiff;
using Chronos.Core.Conversion;
using Chronos.Core.Imaging;
using Chronos.Core.Imaging.Decoders;
using Chronos.Core.Windows.Wic;
using Chronos.Testing;
namespace Chronos.Core.Windows.Tests.Wic;
/// <summary>
/// Conversions on the desktop (owner review 2, R7), read back through the desktop's own decoders, WIC first: what Chronos
/// shows of a photo there never changes with its conversion, whatever the photo's colour space and samples.
/// </summary>
[WindowsOnly]
public sealed class DesktopConversionTests(TestContext context)
{
private static readonly Lazy<ImageDecoder> Desktop = new(() => DecoderFactory.CreateClient(new WicDecoderProvider()));
/// <summary>
/// The desktop decodes the converted file (WIC for a JPEG or TIFF, the managed reader for a DNG) to the values it decodes
/// the original to, within what the target holds, for sRGB, Adobe RGB, ProPhoto and linear-curve sources of 8-bit,
/// 16-bit and float samples; the file carries a profile that says what its codes are. A DNG takes only values that are
/// sRGB's as the decoders read them, and refuses the others.
/// </summary>
[Test]
public void TheDesktopReadsTheConvertedFileAsItReadsTheOriginal()
{
var folder = context.CreateTempDirectory();
using var scene = WicDecoderProviderTests.Scene(48, 32);
using var grays = new ImageBuffer(48, 32, 1);
for (var i = 0; i < grays.PlaneLength; i++) grays.Plane(0)[i] = 0.002f + 0.95f * i / grays.PlaneLength;
var srgb = IccProfile.Synthesize("sRGB IEC61966-2.1", new(0.64, 0.33), new(0.30, 0.60), new(0.15, 0.06), IccCurve.Srgb);
var g10 = IccProfile.Synthesize("sRGB-elle-V4-g10", new(0.64, 0.33), new(0.30, 0.60), new(0.15, 0.06), IccCurve.Gamma(1));
var prophoto = IccProfile.Synthesize("ProPhoto RGB", new(0.7347, 0.2653), new(0.1596, 0.8404), new(0.0366, 0.0001), IccCurve.Gamma(1.8));
(string Name, byte[] File, bool DngKeeps)[] sources =
[
("untagged.jpg", Jpeg(scene, null), true),
("srgb.jpg", Jpeg(scene, srgb), true),
("gray22.jpg", JpegCarrying(grays, Gray(IccCurve.Gamma(2.2))), false),
("sgray.jpg", JpegCarrying(grays, Gray(IccCurve.Srgb)), true),
("adobe.jpg", Jpeg(scene, IccProfile.AdobeRgb), false),
("g10.jpg", Jpeg(scene, g10), false),
("prophoto16.tif", TiffWriter.Write(scene, new TiffWriteOptions { BitsPerSample = 16, IccProfile = prophoto.Data }), false),
("adobe16.tif", TiffWriter.Write(scene, new TiffWriteOptions { BitsPerSample = 16, IccProfile = IccProfile.AdobeRgb.Data }), false),
("float.tif", TestTiffs.FloatRgb(scene), true),
("float-adobe.tif", TestTiffs.FloatRgb(scene, IccProfile.AdobeRgb.Data), false),
("float-prophoto.tif", TestTiffs.FloatRgb(scene, prophoto.Data), false),
("float-g10.tif", TestTiffs.FloatRgb(scene, g10.Data), true),
("float-adobe-predicted.tif", TestTiffs.FloatRgb(scene, IccProfile.AdobeRgb.Data, predicted: true), false),
];
var desktop = Desktop.Value;
foreach (var (name, bytes, dngKeeps) in sources)
{
var path = Path.Combine(folder, name);
File.WriteAllBytes(path, bytes);
var format = name.EndsWith(".jpg", StringComparison.Ordinal) ? ImageFileFormat.Jpeg : ImageFileFormat.Tiff;
var color = SourceColor.Read(path, format);
var source = new ConvertSource(path, Frame(name, format, scene.Width, scene.Height));
using var original = Assert.NotNull(desktop.Decode(new DecodeRequest(path, AllowPreview: false)));
foreach (var target in (ConvertTarget[])[ConvertTarget.Jpeg, ConvertTarget.Tiff16])
{
var options = new ConvertOptions(target, 100);
var file = FrameConverter.Convert(source, options, desktop, Target(options), cancellationToken: context.Cancellation);
using var converted = Assert.NotNull(desktop.Decode(new DecodeRequest(file.Path, AllowPreview: false)));
Assert.Equal("wic", converted.Decoder, name);
var (max, mean) = CodeDifference(original.Pixels, converted.Pixels);
var what = $"{name} ({original.Decoder}) → {target}";
context.Log($"{what}: max {max * 255:0.00}/255, mean {mean * 255:0.000}/255");
Assert.LessOrEqual(max, target == ConvertTarget.Jpeg ? 4 / 255.0 : 2 / 65535.0, what);
Assert.Less(mean, target == ConvertTarget.Jpeg ? 1 / 255.0 : 1 / 65535.0, what);
// What the file says its codes are: the original's profile with the original's codes, else its colorants with the sRGB curve.
var carried = SourceColor.Read(file.Path, file.Format).Profile;
if (color.Profile is not { } profile)
{
Assert.Null(carried, what);
}
else if (profile.IsGray)
{
// A gray profile's codes, kept, with its curve on three channels.
Assert.True(Assert.NotNull(carried, what).IsMatrixShaper, what);
for (var code = 0.0; code <= 1; code += 0.125) Assert.Approximately(profile.Curves![0].Evaluate(code), carried.Curves![1].Evaluate(code), 1e-3, what);
}
else if (!color.IsLinear)
{
Assert.BytesEqual(profile.Data, Assert.NotNull(carried, what).Data);
}
else
{
Assert.Equal(profile.Colorants, Assert.NotNull(carried, what).Colorants);
Assert.True(carried.Curves!.All(c => c.IsSrgb), what);
}
}
var dng = new ConvertOptions(ConvertTarget.LinearDng);
var output = Target(dng);
if (!dngKeeps)
{
Assert.Equal(ConversionFailure.ColorProfile,
Assert.Throws<ConversionException>(() => FrameConverter.Convert(source, dng, desktop, output, cancellationToken: context.Cancellation)).Reason, name);
Assert.FileMissing(output);
continue;
}
FrameConverter.Convert(source, dng, desktop, output, cancellationToken: context.Cancellation);
using var developed = Assert.NotNull(desktop.Decode(new DecodeRequest(output, AllowPreview: false)));
for (var c = 0; c < 3; c++)
{
for (var i = 0; i < scene.PlaneLength; i++) Assert.Approximately(original.Pixels.Plane(c)[i], developed.Pixels.Plane(c)[i], 3e-4, $"{name} → DNG");
}
}
}
private static byte[] Jpeg(ImageBuffer scene, IccProfile? profile) => JpegCarrying(scene, profile?.Data);
private static byte[] JpegCarrying(ImageBuffer scene, byte[]? icc) => JpegEncoder.Encode(scene, new JpegEncodeOptions
{
Quality = 98,
Subsampling = JpegSubsampling.Yuv444,
IccProfile = icc,
});
/// <summary>A gray profile (GRAY, a kTRC of <paramref name="curve"/>): a synthesized RGB one, its red curve renamed.</summary>
private static byte[] Gray(IccCurve curve)
{
var data = (byte[])IccProfile.Synthesize("Gray", new(0.64, 0.33), new(0.30, 0.60), new(0.15, 0.06), curve).Data.Clone();
"GRAY"u8.CopyTo(data.AsSpan(16));
var count = System.Buffers.Binary.BinaryPrimitives.ReadInt32BigEndian(data.AsSpan(128));
for (var i = 0; i < count; i++)
{
var entry = data.AsSpan(132 + 12 * i, 4);
if (entry.SequenceEqual("rTRC"u8)) "kTRC"u8.CopyTo(entry);
}
return data;
}
private static SidecarFrame Frame(string file, ImageFileFormat format, int width, int height) => new()
{
Id = Guid.NewGuid(),
File = "originals/" + file,
Sha256 = new string('a', 64),
CapturedAt = new DateTime(2026, 9, 28, 18, 0, 5),
W = width,
H = height,
Format = format,
};
private string Target(ConvertOptions options) => Path.Combine(context.CreateTempDirectory(), "converted" + options.Extension);
/// <summary>The largest and the mean difference between two decodes, in sRGB code values (what the files hold).</summary>
private static (double Max, double Mean) CodeDifference(ImageBuffer a, ImageBuffer b)
{
Assert.Equal((a.Width, a.Height), (b.Width, b.Height));
double max = 0, sum = 0;
for (var c = 0; c < 3; c++)
{
var pa = a.Plane(c);
var pb = b.Plane(c);
for (var i = 0; i < pa.Length; i++)
{
var d = Math.Abs(ColorSpace.LinearToSrgb(pa[i]) - ColorSpace.LinearToSrgb(pb[i]));
max = Math.Max(max, d);
sum += d;
}
}
return (max, sum / (3.0 * a.PlaneLength));
}
}
@@ -0,0 +1,137 @@
using System.Buffers.Binary;
using System.IO.Compression;
using Chronos.Core.Imaging;
namespace Chronos.Core.Windows.Tests.Wic;
/// <summary>Little-endian TIFFs of one strip, built tag by tag: what WIC and the managed reader are asked about.</summary>
internal static class TestTiffs
{
private const ushort Short = 3, Long = 4, Undefined = 7;
/// <summary>
/// A TIFF of <paramref name="width"/> × <paramref name="height"/> pixels of <paramref name="bits"/>-bit samples,
/// <paramref name="spp"/> a pixel, in one strip holding <paramref name="strip"/> as it is (already compressed or predicted).
/// </summary>
public static byte[] Build(int width, int height, int spp, int bits, byte[] strip, int compression = 1, int photometric = 2, int sampleFormat = 1,
int predictor = 1, byte[]? icc = null)
{
var entries = new List<(ushort Tag, ushort Type, uint[] Values, byte[]? Bytes)>
{
(256, Long, [(uint)width], null),
(257, Long, [(uint)height], null),
(258, Short, [.. Enumerable.Repeat((uint)bits, spp)], null),
(259, Short, [(uint)compression], null),
(262, Short, [(uint)photometric], null),
(273, Long, [0], null),
(277, Short, [(uint)spp], null),
(278, Long, [(uint)height], null),
(279, Long, [(uint)strip.Length], null),
(284, Short, [1], null),
};
if (predictor != 1) entries.Add((317, Short, [(uint)predictor], null));
if (sampleFormat != 1) entries.Add((339, Short, [.. Enumerable.Repeat((uint)sampleFormat, spp)], null));
if (icc is not null) entries.Add((34675, Undefined, [], icc));
entries.Sort((a, b) => a.Tag.CompareTo(b.Tag));
const int ifd = 8;
var position = ifd + 2 + entries.Count * 12 + 4;
var outOfLine = new int[entries.Count];
for (var i = 0; i < entries.Count; i++)
{
var size = Size(entries[i]);
if (size <= 4) continue;
outOfLine[i] = position;
position += size + (size & 1);
}
var data = position;
var bytes = new byte[data + strip.Length];
"II*\0"u8.CopyTo(bytes);
BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(4), ifd);
BinaryPrimitives.WriteUInt16LittleEndian(bytes.AsSpan(ifd), (ushort)entries.Count);
for (var i = 0; i < entries.Count; i++)
{
var (tag, type, values, raw) = entries[i];
var at = ifd + 2 + 12 * i;
BinaryPrimitives.WriteUInt16LittleEndian(bytes.AsSpan(at), tag);
BinaryPrimitives.WriteUInt16LittleEndian(bytes.AsSpan(at + 2), type);
BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(at + 4), (uint)(raw?.Length ?? values.Length));
var target = outOfLine[i] > 0 ? outOfLine[i] : at + 8;
if (outOfLine[i] > 0) BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(at + 8), (uint)outOfLine[i]);
if (raw is not null)
{
raw.CopyTo(bytes, target);
continue;
}
for (var k = 0; k < values.Length; k++)
{
var value = tag == 273 ? (uint)data : values[k];
if (type == Short) BinaryPrimitives.WriteUInt16LittleEndian(bytes.AsSpan(target + 2 * k), (ushort)value);
else BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(target + 4 * k), value);
}
}
strip.CopyTo(bytes, data);
return bytes;
}
private static int Size((ushort Tag, ushort Type, uint[] Values, byte[]? Bytes) entry) =>
entry.Bytes?.Length ?? (entry.Type == Short ? 2 : 4) * entry.Values.Length;
/// <summary>An 8-bit RGB TIFF of <paramref name="image"/>'s sRGB codes, uncompressed, with its strip labelled <paramref name="compression"/>.</summary>
public static byte[] Rgb8(ImageBuffer image, int compression = 1) =>
Build(image.Width, image.Height, 3, 8, PixelConvert.ToInterleaved8(image, 3), compression);
/// <summary>
/// A 32-bit float RGB TIFF of <paramref name="image"/> (linear light, as it is), uncompressed, or Deflate with the
/// floating-point predictor (3) as Photoshop writes its 32-bit files: byte planes most significant first, each byte less
/// the one a pixel before it.
/// </summary>
public static byte[] FloatRgb(ImageBuffer image, byte[]? icc = null, bool predicted = false)
{
const int spp = 3;
var count = image.Width * spp;
var strip = new byte[image.PlaneLength * spp * 4];
var row = new byte[count * 4];
for (var y = 0; y < image.Height; y++)
{
for (var x = 0; x < image.Width; x++)
{
for (var c = 0; c < spp; c++)
{
var bits = BitConverter.SingleToUInt32Bits(image[c, x, y]);
var i = x * spp + c;
if (!predicted)
{
BinaryPrimitives.WriteUInt32LittleEndian(row.AsSpan(i * 4), bits);
continue;
}
for (var k = 0; k < 4; k++) row[k * count + i] = (byte)(bits >> (8 * (3 - k)));
}
}
if (predicted)
{
for (var i = row.Length - 1; i >= spp; i--) row[i] -= row[i - spp];
}
row.CopyTo(strip, y * row.Length);
}
return predicted
? Build(image.Width, image.Height, spp, 32, Deflate(strip), compression: 8, sampleFormat: 3, predictor: 3, icc: icc)
: Build(image.Width, image.Height, spp, 32, strip, sampleFormat: 3, icc: icc);
}
/// <summary>A 32-bit unsigned integer RGB TIFF of <paramref name="image"/>'s codes, scaled to 32 bits.</summary>
public static byte[] Uint32Rgb(ImageBuffer image)
{
var codes = PixelConvert.ToInterleaved8(image, 3);
var strip = new byte[codes.Length * 4];
for (var i = 0; i < codes.Length; i++) BinaryPrimitives.WriteUInt32LittleEndian(strip.AsSpan(i * 4), codes[i] * 16843009u);
return Build(image.Width, image.Height, 3, 32, strip);
}
private static byte[] Deflate(byte[] raw)
{
using var output = new MemoryStream();
using (var z = new ZLibStream(output, CompressionLevel.Optimal, leaveOpen: true)) z.Write(raw);
return output.ToArray();
}
}
@@ -71,6 +71,154 @@ public sealed class WicDecoderProviderTests(TestContext context)
Assert.Less(converted.Pixels[2, 3, 2], 0.1);
}
/// <summary>
/// A TIFF WIC would decode as black or as noise without an error (owner review 2, R7) is left to the other decoders: a
/// compression it does not know (ZSTD, an unknown code), whose data it decodes as zeros; Photoshop's floating-point
/// predictor, which it does not undo; 32-bit integer samples, which it takes for floats. On the desktop the managed
/// reader decodes the predicted float TIFF to its values, and the others are unavailable, never black; a conversion's
/// plan leaves those out and converts the float one.
/// </summary>
[Test]
public void ATiffWicWouldDecodeBlackOrAsNoiseIsLeftToTheOtherDecoders()
{
var folder = context.CreateTempDirectory();
using var scene = Scene(12, 8);
(string Name, byte[] File, string Why)[] refused =
[
("zstd.tif", TestTiffs.Rgb8(scene, compression: 50000), "compression 50000"),
("unknown.tif", TestTiffs.Rgb8(scene, compression: 12345), "compression 12345"),
("jpeg2000.tif", TestTiffs.Rgb8(scene, compression: 34712), "compression 34712"),
("predicted.tif", TestTiffs.FloatRgb(scene, predicted: true), "predictor 3"),
("int32.tif", TestTiffs.Uint32Rgb(scene), "32-bit integer"),
];
var reading = Provider.WithHeaderProbes();
foreach (var (name, bytes, why) in refused)
{
var path = Path.Combine(folder, name);
File.WriteAllBytes(path, bytes);
Assert.Null(Provider.Decode(new DecodeRequest(path)));
var probe = reading.Probe(path, ImageFileFormat.Tiff);
Assert.Equal(DecodeTier.Unavailable, probe.Tier, name);
Assert.Contains(why, probe.Reason);
}
// What WIC decodes of the same kind it still does: the float TIFF unpredicted, the codes uncompressed.
File.WriteAllBytes(Path.Combine(folder, "float.tif"), TestTiffs.FloatRgb(scene));
File.WriteAllBytes(Path.Combine(folder, "plain.tif"), TestTiffs.Rgb8(scene));
foreach (var name in (string[])["float.tif", "plain.tif"])
{
using var decoded = Assert.NotNull(Provider.Decode(new DecodeRequest(Path.Combine(folder, name))));
AssertClose(scene, decoded.Pixels, name == "float.tif" ? 1e-6 : 5e-3, name);
}
var desktop = Chronos.Core.Imaging.Decoders.DecoderFactory.CreateClient(Provider);
using (var floats = Assert.NotNull(desktop.Decode(new DecodeRequest(Path.Combine(folder, "predicted.tif")))))
{
Assert.Equal("managed-tiff", floats.Decoder);
AssertClose(scene, floats.Pixels, 1e-6, "the predicted float TIFF");
}
foreach (var name in (string[])["zstd.tif", "unknown.tif", "jpeg2000.tif", "int32.tif"]) Assert.Null(desktop.Decode(new DecodeRequest(Path.Combine(folder, name))));
var frames = refused.Select((f, i) => new Chronos.Core.Archiving.SidecarFrame
{
Id = Guid.NewGuid(),
I = i,
File = f.Name,
Sha256 = new string('a', 64),
W = 12,
H = 8,
Format = ImageFileFormat.Tiff,
}).ToArray();
var archive = new Chronos.Core.Archiving.ArchivePaths(context.CreateTempDirectory()).Sequence("roof");
Directory.CreateDirectory(archive.Folder);
foreach (var (name, bytes, _) in refused) File.WriteAllBytes(archive.File(name), bytes);
var manifest = new Chronos.Core.Archiving.SequenceManifest { Id = Guid.NewGuid(), Frames = frames };
var plan = Chronos.Core.Conversion.ConvertPlanner.Plan(manifest, archive, [.. frames.Select(f => f.File)],
new Chronos.Core.Conversion.ConvertOptions(Chronos.Core.Conversion.ConvertTarget.Jpeg), desktop, "Roof");
Assert.SequenceEqual([3], plan.Frames.Select(f => f.Index));
Assert.SequenceEqual([(Chronos.Core.Conversion.ConvertSkip.NoNativeDecoder, 4)], plan.Skipped.Select(s => (s.Reason, s.Frames)));
}
/// <summary>
/// A conversion's WIC probe reads what WIC makes of the file's headers (owner review 2, R7): native at the first frame's
/// oriented size, so the plan's size bound applies to what only WIC reads; unavailable, with why, for what it does not
/// decode. Other probes trust the format and open nothing. A file another program holds, or one gone, is a read error,
/// never a verdict on its content.
/// </summary>
[Test]
public void HeaderProbesSayWhatWicDecodesAtItsSize()
{
var folder = context.CreateTempDirectory();
var reading = Provider.WithHeaderProbes();
Assert.True(((WicDecoderProvider)reading).ReadsHeaders);
Assert.Same(reading, reading.WithHeaderProbes());
Assert.False(Provider.ReadsHeaders);
var rotated = Path.Combine(folder, "rotated.jpg");
WicTestImages.WriteGrayJpeg(rotated, 64, 32, (x, _) => (byte)x, exifOrientation: 6);
var png = Path.Combine(folder, "frame.png");
WicTestImages.WriteRgbPng(png, 40, 30, (_, _) => (128, 128, 128));
var tiff = Path.Combine(folder, "frame.tif");
using (var scene = Scene(12, 8)) File.WriteAllBytes(tiff, Chronos.Core.Codecs.Tiff.TiffWriter.Write(scene));
foreach (var (path, format, width, height) in ((string, ImageFileFormat, int, int)[])
[(rotated, ImageFileFormat.Jpeg, 32, 64), (png, ImageFileFormat.Png, 40, 30), (tiff, ImageFileFormat.Tiff, 12, 8)])
{
var probe = reading.Probe(path, format);
Assert.Equal((DecodeTier.Native, "wic", width, height), (probe.Tier, probe.Decoder, probe.Width, probe.Height), Path.GetFileName(path));
}
// Without header reads: the format's tier, nothing opened.
var trusted = Provider.Probe(Path.Combine(folder, "none.jpg"), ImageFileFormat.Jpeg);
Assert.Equal((DecodeTier.Native, 0, 0), (trusted.Tier, trusted.Width, trusted.Height));
var noise = Path.Combine(folder, "noise.png");
File.WriteAllBytes(noise, [.. Enumerable.Range(0, 4096).Select(i => (byte)(i * 37))]);
Assert.Equal(DecodeTier.Unavailable, reading.Probe(noise, ImageFileFormat.Png).Tier);
Assert.Throws<FileNotFoundException>(() => reading.Probe(Path.Combine(folder, "gone.png"), ImageFileFormat.Png));
using (new FileStream(png, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
Assert.Throws<IOException>(() => reading.Probe(png, ImageFileFormat.Png));
// A CMYK strip only WIC reads, 70000 pixels wide, whose sidecar records no size: too large for a JPEG, said by the plan
// rather than by a conversion failing at every retry.
var archive = new Chronos.Core.Archiving.ArchivePaths(context.CreateTempDirectory()).Sequence("roof");
Directory.CreateDirectory(archive.Folder);
File.WriteAllBytes(archive.File("wide.tif"), CmykTiff(70_000, 1, [0, 255, 255, 0]));
var sized = reading.Probe(archive.File("wide.tif"), ImageFileFormat.Tiff);
Assert.Equal((DecodeTier.Native, 70_000, 1), (sized.Tier, sized.Width, sized.Height));
var frame = new Chronos.Core.Archiving.SidecarFrame { Id = Guid.NewGuid(), File = "wide.tif", Sha256 = new string('a', 64), Format = ImageFileFormat.Tiff };
var desktop = Chronos.Core.Imaging.Decoders.DecoderFactory.CreateClient(Provider);
Assert.Equal(Chronos.Core.Conversion.ConvertSkip.TooLarge, Chronos.Core.Conversion.ConvertPlanner.Skip(frame, archive, new HashSet<string>(["wide.tif"]),
new Chronos.Core.Conversion.ConvertOptions(Chronos.Core.Conversion.ConvertTarget.Jpeg), desktop));
Assert.Null(Chronos.Core.Conversion.ConvertPlanner.Skip(frame, archive, new HashSet<string>(["wide.tif"]),
new Chronos.Core.Conversion.ConvertOptions(Chronos.Core.Conversion.ConvertTarget.LinearDng), desktop));
}
/// <summary>A gradient with saturated patches, linear light, every value between 0 and 1.</summary>
internal static ImageBuffer Scene(int width, int height)
{
var image = new ImageBuffer(width, height);
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
image[0, x, y] = 0.02f + 0.7f * x / width;
image[1, x, y] = 0.05f + 0.5f * y / height;
image[2, x, y] = 0.1f + 0.3f * ((x / 4 + y / 4) % 2);
}
}
(image[0, 0, 0], image[1, 0, 0], image[2, 0, 0]) = (0.02f, 0.9f, 0.02f);
(image[0, 1, 0], image[1, 1, 0], image[2, 1, 0]) = (0.9f, 0.02f, 0.02f);
(image[0, 2, 0], image[1, 2, 0], image[2, 2, 0]) = (0.001f, 0.004f, 0.0005f);
return image;
}
private static void AssertClose(ImageBuffer expected, ImageBuffer actual, double tolerance, string what)
{
Assert.Equal((expected.Width, expected.Height), (actual.Width, actual.Height), what);
for (var c = 0; c < 3; c++)
{
for (var i = 0; i < expected.PlaneLength; i++) Assert.Approximately(expected.Plane(c)[i], actual.Plane(c)[i], tolerance, what);
}
}
/// <summary>An uncompressed little-endian TIFF of <paramref name="width"/> × <paramref name="height"/> CMYK pixels, each <paramref name="inks"/>, without a profile.</summary>
private static byte[] CmykTiff(int width, int height, byte[] inks)
{