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

@@ -70,6 +70,26 @@ public sealed record VolumeInfo(
}
}
/// <summary>
/// Free bytes available to the current user on the volume holding the folder <paramref name="folder"/>, quotas counted: a
/// network share's (UNC) too, which <see cref="DriveInfo"/> cannot measure. Null when the folder cannot be reached or measured.
/// </summary>
public static long? FreeBytesAt(string folder)
{
ArgumentException.ThrowIfNullOrEmpty(folder);
// A share's folder must end with a separator.
var directory = Path.EndsInDirectorySeparator(folder) ? folder : folder + Path.DirectorySeparatorChar;
VolumeNative.SetThreadErrorMode(VolumeNative.SEM_FAILCRITICALERRORS, out var previousMode);
try
{
return VolumeNative.GetDiskFreeSpaceEx(directory, out var free, out _, out _) ? (long)Math.Min(free, long.MaxValue) : null;
}
finally
{
VolumeNative.SetThreadErrorMode(previousMode, out _);
}
}
/// <summary>Every ready fixed or removable volume, enumerated through <see cref="DriveInfo"/> (network and optical drives are skipped).</summary>
public static IReadOnlyList<VolumeInfo> ListLocal()
{
@@ -1,6 +1,7 @@
using System.Buffers;
using System.Runtime.InteropServices;
using Chronos.Core.Imaging;
using Chronos.Core.Metadata.Tiff;
using Chronos.Core.Windows.Codecs;
using Chronos.Core.Windows.Interop;
@@ -25,6 +26,12 @@ namespace Chronos.Core.Windows.Wic;
/// (the managed decoder develops it at 16 bits, the Raw Image Extension at 8), and a request that prefers the embedded
/// preview is left to the managed decoder, which reads it without developing anything (R4, owner review 1).
/// </para>
/// <para>
/// WIC's TIFF decoder gives no error for what it does not decode: a compression it does not know (ZSTD, JPEG 2000, LERC…)
/// comes out as zeros, a predictor other than the horizontal one of integer samples (the floating-point predictor of
/// Photoshop's 32-bit files) is not undone, and 32-bit integer samples are taken for floats. Such a TIFF is left to the
/// other decoders (<see cref="TiffRefusal"/>; owner review 2, R7), never decoded black or as noise.
/// </para>
/// </remarks>
public sealed class WicDecoderProvider : IImageDecoderProvider
{
@@ -34,10 +41,17 @@ public sealed class WicDecoderProvider : IImageDecoderProvider
private const int WICBitmapPaletteTypeCustom = 0;
private const uint NumericFixedPoint = 4, NumericFloat = 5;
private const int BandBytes = 4 << 20;
private const uint IntegerSamples = 1, PredictorNone = 1, PredictorHorizontal = 2;
/// <summary>The photo-policy query covers every WIC container; the raw paths catch decoders without the policy.</summary>
private static readonly string[] OrientationQueries = ["System.Photo.Orientation", "/app1/ifd/{ushort=274}", "/ifd/{ushort=274}"];
/// <summary>
/// The TIFF compressions WIC's TIFF decoder decodes (checked on Windows 11 against files of each): none, CCITT RLE, T.4
/// and T.6, LZW, JPEG, Deflate (both codes) and PackBits.
/// </summary>
private static readonly HashSet<uint> TiffCompressions = [1, 2, 3, 4, 5, 7, 8, 32773, 32946];
private volatile CodecInventory _inventory;
/// <param name="inventory">Codec snapshot deciding <see cref="TierFor"/>; detected now when omitted.</param>
@@ -45,6 +59,16 @@ public sealed class WicDecoderProvider : IImageDecoderProvider
public string Name => "wic";
/// <summary>
/// Whether probes open each file and read what WIC makes of its headers (<see cref="Probe"/>): the decoder it picks, the
/// first frame's size, orientation and pixel format, and for a TIFF what <see cref="TiffRefusal"/> refuses. A
/// conversion's decoders do (<see cref="WithHeaderProbes"/>); otherwise a probe trusts the format and opens nothing.
/// </summary>
public bool ReadsHeaders { get; init; }
/// <summary>This provider with probes that read headers (<see cref="ReadsHeaders"/>), on the same codec snapshot.</summary>
public IImageDecoderProvider WithHeaderProbes() => ReadsHeaders ? this : new WicDecoderProvider(_inventory) { ReadsHeaders = true };
/// <summary>The codec snapshot in use; <see cref="Refresh"/> replaces it.</summary>
public CodecInventory Inventory => _inventory;
@@ -53,10 +77,14 @@ public sealed class WicDecoderProvider : IImageDecoderProvider
public DecodeTier? TierFor(ImageFileFormat format) => format != ImageFileFormat.Dng && Inventory.IsNative(format) ? DecodeTier.Native : null;
/// <summary>The Raw Image Extension, the only decoder this provider develops RAWs with, gives 24bppRGB (8 bits per channel).</summary>
public bool DevelopsRawAt8Bits => true;
/// <summary>
/// Decodes the first frame. Returns null when WIC cannot natively decode this format here, when the request
/// forces the embedded preview, or when no installed decoder recognises the content. Corrupt files raise the
/// WIC <see cref="COMException"/>; unreadable files the matching <see cref="IOException"/>.
/// forces the embedded preview, when no installed decoder recognises the content, or for a TIFF WIC would not decode
/// as what it holds (<see cref="TiffRefusal"/>). Corrupt files raise the WIC <see cref="COMException"/>; unreadable
/// files the matching <see cref="IOException"/>.
/// </summary>
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default)
{
@@ -78,7 +106,12 @@ public sealed class WicDecoderProvider : IImageDecoderProvider
if (hr == WINCODEC_ERR_COMPONENTNOTFOUND) return null;
Marshal.ThrowExceptionForHR(hr);
if (format.IsRaw && DecoderClsid(decoder!) != WicGuids.RawDecoder) return null;
frame = decoder!.GetFrame(0);
if (decoder!.GetContainerFormat() == WicGuids.ContainerFormatTiff && TiffRefusal(request.Path) is { } refusal)
{
Chronos.Core.Diagnostics.Log.Debug("Decode", $"{Path.GetFileName(request.Path)}: {refusal}, which WIC does not decode; left to the other decoders");
return null;
}
frame = decoder.GetFrame(0);
frame.GetSize(out var width, out var height);
var orientation = request.ApplyOrientation ? ReadOrientation(frame) : 1;
@@ -112,6 +145,87 @@ public sealed class WicDecoderProvider : IImageDecoderProvider
}
}
/// <summary>
/// The tier for the file at <paramref name="path"/>, whose signature says <paramref name="format"/>. Without
/// <see cref="ReadsHeaders"/>, the format's (<see cref="TierFor"/>), nothing opened. With it, what WIC makes of the
/// file's headers, as <see cref="Decode"/> reaches them: native at the first frame's oriented size when the decoder it
/// picks is one this provider decodes with (the Raw Image Extension for a RAW), the TIFF is one it decodes
/// (<see cref="TiffRefusal"/>), and the frame's pixel format converts to what it requests; unavailable, with why,
/// whatever else WIC answers.
/// </summary>
/// <exception cref="IOException">The file cannot be read (missing, held by another program).</exception>
/// <exception cref="UnauthorizedAccessException">Access to the file is denied.</exception>
public FileTier Probe(string path, ImageFileFormat format)
{
ArgumentException.ThrowIfNullOrWhiteSpace(path);
if (TierFor(format) is not { } tier) return Unavailable(format, $"no decoder for {format}");
return ReadsHeaders ? ReadHeaders(path, format) : new FileTier(format, tier, Name, null, 0, 0);
}
private FileTier ReadHeaders(string path, ImageFileFormat format)
{
Win32Com.EnsureInitialized();
var factory = WicFactory.Instance;
// A read error is the file's, never a verdict on its content: it reaches the caller.
var stream = ShellStream.OpenRead(path);
IWICBitmapDecoder? decoder = null;
IWICBitmapFrameDecode? frame = null;
IWICFormatConverter? converter = null;
try
{
var hr = factory.CreateDecoderFromStream(stream, 0, WicDecodeOptions.MetadataCacheOnDemand, out decoder);
if (hr < 0) return Unavailable(format, $"no decoder takes the file (0x{hr:X8})");
if (ImageFormatInfo.Of(format).IsRaw && DecoderClsid(decoder!) != WicGuids.RawDecoder) return Unavailable(format, "only the Raw Image Extension develops a RAW here");
if (decoder!.GetContainerFormat() == WicGuids.ContainerFormatTiff && TiffRefusal(path) is { } refusal) return Unavailable(format, $"{refusal} is not decoded");
frame = decoder.GetFrame(0);
frame.GetSize(out var width, out var height);
var pixelFormat = frame.GetPixelFormat();
converter = factory.CreateFormatConverter();
var converts = IsFloatingPoint(factory, pixelFormat) && CanConvert(converter, pixelFormat, WicGuids.PixelFormat128bppRGBAFloat)
|| CanConvert(converter, pixelFormat, WicGuids.PixelFormat64bppRGBA);
if (!converts) return Unavailable(format, $"pixel format {pixelFormat} does not convert");
var swap = OrientationMap.SwapsAxes(ReadOrientation(frame));
int w = (int)Math.Min(width, int.MaxValue), h = (int)Math.Min(height, int.MaxValue);
return new FileTier(format, DecodeTier.Native, Name, null, swap ? h : w, swap ? w : h);
}
catch (Exception ex) when (ex is not (IOException or UnauthorizedAccessException or OutOfMemoryException))
{
// Whatever else WIC answers (an HRESULT its interop turns into one exception or another), it does not decode the file.
return Unavailable(format, ex.Message);
}
finally
{
Win32Com.Release(converter);
Win32Com.Release(frame);
Win32Com.Release(decoder);
Marshal.Release(stream);
}
}
private FileTier Unavailable(ImageFileFormat format, string why) => new(format, DecodeTier.Unavailable, null, $"{Name}: {why}", 0, 0);
/// <summary>
/// Why WIC's TIFF decoder would not decode the first image of the TIFF at <paramref name="path"/> (its first directory,
/// WIC's first frame) as what it holds, or null: a compression it does not know, which it decodes as zeros; a predictor
/// it does not undo (any but the horizontal one of integer samples); integer samples of more than 16 bits, which it
/// takes for floats. It says so with no error. What it does report (an unknown photometric interpretation, 64-bit
/// floats) it reports itself.
/// </summary>
/// <exception cref="IOException">The file cannot be read.</exception>
internal static string? TiffRefusal(string path)
{
using var file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, 4096, FileOptions.RandomAccess);
// A file this reader finds no directory in is WIC's to make sense of, or to fail on.
if (TiffReader.Open(file) is not { } reader || reader.ReadIfd(reader.FirstIfdOffset) is not { } ifd) return null;
var compression = reader.GetUInt(ifd, TiffTags.Compression) ?? 1;
if (!TiffCompressions.Contains(compression)) return $"TIFF compression {compression}";
var samples = reader.GetUInt(ifd, TiffTags.SampleFormat) ?? IntegerSamples;
var predictor = reader.GetUInt(ifd, TiffTags.Predictor) ?? PredictorNone;
if (predictor != PredictorNone && !(predictor == PredictorHorizontal && samples == IntegerSamples)) return $"TIFF predictor {predictor}";
var bits = reader.GetUInts(ifd, TiffTags.BitsPerSample) is { Length: > 0 } each ? each.Max() : 1;
return samples == IntegerSamples && bits > 16 ? $"TIFF {bits}-bit integer samples" : null;
}
/// <summary>CLSID of the decoder WIC chose for the stream, or <see cref="Guid.Empty"/> when it does not say.</summary>
private static Guid DecoderClsid(IWICBitmapDecoder decoder)
{
+3
View File
@@ -15,6 +15,9 @@ internal static class WicGuids
/// <summary>CLSID_WICWebpDecoder (WebP Image Extensions).</summary>
public static readonly Guid WebpDecoder = new("7693e886-51c9-4070-8419-9f70738ec8fa");
/// <summary>GUID_ContainerFormatTiff: the container of the in-box TIFF decoder.</summary>
public static readonly Guid ContainerFormatTiff = new("163bcc30-e2e9-4f0b-961d-a3e9fdb788a3");
public static readonly Guid VendorMicrosoft = new("f0e749ca-edef-4589-a73a-ee0e626a2a2b");
public static readonly Guid VendorMicrosoftBuiltIn = new("257a30fd-06b6-462b-aea4-63f70b86e533");