feat: owner review 2 phase 4b: mass conversion after import (#13)
CI / Build and test (push) Failing after 3m1s
CI / PostgreSQL 16 integration (push) Skipped
CI / PostgreSQL 17 integration (push) Skipped
CI / Docker image (push) Skipped
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 10s
Release / Build, test and publish (push) Successful in 15m31s
Release / Server image (push) Successful in 1m13s

Convert photos already in the archive to JPEG, TIFF-16 or Linear DNG, from the panel's Library and the desktop's Archive and Library. Each run asks Replace (same frames, originals to the trash, recoverable) or New sequence. A dry run lists what converts, what is skipped and why, size before and after, and free space; the job converts exactly the confirmed photos, resumes, and commits all or nothing per sequence. A conversion never changes what Chronos reads from a photo. Replaced originals are erased only on the archive's own word that their conversion is gone. Desktop TIFFs that WIC showed black or as noise now go to the managed reader; the Edit picker no longer spins a core.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
This commit was merged in pull request #13.
This commit is contained in:
Alby96andClaude Opus 5.5 authored and Alby96 committed 2026-11-03 10:41:50 +01:00
1 parent 7e4a3b481d
commit efa87f68ce
354 files changed
+24530 -843

No files matched your search

@@ -39,6 +39,9 @@ public static class ArchiveLayout
/// <summary>File renames of a Reorganize that were started but not yet recorded in <see cref="SequenceManifestFile"/> and the index.</summary>
public const string FileRenameJournalFile = "rename-files.json";
/// <summary>Moves of a frame replacement (a conversion, or its undo) that were started but not yet settled in <see cref="SequenceManifestFile"/> and the trash.</summary>
public const string ReplaceJournalFile = "replace.pending.json";
/// <summary>Folder names inside a sequence folder that never hold a sequence of their own.</summary>
public static bool IsReservedFolder(string name) =>
name.Equals(MetaFolder, StringComparison.OrdinalIgnoreCase)
@@ -108,6 +111,7 @@ public sealed class SequencePaths
public string RemovalJournalPath => Path.Combine(MetaDirectory, ArchiveLayout.RemovalJournalFile);
public string RestoreJournalPath => Path.Combine(MetaDirectory, ArchiveLayout.RestoreJournalFile);
public string FileRenameJournalPath => Path.Combine(MetaDirectory, ArchiveLayout.FileRenameJournalFile);
public string ReplaceJournalPath => Path.Combine(MetaDirectory, ArchiveLayout.ReplaceJournalFile);
public string Proxy(int index) => Path.Combine(Proxies, ArchiveLayout.ProxyFileName(index));
+124 -5
View File
@@ -2,6 +2,7 @@ using System.Buffers.Binary;
using Chronos.Core.Codecs.Jpeg;
using Chronos.Core.Codecs.Tiff;
using Chronos.Core.Imaging;
using Chronos.Core.Mathematics;
using Chronos.Core.Metadata.Tiff;
namespace Chronos.Core.Codecs.Dng;
@@ -33,15 +34,29 @@ public sealed record LinearDngOptions
/// <summary>Worker limit for tile encoding (-1 = all cores).</summary>
public int MaxDegreeOfParallelism { get; init; } = -1;
/// <summary>
/// Keeps scene-referred data whole, as a conversion's archival copy needs (owner review 2, R7): the pixels are stored in
/// linear ProPhoto (ROMM) primaries, where colours outside sRGB stay positive, divided by the power of two (at most
/// 2^<see cref="LinearDngWriter.MaxHeadroomStops"/>) that brings their peak below the white level, which BaselineExposure
/// gives back; ColorMatrix1 and ForwardMatrix1 describe that space. Off (exports): linear sRGB primaries, values
/// outside 0..1 clipped.
/// </summary>
public bool SceneReferred { get; init; }
}
/// <summary>
/// Writes linear Rec.709/sRGB-primaries RGB as a 16-bit LinearRaw DNG 1.4 that raw editors render with correct
/// colours: ColorMatrix1 = XYZ(D65) → linear sRGB, ForwardMatrix1 = linear sRGB → XYZ(D50), illuminant D65,
/// AsShotNeutral 1/1/1, WhiteLevel 65535, BaselineExposure 0. Values above 1.0 are clipped.
/// AsShotNeutral 1/1/1, WhiteLevel 65535, BaselineExposure 0. Values above 1.0 are clipped. With
/// <see cref="LinearDngOptions.SceneReferred"/> the data are stored in linear ProPhoto primaries with headroom instead,
/// so highlights above 1 and colours outside sRGB survive.
/// </summary>
public static class LinearDngWriter
{
/// <summary>Most stops of headroom a scene-referred DNG keeps above diffuse white (its data then use 12 of their 16 bits below it).</summary>
public const int MaxHeadroomStops = 4;
/// <summary>XYZ (D65) → linear sRGB, i.e. the inverse of the sRGB → XYZ matrix (IEC 61966-2-1).</summary>
internal static readonly double[] XyzToLinearSrgb =
[
@@ -85,13 +100,18 @@ public static class LinearDngWriter
var start = output.Position;
output.Write("II*\0\0\0\0\0"u8);
var parallel = new ParallelOptions { MaxDegreeOfParallelism = o.MaxDegreeOfParallelism };
var scene = o.SceneReferred ? SceneEncoding.For(image, parallel) : null;
var band = new ushort[w * t * 3];
var tiles = new byte[across][];
for (var ty = 0; ty < down; ty++)
{
var y0 = ty * t;
var rows = Math.Min(t, h - y0);
Parallel.For(0, rows, parallel, r => PixelConvert.EncodeRow16(image, y0 + r, band.AsSpan(r * w * 3, w * 3), 3, TransferFunction.Linear));
Parallel.For(0, rows, parallel, r =>
{
if (scene is null) PixelConvert.EncodeRow16(image, y0 + r, band.AsSpan(r * w * 3, w * 3), 3, TransferFunction.Linear);
else scene.EncodeRow(image, y0 + r, band.AsSpan(r * w * 3, w * 3));
});
Parallel.For(0, across, parallel, tx => tiles[tx] = EncodeTile(band, w, rows, tx * t, t, o.Compression));
for (var tx = 0; tx < across; tx++)
{
@@ -124,11 +144,11 @@ public static class LinearDngWriter
ifd.Bytes(TiffTags.DngVersion, 1, 4, 0, 0);
ifd.Bytes(TiffTags.DngBackwardVersion, 1, 1, 0, 0);
ifd.Ascii(TiffTags.UniqueCameraModel, o.UniqueCameraModel);
ifd.SRational(TiffTags.ColorMatrix1, Rationals(XyzToLinearSrgb));
ifd.SRational(TiffTags.ForwardMatrix1, Rationals(LinearSrgbToXyzD50));
ifd.SRational(TiffTags.ColorMatrix1, Rationals(scene is null ? XyzToLinearSrgb : SceneEncoding.ColorMatrix));
ifd.SRational(TiffTags.ForwardMatrix1, Rationals(scene is null ? LinearSrgbToXyzD50 : SceneEncoding.ForwardMatrix));
ifd.Short(TiffTags.CalibrationIlluminant1, 21); // D65
ifd.Rational(TiffTags.AsShotNeutral, (1u, 1u), (1u, 1u), (1u, 1u));
ifd.SRational(TiffTags.BaselineExposure, (0, 1));
ifd.SRational(TiffTags.BaselineExposure, (scene?.Stops ?? 0, 1));
ifd.Long(TiffTags.WhiteLevel, 65535, 65535, 65535);
TiffWriter.WriteDirectory(output, start, ifd);
}
@@ -152,4 +172,103 @@ public static class LinearDngWriter
}
private static (int, int)[] Rationals(double[] matrix) => matrix.Select(v => ((int)Math.Round(v * 10000), 10000)).ToArray();
/// <summary>The values a matrix tag holds once written (<see cref="Rationals"/>): what a reader computes with.</summary>
private static double[] Written(double[] matrix) => [.. matrix.Select(v => Math.Round(v * 10000) / 10000)];
private static double[] Elements(Matrix3 m) => [m.M11, m.M12, m.M13, m.M21, m.M22, m.M23, m.M31, m.M32, m.M33];
private static Matrix3 Matrix(double[] m) => new(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]);
/// <summary>
/// The scene-referred encoding of one image (<see cref="LinearDngOptions.SceneReferred"/>): linear Rec.709 to linear
/// ProPhoto, white-balanced so (1, 1, 1) stays the scene white, divided by 2^<see cref="Stops"/>.
/// </summary>
private sealed class SceneEncoding
{
/// <summary>Stored values stay below this fraction of the white level, under the 0.999 a reader takes for clipped.</summary>
private const double Ceiling = 0.998;
/// <summary>Linear ProPhoto (ROMM RGB, D50) to XYZ (D50), as written.</summary>
public static readonly double[] ForwardMatrix = Written(Elements(
Matrix3.RgbToXyz(new Chromaticity(0.7347, 0.2653), new Chromaticity(0.1596, 0.8404), new Chromaticity(0.0366, 0.0001), Chromaticity.D50)));
/// <summary>XYZ (D65) to linear ProPhoto with the D65 white on (1, 1, 1), as written.</summary>
public static readonly double[] ColorMatrix = Written(Elements(
Matrix(ForwardMatrix).Inverse() * Matrix3.BradfordAdaptation(Chromaticity.D65.ToXyz(), Chromaticity.D50.ToXyz())));
/// <summary>
/// Linear Rec.709 to the stored space: the inverse of the matrix a DNG reader develops these tags with (the in-house
/// reader's own solve), so the image reads back as it was written, to the 16-bit step.
/// </summary>
private static readonly Matrix3 FromRec709 = new DngColorProfile
{
ColorMatrix1 = Matrix(ColorMatrix),
ForwardMatrix1 = Matrix(ForwardMatrix),
CalibrationIlluminant1 = 21,
AsShotNeutral = new Vector3D(1, 1, 1),
}.Solve([]).BalancedToRec709.Inverse();
private readonly float[] _matrix;
private SceneEncoding(int stops)
{
Stops = stops;
var scale = 1 / double.Exp2(stops);
_matrix = [.. Elements(FromRec709).Select(v => (float)(v * scale))];
}
/// <summary>The stops of headroom: BaselineExposure.</summary>
public int Stops { get; }
/// <summary>The encoding of <paramref name="image"/>: the fewest stops that bring its peak in the stored space below the ceiling.</summary>
public static SceneEncoding For(ImageBuffer image, ParallelOptions parallel)
{
var m = FromRec709;
var peak = 0.0;
var gate = new object();
Parallel.For(0, image.Height, parallel, () => 0.0, (y, _, local) =>
{
Rows(image, y, out var r, out var g, out var b);
for (var x = 0; x < r.Length; x++)
{
double vr = r[x], vg = g[x], vb = b[x];
local = Math.Max(local, m.M11 * vr + m.M12 * vg + m.M13 * vb);
local = Math.Max(local, m.M21 * vr + m.M22 * vg + m.M23 * vb);
local = Math.Max(local, m.M31 * vr + m.M32 * vg + m.M33 * vb);
}
return local;
}, local =>
{
lock (gate) peak = Math.Max(peak, local);
});
var stops = 0;
while (stops < MaxHeadroomStops && !(peak / double.Exp2(stops) < Ceiling)) stops++;
return new SceneEncoding(stops);
}
/// <summary>Row <paramref name="y"/> as interleaved 16-bit stored values.</summary>
public void EncodeRow(ImageBuffer image, int y, Span<ushort> row)
{
var m = _matrix;
Rows(image, y, out var r, out var g, out var b);
for (var x = 0; x < r.Length; x++)
{
float vr = r[x], vg = g[x], vb = b[x];
row[3 * x] = Sample(m[0] * vr + m[1] * vg + m[2] * vb);
row[3 * x + 1] = Sample(m[3] * vr + m[4] * vg + m[5] * vb);
row[3 * x + 2] = Sample(m[6] * vr + m[7] * vg + m[8] * vb);
}
}
private static ushort Sample(float v) => (ushort)(Math.Clamp(float.IsNaN(v) ? 0f : v, 0f, 1f) * 65535f + 0.5f);
/// <summary>The red, green and blue rows of <paramref name="image"/> (gray: its one plane three times).</summary>
private static void Rows(ImageBuffer image, int y, out Span<float> r, out Span<float> g, out Span<float> b)
{
r = image.Row(0, y);
g = image.Channels >= 3 ? image.Row(1, y) : r;
b = image.Channels >= 3 ? image.Row(2, y) : r;
}
}
}
+469
View File
@@ -0,0 +1,469 @@
using System.Buffers.Binary;
using System.Text;
using Chronos.Core.Imaging;
using Chronos.Core.Mathematics;
namespace Chronos.Core.Codecs.Icc;
/// <summary>
/// A tone curve of an ICC profile (ICC.1:2010 §10.6 curveType, §10.18 parametricCurveType): device code value 0..1 →
/// linear 0..1. A curveType with no entry is the identity, with one a gamma (u8Fixed8), with more a table interpolated
/// linearly; a parametricCurveType is one of the functions 0–4.
/// </summary>
public sealed class IccCurve
{
/// <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 IccCurve(int function, double[] parameters, double[]? table)
{
_function = function;
_parameters = parameters;
_table = table;
}
/// <summary>Whether the curve is the sRGB EOTF (<see cref="Srgb"/>), within <see cref="SrgbTolerance"/> at 65 code values.</summary>
public bool IsSrgb
{
get
{
for (var i = 0; i <= 64; i++)
{
var code = i / 64.0;
if (!(Math.Abs(Evaluate(code) - ColorSpace.SrgbToLinear(code)) <= SrgbTolerance)) return false;
}
return true;
}
}
/// <summary>A pure power law.</summary>
public static IccCurve Gamma(double gamma) => new(0, [gamma], null);
/// <summary>The sRGB (IEC 61966-2-1) electro-optical transfer function.</summary>
public static IccCurve Srgb { get; } = new(3, [2.4, 1 / 1.055, 0.055 / 1.055, 1 / 12.92, 0.04045], null);
/// <summary>The linear value of code value <paramref name="code"/> (clamped to 0..1).</summary>
public double Evaluate(double code)
{
var x = double.IsNaN(code) ? 0 : Math.Clamp(code, 0, 1);
if (_table is { } table)
{
var t = x * (table.Length - 1);
var i = Math.Min((int)t, table.Length - 2);
return table[i] + (table[i + 1] - table[i]) * (t - i);
}
double P(int i) => i < _parameters.Length ? _parameters[i] : 0;
double g = P(0), a = P(1), b = P(2), c = P(3), d = P(4), e = P(5), f = P(6);
var y = _function switch
{
0 => Math.Pow(x, g),
1 => a != 0 && x >= -b / a ? Math.Pow(Math.Max(0, a * x + b), g) : 0,
2 => a != 0 && x >= -b / a ? Math.Pow(Math.Max(0, a * x + b), g) + c : c,
3 => x >= d ? Math.Pow(Math.Max(0, a * x + b), g) : c * x,
_ => x >= d ? Math.Pow(Math.Max(0, a * x + b), g) + e : c * x + f,
};
return double.IsFinite(y) ? Math.Clamp(y, 0, 1) : 0;
}
/// <summary>The curve of a curveType or parametricCurveType tag, or null when the tag is neither or is malformed.</summary>
internal static IccCurve? Parse(ReadOnlySpan<byte> tag)
{
if (tag.Length < 12) return null;
var type = tag[..4];
if (type.SequenceEqual("curv"u8))
{
var count = BinaryPrimitives.ReadUInt32BigEndian(tag[8..]);
if (count == 0) return Gamma(1);
if (count > (uint)(tag.Length - 12) / 2) return null;
if (count == 1) return BinaryPrimitives.ReadUInt16BigEndian(tag[12..]) is var gamma and > 0 ? Gamma(gamma / 256.0) : null;
var table = new double[count];
for (var i = 0; i < table.Length; i++) table[i] = BinaryPrimitives.ReadUInt16BigEndian(tag[(12 + 2 * i)..]) / 65535.0;
return new IccCurve(-1, [], table);
}
if (!type.SequenceEqual("para"u8)) return null;
var function = BinaryPrimitives.ReadUInt16BigEndian(tag[8..]);
int[] counts = [1, 3, 4, 5, 7];
if (function >= counts.Length || tag.Length < 12 + 4 * counts[function]) return null;
var parameters = new double[counts[function]];
for (var i = 0; i < parameters.Length; i++) parameters[i] = IccProfile.S15Fixed16(tag[(12 + 4 * i)..]);
return new IccCurve(function, parameters, null);
}
/// <summary>This curve as a version 2 curveType tag: a gamma when it is one that u8Fixed8 holds, else a 1024-entry table.</summary>
internal byte[] Encode()
{
if (_table is null && _function == 0 && Math.Abs(_parameters[0] * 256 - Math.Round(_parameters[0] * 256)) < 1e-9)
{
var tag = new byte[14];
"curv"u8.CopyTo(tag);
BinaryPrimitives.WriteUInt32BigEndian(tag.AsSpan(8), 1);
BinaryPrimitives.WriteUInt16BigEndian(tag.AsSpan(12), (ushort)Math.Round(_parameters[0] * 256));
return tag;
}
const int entries = 1024;
var table = new byte[12 + 2 * entries];
"curv"u8.CopyTo(table);
BinaryPrimitives.WriteUInt32BigEndian(table.AsSpan(8), entries);
for (var i = 0; i < entries; i++)
BinaryPrimitives.WriteUInt16BigEndian(table.AsSpan(12 + 2 * i), (ushort)Math.Round(Evaluate(i / (double)(entries - 1)) * 65535));
return table;
}
}
/// <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, 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
{
/// <summary>Largest profile read: big camera and printer profiles stay well below.</summary>
public const int MaxBytes = 4 << 20;
private const uint Rgb = 0x52474220, Gray = 0x47524159, Xyz = 0x58595A20;
private static readonly uint[] RgbColorants = [Tag("rXYZ"), Tag("gXYZ"), Tag("bXYZ")];
private static readonly uint[] RgbCurves = [Tag("rTRC"), Tag("gTRC"), Tag("bTRC")];
/// <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 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 = IccCurve.SrgbTolerance;
private readonly Matrix3? _toLinearRec709;
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;
// 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>
public byte[] Data { get; }
/// <summary>The data colour space signature ("RGB ", "GRAY", "CMYK"…) as a big-endian four-character code.</summary>
public uint ColorSpace { get; }
public bool IsRgb => ColorSpace == Rgb;
public bool IsGray => ColorSpace == Gray;
/// <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 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,
/// a matrix that cannot be inverted).
/// </summary>
public Matrix3? Colorants { get; }
/// <summary>The tone curves: red, green and blue of a matrix/TRC profile, or the gray one; null when absent.</summary>
public IReadOnlyList<IccCurve>? Curves { get; }
/// <summary>Whether pixels in this profile's space convert to another through its matrix and curves alone.</summary>
public bool IsMatrixShaper => IsRgb && Colorants is not null && Curves is { Count: 3 };
/// <summary>Whether this is a gray profile with its tone curve.</summary>
public bool IsGrayCurve => IsGray && Curves is { Count: 1 };
/// <summary>Adobe RGB (1998) as a camera marks it in its Exif (ColorSpace uncalibrated, Interop index "R03").</summary>
public static IccProfile AdobeRgb { get; } = Synthesize("Compatible with Adobe RGB (1998)", new(0.64, 0.33), new(0.21, 0.71), new(0.15, 0.06), IccCurve.Gamma(563 / 256.0));
/// <summary>Display P3 (P3 primaries, D65, the sRGB curve) as a HEIF nclx box names it (colour primaries 12).</summary>
public static IccProfile DisplayP3 { get; } = Synthesize("Display P3", new(0.680, 0.320), new(0.265, 0.690), new(0.150, 0.060), IccCurve.Srgb);
/// <summary>
/// Linear RGB of a matrix/TRC profile → linear Rec.709 (D65): its colorants, adapted from the white they add up to
/// (the profile's white in the PCS) to D65, so neutrals stay neutral. Checked when the profile is parsed: finite and
/// invertible.
/// </summary>
/// <exception cref="InvalidOperationException">The profile is not a matrix/TRC one.</exception>
public Matrix3 ToLinearRec709() => _toLinearRec709 ?? throw new InvalidOperationException("Only a matrix/TRC profile has a matrix.");
/// <summary>
/// <see cref="ToLinearRec709"/> of <paramref name="colorants"/>, or null when they convert nothing: the white they add up
/// to has a cone response that is not positive (Bradford cannot adapt it), or the matrix is not finite or not
/// invertible (colours would collapse).
/// </summary>
private static Matrix3? LinearRec709(Matrix3 colorants)
{
var white = colorants * new Vector3D(1, 1, 1);
var cones = Matrix3.Bradford * white;
if (!(cones.X > 0 && cones.Y > 0 && cones.Z > 0) || !double.IsFinite(cones.X + cones.Y + cones.Z)) return null;
var m = Matrix3.XyzToRec709 * Matrix3.BradfordAdaptation(white, Chromaticity.D65.ToXyz()) * colorants;
double[] elements = [m.M11, m.M12, m.M13, m.M21, m.M22, m.M23, m.M31, m.M32, m.M33];
if (!elements.All(double.IsFinite) || !m.TryInvert(out var inverse)) return null;
double[] inverted = [inverse.M11, inverse.M12, inverse.M13, inverse.M21, inverse.M22, inverse.M23, inverse.M31, inverse.M32, inverse.M33];
return inverted.All(double.IsFinite) ? m : null;
}
/// <summary>Whether a matrix to linear Rec.709 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];
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
/// that does not fit, or a broken tag table). Tags it does not use are not checked.</summary>
public static IccProfile? Parse(byte[] data)
{
ArgumentNullException.ThrowIfNull(data);
if (data.Length < 132 || data.Length > MaxBytes) return null;
var span = data.AsSpan();
var size = BinaryPrimitives.ReadUInt32BigEndian(span);
if (size < 132 || size > data.Length || !span.Slice(36, 4).SequenceEqual("acsp"u8)) return null;
span = span[..(int)size];
var count = BinaryPrimitives.ReadUInt32BigEndian(span[128..]);
if (count > (span.Length - 132) / 12) return null;
var tags = new Dictionary<uint, (int Offset, int Length)>();
for (var i = 0; i < count; i++)
{
var entry = span.Slice(132 + 12 * i, 12);
var signature = BinaryPrimitives.ReadUInt32BigEndian(entry);
var offset = BinaryPrimitives.ReadUInt32BigEndian(entry[4..]);
var length = BinaryPrimitives.ReadUInt32BigEndian(entry[8..]);
if (offset < 128 || length > span.Length || offset > span.Length - length) continue;
tags.TryAdd(signature, ((int)offset, (int)length));
}
ReadOnlySpan<byte> TagData(uint signature, ReadOnlySpan<byte> all) =>
tags.TryGetValue(signature, out var t) ? all.Slice(t.Offset, t.Length) : default;
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;
if (pcs == Xyz && colorSpace == Rgb)
{
var columns = new Vector3D[3];
var matrix = true;
for (var c = 0; c < 3 && matrix; c++)
{
if (XyzValue(TagData(RgbColorants[c], span)) is { } column) columns[c] = column;
else matrix = false;
}
curves = new IccCurve[3];
for (var c = 0; c < 3 && curves is not null; c++)
{
if (IccCurve.Parse(TagData(RgbCurves[c], span)) is { } curve) curves[c] = curve;
else curves = null;
}
// Colorants that convert nothing (a damaged or odd profile) leave a profile that can be carried, not converted.
if (matrix && LinearRec709(Matrix3.FromColumns(columns[0], columns[1], columns[2])) is { } m)
{
colorants = Matrix3.FromColumns(columns[0], columns[1], columns[2]);
toLinearRec709 = m;
}
}
else if (pcs == Xyz && colorSpace == Gray && IccCurve.Parse(TagData(Tag("kTRC"), span)) is { } gray)
{
curves = [gray];
}
return new IccProfile(data, colorSpace, description, mediaWhite, colorantTags, colorants, toLinearRec709, curves);
}
/// <summary>
/// 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>
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);
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);
text.CopyTo(desc.AsSpan(12));
var cprt = Encoding.ASCII.GetBytes("text\0\0\0\0No copyright, use freely\0");
var trc = curve.Encode();
(uint Signature, byte[] Data)[] tags =
[
(Tag("desc"), desc),
(Tag("cprt"), cprt),
(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))),
(Tag("rTRC"), trc),
(Tag("gTRC"), trc),
(Tag("bTRC"), trc),
];
using var body = new MemoryStream();
var offsets = new Dictionary<byte[], int>(ReferenceEqualityComparer.Instance);
var start = 128 + 4 + 12 * tags.Length;
var table = new byte[4 + 12 * tags.Length];
BinaryPrimitives.WriteUInt32BigEndian(table, (uint)tags.Length);
for (var i = 0; i < tags.Length; i++)
{
var (signature, data) = tags[i];
if (!offsets.TryGetValue(data, out var offset))
{
offset = start + (int)body.Length;
offsets[data] = offset;
body.Write(data);
while (body.Length % 4 != 0) body.WriteByte(0);
}
BinaryPrimitives.WriteUInt32BigEndian(table.AsSpan(4 + 12 * i), signature);
BinaryPrimitives.WriteUInt32BigEndian(table.AsSpan(8 + 12 * i), (uint)offset);
BinaryPrimitives.WriteUInt32BigEndian(table.AsSpan(12 + 12 * i), (uint)data.Length);
}
var profile = new byte[start + body.Length];
var header = profile.AsSpan(0, 128);
BinaryPrimitives.WriteUInt32BigEndian(header, (uint)profile.Length);
BinaryPrimitives.WriteUInt32BigEndian(header[8..], 0x02100000);
"mntrRGB XYZ "u8.CopyTo(header[12..]);
ushort[] date = [2026, 10, 27, 0, 0, 0];
for (var i = 0; i < date.Length; i++) BinaryPrimitives.WriteUInt16BigEndian(header[(24 + 2 * i)..], date[i]);
"acsp"u8.CopyTo(header[36..]);
S15Fixed16(PcsWhite, header[68..]);
table.CopyTo(profile.AsSpan(128));
body.ToArray().CopyTo(profile.AsSpan(start));
return Parse(profile) ?? throw new InvalidOperationException("A synthesized ICC profile did not parse.");
}
internal static double S15Fixed16(ReadOnlySpan<byte> value) => BinaryPrimitives.ReadInt32BigEndian(value) / 65536.0;
private static void S15Fixed16(Vector3D value, Span<byte> destination)
{
BinaryPrimitives.WriteInt32BigEndian(destination, (int)Math.Round(value.X * 65536));
BinaryPrimitives.WriteInt32BigEndian(destination[4..], (int)Math.Round(value.Y * 65536));
BinaryPrimitives.WriteInt32BigEndian(destination[8..], (int)Math.Round(value.Z * 65536));
}
private static byte[] XyzTag(Vector3D value)
{
var tag = new byte[20];
"XYZ "u8.CopyTo(tag);
S15Fixed16(value, tag.AsSpan(8));
return tag;
}
private static Vector3D? XyzValue(ReadOnlySpan<byte> tag)
{
if (tag.Length < 20 || !tag[..4].SequenceEqual("XYZ "u8)) return null;
return new Vector3D(S15Fixed16(tag[8..]), S15Fixed16(tag[12..]), S15Fixed16(tag[16..]));
}
/// <summary>The text of a textDescriptionType (v2 'desc'), multiLocalizedUnicodeType (v4 'mluc', its first record) or
/// textType tag; null for anything else.</summary>
private static string? Text(ReadOnlySpan<byte> tag)
{
if (tag.Length < 12) return null;
var type = tag[..4];
if (type.SequenceEqual("desc"u8))
{
var count = BinaryPrimitives.ReadUInt32BigEndian(tag[8..]);
if (count == 0 || count > (uint)(tag.Length - 12)) return null;
return Encoding.ASCII.GetString(tag.Slice(12, (int)count)).TrimEnd('\0');
}
if (type.SequenceEqual("mluc"u8) && tag.Length >= 28)
{
var records = BinaryPrimitives.ReadUInt32BigEndian(tag[8..]);
var length = BinaryPrimitives.ReadUInt32BigEndian(tag[20..]);
var offset = BinaryPrimitives.ReadUInt32BigEndian(tag[24..]);
if (records == 0 || length > (uint)tag.Length || offset > (uint)tag.Length - length) return null;
return Encoding.BigEndianUnicode.GetString(tag.Slice((int)offset, (int)length & ~1)).TrimEnd('\0');
}
if (type.SequenceEqual("text"u8)) return Encoding.ASCII.GetString(tag[8..]).TrimEnd('\0');
return null;
}
private static uint Tag(string signature) => BinaryPrimitives.ReadUInt32BigEndian(Encoding.ASCII.GetBytes(signature));
}
+278
View File
@@ -0,0 +1,278 @@
using System.Buffers.Binary;
using System.IO.Compression;
using Chronos.Core.Codecs.Tiff;
using Chronos.Core.Imaging;
using Chronos.Core.Metadata;
using Chronos.Core.Metadata.Bmff;
using Chronos.Core.Metadata.Jpeg;
using Chronos.Core.Metadata.Tiff;
namespace Chronos.Core.Codecs.Icc;
/// <summary>
/// What a photo's file says of the colour space its pixel values are in (owner review 2, R7), read from its headers only:
/// nothing (<see cref="Untagged"/>: sRGB, as every decoder reads it), an ICC profile (embedded: JPEG APP2 ICC_PROFILE, PNG
/// iCCP, TIFF InterColorProfile, HEIF/AVIF colr 'prof', WebP ICCP; or the one a marker names: Exif ColorSpace uncalibrated
/// with Interop index "R03" is Adobe RGB, a HEIF nclx box with P3 primaries is Display P3), or something no profile here
/// describes (<see cref="Unknown"/>: a profile that does not parse, Exif uncalibrated without R03, an nclx box with other
/// primaries or an HDR transfer). Camera RAWs and other formats say nothing here: their decoders give Rec.709 pixels.
/// It also says how the decoders take the samples (<see cref="IsLinear"/>).
/// </summary>
public sealed class SourceColor
{
private const int MaxChunks = 4096;
private const ushort Uncalibrated = 0xFFFF, AdobeRgbColorSpace = 2;
private const uint FloatSamples = 3, LinearRaw = 34892;
private static ReadOnlySpan<byte> IccSignature => "ICC_PROFILE\0"u8;
private SourceColor(IccProfile? profile, bool unknown, bool linear = false)
{
Profile = profile;
IsUnknown = unknown;
IsLinear = linear;
}
/// <summary>No colour space recorded: the pixel values are sRGB.</summary>
public static SourceColor Untagged { get; } = new(null, false);
/// <summary>A colour space recorded that no profile here describes.</summary>
public static SourceColor Unknown { get; } = new(null, true);
/// <summary>The profile the pixel values are in; null when untagged or unknown.</summary>
public IccProfile? Profile { get; }
public bool IsUnknown { get; }
/// <summary>
/// Whether the samples are linear light, which the decoders take as they are (a TIFF of floating-point samples, or
/// linear RAW): values in the profile's primaries, not code values through its curves. Otherwise the samples are code
/// values, which the decoders linearise with the sRGB curve.
/// </summary>
public bool IsLinear { get; }
/// <summary>The colour space of the photo at <paramref name="path"/>, whose signature says <paramref name="format"/>.</summary>
/// <exception cref="IOException">The file cannot be read.</exception>
public static SourceColor Read(string path, ImageFileFormat format)
{
ArgumentException.ThrowIfNullOrWhiteSpace(path);
if (!Reads(format)) return Untagged;
using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, 1 << 16, FileOptions.RandomAccess);
return Read(stream, format);
}
/// <inheritdoc cref="Read(string, ImageFileFormat)"/>
/// <remarks>Headers this reader cannot make sense of record a space it does not know (<see cref="Unknown"/>).</remarks>
public static SourceColor Read(Stream stream, ImageFileFormat format)
{
ArgumentNullException.ThrowIfNull(stream);
try
{
return ImageFormatInfo.Of(format).Family switch
{
ImageFamily.Jpeg => Jpeg(stream),
ImageFamily.Png => Png(stream),
ImageFamily.Tiff => Tiff(stream),
ImageFamily.Heif => Heif(stream),
ImageFamily.WebP => WebP(stream),
_ => Untagged,
};
}
catch (Exception ex) when (ex is InvalidDataException or ArgumentException or OverflowException or FormatException)
{
return Unknown;
}
}
/// <summary>Whether files of <paramref name="format"/> are looked into (the formats whose pixel values a profile may describe).</summary>
public static bool Reads(ImageFileFormat format) =>
ImageFormatInfo.Of(format).Family is ImageFamily.Jpeg or ImageFamily.Png or ImageFamily.Tiff or ImageFamily.Heif or ImageFamily.WebP;
private static SourceColor Of(byte[]? icc) => icc is null ? Unknown : IccProfile.Parse(icc) is { } profile ? new SourceColor(profile, false) : Unknown;
/// <summary>APP2 ICC_PROFILE chunks in sequence order (ICC.1 Annex B.4), else the Exif colour space of the first APP1 Exif.</summary>
private static SourceColor Jpeg(Stream stream)
{
var scanner = new JpegParser.SegmentScanner(stream, 0, stream.Length, long.MaxValue);
if (!scanner.StartsWithSoi()) return Untagged;
SortedDictionary<int, byte[]>? chunks = null;
var declared = 0;
long total = 0;
SourceColor? exif = null;
Span<byte> head = stackalloc byte[14];
while (scanner.Next(out var segment))
{
if (segment.Marker is 0xDA or 0xD9) break;
if (segment.Marker == 0xE2 && segment.Length > head.Length && stream.TryReadAt(segment.Payload, head) && head[..12].SequenceEqual(IccSignature))
{
chunks ??= [];
declared = head[13];
total += segment.Length - head.Length;
if (total > IccProfile.MaxBytes) return Unknown;
chunks[head[12]] = stream.ReadAt(segment.Payload + head.Length, segment.Length - head.Length, IccProfile.MaxBytes) ?? [];
}
else if (segment.Marker == 0xE1 && exif is null && segment.Length > 14 && stream.TryReadAt(segment.Payload, head[..6]) && head[..6].SequenceEqual("Exif\0\0"u8))
{
exif = Exif(TiffReader.Open(stream, segment.Payload + 6));
}
}
if (chunks is null) return exif ?? Untagged;
// Every chunk from 1 to the count the chunks declare, once each.
if (declared == 0 || chunks.Count != declared || chunks.Keys.First() != 1 || chunks.Keys.Last() != declared) return Unknown;
return Of([.. chunks.Values.SelectMany(c => c)]);
}
/// <summary>iCCP (zlib-compressed after its name and method byte) or sRGB before the image data.</summary>
private static SourceColor Png(Stream stream)
{
Span<byte> head = stackalloc byte[8];
long position = 8;
for (var i = 0; i < MaxChunks && stream.TryReadAt(position, head); i++)
{
var length = BinaryPrimitives.ReadUInt32BigEndian(head);
var type = head[4..];
var data = position + 8;
if (type.SequenceEqual("IDAT"u8) || type.SequenceEqual("IEND"u8) || length > int.MaxValue) break;
if (type.SequenceEqual("sRGB"u8)) return Untagged;
if (type.SequenceEqual("iCCP"u8))
{
if (stream.ReadAt(data, length, IccProfile.MaxBytes) is not { } chunk) return Unknown;
var name = Array.IndexOf(chunk, (byte)0);
if (name < 1 || name + 2 > chunk.Length || chunk[name + 1] != 0) return Unknown;
return Of(Inflate(chunk.AsMemory(name + 2)));
}
position = data + length + 4;
}
return Untagged;
}
/// <summary>
/// The InterColorProfile of the image the decoders read (<see cref="TiffImageReader.MainImage"/>), else IFD0's, else
/// the colour space IFD0's Exif IFD records; linear when that image holds floating-point samples or linear RAW.
/// </summary>
private static SourceColor Tiff(Stream stream)
{
if (TiffReader.Open(stream) is not { } reader || reader.ReadIfd(reader.FirstIfdOffset) is not { } ifd0) return Untagged;
var main = TiffImageReader.MainImage(reader);
SourceColor? Profile(Ifd ifd) => ifd.Entries.TryGetValue(TiffTags.InterColorProfile, out var entry) ? Of(reader.ReadBytes(entry, IccProfile.MaxBytes)) : null;
var color = Profile(main) ?? Profile(ifd0) ?? Exif(reader, ifd0) ?? Untagged;
var linear = reader.GetUInt(main, TiffTags.SampleFormat) == FloatSamples || reader.GetUInt(main, TiffTags.PhotometricInterpretation) == LinearRaw;
return linear ? new SourceColor(color.Profile, color.IsUnknown, linear: true) : color;
}
/// <summary>The colr properties of the primary item: an ICC profile ('prof', 'rICC') first, else its nclx box.</summary>
private static SourceColor Heif(Stream stream)
{
if (BmffReader.Find(BmffReader.ReadChildren(stream, 0, stream.Length), "meta") is not { } meta) return Untagged;
var children = BmffReader.ReadChildren(stream, meta.PayloadStart + 4, meta.End);
if (BmffReader.Find(children, "pitm") is not { } pitm || BmffReader.Find(children, "iprp") is not { } iprp) return Untagged;
Span<byte> item = stackalloc byte[8];
var read = stream.ReadUpTo(pitm.PayloadStart, item[..(int)Math.Min(item.Length, pitm.PayloadLength)]);
if (read < 6) return Untagged;
uint primary = item[0] == 0 ? BinaryPrimitives.ReadUInt16BigEndian(item[4..]) : read >= 8 ? BinaryPrimitives.ReadUInt32BigEndian(item[4..]) : 0;
var properties = BmffReader.ReadChildren(stream, iprp.PayloadStart, iprp.End);
if (BmffReader.Find(properties, "ipco") is not { } ipco || BmffReader.Find(properties, "ipma") is not { } ipma) return Untagged;
var boxes = BmffReader.ReadChildren(stream, ipco.PayloadStart, ipco.End);
var colours = Associations(stream, ipma, primary).Where(i => i >= 1 && i <= boxes.Count && boxes[i - 1].Is("colr")).ToList();
// A grid whose colour only its tiles carry (as libheif reads it): the colour of the items it is made of.
if (colours.Count == 0) colours = [.. Enumerable.Range(1, boxes.Count).Where(i => boxes[i - 1].Is("colr"))];
SourceColor? nclx = null;
foreach (var index in colours)
{
var colr = boxes[index - 1];
if (stream.ReadAt(colr.PayloadStart, colr.PayloadLength, IccProfile.MaxBytes + 4) is not { Length: >= 4 } data) return Unknown;
var type = data.AsSpan(0, 4);
if (type.SequenceEqual("prof"u8) || type.SequenceEqual("rICC"u8)) return Of(data[4..]);
if (type.SequenceEqual("nclx"u8) && data.Length >= 10) nclx ??= Nclx(BinaryPrimitives.ReadUInt16BigEndian(data.AsSpan(4)), BinaryPrimitives.ReadUInt16BigEndian(data.AsSpan(6)));
}
return nclx ?? Untagged;
}
/// <summary>
/// An nclx box (ISO/IEC 23091-2 code points): BT.709 primaries (or unspecified) with an SDR transfer are sRGB, P3 D65
/// primaries (12) with one are Display P3; other primaries (BT.2020…) or an HDR transfer (PQ, HLG) are unknown here.
/// </summary>
private static SourceColor Nclx(int primaries, int transfer)
{
var sdr = transfer is 1 or 2 or 6 or 13 or 14 or 15;
if (!sdr) return Unknown;
return primaries switch
{
1 or 2 => Untagged,
12 => new SourceColor(IccProfile.DisplayP3, false),
_ => Unknown,
};
}
/// <summary>The ipco indices (1-based) associated with <paramref name="primary"/> in an ipma box.</summary>
private static List<int> Associations(Stream stream, BmffBox ipma, uint primary)
{
var result = new List<int>();
if (stream.ReadAt(ipma.PayloadStart, ipma.PayloadLength, 1 << 20) is not { } data) return result;
var r = new SpanReader(data);
var version = r.U8();
r.Skip(2);
var wide = (r.U8() & 1) != 0;
var count = r.U32();
for (uint i = 0; i < count && r.Ok; i++)
{
var id = version < 1 ? r.U16() : r.U32();
var associations = r.U8();
for (var a = 0; a < associations && r.Ok; a++)
{
var index = wide ? r.U16() & 0x7FFF : r.U8() & 0x7F;
if (id == primary && r.Ok) result.Add(index);
}
}
return result;
}
/// <summary>The ICCP chunk of an extended WebP (RIFF), which comes before the image data.</summary>
private static SourceColor WebP(Stream stream)
{
Span<byte> head = stackalloc byte[12];
if (!stream.TryReadAt(0, head) || !head[..4].SequenceEqual("RIFF"u8) || !head[8..].SequenceEqual("WEBP"u8)) return Untagged;
long position = 12;
for (var i = 0; i < MaxChunks && stream.TryReadAt(position, head[..8]); i++)
{
var size = BinaryPrimitives.ReadUInt32LittleEndian(head[4..]);
if (head[..4].SequenceEqual("ICCP"u8)) return Of(stream.ReadAt(position + 8, size, IccProfile.MaxBytes));
if (head[..4].SequenceEqual("VP8 "u8) || head[..4].SequenceEqual("VP8L"u8) || head[..4].SequenceEqual("ANIM"u8)) break;
position += 8 + size + (size & 1);
}
return Untagged;
}
/// <summary>The colour space an Exif block records: uncalibrated with Interop index "R03" (or the non-standard 2) is Adobe RGB, uncalibrated otherwise unknown; null when it records sRGB or nothing.</summary>
private static SourceColor? Exif(TiffReader? reader) => reader?.ReadIfd(reader.FirstIfdOffset) is { } ifd0 ? Exif(reader, ifd0) : null;
private static SourceColor? Exif(TiffReader reader, Ifd ifd0)
{
if (reader.GetUInt(ifd0, TiffTags.ExifIfd) is not { } offset || reader.ReadIfd(offset) is not { } exif) return null;
var space = reader.GetUInt(exif, TiffTags.ColorSpace);
if (space == AdobeRgbColorSpace) return new SourceColor(IccProfile.AdobeRgb, false);
if (space != Uncalibrated) return null;
var interop = reader.GetUInt(exif, TiffTags.InteropIfd) is { } at && reader.ReadIfd(at) is { } ifd ? reader.GetString(ifd, TiffTags.InteropIndex) : null;
return interop == "R03" ? new SourceColor(IccProfile.AdobeRgb, false) : Unknown;
}
private static byte[]? Inflate(ReadOnlyMemory<byte> compressed)
{
try
{
using var z = new ZLibStream(new MemoryStream(compressed.ToArray(), writable: false), CompressionMode.Decompress);
using var profile = new MemoryStream();
var buffer = new byte[16 * 1024];
int read;
while ((read = z.Read(buffer)) > 0)
{
if (profile.Length + read > IccProfile.MaxBytes) return null;
profile.Write(buffer, 0, read);
}
return profile.ToArray();
}
catch (InvalidDataException)
{
return null;
}
}
}
@@ -20,6 +20,21 @@ public static class JpegDecoder
return parser.Info();
}
/// <summary>
/// The frame header of a stream <see cref="DecodeToLinear"/> decodes, from the markers before its first scan (all of
/// <paramref name="head"/> needs to hold): what it refuses there is refused here too (a coding process or precision it
/// does not decode, more pixels than <see cref="JpegDecodeOptions.MaxPixels"/>, components without a colour
/// interpretation). Damage in the scans shows only when decoding.
/// </summary>
/// <exception cref="InvalidDataException">The headers are malformed, or describe what this decoder does not decode.</exception>
public static JpegInfo ReadDecodableInfo(ReadOnlySpan<byte> head)
{
var info = ReadInfo(head);
var renders = info.Process == JpegProcess.Lossless ? info.Components is 1 or 3 : info.ColorSpace != JpegColorSpace.Other;
if (!renders) throw new InvalidDataException($"JPEG: {info.Components}-component {info.Process} images have no colour interpretation");
return info;
}
/// <summary>Decodes to interleaved gray or RGB samples (8-bit, or 16-bit containers for higher precisions).</summary>
public static JpegImage Decode(ReadOnlySpan<byte> data, JpegDecodeOptions? options = null)
{
@@ -34,6 +34,12 @@ public sealed record JpegEncodeOptions
/// namespace + packet); at most 65533 bytes each. When present the JFIF APP0 segment is omitted (Exif layout).</summary>
public IReadOnlyList<byte[]>? App1Segments { get; init; }
/// <summary>
/// ICC profile of the colour space the encoded values are in, written after the APP1 segments as APP2 "ICC_PROFILE"
/// chunks (ICC.1 Annex B.4: at most 65519 bytes each, numbered from 1); null writes none (sRGB).
/// </summary>
public byte[]? IccProfile { get; init; }
/// <summary>Ordered dither when quantising linear float input to 8-bit sRGB.</summary>
public bool Dither { get; init; }
@@ -407,6 +413,7 @@ public static class JpegEncoder
{
Segment(h, JpegTables.App0, [(byte)'J', (byte)'F', (byte)'I', (byte)'F', 0, 1, 1, 0, 0, 1, 0, 1, 0, 0]);
}
if (o.IccProfile is { Length: > 0 } icc) IccSegments(h, icc);
var tables = p.Components == 1 ? 1 : 2;
var dqt = new byte[65 * tables];
for (var t = 0; t < tables; t++)
@@ -464,6 +471,24 @@ public static class JpegEncoder
s.Write(code.Values);
}
/// <summary>The profile split into APP2 chunks: "ICC_PROFILE\0", the chunk's number from 1, the count, its bytes.</summary>
private static void IccSegments(Stream s, byte[] profile)
{
const int chunk = 65533 - 14;
var count = (profile.Length + chunk - 1) / chunk;
if (count > 255) throw new ArgumentException("ICC profile exceeds 255 APP2 chunks", nameof(profile));
for (var i = 0; i < count; i++)
{
var part = profile.AsSpan(i * chunk, Math.Min(chunk, profile.Length - i * chunk));
var payload = new byte[14 + part.Length];
"ICC_PROFILE\0"u8.CopyTo(payload);
payload[12] = (byte)(i + 1);
payload[13] = (byte)count;
part.CopyTo(payload.AsSpan(14));
Segment(s, JpegTables.App2, payload);
}
}
internal static void Segment(Stream s, byte marker, ReadOnlySpan<byte> payload)
{
var length = payload.Length + 2;
+1 -1
View File
@@ -4,7 +4,7 @@ namespace Chronos.Core.Codecs.Jpeg;
internal static class JpegTables
{
public const byte Soi = 0xD8, Eoi = 0xD9, Sos = 0xDA, Dqt = 0xDB, Dri = 0xDD, Dht = 0xC4, Dac = 0xCC;
public const byte Sof0 = 0xC0, Sof1 = 0xC1, Sof2 = 0xC2, Sof3 = 0xC3, Rst0 = 0xD0, App0 = 0xE0, App1 = 0xE1, App14 = 0xEE;
public const byte Sof0 = 0xC0, Sof1 = 0xC1, Sof2 = 0xC2, Sof3 = 0xC3, Rst0 = 0xD0, App0 = 0xE0, App1 = 0xE1, App2 = 0xE2, App14 = 0xEE;
/// <summary>Natural (row-major) index of the coefficient at each zig-zag position.</summary>
public static ReadOnlySpan<byte> NaturalOrder =>
+4 -1
View File
@@ -44,6 +44,9 @@ public sealed class PngImage
/// </summary>
public static class PngDecoder
{
/// <summary>Largest width × height <see cref="Decode"/> decodes by default.</summary>
public const long MaxPixels = 1L << 28;
private static ReadOnlySpan<byte> Signature => [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
private static readonly (int X, int Y, int Dx, int Dy)[] Adam7 =
@@ -57,7 +60,7 @@ public static class PngDecoder
}
/// <param name="maxPixels">Upper bound on width × height, checked before allocation.</param>
public static PngImage Decode(ReadOnlySpan<byte> data, long maxPixels = 1L << 28)
public static PngImage Decode(ReadOnlySpan<byte> data, long maxPixels = MaxPixels)
{
var info = ReadInfo(data);
if ((long)info.Width * info.Height > maxPixels) throw new InvalidDataException($"PNG: {info.Width}x{info.Height} exceeds the pixel limit");
+75 -14
View File
@@ -74,30 +74,93 @@ public static class TiffImageReader
{
private const int CompressionNone = 1, CompressionLzw = 5, CompressionJpeg = 7, CompressionDeflate = 8;
private const int CompressionAdobeDeflate = 32946, CompressionPackBits = 32773, CompressionLossyJpeg = 34892;
private const int PhotometricMinIsWhite = 0, PhotometricRgb = 2, PhotometricPalette = 3, PhotometricYCbCr = 6;
private const int PhotometricMinIsWhite = 0, PhotometricMinIsBlack = 1, PhotometricRgb = 2, PhotometricPalette = 3, PhotometricYCbCr = 6;
private const int PhotometricCfa = 32803, PhotometricLinearRaw = 34892;
private const int MaxChunks = 1 << 24;
private const long MaxChunkBytes = int.MaxValue / 2;
/// <summary>Decodes the first full-resolution image of a TIFF (or a DNG whose main image is not CFA).</summary>
/// <summary>
/// Decodes the first full-resolution image of a TIFF (or a DNG whose main image is not CFA). An image whose samples are
/// not gray, RGB or linear RAW (palette, CMYK, CIELab, a CFA…) is refused before its pixels are read, never decoded as
/// something else (<see cref="ReadInfo"/>).
/// </summary>
/// <exception cref="InvalidDataException">The file is malformed, or holds what this reader does not decode.</exception>
public static ImageBuffer Decode(Stream stream, TiffDecodeOptions? options = null)
{
var reader = TiffReader.Open(stream) ?? throw new InvalidDataException("TIFF: missing header");
var chain = reader.ReadChain();
if (chain.Count == 0) throw new InvalidDataException("TIFF: no image directory");
var ifd = chain.FirstOrDefault(i => ((reader.GetUInt(i, TiffTags.NewSubfileType) ?? 0) & 1) == 0) ?? chain[0];
var ifd = MainImage(reader);
var o = options ?? TiffDecodeOptions.Default;
var g = Geometry.Read(reader, ifd, o);
Renders(g.IsJpeg && g.Photometric == PhotometricYCbCr ? PhotometricRgb : g.Photometric, g.Spp, g.Format);
return ToImageBuffer(ReadRaster(reader, ifd, o), o);
}
public static ImageBuffer Decode(byte[] data, TiffDecodeOptions? options = null) => Decode(new MemoryStream(data, writable: false), options);
/// <summary>
/// The size of the image <see cref="Decode"/> decodes, from the headers alone: whatever <see cref="Decode"/> refuses
/// before reading pixels is refused here too (a layout, compression, sample format or photometric interpretation it does
/// not read, more samples than <see cref="TiffDecodeOptions.MaxSamples"/>, more strips or tiles than it reads, none at
/// all). Damage in the strips or tiles shows only when decoding.
/// </summary>
/// <exception cref="InvalidDataException">The file is malformed, or holds what <see cref="Decode"/> does not decode.</exception>
public static (int Width, int Height) ReadInfo(Stream stream, TiffDecodeOptions? options = null)
{
var reader = TiffReader.Open(stream) ?? throw new InvalidDataException("TIFF: missing header");
var ifd = MainImage(reader);
var g = Geometry.Read(reader, ifd, options ?? TiffDecodeOptions.Default);
Renders(g.IsJpeg && g.Photometric == PhotometricYCbCr ? PhotometricRgb : g.Photometric, g.Spp, g.Format);
ChunkTables(reader, ifd, g);
return (g.Width, g.Height);
}
/// <summary>The offsets and byte counts of the strips or tiles of <paramref name="ifd"/>.</summary>
/// <exception cref="InvalidDataException">Either table is missing or unreadable (one uncompressed strip may go without its count).</exception>
private static (uint[] Offsets, uint[] Counts) ChunkTables(TiffReader reader, Ifd ifd, Geometry g)
{
var offsets = reader.GetUInts(ifd, g.Tiled ? TiffTags.TileOffsets : TiffTags.StripOffsets) ?? throw new InvalidDataException("TIFF: missing strip/tile offsets");
var counts = reader.GetUInts(ifd, g.Tiled ? TiffTags.TileByteCounts : TiffTags.StripByteCounts);
if (counts is null && g.ChunksPerPlane * g.Planes == 1 && g.Compression == CompressionNone) counts = [(uint)Math.Min(int.MaxValue, g.RowBytes * (long)g.ChunkHeight)];
return (offsets, counts ?? throw new InvalidDataException("TIFF: missing strip/tile byte counts"));
}
/// <summary>The directory <see cref="Decode"/> reads: the first full-resolution one (NewSubfileType without its
/// reduced-resolution bit), else the first.</summary>
/// <exception cref="InvalidDataException">The file has no image directory.</exception>
internal static Ifd MainImage(TiffReader reader)
{
var chain = reader.ReadChain();
if (chain.Count == 0) throw new InvalidDataException("TIFF: no image directory");
return chain.FirstOrDefault(i => ((reader.GetUInt(i, TiffTags.NewSubfileType) ?? 0) & 1) == 0) ?? chain[0];
}
/// <summary>
/// Refuses samples <see cref="ToImageBuffer"/> would not render as what they are: only gray (min-is-black, or integer
/// min-is-white), RGB of at least three samples and linear RAW (one sample, or three and more) are; palette, CFA, CMYK,
/// CIELab, LogLuv and the others are not, nor RGB of fewer than three samples.
/// </summary>
/// <param name="photometric">The photometric interpretation of the samples as decoded (JPEG-coded YCbCr is RGB).</param>
private static void Renders(int photometric, int spp, TiffSampleFormat format)
{
var renders = photometric switch
{
PhotometricMinIsWhite => format == TiffSampleFormat.UnsignedInteger,
PhotometricMinIsBlack => true,
PhotometricRgb => spp >= 3,
PhotometricLinearRaw => spp is 1 or >= 3,
_ => false,
};
if (!renders) throw new InvalidDataException($"TIFF: photometric {photometric} with {spp} {format} samples per pixel is not decoded here");
}
/// <summary>Converts a gray/RGB raster to linear light (min-is-white inverted; extra samples dropped).</summary>
/// <exception cref="InvalidDataException">The samples are not gray, RGB or linear RAW (palette, CFA, CMYK, CIELab…).</exception>
public static ImageBuffer ToImageBuffer(TiffRaster raster, TiffDecodeOptions? options = null)
{
var o = options ?? TiffDecodeOptions.Default;
if (raster.Photometric is PhotometricCfa or PhotometricPalette)
throw new InvalidDataException($"TIFF: photometric {raster.Photometric} needs colour reconstruction (CFA/palette) not done here");
Renders(raster.Photometric, raster.SamplesPerPixel, raster.SampleFormat);
int w = raster.Width, h = raster.Height, spp = raster.SamplesPerPixel;
var colour = spp >= 3 && raster.Photometric is PhotometricRgb or PhotometricLinearRaw;
var channels = colour ? 3 : 1;
@@ -140,11 +203,7 @@ public static class TiffImageReader
var g = Geometry.Read(reader, ifd, o);
var warnings = new List<string>();
var count = g.ChunksPerPlane * g.Planes;
if (count > MaxChunks) throw new InvalidDataException($"TIFF: {count} strips/tiles");
var offsets = reader.GetUInts(ifd, g.Tiled ? TiffTags.TileOffsets : TiffTags.StripOffsets) ?? throw new InvalidDataException("TIFF: missing strip/tile offsets");
var counts = reader.GetUInts(ifd, g.Tiled ? TiffTags.TileByteCounts : TiffTags.StripByteCounts);
if (counts is null && count == 1 && g.Compression == CompressionNone) counts = [(uint)Math.Min(int.MaxValue, g.RowBytes * (long)g.ChunkHeight)];
if (counts is null) throw new InvalidDataException("TIFF: missing strip/tile byte counts");
var (offsets, counts) = ChunkTables(reader, ifd, g);
if (offsets.Length < count || counts.Length < count) warnings.Add($"TIFF: {count} strips/tiles expected, {Math.Min(offsets.Length, counts.Length)} present");
// I/O is sequential (one stream); decoding is parallel.
@@ -245,16 +304,18 @@ public static class TiffImageReader
g.ChunkWidth = (int)Math.Min(r.GetUInt(ifd, TiffTags.TileWidth) ?? 0, 1 << 16);
g.ChunkHeight = (int)Math.Min(r.GetUInt(ifd, TiffTags.TileLength) ?? 0, 1 << 16);
if (g.ChunkWidth == 0 || g.ChunkHeight == 0) throw new InvalidDataException("TIFF: zero tile size");
g.Across = (g.Width + g.ChunkWidth - 1) / g.ChunkWidth;
g.ChunksPerPlane = checked(g.Across * ((g.Height + g.ChunkHeight - 1) / g.ChunkHeight));
}
else
{
g.ChunkWidth = g.Width;
g.ChunkHeight = (int)Math.Clamp(r.GetUInt(ifd, TiffTags.RowsPerStrip) ?? (uint)g.Height, 1, (uint)g.Height);
g.Across = 1;
g.ChunksPerPlane = (g.Height + g.ChunkHeight - 1) / g.ChunkHeight;
}
// Counted in 64 bits: more strips or tiles than this reader takes is refused as data, never as an arithmetic error.
var across = ((long)g.Width + g.ChunkWidth - 1) / g.ChunkWidth;
var perPlane = across * (((long)g.Height + g.ChunkHeight - 1) / g.ChunkHeight);
if (perPlane * g.Planes > MaxChunks) throw new InvalidDataException($"TIFF: {perPlane * g.Planes} strips/tiles");
g.Across = (int)across;
g.ChunksPerPlane = (int)perPlane;
g.RowBytes = ((long)g.ChunkWidth * g.ChunkSpp * g.Bps + 7) / 8;
if (g.RowBytes * g.ChunkHeight > MaxChunkBytes) throw new InvalidDataException("TIFF: strip/tile too large");
return g;
@@ -34,6 +34,9 @@ public sealed record TiffWriteOptions
/// <summary>TIFF-structured Exif block (JPEG APP1 payload) whose capture metadata is embedded.</summary>
public byte[]? Exif { get; init; }
/// <summary>ICC profile of the colour space the samples are in (InterColorProfile, tag 34675); null writes none (sRGB).</summary>
public byte[]? IccProfile { get; init; }
/// <summary>Worker limit for strip encoding (-1 = all cores).</summary>
public int MaxDegreeOfParallelism { get; init; } = -1;
}
@@ -171,6 +174,7 @@ public static class TiffWriter
ifd.Ascii(TiffTags.Software, "Chronos");
if (predictor) ifd.Short(TiffTags.Predictor, 2);
if (channels == 4) ifd.Short(TiffTags.ExtraSamples, 2);
if (o.IccProfile is { Length: > 0 } icc) ifd.Add(TiffTags.InterColorProfile, TiffType.Undefined, icc.Length, icc);
if (o.Exif is { } exif) ifd.AttachExif(exif);
WriteDirectory(output, start, ifd);
}
@@ -0,0 +1,104 @@
using System.Globalization;
using Chronos.Core.Archiving;
using Chronos.Core.Imaging;
using Chronos.Core.Naming;
namespace Chronos.Core.Conversion;
/// <summary>The format a mass conversion writes (owner review 2, R7; BRIEF §12.5).</summary>
public enum ConvertTarget
{
/// <summary>Baseline JPEG, 8-bit sRGB, 4:4:4 chroma, quality 1–100 (<see cref="ConvertOptions.Quality"/>).</summary>
Jpeg,
/// <summary>TIFF, 16-bit sRGB RGB, Deflate with the horizontal predictor.</summary>
Tiff16,
/// <summary>Linear DNG (LinearRaw, 16-bit, lossless JPEG tiles): the archival choice.</summary>
LinearDng,
}
/// <summary>What a conversion does with the frames it converts; chosen at every run (owner review 2, R7).</summary>
public enum ConvertMode
{
/// <summary>The converted files become the sequence's frames (same ids and indices); the old files go to the archive's trash.</summary>
Replace,
/// <summary>The converted files go into a new sequence; the source is left as it is.</summary>
NewSequence,
}
/// <summary>The target format of a conversion and its quality (JPEG only).</summary>
/// <param name="Quality">IJG quality 1–100 of a JPEG; ignored by the other targets.</param>
public sealed record ConvertOptions(ConvertTarget Target, int Quality = ConvertOptions.DefaultQuality)
{
/// <summary>JPEG quality when none is given.</summary>
public const int DefaultQuality = 95;
/// <summary>The target suggested for archiving: lossless, 16-bit, and read by every raw editor.</summary>
public const ConvertTarget ArchivalTarget = ConvertTarget.LinearDng;
/// <summary>The format the converted files have.</summary>
public ImageFileFormat Format => Target switch
{
ConvertTarget.Jpeg => ImageFileFormat.Jpeg,
ConvertTarget.Tiff16 => ImageFileFormat.Tiff,
_ => ImageFileFormat.Dng,
};
/// <summary>The extension of the converted files, lower case with its dot.</summary>
public string Extension => Target switch
{
ConvertTarget.Jpeg => ".jpg",
ConvertTarget.Tiff16 => ".tif",
_ => ".dng",
};
/// <summary>The format's short name, as a new sequence's title ends with it: "Harbour (DNG)".</summary>
public string Label => Target switch
{
ConvertTarget.Jpeg => "JPEG",
ConvertTarget.Tiff16 => "TIFF",
_ => "DNG",
};
/// <summary>Whether the target keeps 16 bits per channel (TIFF-16, linear DNG), which an 8-bit development of a RAW does not fill.</summary>
public bool IsSixteenBit => Target != ConvertTarget.Jpeg;
/// <summary>The quality that counts: the JPEG's, 0 for the lossless targets.</summary>
public int EffectiveQuality => Target == ConvertTarget.Jpeg ? Quality : 0;
/// <summary>Whether the target is known and the quality in 1–100.</summary>
public bool IsValid => Enum.IsDefined(Target) && Quality is >= 1 and <= 100;
/// <summary>What identifies the options in a plan's fingerprint.</summary>
public string Key => string.Create(CultureInfo.InvariantCulture, $"{Target}|{EffectiveQuality}");
/// <summary>
/// <paramref name="file"/> with this target's extension in place of its own (<c>originals/IMG_0001.CR2</c> →
/// <c>originals/IMG_0001.dng</c>): the file a converted copy takes its <c>{ext}</c> from when it is named
/// (<see cref="FileName"/>, or the ingest filing it into a new sequence).
/// </summary>
public string WithExtension(string file)
{
ArgumentNullException.ThrowIfNull(file);
return file[..^Path.GetExtension(file).Length] + Extension;
}
/// <summary>
/// The name the converted copy of <paramref name="frame"/> is filed under in <paramref name="sequence"/> (relative to its
/// folder), as filing names a photo there (<see cref="FrameNaming"/>: an import, an upload and Reorganize name it alike):
/// the archive's file <paramref name="template"/> with the frame's context in the sequence (the sequence's camera and lens,
/// <c>{n}</c> = its index + 1, the sequence's title), <c>{name}</c> the name the photo had on the card and <c>{ext}</c> this
/// target's extension, a long name shortened before its extension; in the folder of the frame's file. Not made unique
/// (<see cref="UniqueNames"/> does).
/// </summary>
public string FileName(PathTemplate template, SidecarFrame frame, SequenceManifest sequence)
{
ArgumentNullException.ThrowIfNull(template);
ArgumentNullException.ThrowIfNull(frame);
ArgumentNullException.ThrowIfNull(sequence);
var directory = frame.File[..(frame.File.LastIndexOf('/') + 1)];
return directory + template.ExpandFile(FrameNaming.Context(frame with { File = WithExtension(frame.File) }, sequence, sequence.Title));
}
}
@@ -0,0 +1,384 @@
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
using Chronos.Core.Archiving;
using Chronos.Core.Codecs.Icc;
using Chronos.Core.Codecs.Jpeg;
using Chronos.Core.Codecs.Tiff;
using Chronos.Core.Export;
using Chronos.Core.Imaging;
using Chronos.Core.Imaging.Decoders;
using Chronos.Core.Model;
namespace Chronos.Core.Conversion;
/// <summary>Why a frame of a sequence is not converted (owner review 2, R7); nothing of it is touched.</summary>
public enum ConvertSkip
{
/// <summary>The photo is in the target's format already (a TIFF of 8 bits included).</summary>
SameFormat,
/// <summary>
/// This host has no full-resolution decoder for the photo's format (a server and a CR3 or HEIC), or its decoders do not
/// read this file at full resolution (a CMYK, CIELab or palette TIFF, a compression or size they do not take, a RAW body
/// they do not develop and without a preview): a PC may have one.
/// </summary>
NoNativeDecoder,
/// <summary>This host reads the photo only from its embedded preview (a camera body its RAW decoder does not support).</summary>
PreviewOnly,
/// <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>
Missing,
/// <summary>The photo was never hashed (adopted, not verified yet): its replacement could not name it. Verify it first.</summary>
Unhashed,
/// <summary>A 16-bit target, and this host develops the RAW at 8 bits only (WIC's Raw Image Extension): JPEG converts it.</summary>
EightBit,
/// <summary>The photo's colour profile can be neither carried into the target nor converted to it (CMYK, an unknown space…).</summary>
ColorProfile,
/// <summary>
/// The photo is larger than the target holds (a JPEG side above 65535) or than Chronos reads back: the server's decoders,
/// which every converted file must decode with (<see cref="ConvertPlanner.TooLarge"/>).
/// </summary>
TooLarge,
/// <summary>
/// The photo's file is there but could not be opened when the plan was made: another program holds it without sharing
/// (a sync client, an editor), or access is denied. A plan made once it is free converts it.
/// </summary>
Inaccessible,
}
/// <summary>Why a whole sequence is not converted now; nothing of it is touched.</summary>
public enum ConvertBlock
{
/// <summary>A running or paused job works on the sequence or its whole archive, or a conversion of it waits in the queue.</summary>
Busy,
/// <summary>The archive is offline or read-only.</summary>
Offline,
/// <summary>The sequence's sidecar cannot be read, or belongs to another sequence.</summary>
Unreadable,
/// <summary>
/// The photos the sequence converts are no longer those of the plan confirmed (found by the job, which then leaves it as
/// it is).
/// </summary>
FramesChanged,
}
/// <summary>Frames of a sequence that are not converted, and why.</summary>
public sealed record ConvertSkipped(ConvertSkip Reason, int Frames);
/// <summary>
/// One frame a conversion converts: its id and index, its file and format, its size, the converted file's estimated size,
/// and the SHA-256 of the content converted.
/// </summary>
public sealed record ConvertFramePlan(Guid FrameId, int Index, string File, ImageFileFormat Format, long Size, long EstimatedBytes, string? Sha256 = null);
/// <summary>
/// One sequence of a conversion plan: the frames converted, those skipped with why, the frames version the plan was made at,
/// and why the whole sequence cannot be converted now (<see cref="Blocked"/>).
/// </summary>
public sealed record ConvertSequencePlan(Guid SequenceId, string Title, string Folder, int FramesVersion, IReadOnlyList<ConvertFramePlan> Frames,
IReadOnlyList<ConvertSkipped> Skipped, IReadOnlyList<ConvertBlock> Blocked)
{
/// <summary>Whether the sequence is converted: something to convert and nothing blocking it.</summary>
public bool Converts => Blocked.Count == 0 && Frames.Count > 0;
/// <summary>Size of the originals converted.</summary>
public long BytesBefore => Frames.Sum(f => f.Size);
/// <summary>Estimated size of the converted files.</summary>
public long BytesAfter => Frames.Sum(f => f.EstimatedBytes);
public int SkippedFrames => Skipped.Sum(s => s.Frames);
/// <summary>
/// A digest of the photos the sequence converts: each frame in order with the SHA-256 of the content converted. A
/// conversion job converts the sequence only while its plan there still has the digest the user confirmed.
/// </summary>
public string Digest() => DigestOf(Frames.Select(f => (f.FrameId, f.Sha256)));
/// <summary>The digest of frames, each with the SHA-256 of its content, in order (<see cref="Digest"/>).</summary>
public static string DigestOf(IEnumerable<(Guid FrameId, string? Sha256)> frames)
{
ArgumentNullException.ThrowIfNull(frames);
var text = new StringBuilder();
foreach (var (id, sha) in frames) text.Append(CultureInfo.InvariantCulture, $"{id:N}|{sha?.ToLowerInvariant()}\n");
return Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(text.ToString())));
}
}
/// <summary>
/// The free space a conversion needs on the archive's volume (<see cref="Required"/>: the estimate with a margin and a
/// reserve, <see cref="ConvertPlanner.Required"/>) and what it has; <see cref="Free"/> null when it cannot be measured.
/// </summary>
public sealed record ConvertSpace(long Required, long? Free)
{
public bool Enough => Free is not { } free || free >= Required;
}
/// <summary>
/// The dry run of a mass conversion (owner review 2, R7): the options and mode, each sequence's plan, the space it needs,
/// and the fingerprint the confirmation sends back (<see cref="Fingerprint"/>).
/// </summary>
public sealed record ConvertPlan(Guid ArchiveId, ConvertOptions Options, ConvertMode Mode, IReadOnlyList<ConvertSequencePlan> Sequences, ConvertSpace Space)
{
/// <summary>The sequences that are converted.</summary>
public IEnumerable<ConvertSequencePlan> Converted => Sequences.Where(s => s.Converts);
public int Frames => Converted.Sum(s => s.Frames.Count);
public long BytesBefore => Converted.Sum(s => s.BytesBefore);
public long BytesAfter => Converted.Sum(s => s.BytesAfter);
public int Skipped => Sequences.Sum(s => s.SkippedFrames);
/// <summary>
/// A digest of what the plan converts (options, mode, and each sequence in order with its frames version, the photos
/// converted with their content (<see cref="ConvertSequencePlan.Digest"/>), those skipped, and what blocks it), the
/// same for two plans of the same archive state.
/// </summary>
public string Fingerprint()
{
var text = new StringBuilder().Append(Options.Key).Append('|').Append(Mode).Append('\n');
foreach (var s in Sequences)
{
text.Append(CultureInfo.InvariantCulture, $"{s.SequenceId:N}|{s.FramesVersion}|{s.Frames.Count}|{s.BytesBefore}|{s.Digest()}|");
foreach (var skipped in s.Skipped) text.Append(CultureInfo.InvariantCulture, $"{skipped.Reason}:{skipped.Frames};");
foreach (var blocked in s.Blocked) text.Append(blocked).Append(';');
text.Append('\n');
}
return Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(text.ToString())));
}
}
/// <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 (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;
/// <summary>Space left free on the volume after the conversion, for the index, sidecars and everything else writing there.</summary>
public const long SpaceReserve = 512L << 20;
/// <summary>Pixels assumed for a frame whose size is not known (a 24 MP sensor).</summary>
public const long UnknownPixels = 24_000_000;
/// <summary>
/// Bits per pixel of a 4:4:4 JPEG over a 4:2:0 one of the same quality: the chroma is coded at full resolution, about a
/// quarter more bits on photographs.
/// </summary>
private const double FullChroma = 1.25;
/// <summary>
/// Bits per pixel of pixels that do not compress at all (noise) in the costliest target: three 16-bit samples, 48 bits
/// as Deflate stores them, and less than a bit more each as lossless JPEG codes them (an optimal Huffman code exceeds
/// the entropy of what it codes by less than one bit); a 4:4:4 JPEG of noise at quality 100 takes about 22.
/// </summary>
public const int WorstBitsPerPixel = 51;
/// <summary>
/// Margin on <see cref="WorstBitsPerPixel"/>: Deflate's stored blocks and zlib framing per strip, lossless JPEG's byte
/// stuffing (a 0xFF byte in 256 of noise) and headers per tile stay below 1 %.
/// </summary>
private const double WorstMargin = 1.01;
/// <summary>What a converted file holds beyond its pixels at most: an ICC profile carried whole, the Exif block, the directory and its strip or tile tables.</summary>
private const long WorstOverhead = IccProfile.MaxBytes + (4L << 20);
/// <summary>The plan of one sequence from its sidecar.</summary>
/// <param name="present">Every file in the folders of the sequence's frames, relative to the sequence folder
/// (<see cref="FileRenames.ListPresent"/>): the frames whose file is not among them are missing.</param>
/// <param name="decoder">This host's decoders.</param>
public static ConvertSequencePlan Plan(SequenceManifest manifest, SequencePaths folder, IReadOnlyCollection<string> present, ConvertOptions options,
ImageDecoder decoder, string title)
{
ArgumentNullException.ThrowIfNull(manifest);
ArgumentNullException.ThrowIfNull(folder);
ArgumentNullException.ThrowIfNull(present);
ArgumentNullException.ThrowIfNull(options);
ArgumentNullException.ThrowIfNull(decoder);
var onDisk = present as IReadOnlySet<string> ?? new HashSet<string>(present, StringComparer.OrdinalIgnoreCase);
var reasons = new ConvertSkip?[manifest.Frames.Count];
Parallel.For(0, reasons.Length, new ParallelOptions { MaxDegreeOfParallelism = ProbesAtOnce },
i => reasons[i] = Skip(manifest.Frames[i], folder, onDisk, options, decoder));
var skipped = new Dictionary<ConvertSkip, int>();
var frames = new List<ConvertFramePlan>();
for (var i = 0; i < reasons.Length; i++)
{
var frame = manifest.Frames[i];
if (reasons[i] is { } reason)
{
skipped[reason] = skipped.GetValueOrDefault(reason) + 1;
continue;
}
frames.Add(new ConvertFramePlan(frame.Id, frame.I, frame.File, frame.Format, frame.Size, EstimateBytes(options, frame.W, frame.H), frame.Sha256));
}
return new ConvertSequencePlan(manifest.Id, title, folder.RelativeFolder, manifest.Versions.Frames, frames,
[.. skipped.OrderBy(s => s.Key).Select(s => new ConvertSkipped(s.Key, s.Value))], []);
}
/// <summary>Why <paramref name="frame"/> is not converted, or null when it is.</summary>
/// <param name="decoder">This host's decoders; the conversion's are made from them (<see cref="FrameConverter.Decoders"/>).</param>
public static ConvertSkip? Skip(SidecarFrame frame, SequencePaths folder, IReadOnlySet<string> present, ConvertOptions options, ImageDecoder decoder)
{
ArgumentNullException.ThrowIfNull(frame);
ArgumentNullException.ThrowIfNull(folder);
ArgumentNullException.ThrowIfNull(present);
ArgumentNullException.ThrowIfNull(options);
ArgumentNullException.ThrowIfNull(decoder);
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.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
// body this host's RAW decoder does not support), the file itself is asked: headers only, and nothing at all for the
// formats every file of which decodes alike.
var path = folder.File(frame.File);
try
{
// Not read at full resolution here, although the format is: what the file holds (its encoding, its size, a RAW
// body) is beyond these decoders, and only a preview, or nothing, is left.
var probe = decoders.Probe(path, frame.Format);
if (probe.Tier != DecodeTier.Native)
{
return 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)
{
// Gone since the folder was listed.
return ConvertSkip.Missing;
}
catch (EndOfStreamException)
{
// Opened, and shorter than its own headers say: no decoder gets its pixels.
return ConvertSkip.Undecodable;
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
// 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"/>), 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. 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 <= 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; <see cref="long.MaxValue"/> for sizes beyond it.
/// </summary>
public static long WorstBytes(int width, int height) =>
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:
/// <see cref="UnknownPixels"/>), from the bits per pixel the Export tab estimates with (<see cref="ExportEstimator"/>):
/// JPEG by quality (×1.25 for 4:4:4 chroma), TIFF-16 30, linear DNG 26.
/// </summary>
public static long EstimateBytes(ConvertOptions options, int width, int height)
{
ArgumentNullException.ThrowIfNull(options);
var pixels = width > 0 && height > 0 ? (double)width * height : UnknownPixels;
var bits = options.Target switch
{
ConvertTarget.Jpeg => ExportEstimator.JpegBitsPerPixel(options.Quality) * FullChroma,
ConvertTarget.Tiff16 => 30,
_ => 26,
};
return Saturated(Math.Ceiling(pixels * bits / 8));
}
/// <summary>The free space a conversion whose files are estimated at <paramref name="estimated"/> bytes needs.</summary>
public static long Required(long estimated) => (long)Math.Ceiling(Math.Max(0, estimated) * SpaceMargin) + SpaceReserve;
}
@@ -0,0 +1,490 @@
using System.Numerics;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using Chronos.Core.Archiving;
using Chronos.Core.Codecs.Dng;
using Chronos.Core.Codecs.Icc;
using Chronos.Core.Codecs.Jpeg;
using Chronos.Core.Codecs.Tiff;
using Chronos.Core.Imaging;
using Chronos.Core.Imaging.Decoders;
using Chronos.Core.IO;
using Chronos.Core.Localization;
using Chronos.Core.Metadata;
using Chronos.Core.Model;
namespace Chronos.Core.Conversion;
/// <summary>The photo a conversion reads: its file, its sidecar record, and its sequence's camera and lens.</summary>
/// <param name="Path">Absolute path of the original.</param>
public sealed record ConvertSource(string Path, SidecarFrame Frame, SidecarCamera? Camera = null, string? Lens = null);
/// <summary>A converted file as written and verified: where it is, its size and SHA-256, its format and its upright size.</summary>
public sealed record ConvertedFile(string Path, long Size, string Sha256, ImageFileFormat Format, int Width, int Height);
/// <summary>Why a photo was not converted; each has its sentence, <c>Archive.Convert.Failed.&lt;reason&gt;</c>.</summary>
public enum ConversionFailure
{
/// <summary>No decoder of this host reads the photo at full resolution.</summary>
NoDecoder,
/// <summary>This host reads the photo only from its embedded preview.</summary>
PreviewOnly,
/// <summary>A 16-bit target, and this host develops the RAW at 8 bits only (WIC's Raw Image Extension).</summary>
EightBit,
/// <summary>The photo's colour profile cannot be carried into the target or converted to it.</summary>
ColorProfile,
/// <summary>
/// The photo is larger than the target holds, than the server's decoders read back (<see cref="ConvertPlanner.TooLarge"/>)
/// or than one image in memory can.
/// </summary>
TooLarge,
/// <summary>The converted file did not verify: read back, metadata, or a decode by the server's decoders.</summary>
NotVerified,
/// <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
/// diagnostics log's (English, technical); <see cref="Reason"/> chooses what the user reads (<see cref="FrameConverter.FailureText"/>).</summary>
public sealed class ConversionException(ConversionFailure reason, string message, Exception? inner = null) : IOException(message, inner)
{
public ConversionFailure Reason { get; } = reason;
}
/// <summary>
/// Converts one photo (owner review 2, R7; BRIEF §12.5): its original is decoded at full resolution only, never from an
/// embedded preview (<see cref="DecodeTier.Native"/>, oriented upright), by the managed decoders wherever they read it
/// natively (RAWs developed at float precision, the same on every host) and by the host's others (WIC) only for what they
/// do not (<see cref="Decoders"/>); a 16-bit target never takes a RAW developed at 8 bits. It is encoded by the in-house
/// 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. 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
{
/// <summary>Long edge of the verification decode: enough to prove the full-resolution data decodes, cheap for JPEG and RAW.</summary>
private const int VerifyEdge = 256;
/// <summary>Largest side of a JPEG (its frame header holds 16 bits).</summary>
public const int MaxJpegSide = 65535;
private const int DiskFull = unchecked((int)0x80070070);
private const int HandleDiskFull = unchecked((int)0x80070027);
/// <summary>ENOSPC, the HResult .NET gives a full disk on Linux.</summary>
private const int NoSpace = 28;
private static readonly Lazy<ImageDecoder> ServerDecoders = new(DecoderFactory.CreateServer);
/// <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;
/// <summary>
/// The decoders a conversion to <paramref name="options"/> reads a <paramref name="format"/> photo with, from this host's
/// <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"/>).
/// 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)
{
ArgumentNullException.ThrowIfNull(decoder);
ArgumentNullException.ThrowIfNull(options);
var eightBit = options.IsSixteenBit && ImageFormatInfo.Of(format).IsRaw;
var providers = decoder.Providers.Where(p => !(eightBit && p.DevelopsRawAt8Bits))
.Select(p => p.WithHeaderProbes())
.OrderBy(p => p is ManagedDecoderProvider ? 0 : 1).ToList();
return providers.SequenceEqual(decoder.Providers) ? decoder : new ImageDecoder(providers);
}
/// <summary>
/// Whether <paramref name="decoder"/> reads a <paramref name="format"/> photo natively only through a provider that
/// develops RAWs at 8 bits, which a conversion to <paramref name="options"/> does not use: the reason it is not converted.
/// </summary>
public static bool OnlyAt8Bits(ImageDecoder decoder, ConvertOptions options, ImageFileFormat format)
{
ArgumentNullException.ThrowIfNull(decoder);
ArgumentNullException.ThrowIfNull(options);
return options.IsSixteenBit && ImageFormatInfo.Of(format).IsRaw
&& decoder.Providers.Any(p => p.DevelopsRawAt8Bits && p.TierFor(format) == DecodeTier.Native);
}
/// <summary>
/// What a conversion to <paramref name="target"/> does with a photo whose values are in <paramref name="color"/>, 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 (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 (!color.IsLinear) return ColorTreatment.Carry;
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, 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)
{
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(options);
ArgumentNullException.ThrowIfNull(decoder);
ArgumentException.ThrowIfNullOrWhiteSpace(target);
if (!options.IsValid) throw new ArgumentException("Unknown target or a quality outside 1–100.", nameof(options));
var name = System.IO.Path.GetFileName(source.Path);
var format = source.Frame.Format;
var color = SourceColor.Read(source.Path, format);
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
{
using var decoded = Decoders(decoder, options, format)
.Decode(new DecodeRequest(source.Path, 0, DecodePurpose.Render, ApplyOrientation: true, AllowPreview: false), cancellationToken);
var eightBit = OnlyAt8Bits(decoder, options, format);
if (decoded is null)
{
throw new ConversionException(eightBit ? ConversionFailure.EightBit : ConversionFailure.NoDecoder,
$"{name}: no decoder here reads it at full resolution{(eightBit ? " at 16 bits" : "")}");
}
if (decoded.Tier != DecodeTier.Native || decoded.ByChoice)
{
throw new ConversionException(eightBit ? ConversionFailure.EightBit : ConversionFailure.PreviewOnly,
$"{name}: decoded from its {decoded.Tier} only ({decoded.Decoder}), not at full resolution");
}
var pixels = decoded.Pixels;
// What the plan leaves out (ConvertPlanner.TooLarge) is refused here too, at the size decoded.
if (ConvertPlanner.TooLarge(options, pixels.Width, pixels.Height))
throw new ConversionException(ConversionFailure.TooLarge, $"{name}: {pixels.Width}×{pixels.Height} is too large for {options.Label}");
// 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;
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);
var sha = System.Convert.ToHexStringLower(SHA256.HashData(bytes));
AtomicFile.WriteAllBytes(target, bytes);
file = new ConvertedFile(target, bytes.Length, sha, options.Format, image.Width, image.Height);
}
catch (OverflowException ex)
{
// An image whose samples exceed what one buffer indexes (a stitched panorama of hundreds of megapixels).
throw new ConversionException(ConversionFailure.TooLarge, $"{name}: too large to convert in memory", ex);
}
try
{
Verify(file, metadata, cancellationToken);
}
catch
{
TryDelete(target);
throw;
}
return file;
}
/// <summary>
/// Why a photo was not converted, as one localised sentence for the job log (BRIEF §0.2 rule 6): the conversion's own
/// checks by reason (<see cref="ConversionException.Reason"/>), a full disk, a denied access, a file that does not
/// decode, else a file that could not be read or written. The exception's text, technical English, goes to the
/// diagnostics log only.
/// </summary>
public static string FailureText(Exception ex)
{
ArgumentNullException.ThrowIfNull(ex);
return ex switch
{
ConversionException conversion => Loc.L("Archive.Convert.Failed." + conversion.Reason),
IOException { HResult: DiskFull or HandleDiskFull } => Loc.L("Archive.Convert.Failed.Full"),
IOException { HResult: NoSpace } when !OperatingSystem.IsWindows() => Loc.L("Archive.Convert.Failed.Full"),
UnauthorizedAccessException => Loc.L("Archive.Failed.Access"),
InvalidDataException or NotSupportedException or FormatException => Loc.L("Archive.Convert.Failed.Unreadable"),
IOException => Loc.L("Archive.Failed.Files"),
_ => Loc.L("Archive.Convert.Failed.Other"),
};
}
/// <summary>
/// The Exif a converted file carries: the sidecar's capture time (to the tick), UTC offset, exposure, f-number, ISO,
/// focal length, bias and white balance; the original's camera, serial, lens, 35 mm focal length, exposure program,
/// metering mode, flash, GPS, artist and copyright, the sequence's camera and lens where the original names none. A
/// frame without a capture time keeps the original's own (Exif or XMP), never one taken from the file system.
/// </summary>
public static FrameMetadata Metadata(FrameMetadata original, ConvertSource source)
{
ArgumentNullException.ThrowIfNull(original);
ArgumentNullException.ThrowIfNull(source);
var f = source.Frame;
var recorded = original.TimestampSource is TimestampSource.ExifOriginal or TimestampSource.ExifCreate or TimestampSource.Xmp;
var captured = f.CapturedAt ?? (recorded ? original.CapturedAt : null);
return new FrameMetadata
{
Make = original.Make ?? source.Camera?.Make,
Model = original.Model ?? source.Camera?.Model,
BodySerial = original.BodySerial ?? source.Camera?.Serial,
LensModel = original.LensModel ?? source.Lens,
Software = "Chronos",
Artist = original.Artist,
Copyright = original.Copyright,
CapturedAt = captured is { } at ? DateTime.SpecifyKind(at, DateTimeKind.Unspecified) : null,
UtcOffsetMinutes = f.CapturedAt is not null ? f.UtcOffset : recorded ? original.UtcOffsetMinutes : null,
ExposureTime = f.Exposure ?? original.ExposureTime,
FNumber = f.FNumber ?? original.FNumber,
Iso = f.Iso ?? original.Iso,
FocalLength = f.Focal ?? original.FocalLength,
FocalLength35 = original.FocalLength35,
ExposureBias = f.Bias ?? original.ExposureBias,
ExposureProgram = original.ExposureProgram,
MeteringMode = original.MeteringMode,
WhiteBalanceMode = f.Wb ?? original.WhiteBalanceMode,
Flash = original.Flash,
Orientation = 1,
Gps = original.Gps,
};
}
/// <summary>The file bytes of <paramref name="image"/> (linear light, RGB) in the target format with <paramref name="metadata"/>.</summary>
/// <param name="iccProfile">The ICC profile the encoded values are in (a JPEG or TIFF carries it); null for sRGB.</param>
public static byte[] Encode(ImageBuffer image, FrameMetadata metadata, ConvertOptions options, int encoderThreads = -1, byte[]? iccProfile = null)
{
using var output = Encoded(image, metadata, options, iccProfile, encoderThreads);
return output.ToArray();
}
/// <summary>
/// <see cref="Encoded"/>, where a file that outgrows the array it is made in (2 GB: the memory stream says its stream
/// was too long) is too large, the photo's failure, never one of the archive's files. The plan leaves out what can
/// (<see cref="ConvertPlanner.TooLarge"/>); this is what content beyond its worst case would do.
/// </summary>
private static MemoryStream EncodedWithin(string name, ImageBuffer image, FrameMetadata metadata, ConvertOptions options, byte[]? iccProfile, int encoderThreads)
{
try
{
return Encoded(image, metadata, options, iccProfile, encoderThreads);
}
catch (IOException ex)
{
// Encoding writes to memory only: an I/O error there is the stream's length limit.
throw new ConversionException(ConversionFailure.TooLarge, $"{name}: its {options.Label} outgrew the memory one file is made in", ex);
}
}
/// <summary><see cref="Encode"/> into a stream the caller disposes.</summary>
private static MemoryStream Encoded(ImageBuffer image, FrameMetadata metadata, ConvertOptions options, byte[]? iccProfile, int encoderThreads)
{
ArgumentNullException.ThrowIfNull(image);
ArgumentNullException.ThrowIfNull(metadata);
ArgumentNullException.ThrowIfNull(options);
// About the size the planner estimates, so the buffer seldom grows (and is copied) while the file is made.
var capacity = (int)Math.Clamp(ConvertPlanner.EstimateBytes(options, image.Width, image.Height) * 5 / 4, 1 << 16, Array.MaxLength);
var output = new MemoryStream(capacity);
switch (options.Target)
{
case ConvertTarget.Jpeg:
JpegEncoder.Encode(output, image, new JpegEncodeOptions
{
Quality = options.Quality,
Subsampling = JpegSubsampling.Yuv444,
OptimizeHuffman = true,
App1Segments = [ExifWriter.BuildJpegApp1(metadata)[4..]],
IccProfile = iccProfile,
MaxDegreeOfParallelism = encoderThreads,
});
break;
case ConvertTarget.Tiff16:
TiffWriter.Write(output, image, new TiffWriteOptions
{
BitsPerSample = 16,
Compression = TiffCompression.Deflate,
Predictor = true,
Transfer = TransferFunction.Srgb,
Exif = ExifWriter.BuildTiff(metadata),
IccProfile = iccProfile,
MaxDegreeOfParallelism = encoderThreads,
});
break;
default:
// A DNG's colour is its matrices: values in another profile are converted before (Treatment), never tagged.
LinearDngWriter.Write(output, image, new LinearDngOptions
{
Compression = DngCompression.LosslessJpeg,
Exif = ExifWriter.BuildTiff(metadata),
SceneReferred = true,
MaxDegreeOfParallelism = encoderThreads,
});
break;
}
return output;
}
/// <summary>
/// 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 bool AllFinite(ReadOnlySpan<float> values)
{
var i = 0;
if (Vector.IsHardwareAccelerated && values.Length >= Vector<float>.Count)
{
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)
{
if (!Vector.LessThanAll(Vector.Abs(v), infinity)) return false;
}
i = vectors.Length * Vector<float>.Count;
}
for (; i < values.Length; i++)
{
if (!float.IsFinite(values[i])) return false;
}
return true;
}
/// <summary>
/// Checks a converted file: read back whole, its size and SHA-256 are those it was written with; its metadata names its
/// format and the capture time and offset written into it; and the server's decoders read it at full-resolution tier,
/// at the size it was written at.
/// </summary>
/// <param name="written">The metadata the file was written with (<see cref="Metadata"/>).</param>
/// <exception cref="ConversionException">The file does not verify.</exception>
public static void Verify(ConvertedFile file, FrameMetadata written, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(file);
ArgumentNullException.ThrowIfNull(written);
var name = System.IO.Path.GetFileName(file.Path);
string sha;
long length;
using (var stream = new FileStream(file.Path, FileMode.Open, FileAccess.Read, FileShare.Read, 1 << 20, FileOptions.SequentialScan))
{
length = stream.Length;
sha = System.Convert.ToHexStringLower(SHA256.HashData(stream));
}
if (length != file.Size || !string.Equals(sha, file.Sha256, StringComparison.OrdinalIgnoreCase))
throw NotVerified($"{name}: read back as {length} bytes with SHA-256 {sha}, written as {file.Size} bytes with {file.Sha256}");
cancellationToken.ThrowIfCancellationRequested();
var read = MetadataReader.Read(file.Path, cancellationToken);
if (read.Format != file.Format) throw NotVerified($"{name}: reads as {read.Format}, written as {file.Format}");
if (written.CapturedAt is { } captured && read.CapturedAt != captured)
throw NotVerified($"{name}: its capture time reads {read.CapturedAt:O}, written {captured:O}");
if (written.CapturedAt is not null && written.UtcOffsetMinutes is { } offset && Math.Abs(offset) <= 14 * 60 && read.UtcOffsetMinutes != offset)
throw NotVerified($"{name}: its UTC offset reads {read.UtcOffsetMinutes}, written {offset}");
using var decoded = Verifier.Decode(new DecodeRequest(file.Path, VerifyEdge, DecodePurpose.Thumbnail, ApplyOrientation: true, AllowPreview: false),
cancellationToken);
if (decoded is not { Tier: DecodeTier.Native })
throw NotVerified($"{name}: the server's decoders do not read it at full resolution");
if (decoded.SourceWidth != file.Width || decoded.SourceHeight != file.Height)
throw NotVerified($"{name}: decodes as {decoded.SourceWidth}×{decoded.SourceHeight}, written as {file.Width}×{file.Height}");
}
private static ConversionException NotVerified(string message) => new(ConversionFailure.NotVerified, message);
/// <summary>Three channels from one (gray) or four (alpha dropped).</summary>
private static ImageBuffer Rgb(ImageBuffer image)
{
if (image.Channels == 1) return PixelConvert.GrayToRgb(image);
var rgb = new ImageBuffer(image.Width, image.Height, 3, clear: false);
for (var c = 0; c < 3; c++) image.Plane(c).CopyTo(rgb.Plane(c));
return rgb;
}
private static void TryDelete(string path)
{
try
{
if (File.Exists(path)) File.Delete(path);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
Diagnostics.Log.Warning("archive", $"{path} did not verify and could not be deleted", ex);
}
}
}
/// <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>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 keeps the original's codes and carries its ICC profile, which says what they are.</summary>
Carry,
/// <summary>
/// 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>
Describe,
/// <summary>
/// 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,8 +24,23 @@ 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>
/// Whether probes read the headers of JPEG, PNG and TIFF files too (<see cref="Probe(string, ImageFileFormat, string?)"/>)
/// and find unreadable what <see cref="Decode"/> refuses before decoding pixels: an encoding, layout or colour model its
/// decoders do not read (a CMYK, CIELab or palette TIFF, a compression it lacks, arithmetic coding), more pixels than
/// they take, a file larger than it reads whole. A conversion's decoders do (<see cref="Conversion.FrameConverter.Decoders"/>):
/// its dry run promises what it lists. Off, those files are not even opened.
/// </summary>
public bool ReadsHeaders { get; init; }
/// <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);
@@ -57,9 +72,11 @@ public sealed class ManagedDecoderProvider : IImageDecoderProvider
}
/// <summary>
/// The tier for a file whose signature format is already known (the index's): JPEG, PNG and TIFF decode natively
/// whatever they hold (damage shows only when decoding), so only RAW and HEIF files are opened.
/// The tier for a file whose signature format is already known (the index's): JPEG, PNG and TIFF are taken to decode
/// natively, so only RAW and HEIF files are opened, unless the provider <see cref="ReadsHeaders"/>.
/// </summary>
/// <exception cref="InvalidDataException">With <see cref="ReadsHeaders"/>: a JPEG, PNG or TIFF that <see cref="Decode"/>
/// refuses from its headers (<see cref="ReadHeaders"/>).</exception>
public FileTier Probe(string path, ImageFileFormat format) => Probe(path, format, null);
/// <inheritdoc cref="Probe(string, ImageFileFormat)"/>
@@ -68,8 +85,75 @@ public sealed class ManagedDecoderProvider : IImageDecoderProvider
{
var family = ImageFormatInfo.Of(format).Family;
if (family is not (ImageFamily.Jpeg or ImageFamily.Png or ImageFamily.Tiff)) return Probe(path, companion);
var decoder = family switch { ImageFamily.Jpeg => "managed-jpeg", ImageFamily.Png => "managed-png", _ => "managed-tiff" };
return new FileTier(format, DecodeTier.Native, decoder, null, 0, 0);
if (ReadsHeaders) return ReadHeaders(path, companion);
return new FileTier(format, DecodeTier.Native, DecoderName(family), null, 0, 0);
}
/// <summary>
/// The tier of the file at <paramref name="path"/> from its headers, as <see cref="Decode"/> would reach it: a JPEG, PNG
/// or TIFF (by its signature) is native, at its oriented size, only when its decoder takes what its headers describe
/// (<see cref="JpegDecoder.ReadDecodableInfo"/>, <see cref="PngDecoder.MaxPixels"/>, <see cref="TiffImageReader.ReadInfo"/>)
/// and a JPEG or PNG is no larger than one array holds (it is read whole); any other file is probed as
/// <see cref="Probe(string, string?)"/> probes it.
/// </summary>
/// <exception cref="InvalidDataException">The file is malformed, or holds what <see cref="Decode"/> does not decode.</exception>
/// <exception cref="IOException">The file cannot be read.</exception>
private FileTier ReadHeaders(string path, string? companion)
{
using (var stream = Open(path))
{
var format = ImageSignature.Detect(stream, path);
var family = ImageFormatInfo.Of(format).Family;
if (family is ImageFamily.Jpeg or ImageFamily.Png or ImageFamily.Tiff)
{
// JPEG and PNG files are decoded from one array holding them whole.
if (family != ImageFamily.Tiff && stream.Length > Array.MaxLength) throw new InvalidDataException($"{format}: the file is larger than one array holds");
var (width, height) = family switch
{
ImageFamily.Jpeg => JpegSize(stream),
ImageFamily.Png => PngSize(stream),
_ => TiffImageReader.ReadInfo(stream),
};
(width, height) = PixelConvert.OrientedSize(width, height, MetadataReader.Read(stream, path).Orientation);
return new FileTier(format, DecodeTier.Native, DecoderName(family), null, width, height);
}
}
// The signature says another format than the index: probed as any file.
return Probe(path, companion);
}
private static string DecoderName(ImageFamily family) => family switch { ImageFamily.Jpeg => "managed-jpeg", ImageFamily.Png => "managed-png", _ => "managed-tiff" };
/// <summary>The size of a JPEG its decoder takes, from the markers before its first scan.</summary>
private static (int Width, int Height) JpegSize(Stream stream)
{
var scanner = new Metadata.Jpeg.JpegParser.SegmentScanner(stream, 0, stream.Length, long.MaxValue);
if (!scanner.StartsWithSoi()) throw new InvalidDataException("JPEG: missing SOI marker");
var end = 0L;
while (scanner.Next(out var segment))
{
if (segment.Marker is 0xD9) break;
if (segment.Marker != 0xDA) continue;
end = segment.Payload + segment.Length;
break;
}
if (end == 0) throw new InvalidDataException("JPEG: no scan");
var head = new byte[end];
stream.Position = 0;
stream.ReadExactly(head);
var info = JpegDecoder.ReadDecodableInfo(head);
return (info.Width, info.Height);
}
/// <summary>The size of a PNG its decoder takes, from its header chunk.</summary>
private static (int Width, int Height) PngSize(Stream stream)
{
var head = new byte[33];
stream.Position = 0;
if (stream.ReadAtLeast(head, head.Length, throwOnEndOfStream: false) < head.Length) throw new InvalidDataException("PNG: missing signature or IHDR");
var info = PngDecoder.ReadInfo(head);
if ((long)info.Width * info.Height > PngDecoder.MaxPixels) throw new InvalidDataException($"PNG: {info.Width}x{info.Height} exceeds the pixel limit");
return (info.Width, info.Height);
}
/// <summary>The tier this provider would use for the file at <paramref name="path"/>, without decoding pixels.</summary>
@@ -82,8 +166,7 @@ public sealed class ManagedDecoderProvider : IImageDecoderProvider
if (family is ImageFamily.Jpeg or ImageFamily.Png or ImageFamily.Tiff)
{
var (w, h) = MetadataReader.Read(stream, path).OrientedSize;
var decoder = family switch { ImageFamily.Jpeg => "managed-jpeg", ImageFamily.Png => "managed-png", _ => "managed-tiff" };
return new FileTier(format, DecodeTier.Native, decoder, null, w, h);
return new FileTier(format, DecodeTier.Native, DecoderName(family), null, w, h);
}
if (family is not (ImageFamily.Dng or ImageFamily.CameraRaw or ImageFamily.Heif))
return new FileTier(format, DecodeTier.Unavailable, null, $"no managed decoder for {format}", 0, 0);
+14
View File
@@ -93,6 +93,12 @@ public interface IImageDecoderProvider
bool CanDecode(ImageFormatInfo format) => TierFor(format.Format) is not null;
/// <summary>
/// Whether the camera RAWs this provider develops come out at 8 bits per channel (WIC's Raw Image Extension does), too
/// few to fill a 16-bit file: a conversion to TIFF-16 or linear DNG does not read RAWs through it.
/// </summary>
bool DevelopsRawAt8Bits => false;
/// <summary>
/// The tier this provider would use for the file at <paramref name="path"/>, whose signature says
/// <paramref name="format"/>, without decoding pixels. The default trusts <see cref="TierFor"/>; a provider whose
@@ -111,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);
}
+14 -3
View File
@@ -6,9 +6,10 @@ using Chronos.Core.Metadata.Tiff;
namespace Chronos.Core.Metadata;
/// <summary>
/// Writes a minimal, valid little-endian Exif block for proxies and exports (BRIEF §8.5, §9): IFD0 (camera, orientation,
/// software, date, authorship), the Exif IFD (capture time with sub-seconds and offset, exposure, lens and body) and,
/// when known, the GPS IFD. Strings longer than <see cref="MaxStringBytes"/> UTF-8 bytes are truncated.
/// Writes a minimal, valid little-endian Exif block for proxies, exports and converted frames (BRIEF §8.5, §9; owner
/// review 2, R7): IFD0 (camera, orientation, software, date, authorship), the Exif IFD (capture time with sub-seconds and
/// offset, exposure, exposure program, metering mode, flash, white balance, lens and body) and, when known, the GPS IFD.
/// Strings longer than <see cref="MaxStringBytes"/> UTF-8 bytes are truncated.
/// </summary>
public static class ExifWriter
{
@@ -60,6 +61,10 @@ public static class ExifWriter
if (m.UtcOffsetMinutes is { } offset && Math.Abs(offset) < 24 * 60)
exif.Ascii(TiffTags.OffsetTimeOriginal, $"{(offset < 0 ? '-' : '+')}{Math.Abs(offset) / 60:D2}:{Math.Abs(offset) % 60:D2}");
exif.SRational(TiffTags.ExposureBiasValue, m.ExposureBias);
exif.Mode(TiffTags.ExposureProgram, m.ExposureProgram);
exif.Mode(TiffTags.MeteringMode, m.MeteringMode);
exif.Mode(TiffTags.Flash, m.Flash);
exif.Mode(TiffTags.WhiteBalance, m.WhiteBalanceMode);
exif.Rational(TiffTags.FocalLength, m.FocalLength);
if (m.FocalLength35 is { } f35 and > 0 and < MaxShortIso) exif.Short(TiffTags.FocalLengthIn35mmFilm, (ushort)Math.Round(f35));
exif.Ascii(TiffTags.BodySerialNumber, m.BodySerial);
@@ -190,6 +195,12 @@ public static class ExifWriter
Bytes(tag, TiffType.Short, data);
}
/// <summary>A SHORT mode or bit field (exposure program, metering, flash, white balance), when known and in range.</summary>
public void Mode(ushort tag, int? value)
{
if (value is { } v and >= 0 and <= ushort.MaxValue) Short(tag, (ushort)v);
}
public void Long(ushort tag, uint value)
{
var data = new byte[4];
+3 -2
View File
@@ -187,12 +187,13 @@ internal static class JpegParser
m.AddXmp(buffer);
}
private readonly record struct Segment(byte Marker, long Payload, int Length);
/// <summary>A marker segment: its marker, where its payload starts, and the payload bytes present (0 for markers without one).</summary>
internal readonly record struct Segment(byte Marker, long Payload, int Length);
private readonly record struct ExtendedXmpChunk(string Guid, uint FullLength, uint Offset, long Position, int Length);
/// <summary>Walks marker segments of one JPEG, reading only marker and length bytes within a read budget.</summary>
private sealed class SegmentScanner(Stream stream, long start, long end, long readBudget)
internal sealed class SegmentScanner(Stream stream, long start, long end, long readBudget)
{
private readonly byte[] _buffer = new byte[16];
private long _position = start + 2;
@@ -42,9 +42,16 @@ public static class TiffTags
public const ushort Xmp = 0x02BC;
public const ushort Copyright = 0x8298;
public const ushort ExifIfd = 0x8769;
/// <summary>InterColorProfile: the ICC profile of the image's colour space (UNDEFINED bytes).</summary>
public const ushort InterColorProfile = 0x8773;
public const ushort GpsIfd = 0x8825;
public const ushort InteropIfd = 0xA005;
/// <summary>InteroperabilityIndex in the Interop IFD: "R98" (sRGB) or "R03" (Adobe RGB, DCF option file).</summary>
public const ushort InteropIndex = 0x0001;
// Exif
public const ushort ExposureTime = 0x829A;
public const ushort FNumber = 0x829D;
+7 -1
View File
@@ -93,6 +93,12 @@ public enum JobKind
/// as 18.
/// </summary>
EmptyTrash = 18,
/// <summary>
/// A mass conversion of sequences to JPEG, TIFF-16 or linear DNG, replacing their frames or into new sequences, as
/// confirmed (owner review 2, R7); stored as 19.
/// </summary>
Convert = 19,
}
/// <summary>Why a photo left a sequence (its tombstone, owner review 2, R6); stored as a number.</summary>
@@ -101,7 +107,7 @@ public enum RemovalReason
/// <summary>Deleted by a user into the archive's trash.</summary>
Deleted = 0,
/// <summary>Replaced by a converted file (a later phase): the tombstone names the replacement's SHA-256.</summary>
/// <summary>Replaced by a converted file (owner review 2, R7): the tombstone names the replacement's SHA-256.</summary>
Replaced = 1,
}