- Preview sizes come from probes and unscaled decodes: a size-limited decode picks the smallest preview that reaches its edge (CR3 PRVW 1620x1080), so renders and deep passes record its tier without a size and the output cap keeps the largest preview's. - A read error is not an undecodable file: ImageDecoder.Decode throws the IOException when a provider could not read the file and none produced pixels. Deep passes never flag frames they could not read or that vanished, and frames only an interrupted earlier run found undecodable are decoded again before they are flagged. - Renders resolve stand-ins only for frames the pipeline asks for; frames the read-ahead meets on the way (skipped by speed-ups) are left alone. - A GoPro GPR finds its JPEG in the index when an import or upload kept it in a sequence of its own (same original stem and capture time), for probes, deep analysis, renders, thumbnails, proxies and the editor; GPRs whose JPEG is indexed later are probed again. - Thumbnails judge an embedded preview by its own JPEG header. - PostgreSQL writes tier fields 1000 frames per statement. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
131 lines
6.9 KiB
C#
131 lines
6.9 KiB
C#
using Titano.Core.Codecs.Jpeg;
|
|
using Titano.Core.Imaging;
|
|
using Titano.Core.Metadata;
|
|
|
|
namespace Titano.Core.Archiving;
|
|
|
|
/// <summary>A JPEG made from a frame.</summary>
|
|
public sealed record EncodedImage(byte[] Jpeg, int Width, int Height, DecodeTier Tier);
|
|
|
|
/// <summary>
|
|
/// Makes thumbnails and proxies of frames (BRIEF §8.5): the managed JPEG decoder, or for thumbnails an embedded preview
|
|
/// at least 320 px long, first, then the host's decode providers (WIC on Windows); orientation applied, resampled in
|
|
/// linear light, encoded in-house. <see cref="EncodedImage.Tier"/> says how the pixels were obtained.
|
|
/// </summary>
|
|
public sealed class FrameImageRenderer(ImageDecoder decoder)
|
|
{
|
|
public const int ThumbnailLongEdge = 320;
|
|
public const int ThumbnailQuality = 80;
|
|
|
|
/// <summary>A 320 px q80 thumbnail, or null when no decoder can read the frame.</summary>
|
|
/// <param name="companion">Where a GPR's JPEG is kept when it is not beside it (<see cref="DecodeRequest.Companion"/>).</param>
|
|
public EncodedImage? Thumbnail(SequencePaths sequence, SidecarFrame frame, CancellationToken cancellationToken = default, string? companion = null)
|
|
{
|
|
using var decoded = Decode(sequence.File(frame.File), frame, ThumbnailLongEdge, DecodePurpose.Thumbnail, preferPreview: true, companion, cancellationToken);
|
|
if (decoded is null) return null;
|
|
using var small = Resampler.Thumbnail(decoded.Pixels, ThumbnailLongEdge);
|
|
return Encode(small, new JpegEncodeOptions { Quality = ThumbnailQuality }, decoded.Tier);
|
|
}
|
|
|
|
/// <summary>A proxy at the policy's long edge and quality with capture time and camera in its Exif; null when undecodable.</summary>
|
|
/// <param name="companion">Where a GPR's JPEG is kept when it is not beside it (<see cref="DecodeRequest.Companion"/>).</param>
|
|
public EncodedImage? Proxy(SequencePaths sequence, SidecarFrame frame, SidecarCamera? camera, ProxyPolicy policy, CancellationToken cancellationToken = default,
|
|
string? companion = null)
|
|
{
|
|
if (policy.LongEdge == ProxyPolicy.Disabled) throw new InvalidOperationException("Proxies are disabled for this archive.");
|
|
using var decoded = Decode(sequence.File(frame.File), frame, policy.LongEdge, DecodePurpose.Proxy, preferPreview: false, companion, cancellationToken);
|
|
if (decoded is null) return null;
|
|
var source = decoded.Pixels;
|
|
var (w, h) = policy.LongEdge == ProxyPolicy.FullSize ? (source.Width, source.Height) : Resampler.FitWithin(source.Width, source.Height, policy.LongEdge);
|
|
using var resized = Resampler.Resize(source, w, h);
|
|
var exif = ExifWriter.BuildJpegApp1(new FrameMetadata
|
|
{
|
|
Make = camera?.Make,
|
|
Model = camera?.Model,
|
|
BodySerial = camera?.Serial,
|
|
CapturedAt = frame.CapturedAt,
|
|
UtcOffsetMinutes = frame.UtcOffset,
|
|
ExposureTime = frame.Exposure,
|
|
FNumber = frame.FNumber,
|
|
Iso = frame.Iso,
|
|
FocalLength = frame.Focal,
|
|
ExposureBias = frame.Bias,
|
|
Software = "Titano",
|
|
});
|
|
// The encoder takes the APP1 payload; ExifWriter returns the whole segment (marker and length first).
|
|
return Encode(resized, new JpegEncodeOptions { Quality = policy.Quality, App1Segments = [exif[4..]] }, decoded.Tier);
|
|
}
|
|
|
|
private static EncodedImage Encode(ImageBuffer pixels, JpegEncodeOptions options, DecodeTier tier) =>
|
|
new(JpegEncoder.Encode(pixels, options), pixels.Width, pixels.Height, tier);
|
|
|
|
/// <summary>
|
|
/// The frame's pixels: a JPEG through the managed decoder, the stored embedded preview when <paramref name="preferPreview"/>
|
|
/// and it reaches <paramref name="maxLongEdge"/> (a smaller one would be upscaled or leave the thumbnail short), then
|
|
/// the host's decoders (with the GPR's <paramref name="companion"/>), and the stored preview as the last resort. Null
|
|
/// when nothing decodes.
|
|
/// </summary>
|
|
/// <exception cref="IOException">The file could not be read.</exception>
|
|
private DecodedFrame? Decode(string path, SidecarFrame frame, int maxLongEdge, DecodePurpose purpose, bool preferPreview, string? companion,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
if (frame.Format == ImageFileFormat.Jpeg && DecodeJpeg(File.ReadAllBytes(path), maxLongEdge, frame.Orientation, DecodeTier.Native) is { } jpeg) return jpeg;
|
|
byte[]? preview = null;
|
|
if (preferPreview && (preview = ReadPreview(path, frame)) is not null && Reaches(preview, maxLongEdge)
|
|
&& DecodeJpeg(preview, maxLongEdge, frame.Orientation, DecodeTier.EmbeddedPreview) is { } fast)
|
|
return fast;
|
|
var request = new DecodeRequest(path, System.Math.Max(0, maxLongEdge), purpose, ApplyOrientation: true, AllowPreview: true, Companion: companion);
|
|
if (decoder.Decode(request, cancellationToken) is { } decoded) return new DecodedFrame(decoded.Pixels, decoded.Tier);
|
|
return (preview ?? ReadPreview(path, frame)) is { } fallback ? DecodeJpeg(fallback, maxLongEdge, frame.Orientation, DecodeTier.EmbeddedPreview) : null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Whether the stored preview's long edge reaches <paramref name="maxLongEdge"/>, from the JPEG's own header: the
|
|
/// sidecar's preview size may be a GPR companion's, larger than the preview embedded in the file.
|
|
/// </summary>
|
|
private static bool Reaches(byte[] preview, int maxLongEdge)
|
|
{
|
|
if (maxLongEdge <= 0) return true;
|
|
try
|
|
{
|
|
var info = JpegDecoder.ReadInfo(preview);
|
|
return int.Max(info.Width, info.Height) >= maxLongEdge;
|
|
}
|
|
catch (InvalidDataException)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private static DecodedFrame? DecodeJpeg(byte[] data, int maxLongEdge, int orientation, DecodeTier tier)
|
|
{
|
|
ImageBuffer decoded;
|
|
try
|
|
{
|
|
decoded = JpegDecoder.DecodeToLinear(data, new JpegDecodeOptions { MaxLongEdge = System.Math.Max(0, maxLongEdge) }, expandGray: true);
|
|
}
|
|
catch (InvalidDataException)
|
|
{
|
|
return null;
|
|
}
|
|
if (orientation is <= 1 or > 8) return new DecodedFrame(decoded, tier);
|
|
using (decoded) return new DecodedFrame(PixelConvert.ApplyOrientation(decoded, orientation), tier);
|
|
}
|
|
|
|
private static byte[]? ReadPreview(string path, SidecarFrame frame)
|
|
{
|
|
if (frame.PreviewOffset is not { } offset || frame.PreviewLength is not ({ } length and > 0 and < int.MaxValue)) return null;
|
|
using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete, 4096, FileOptions.RandomAccess);
|
|
if (offset < 0 || offset + length > stream.Length) return null;
|
|
var data = new byte[length];
|
|
stream.Position = offset;
|
|
stream.ReadExactly(data);
|
|
return data;
|
|
}
|
|
|
|
private sealed record DecodedFrame(ImageBuffer Pixels, DecodeTier Tier) : IDisposable
|
|
{
|
|
public void Dispose() => Pixels.Dispose();
|
|
}
|
|
}
|