fix(convert): keep what the original holds and plan only what converts
Phase 4b review, codec findings. A Replace makes the converted file the photo, so a conversion must not lose what the original held, and the dry run must promise only what the job will do. - Linear DNG from a conversion is scene-referred: the pixels are stored in linear ProPhoto primaries (colours outside sRGB stay positive) and divided by the power of two that brings their peak under the white level (at most 4 stops), with BaselineExposure giving it back and ColorMatrix1/ForwardMatrix1 describing that space. Highlights a RAW develops above 1 and its wide-gamut colours now read back as written. Exports keep the clipped linear-sRGB DNG. - A conversion reads with the managed decoders first for every format they read natively (RAWs developed at float precision, as on the server) and with WIC only for what they cannot. A RAW only WIC's Raw Image Extension develops (8 bits) is refused for TIFF-16 and linear DNG and planned out as "eight_bit"; JPEG still converts it. Providers say so through IImageDecoderProvider.DevelopsRawAt8Bits. - The original's colour space is kept: a JPEG (APP2) or TIFF (tag 34675) carries its ICC profile, with the values unchanged; a linear DNG gets the values converted through a matrix/TRC profile; a gray profile converts to sRGB. Exif Adobe RGB (uncalibrated + R03) and HEIF nclx P3 get an in-house profile. A space nothing here can keep (CMYK, a lookup-table profile into a DNG, uncalibrated without R03, HDR nclx) is planned out as "color_profile". New in-house ICC parser/writer and header reader for JPEG, PNG, TIFF, HEIF/AVIF and WebP. - The planner asks every file, whatever tier was recorded (unknown before the light analysis, or by another host), with the conversion's decoders, headers only and eight at a time; a RAW the server cannot develop is "convert_on_pc" in the plan instead of failing its sequence. Photos a target cannot hold (a JPEG side above 65535, more samples than one buffer) are planned out as "too_large", and an overflow fails its photo rather than the job. - A photo that fails reads in the job log as one catalog sentence by cause (Archive.Convert.Failed.*), the file named once; the exception's English text goes to the diagnostics log only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 0ca1e91ea09946d7a55f45695ee58e1a0ff5f779)
This commit is contained in:
1 parent
341fb77507
commit
f86a151a5d
26 files changed
+1755
-82
No files matched your search
@@ -2,6 +2,7 @@ using System.Globalization;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Chronos.Core.Archiving;
|
||||
using Chronos.Core.Codecs.Icc;
|
||||
using Chronos.Core.Export;
|
||||
using Chronos.Core.Imaging;
|
||||
using Chronos.Core.Model;
|
||||
@@ -28,6 +29,15 @@ public enum ConvertSkip
|
||||
|
||||
/// <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 one image in memory can.</summary>
|
||||
TooLarge,
|
||||
}
|
||||
|
||||
/// <summary>Why a whole sequence is not converted now; nothing of it is touched.</summary>
|
||||
@@ -117,12 +127,18 @@ public sealed record ConvertPlan(Guid ArchiveId, ConvertOptions Options, Convert
|
||||
|
||||
/// <summary>
|
||||
/// Plans the conversion of a sequence's frames (owner review 2, R7): a frame is converted when it is not in the target's
|
||||
/// format already, its file is there, it was hashed, it is not flagged unreadable, and this host decodes it at full
|
||||
/// resolution (its format has a native decoder here, and the tier the last host to decode it recorded is native, or the
|
||||
/// file probes native now). Sizes after are estimated from the pixels (<see cref="EstimateBytes"/>).
|
||||
/// format already, its file is there, it was hashed, it is not flagged unreadable, the target holds its size, this host
|
||||
/// decodes it at full resolution with the decoders the conversion uses (<see cref="FrameConverter.Decoders"/>: its format
|
||||
/// has a native decoder here and the file itself probes native now, whatever tier another host or an earlier pass
|
||||
/// recorded; for a 16-bit target, not through a RAW development at 8 bits), and its colour profile can be kept
|
||||
/// (<see cref="FrameConverter.Treatment"/>). The plan asks the files what the conversion will ask them, reading headers
|
||||
/// only, several at once. Sizes after are estimated from the pixels (<see cref="EstimateBytes"/>).
|
||||
/// </summary>
|
||||
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>Margin over the estimate the free space must hold (estimates of compressed files vary with the content).</summary>
|
||||
public const double SpaceMargin = 1.15;
|
||||
|
||||
@@ -151,11 +167,15 @@ public static class ConvertPlanner
|
||||
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>();
|
||||
foreach (var frame in manifest.Frames)
|
||||
for (var i = 0; i < reasons.Length; i++)
|
||||
{
|
||||
if (Skip(frame, folder, onDisk, options, decoder) is { } reason)
|
||||
var frame = manifest.Frames[i];
|
||||
if (reasons[i] is { } reason)
|
||||
{
|
||||
skipped[reason] = skipped.GetValueOrDefault(reason) + 1;
|
||||
continue;
|
||||
@@ -167,21 +187,51 @@ public static class ConvertPlanner
|
||||
}
|
||||
|
||||
/// <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.Undecodable | FrameFlags.Corrupt)) != 0) return ConvertSkip.Undecodable;
|
||||
if (decoder.TierFor(frame.Format) != DecodeTier.Native) return ConvertSkip.NoNativeDecoder;
|
||||
// Recorded below the native tier by the last host to decode it (one without this host's decoders, or a body its RAW
|
||||
// decoder does not support): this host asks the file itself.
|
||||
if (frame.Tier is DecodeTier.EmbeddedPreview or DecodeTier.Unavailable || (frame.Flags & FrameFlags.PreviewOnly) != 0)
|
||||
return Probe(frame, folder, decoder);
|
||||
return null;
|
||||
if (TooLarge(options, frame.W, frame.H)) return ConvertSkip.TooLarge;
|
||||
var decoders = FrameConverter.Decoders(decoder, options, frame.Format);
|
||||
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
|
||||
{
|
||||
var probe = decoders.Probe(path, frame.Format);
|
||||
if (probe.Tier != DecodeTier.Native)
|
||||
return probe.Tier == DecodeTier.EmbeddedPreview ? eightBit ? ConvertSkip.EightBit : ConvertSkip.PreviewOnly : ConvertSkip.Undecodable;
|
||||
if (TooLarge(options, probe.Width, probe.Height)) return ConvertSkip.TooLarge;
|
||||
return FrameConverter.Treatment(SourceColor.Read(path, frame.Format), options.Target) == ColorTreatment.Refuse ? ConvertSkip.ColorProfile : null;
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
return ConvertSkip.Missing;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether a frame of <paramref name="width"/> × <paramref name="height"/> (unknown: 0) cannot be converted to
|
||||
/// <paramref name="options"/>: a JPEG side above <see cref="FrameConverter.MaxJpegSide"/>, more samples than one image
|
||||
/// buffer indexes, or an estimated file larger than the array it is made in.
|
||||
/// </summary>
|
||||
public static bool TooLarge(ConvertOptions options, int width, int height)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
if (width <= 0 || height <= 0) return false;
|
||||
if (options.Target == ConvertTarget.Jpeg && Math.Max(width, height) > FrameConverter.MaxJpegSide) return true;
|
||||
return (long)width * height * 3 > Array.MaxLength || EstimateBytes(options, width, height) > Array.MaxLength;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -204,21 +254,4 @@ public static class ConvertPlanner
|
||||
|
||||
/// <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;
|
||||
|
||||
private static ConvertSkip? Probe(SidecarFrame frame, SequencePaths folder, ImageDecoder decoder)
|
||||
{
|
||||
try
|
||||
{
|
||||
return decoder.Probe(folder.File(frame.File), frame.Format).Tier switch
|
||||
{
|
||||
DecodeTier.Native => null,
|
||||
DecodeTier.EmbeddedPreview => ConvertSkip.PreviewOnly,
|
||||
_ => ConvertSkip.Undecodable,
|
||||
};
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
return ConvertSkip.Missing;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user