BRIEF §8.2 tiers 2 and 3 had no writer: PreviewOnly and Undecodable were never set, so the advisor could not cap the output at the preview size and one unreadable frame failed a whole render. - ImageDecoder.Probe names a file's tier without decoding it (JPEG, PNG and TIFF without opening the file); the light analysis probes frames whose tier is unknown, and the deep analysis, renders from originals and thumbnail/proxy passes record what decoding actually found (FrameTiers). Sidecar frames gain "tier", "previewW" and "previewH"; the index gains frames.preview_width/height and sequences.preview_only_count (migration 0005, SQLite and PostgreSQL). - SequenceContext takes the preview size from preview-only frames, so the advisor's output cap applies; the light analysis reports format tiers (preview-only frames as information, undecodable ones as a warning under the deep analysis' key, listed once); a sequence that gains undecodable frames warns at once. - Only a file in a format the host decodes natively is flagged undecodable; files beyond the host's decoders keep their tier without the flag, so render nodes and proxies can still supply them. - A frame that stops decoding during a render is replaced by its nearest planned neighbour and logged instead of failing the job; the render fails only when no planned frame decodes. - The Library card shows a preview-only chip with the count (local and server sequences: SequenceDto.PreviewOnlyCount, FrameDto preview size). - GoPro GPR decodes through the JPEG the camera writes beside it when that is larger than the embedded preview (BRIEF §8.1). - Thumbnails use a stored embedded preview only when it reaches 320 px; a smaller one sends the frame through the host's decoders instead (BRIEF §8.5). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
290 lines
14 KiB
C#
290 lines
14 KiB
C#
using Titano.Core.Codecs.Jpeg;
|
||
using Titano.Core.Codecs.Png;
|
||
using Titano.Core.Codecs.Raw;
|
||
using Titano.Core.Codecs.Tiff;
|
||
using Titano.Core.Imaging;
|
||
using Titano.Core.Imaging.Decoders;
|
||
using Titano.Core.Metadata;
|
||
using Titano.Core.Metadata.Tiff;
|
||
using Titano.Core.Tests.Metadata.Builders;
|
||
using Titano.Core.Tests.Raw;
|
||
using Titano.Testing;
|
||
using static Titano.Core.Tests.Raw.NativeRawWriters;
|
||
|
||
namespace Titano.Core.Tests.Decoding;
|
||
|
||
/// <summary>Tier selection (native → embedded preview → unavailable), decoder chains and <see cref="DecodeRequest"/> fast paths.</summary>
|
||
public sealed class ManagedDecoderProviderTests(TestContext context)
|
||
{
|
||
private const int W = 96, H = 64;
|
||
private readonly string _dir = context.CreateTempDirectory();
|
||
|
||
private string Write(string name, byte[] data)
|
||
{
|
||
var path = Path.Combine(_dir, name);
|
||
File.WriteAllBytes(path, data);
|
||
return path;
|
||
}
|
||
|
||
private static byte[] Cr2File(string model, int orientation = 1)
|
||
{
|
||
using var scene = SyntheticRaw.Smooth(W, H);
|
||
var active = new RawRect(24, 4, 24 + W, 4 + H);
|
||
return Cr2(Sensor(scene, "Canon EOS 5D Mark III", CfaPattern.Bayer("RGGB"), 128, 72, active.Left, active.Top, 2048, 16383), active, orientation, model);
|
||
}
|
||
|
||
private static byte[] JpegFile(int width, int height, int orientation)
|
||
{
|
||
using var image = SyntheticRaw.Smooth(width, height);
|
||
var exif = ExifWriter.BuildTiff(new FrameMetadata { Make = "TestCam", Model = "T1", Orientation = orientation });
|
||
return JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 90, App1Segments = [[.. "Exif\0\0"u8, .. exif]] });
|
||
}
|
||
|
||
[Test]
|
||
public void ServerFormatTableReportsTiers()
|
||
{
|
||
var rows = DecoderFactory.DescribeFormats(DecoderFactory.CreateServer()).ToDictionary(r => r.Format.Format);
|
||
foreach (var f in new[] { ImageFileFormat.Jpeg, ImageFileFormat.Png, ImageFileFormat.Tiff, ImageFileFormat.Dng, ImageFileFormat.Cr2, ImageFileFormat.Nef, ImageFileFormat.Arw, ImageFileFormat.Pef, ImageFileFormat.Raf })
|
||
{
|
||
Assert.Equal(DecodeTier.Native, rows[f].Tier);
|
||
Assert.Equal("managed", rows[f].Provider);
|
||
}
|
||
foreach (var f in new[] { ImageFileFormat.Cr3, ImageFileFormat.Orf, ImageFileFormat.Rw2, ImageFileFormat.Heif }) Assert.Equal(DecodeTier.EmbeddedPreview, rows[f].Tier);
|
||
foreach (var f in new[] { ImageFileFormat.WebP, ImageFileFormat.JpegXl, ImageFileFormat.Gif })
|
||
{
|
||
Assert.Equal(DecodeTier.Unavailable, rows[f].Tier);
|
||
Assert.Null(rows[f].Provider);
|
||
}
|
||
}
|
||
|
||
[Test]
|
||
public void ClientChainPrefersTheWindowsProvider()
|
||
{
|
||
var wic = new FakeProvider();
|
||
var decoder = DecoderFactory.CreateClient(wic);
|
||
Assert.Equal(2, decoder.Providers.Count);
|
||
var rows = DecoderFactory.DescribeFormats(decoder).ToDictionary(r => r.Format.Format);
|
||
Assert.Equal("fake", rows[ImageFileFormat.Cr3].Provider);
|
||
Assert.Equal(DecodeTier.Native, rows[ImageFileFormat.Cr3].Tier);
|
||
Assert.Equal("fake", rows[ImageFileFormat.Jpeg].Provider);
|
||
Assert.Equal("managed", rows[ImageFileFormat.Dng].Provider);
|
||
|
||
// The Windows provider declines this file: the managed provider decodes it.
|
||
var path = Write("a.jpg", JpegFile(64, 48, 1));
|
||
using var image = decoder.Decode(new DecodeRequest(path));
|
||
Assert.Equal(1, wic.Calls);
|
||
Assert.Equal("managed-jpeg", image!.Decoder);
|
||
Assert.Count(1, DecoderFactory.CreateClient(null).Providers);
|
||
}
|
||
|
||
[Test]
|
||
public void SupportedRawDecodesNatively()
|
||
{
|
||
var path = Write("IMG_1.CR2", Cr2File("Canon EOS 5D Mark III"));
|
||
var provider = new ManagedDecoderProvider();
|
||
Assert.Equal(DecodeTier.Native, provider.Probe(path).Tier);
|
||
using var image = DecoderFactory.CreateServer().Decode(new DecodeRequest(path));
|
||
Assert.Equal(DecodeTier.Native, image!.Tier);
|
||
Assert.Equal("managed-cr2", image.Decoder);
|
||
Assert.Equal(W, image.Pixels.Width);
|
||
Assert.Equal(W, image.SourceWidth);
|
||
}
|
||
|
||
[Test]
|
||
public void UnsupportedBodyFallsBackToTheOrientedPreview()
|
||
{
|
||
var path = Write("IMG_2.CR2", Cr2File("Canon EOS R99", orientation: 6));
|
||
var probe = new ManagedDecoderProvider().Probe(path);
|
||
Assert.Equal(DecodeTier.EmbeddedPreview, probe.Tier);
|
||
Assert.Contains("no colour matrix", probe.Reason);
|
||
using var image = DecoderFactory.CreateServer().Decode(new DecodeRequest(path));
|
||
Assert.Equal(DecodeTier.EmbeddedPreview, image!.Tier);
|
||
Assert.Equal("managed-preview", image.Decoder);
|
||
// The 64×48 preview is rotated by the RAW's orientation (6).
|
||
Assert.Equal(48, image.Pixels.Width);
|
||
Assert.Equal(64, image.Pixels.Height);
|
||
Assert.Equal(48, image.SourceWidth);
|
||
Assert.Null(DecoderFactory.CreateServer().Decode(new DecodeRequest(path, AllowPreview: false)));
|
||
}
|
||
|
||
[Test]
|
||
public void ForcePreviewSkipsNativeDevelopment()
|
||
{
|
||
var path = Write("IMG_3.CR2", Cr2File("Canon EOS 5D Mark III"));
|
||
using var image = DecoderFactory.CreateServer().Decode(new DecodeRequest(path, ForcePreview: true));
|
||
Assert.Equal(DecodeTier.EmbeddedPreview, image!.Tier);
|
||
Assert.Equal(64, image.Pixels.Width);
|
||
}
|
||
|
||
[Test]
|
||
public void FilesWithoutDecoderOrPreviewAreUnavailable()
|
||
{
|
||
// A NEF whose body has no matrix and no preview, and a WebP.
|
||
using var scene = SyntheticRaw.Smooth(W, H);
|
||
var nef = Nef(Sensor(scene, "Nikon D850", CfaPattern.Bayer("RGGB"), W, H, 0, 0, 600, 16383), 14, NefEncoding.Lossless);
|
||
var text = System.Text.Encoding.Latin1.GetString(nef).Replace("NIKON D850", "NIKON Z99X");
|
||
var path = Write("DSC_9.NEF", System.Text.Encoding.Latin1.GetBytes(text));
|
||
Assert.Equal(DecodeTier.Unavailable, new ManagedDecoderProvider().Probe(path).Tier);
|
||
Assert.Null(DecoderFactory.CreateServer().Decode(new DecodeRequest(path)));
|
||
var webp = Write("x.webp", Metadata.Builders.Fixtures.WebP());
|
||
Assert.Null(DecoderFactory.CreateServer().Decode(new DecodeRequest(webp)));
|
||
Assert.Equal(DecodeTier.Unavailable, new ManagedDecoderProvider().Probe(webp).Tier);
|
||
}
|
||
|
||
[Test]
|
||
public void JpegUsesDctScalingAndOrientation()
|
||
{
|
||
var path = Write("big.jpg", JpegFile(1600, 1200, 6));
|
||
using var small = DecoderFactory.CreateServer().Decode(new DecodeRequest(path, MaxLongEdge: 300));
|
||
Assert.Equal(225, small!.Pixels.Width);
|
||
Assert.Equal(300, small.Pixels.Height);
|
||
Assert.Equal(1200, small.SourceWidth);
|
||
Assert.Equal(1600, small.SourceHeight);
|
||
using var stored = DecoderFactory.CreateServer().Decode(new DecodeRequest(path, MaxLongEdge: 300, ApplyOrientation: false));
|
||
Assert.Equal(300, stored!.Pixels.Width);
|
||
Assert.Equal(3, stored.Pixels.Channels);
|
||
}
|
||
|
||
[Test]
|
||
public void RawFastPathsBinAndFit()
|
||
{
|
||
using var scene = SyntheticRaw.Smooth(W, H);
|
||
var path = Write("f.dng", SyntheticDng.Write(scene, new DngSpec()));
|
||
using var thumb = DecoderFactory.CreateServer().Decode(new DecodeRequest(path, MaxLongEdge: 40, Purpose: DecodePurpose.Thumbnail));
|
||
Assert.Equal(40, thumb!.Pixels.Width);
|
||
Assert.Equal(27, thumb.Pixels.Height);
|
||
Assert.Equal(W, thumb.SourceWidth);
|
||
using var expected = Resampler.Resize(scene, 40, 27, ResampleFilter.Box);
|
||
Assert.GreaterOrEqual(SyntheticRaw.Psnr(expected, thumb.Pixels, 1), 35);
|
||
}
|
||
|
||
[Test]
|
||
public void PngAndTiffDecodeToRgb()
|
||
{
|
||
using var gray = new ImageBuffer(20, 10, 1);
|
||
gray.Plane(0).Fill(0.25f);
|
||
var png = Write("g.png", PngEncoder.Encode(gray));
|
||
using var p = DecoderFactory.CreateServer().Decode(new DecodeRequest(png));
|
||
Assert.Equal(3, p!.Pixels.Channels);
|
||
Assert.Approximately(0.25, p.Pixels[2, 5, 5], 0.01);
|
||
var tif = Write("g.tif", TiffWriter.Write(gray));
|
||
using var t = DecoderFactory.CreateServer().Decode(new DecodeRequest(tif, MaxLongEdge: 10));
|
||
Assert.Equal(3, t!.Pixels.Channels);
|
||
Assert.Equal(10, t.Pixels.Width);
|
||
Assert.Equal("managed-tiff", t.Decoder);
|
||
}
|
||
|
||
[Test]
|
||
public void CorruptedRawFilesOnlyRaiseInvalidDataOrReturnNull()
|
||
{
|
||
var file = Cr2File("Canon EOS 5D Mark III");
|
||
var provider = new ManagedDecoderProvider();
|
||
var random = new Random(17);
|
||
for (var i = 0; i < 60; i++)
|
||
{
|
||
var variant = i < 20 ? file[..(file.Length * i / 20)] : (byte[])file.Clone();
|
||
if (i >= 20)
|
||
{
|
||
for (var k = 0; k < 3; k++) variant[random.Next(variant.Length)] = (byte)random.Next(256);
|
||
}
|
||
var path = Write($"fuzz{i}.cr2", variant);
|
||
try
|
||
{
|
||
provider.Probe(path);
|
||
using var image = provider.Decode(new DecodeRequest(path, MaxLongEdge: i % 2 == 0 ? 0 : 32, Purpose: DecodePurpose.Thumbnail));
|
||
}
|
||
catch (InvalidDataException)
|
||
{
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Assert.Fail($"variant {i}: {ex.GetType().Name}: {ex.Message}\n{ex.StackTrace}");
|
||
}
|
||
}
|
||
}
|
||
|
||
[Test]
|
||
public void TheChainProbesEachFileForItsTier()
|
||
{
|
||
var server = DecoderFactory.CreateServer();
|
||
// JPEG, PNG and TIFF decode natively whatever they hold: the probe does not even open them.
|
||
Assert.Equal(DecodeTier.Native, server.Probe(Path.Combine(_dir, "absent.jpg"), ImageFileFormat.Jpeg).Tier);
|
||
|
||
var supported = server.Probe(Write("IMG_5.CR2", Cr2File("Canon EOS 5D Mark III")), ImageFileFormat.Cr2);
|
||
Assert.Equal((DecodeTier.Native, "managed-cr2"), (supported.Tier, supported.Decoder));
|
||
|
||
// An unsupported body decodes only through its 64×48 preview, rotated like the main image (orientation 6).
|
||
var preview = server.Probe(Write("IMG_6.CR2", Cr2File("Canon EOS R99", orientation: 6)), ImageFileFormat.Cr2);
|
||
Assert.Equal((DecodeTier.EmbeddedPreview, 48, 64), (preview.Tier, preview.Width, preview.Height));
|
||
Assert.Contains("no colour matrix", preview.Reason);
|
||
|
||
using var scene = SyntheticRaw.Smooth(W, H);
|
||
var nef = Nef(Sensor(scene, "Nikon D850", CfaPattern.Bayer("RGGB"), W, H, 0, 0, 600, 16383), 14, NefEncoding.Lossless);
|
||
var unknownBody = System.Text.Encoding.Latin1.GetBytes(System.Text.Encoding.Latin1.GetString(nef).Replace("NIKON D850", "NIKON Z99X"));
|
||
var none = server.Probe(Write("DSC_7.NEF", unknownBody), ImageFileFormat.Nef);
|
||
Assert.Equal(DecodeTier.Unavailable, none.Tier);
|
||
Assert.Contains("no embedded preview", none.Reason);
|
||
|
||
// A Windows-like provider native for CR3 answers from its codec list, before the managed preview tier.
|
||
var client = DecoderFactory.CreateClient(new FakeProvider());
|
||
var cr3 = client.Probe(Path.Combine(_dir, "absent.cr3"), ImageFileFormat.Cr3);
|
||
Assert.Equal((DecodeTier.Native, "fake"), (cr3.Tier, cr3.Decoder));
|
||
Assert.Equal(DecodeTier.Unavailable, client.Probe(Write("y.webp", Fixtures.WebP()), ImageFileFormat.WebP).Tier);
|
||
}
|
||
|
||
[Test]
|
||
public void GoProRawDecodesThroughTheJpegBesideIt()
|
||
{
|
||
var gpr = Write("GOPR0001.GPR", Gpr(previewWidth: 64, previewHeight: 48));
|
||
var server = DecoderFactory.CreateServer();
|
||
// Alone, a GPR (VC-5, out of scope) shows its embedded preview.
|
||
using (var alone = server.Decode(new DecodeRequest(gpr)))
|
||
Assert.Equal(("managed-preview", 64), (alone!.Decoder, alone.Pixels.Width));
|
||
|
||
Write("GOPR0001.JPG", JpegFile(160, 120, 1));
|
||
using (var withJpeg = server.Decode(new DecodeRequest(gpr)))
|
||
{
|
||
Assert.Equal((DecodeTier.EmbeddedPreview, "managed-sidecar"), (withJpeg!.Tier, withJpeg.Decoder));
|
||
Assert.Equal((160, 120, 160, 120), (withJpeg.Pixels.Width, withJpeg.Pixels.Height, withJpeg.SourceWidth, withJpeg.SourceHeight));
|
||
}
|
||
using (var scaled = server.Decode(new DecodeRequest(gpr, MaxLongEdge: 40, Purpose: DecodePurpose.Thumbnail)))
|
||
Assert.Equal(40, scaled!.Pixels.Width);
|
||
var probe = new ManagedDecoderProvider().Probe(gpr);
|
||
Assert.Equal((DecodeTier.EmbeddedPreview, "managed-sidecar", 160, 120), (probe.Tier, probe.Decoder, probe.Width, probe.Height));
|
||
|
||
// A companion smaller than the embedded preview is not the better source.
|
||
var big = Write("GOPR0002.GPR", Gpr(previewWidth: 320, previewHeight: 240));
|
||
Write("GOPR0002.JPG", JpegFile(160, 120, 1));
|
||
using (var preview = server.Decode(new DecodeRequest(big)))
|
||
Assert.Equal(("managed-preview", 320), (preview!.Decoder, preview.Pixels.Width));
|
||
Assert.Equal("managed-preview", new ManagedDecoderProvider().Probe(big).Decoder);
|
||
}
|
||
|
||
/// <summary>A GoPro GPR as far as Titano reads it: a DNG made by "GoPro" whose only decodable image is a YCbCr JPEG preview.</summary>
|
||
private static byte[] Gpr(int previewWidth, int previewHeight)
|
||
{
|
||
var preview = new IfdSpec().Long(TiffTags.NewSubfileType, 1)
|
||
.Long(TiffTags.ImageWidth, (uint)previewWidth).Long(TiffTags.ImageLength, (uint)previewHeight)
|
||
.Short(TiffTags.BitsPerSample, 8, 8, 8).Short(TiffTags.Compression, 7)
|
||
.Short(TiffTags.PhotometricInterpretation, 6).Strip(JpegFile(previewWidth, previewHeight, 1));
|
||
var ifd0 = Fixtures.Ifd0("GoPro", "HERO8 Black", 1)
|
||
.Bytes(TiffTags.DngVersion, 1, 4, 0, 0)
|
||
.Child(TiffTags.SubIfds, preview);
|
||
return TiffFile.Build(ifd0);
|
||
}
|
||
|
||
/// <summary>A Windows-like provider claiming native support for everything but declining every file.</summary>
|
||
private sealed class FakeProvider : IImageDecoderProvider
|
||
{
|
||
public int Calls { get; private set; }
|
||
public string Name => "fake";
|
||
public DecodeTier? TierFor(ImageFileFormat format) => format is ImageFileFormat.Jpeg or ImageFileFormat.Cr3 ? DecodeTier.Native : null;
|
||
|
||
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default)
|
||
{
|
||
Calls++;
|
||
return null;
|
||
}
|
||
}
|
||
}
|