diff --git a/src/Titano.Core/Imaging/Decoders/ManagedDecoderProvider.cs b/src/Titano.Core/Imaging/Decoders/ManagedDecoderProvider.cs index 2021371f..9b8c7e2f 100644 --- a/src/Titano.Core/Imaging/Decoders/ManagedDecoderProvider.cs +++ b/src/Titano.Core/Imaging/Decoders/ManagedDecoderProvider.cs @@ -12,7 +12,9 @@ namespace Titano.Core.Imaging.Decoders; /// Best tier available for this file. /// Decoder that would produce it ("managed-cr2", "managed-preview"…), null when unavailable. /// Why the native tier is not used (unsupported body or encoding), else null. -public sealed record FileTier(ImageFileFormat Format, DecodeTier Tier, string? Decoder, string? Reason); +/// Full-size width that tier delivers (developed RAW, preview or image size), oriented; 0 when unknown. +/// Full-size height, oriented; 0 when unknown. +public sealed record FileTier(ImageFileFormat Format, DecodeTier Tier, string? Decoder, string? Reason, int Width, int Height); /// /// The managed decoder (BRIEF §8.2), available on every platform and the only one on the server: JPEG, PNG and TIFF @@ -66,34 +68,29 @@ public sealed class ManagedDecoderProvider : IImageDecoderProvider using var stream = Open(path); var format = ImageSignature.Detect(stream, path); var family = ImageFormatInfo.Of(format).Family; - switch (family) + if (family is ImageFamily.Jpeg or ImageFamily.Png or ImageFamily.Tiff) { - case ImageFamily.Jpeg: - return new FileTier(format, DecodeTier.Native, "managed-jpeg", null); - case ImageFamily.Png: - return new FileTier(format, DecodeTier.Native, "managed-png", null); - case ImageFamily.Tiff: - return new FileTier(format, DecodeTier.Native, "managed-tiff", null); + 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); } - string? reason = null; - if (family is ImageFamily.Dng or ImageFamily.CameraRaw && RawDecoder.Formats.Contains(format)) + 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); + string reason; + if (RawDecoder.Formats.Contains(format)) { var probe = RawDecoder.Probe(stream, path); - if (probe.IsSupported) return new FileTier(format, DecodeTier.Native, probe.Info!.Decoder, null); - reason = probe.Reason; - } - else if (family is ImageFamily.Dng or ImageFamily.CameraRaw or ImageFamily.Heif) - { - reason = $"no managed decoder for {format}"; + if (probe.Info is { } info) return new FileTier(format, DecodeTier.Native, info.Decoder, null, info.Width, info.Height); + reason = probe.Reason ?? "unsupported"; } else { - return new FileTier(format, DecodeTier.Unavailable, null, $"no managed decoder for {format}"); + reason = $"no managed decoder for {format}"; } - var previews = MetadataReader.Read(stream, path).Previews; - return previews.Count > 0 - ? new FileTier(format, DecodeTier.EmbeddedPreview, "managed-preview", reason) - : new FileTier(format, DecodeTier.Unavailable, null, reason + "; no embedded preview"); + var metadata = MetadataReader.Read(stream, path); + if (metadata.LargestPreview is not { } preview) return new FileTier(format, DecodeTier.Unavailable, null, reason + "; no embedded preview", 0, 0); + var (pw, ph) = PixelConvert.OrientedSize(preview.Width, preview.Height, metadata.Orientation); + return new FileTier(format, DecodeTier.EmbeddedPreview, "managed-preview", reason, pw, ph); } private static FileStream Open(string path) => diff --git a/src/Titano.Tools/Commands/DiagnoseCommand.cs b/src/Titano.Tools/Commands/DiagnoseCommand.cs new file mode 100644 index 00000000..d6c1d958 --- /dev/null +++ b/src/Titano.Tools/Commands/DiagnoseCommand.cs @@ -0,0 +1,100 @@ +using System.Globalization; +using Titano.Core.Imaging; +using Titano.Core.Imaging.Decoders; +using Titano.Core.Metadata; + +namespace Titano.Tools.Commands; + +/// +/// Per-file decode report for a folder of photos: format, the tier the managed decoders reach (the server's view — WIC +/// does not exist in this tool), dimensions, camera, capture time and its source, warnings; then a summary per format. +/// +internal sealed class DiagnoseCommand : ICommand +{ + public string Name => "diagnose"; + public string Summary => "Decode tiers and metadata of the photos in a folder (--recursive, --formats)"; + + public Task RunAsync(string[] args) + { + var a = new Args(args, "recursive", "formats"); + if (a.Has("formats")) PrintFormats(); + if (a.Positional.Count == 0) + { + if (a.Has("formats")) return Task.FromResult(0); + throw new ToolException("Usage: diagnose [--recursive] [--formats]"); + } + var folder = Path.GetFullPath(a.Positional[0]); + if (!Directory.Exists(folder)) throw new ToolException($"Folder not found: {folder}"); + var files = Directory.EnumerateFiles(folder, "*", a.Has("recursive") ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly) + .Where(ImageFormatInfo.IsSupportedExtension) + .Order(StringComparer.OrdinalIgnoreCase) + .ToList(); + Console.WriteLine($"{files.Count} photo(s) in {folder}. Tiers are the managed (server) view; WIC is not available in Titano.Tools."); + var provider = new ManagedDecoderProvider(); + var summary = new SortedDictionary(StringComparer.Ordinal); + foreach (var file in files) + { + var line = Describe(provider, folder, file, out var format, out var tier); + Console.WriteLine(line); + if (!summary.TryGetValue(format, out var counts)) summary[format] = counts = new int[3]; + counts[(int)tier - 1]++; + } + if (summary.Count > 0) + { + Console.WriteLine(); + Console.WriteLine($"{"Format",-8} {"Files",6} {"Native",7} {"Preview",8} {"None",5}"); + foreach (var (format, c) in summary) Console.WriteLine($"{format,-8} {c.Sum(),6} {c[0],7} {c[1],8} {c[2],5}"); + } + return Task.FromResult(0); + } + + private static string Describe(ManagedDecoderProvider provider, string folder, string file, out string format, out DecodeTier tier) + { + var name = Path.GetRelativePath(folder, file); + FrameMetadata metadata; + FileTier fileTier; + try + { + metadata = MetadataReader.Read(file); + fileTier = provider.Probe(file); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + format = ImageFormatInfo.FromPath(file).Format.ToString(); + tier = DecodeTier.Unavailable; + return $"{name}\n unreadable: {ex.Message}"; + } + format = fileTier.Format.ToString(); + tier = fileTier.Tier; + var (w, h) = fileTier.Width > 0 ? (fileTier.Width, fileTier.Height) : metadata.OrientedSize; + var camera = string.Join(' ', new[] { metadata.Make, metadata.Model }.Where(s => !string.IsNullOrEmpty(s))); + var captured = metadata.CapturedAt is { } t + ? $"{t.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture)} ({metadata.TimestampSource})" + : "unknown"; + var tierText = fileTier.Tier switch + { + DecodeTier.Native => $"native ({fileTier.Decoder})", + DecodeTier.EmbeddedPreview => "embedded preview", + _ => "unavailable", + }; + var lines = new List { $"{name}\n {format,-5} {tierText,-24} {$"{w}x{h}",-11} {(camera.Length > 0 ? camera : "camera unknown")}; captured {captured}" }; + if (fileTier.Reason is { } reason) lines.Add($" reason: {reason}"); + lines.AddRange(metadata.Warnings.Select(warning => $" warning: {warning}")); + return string.Join('\n', lines); + } + + private static void PrintFormats() + { + Console.WriteLine("Format tiers (managed decoders):"); + foreach (var row in DecoderFactory.DescribeFormats(DecoderFactory.CreateServer())) + { + var tier = row.Tier switch + { + DecodeTier.Native => "native", + DecodeTier.EmbeddedPreview => "embedded preview", + _ => "unavailable", + }; + Console.WriteLine($" {row.Format.Format,-8} {string.Join(' ', row.Format.Extensions),-18} {tier}"); + } + } +} diff --git a/src/Titano.Tools/Commands/FixturesCommand.cs b/src/Titano.Tools/Commands/FixturesCommand.cs new file mode 100644 index 00000000..65d823af --- /dev/null +++ b/src/Titano.Tools/Commands/FixturesCommand.cs @@ -0,0 +1,147 @@ +using System.Net; +using System.Net.Http.Headers; +using System.Security.Cryptography; +using System.Text.Json; + +namespace Titano.Tools.Commands; + +/// +/// Fetches the CC0 RAW samples listed in tests/fixtures/manifest.json into tests/fixtures/external (BRIEF +/// §20.2): interrupted downloads resume from their .part file with an HTTP range request, every file is verified +/// against its SHA-256, and verified files are skipped. An entry whose sha256 is null gets the hash of its first +/// download recorded in external/recorded-hashes.json (later runs verify against it) and printed so it can be +/// copied into the manifest. +/// +internal sealed class FixturesCommand : ICommand +{ + private const string RecordedHashesFile = "recorded-hashes.json"; + + public string Name => "fixtures"; + public string Summary => "Download and verify the CC0 RAW samples in tests/fixtures/manifest.json (--manifest, --out)"; + + public async Task RunAsync(string[] args) + { + var a = new Args(args); + var root = Args.RepoRoot(); + var manifestPath = a.Get("manifest") ?? Path.Combine(root, "tests", "fixtures", "manifest.json"); + var outDir = a.Get("out") ?? Path.Combine(root, "tests", "fixtures", "external"); + if (!File.Exists(manifestPath)) throw new ToolException($"Manifest not found: {manifestPath}"); + var entries = ReadManifest(manifestPath); + Directory.CreateDirectory(outDir); + var recordedPath = Path.Combine(outDir, RecordedHashesFile); + var recorded = File.Exists(recordedPath) + ? JsonSerializer.Deserialize>(await File.ReadAllTextAsync(recordedPath)) ?? [] + : []; + var newlyRecorded = new List(); + var failures = 0; + using var http = new HttpClient { Timeout = TimeSpan.FromMinutes(10) }; + http.DefaultRequestHeaders.UserAgent.ParseAdd("Titano.Tools/1.0 (+fixtures)"); + foreach (var entry in entries) + { + var target = Path.Combine(outDir, entry.Name); + var expected = entry.Sha256 ?? recorded.GetValueOrDefault(entry.Name); + try + { + if (File.Exists(target)) + { + var hash = await HashAsync(target); + if (expected is null || hash.Equals(expected, StringComparison.OrdinalIgnoreCase)) + { + if (expected is null) Record(entry.Name, hash); + Console.WriteLine($"ok {entry.Name} (verified)"); + continue; + } + Console.WriteLine($"mismatch {entry.Name}: downloading again"); + File.Delete(target); + } + var actual = await DownloadAsync(http, entry, target); + if (expected is not null && !actual.Equals(expected, StringComparison.OrdinalIgnoreCase)) + { + File.Delete(target); + throw new InvalidDataException($"SHA-256 {actual} does not match {expected}"); + } + if (expected is null) Record(entry.Name, actual); + Console.WriteLine($"fetched {entry.Name} ({new FileInfo(target).Length / 1e6:F1} MB)"); + } + catch (Exception ex) when (ex is HttpRequestException or IOException or InvalidDataException or TaskCanceledException or UnauthorizedAccessException) + { + failures++; + Console.Error.WriteLine($"failed {entry.Name}: {ex.Message}"); + } + } + await File.WriteAllTextAsync(recordedPath, JsonSerializer.Serialize(recorded, new JsonSerializerOptions { WriteIndented = true })); + if (newlyRecorded.Count > 0) + { + Console.WriteLine("Recorded SHA-256 of first downloads (copy into the manifest to pin them):"); + foreach (var line in newlyRecorded) Console.WriteLine(" " + line); + } + Console.WriteLine(failures == 0 ? $"{entries.Count} fixture(s) ready in {outDir}" : $"{failures} of {entries.Count} fixture(s) failed"); + return failures == 0 ? 0 : 1; + + void Record(string name, string hash) + { + if (recorded.GetValueOrDefault(name) == hash) return; + recorded[name] = hash; + newlyRecorded.Add($"{name}: {hash}"); + } + } + + internal sealed record Entry(string Name, Uri Url, string? Sha256, long? Size); + + internal static List ReadManifest(string path) + { + using var doc = JsonDocument.Parse(File.ReadAllText(path)); + if (!doc.RootElement.TryGetProperty("files", out var files) || files.ValueKind != JsonValueKind.Array) throw new ToolException("Manifest has no 'files' array"); + var entries = new List(); + foreach (var f in files.EnumerateArray()) + { + var name = f.GetProperty("name").GetString() ?? throw new ToolException("Fixture without a name"); + if (name != Path.GetFileName(name) || name.StartsWith('.')) throw new ToolException($"Fixture name must be a plain file name: {name}"); + if (!Uri.TryCreate(f.GetProperty("url").GetString(), UriKind.Absolute, out var url) || url.Scheme != Uri.UriSchemeHttps) + throw new ToolException($"Fixture {name} needs an https URL"); + var sha = f.TryGetProperty("sha256", out var s) && s.ValueKind == JsonValueKind.String ? s.GetString() : null; + long? size = f.TryGetProperty("size", out var z) && z.ValueKind == JsonValueKind.Number ? z.GetInt64() : null; + entries.Add(new Entry(name, url, sha, size)); + } + return entries; + } + + /// Downloads into target.part, resuming from its length with a range request, then renames it. + private static async Task DownloadAsync(HttpClient http, Entry entry, string target) + { + var part = target + ".part"; + var existing = File.Exists(part) ? new FileInfo(part).Length : 0; + if (entry.Size is not { } size || existing < size) + { + using var request = new HttpRequestMessage(HttpMethod.Get, entry.Url); + if (existing > 0) request.Headers.Range = new RangeHeaderValue(existing, null); + using var response = await http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); + if (response.StatusCode == HttpStatusCode.RequestedRangeNotSatisfiable && existing > 0) + { + // The partial file is already complete (or the server lost it): verified below. + } + else + { + response.EnsureSuccessStatusCode(); + var resume = response.StatusCode == HttpStatusCode.PartialContent; + await using var output = new FileStream(part, resume ? FileMode.Append : FileMode.Create, FileAccess.Write, FileShare.None, 1 << 16, FileOptions.Asynchronous); + await using var input = await response.Content.ReadAsStreamAsync(); + await input.CopyToAsync(output); + } + } + if (entry.Size is { } expected && new FileInfo(part).Length is var length && length != expected) + { + File.Delete(part); + throw new InvalidDataException($"size {length} differs from {expected}"); + } + var hash = await HashAsync(part); + File.Move(part, target, overwrite: true); + return hash; + } + + private static async Task HashAsync(string path) + { + await using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 1 << 16, FileOptions.Asynchronous | FileOptions.SequentialScan); + return Convert.ToHexStringLower(await SHA256.HashDataAsync(stream)); + } +} diff --git a/src/Titano.Tools/Program.cs b/src/Titano.Tools/Program.cs index d5575d16..550f05a4 100644 --- a/src/Titano.Tools/Program.cs +++ b/src/Titano.Tools/Program.cs @@ -8,6 +8,8 @@ var commands = new ICommand[] new XamlLintCommand(), new PackageGuardCommand(), new ChangelogCommand(), + new DiagnoseCommand(), + new FixturesCommand(), }; if (args.Length == 0 || args[0] is "-h" or "--help" or "help") diff --git a/tests/Titano.Core.Tests/Decoding/ExternalFixtureTests.cs b/tests/Titano.Core.Tests/Decoding/ExternalFixtureTests.cs new file mode 100644 index 00000000..f641ff7c --- /dev/null +++ b/tests/Titano.Core.Tests/Decoding/ExternalFixtureTests.cs @@ -0,0 +1,84 @@ +using System.Text.Json; +using Titano.Core.Imaging; +using Titano.Core.Imaging.Decoders; +using Titano.Core.Metadata; +using Titano.Testing; + +namespace Titano.Core.Tests.Decoding; + +/// Real camera files from tests/fixtures/manifest.json (fetched by Titano.Tools fixtures); each test is +/// skipped when its file is absent. A native decode must reach the expected size and look like the camera's own +/// embedded preview (luminance correlation), which catches wrong byte orders, CFA phases or colour matrices. +public sealed class ExternalFixtureTests(TestContext context) +{ + private const int Edge = 256; + + private static JsonElement Entry(string name) + { + var manifest = JsonDocument.Parse(File.ReadAllText(Path.Combine(TestContext.FixturesDirectory, "manifest.json"))); + foreach (var f in manifest.RootElement.GetProperty("files").EnumerateArray()) + { + if (f.GetProperty("name").GetString() == name) return f.Clone(); + } + throw new InvalidOperationException("No manifest entry " + name); + } + + [Theory] + [Case("google-pixel-4a.dng")] + [Case("canon-eos-5d-mark-iii.cr2")] + [Case("nikon-d850.nef")] + [Case("sony-ilce-7m3.arw")] + [Case("fujifilm-x-t2.raf")] + [Case("pentax-k-1.pef")] + [Timeout(20_000)] + public void DecodesNativelyAndMatchesThePreview(string name) + { + var path = Path.Combine(TestContext.FixturesDirectory, "external", name); + if (!File.Exists(path)) TestContext.Skip($"{name} not fetched (run Titano.Tools fixtures)"); + var entry = Entry(name); + var metadata = MetadataReader.Read(path); + Assert.Equal(entry.GetProperty("format").GetString(), metadata.Format.ToString()); + var probe = new ManagedDecoderProvider().Probe(path); + context.Log($"{name}: {probe}; {metadata.Make} {metadata.Model}, {metadata.Width}x{metadata.Height}, orientation {metadata.Orientation}"); + Assert.Equal(DecodeTier.Native, probe.Tier); + + var decoder = DecoderFactory.CreateServer(); + using var native = decoder.Decode(new DecodeRequest(path, Edge, DecodePurpose.Render, ApplyOrientation: false)); + Assert.Equal(DecodeTier.Native, native!.Tier); + var (w, h) = (entry.GetProperty("width").GetInt32(), entry.GetProperty("height").GetInt32()); + Assert.InRange(native.SourceWidth, w * 0.98, w * 1.02); + Assert.InRange(native.SourceHeight, h * 0.98, h * 1.02); + foreach (var v in native.Pixels.Data) + { + if (!float.IsFinite(v)) Assert.Fail("non-finite pixel"); + } + + using var preview = decoder.Decode(new DecodeRequest(path, Edge, DecodePurpose.Render, ApplyOrientation: false, ForcePreview: true)); + if (preview is null || preview.Tier != DecodeTier.EmbeddedPreview) TestContext.Skip("no embedded preview to compare with"); + var correlation = Correlation(native.Pixels, preview.Pixels); + context.Log($"luminance correlation with the embedded preview: {correlation:F3}"); + Assert.Greater(correlation, 0.8); + } + + /// Pearson correlation of sRGB-encoded luminance after resizing both to a common 64-pixel-wide grid. + private static double Correlation(ImageBuffer a, ImageBuffer b) + { + var height = int.Max(1, (int)double.Round(64.0 * a.Height / a.Width)); + using var ra = Resampler.Resize(a, 64, height, ResampleFilter.Box); + using var rb = Resampler.Resize(b, 64, height, ResampleFilter.Box); + var n = ra.PlaneLength; + double sa = 0, sb = 0, saa = 0, sbb = 0, sab = 0; + for (var i = 0; i < n; i++) + { + double la = ColorSpace.EncodeFast(ColorSpace.Luminance(ra.Plane(0)[i], ra.Plane(1)[i], ra.Plane(2)[i])); + double lb = ColorSpace.EncodeFast(ColorSpace.Luminance(rb.Plane(0)[i], rb.Plane(1)[i], rb.Plane(2)[i])); + sa += la; + sb += lb; + saa += la * la; + sbb += lb * lb; + sab += la * lb; + } + var cov = sab / n - sa / n * (sb / n); + return cov / double.Sqrt(double.Max(saa / n - sa / n * (sa / n), 1e-12) * double.Max(sbb / n - sb / n * (sb / n), 1e-12)); + } +} diff --git a/tests/fixtures/manifest.json b/tests/fixtures/manifest.json new file mode 100644 index 00000000..73815b4d --- /dev/null +++ b/tests/fixtures/manifest.json @@ -0,0 +1,78 @@ +{ + "description": "CC0 RAW samples from raw.pixls.us (one per managed RAW decoder), fetched by 'Titano.Tools fixtures' into tests/fixtures/external (git-ignored, CI-cached on this file). sha256 null: the first download records its hash in external/recorded-hashes.json and prints it for pinning here. width/height: size the managed decoder develops (before orientation). urlVerified false: the URL follows the site's data/// layout but was written without network access; replace it with the sample's download link if the fetch reports 404.", + "license": "CC0-1.0", + "files": [ + { + "name": "google-pixel-4a.dng", + "url": "https://raw.pixls.us/data/Google/Pixel%204a/PXL_20201101_120000000.dng", + "sha256": null, + "size": null, + "format": "Dng", + "make": "Google", + "model": "Pixel 4a", + "width": 4032, + "height": 3024, + "urlVerified": false + }, + { + "name": "canon-eos-5d-mark-iii.cr2", + "url": "https://raw.pixls.us/data/Canon/EOS%205D%20Mark%20III/IMG_0001.CR2", + "sha256": null, + "size": null, + "format": "Cr2", + "make": "Canon", + "model": "Canon EOS 5D Mark III", + "width": 5760, + "height": 3840, + "urlVerified": false + }, + { + "name": "nikon-d850.nef", + "url": "https://raw.pixls.us/data/Nikon/D850/DSC_0001.NEF", + "sha256": null, + "size": null, + "format": "Nef", + "make": "NIKON CORPORATION", + "model": "NIKON D850", + "width": 8288, + "height": 5520, + "urlVerified": false + }, + { + "name": "sony-ilce-7m3.arw", + "url": "https://raw.pixls.us/data/Sony/ILCE-7M3/DSC00001.ARW", + "sha256": null, + "size": null, + "format": "Arw", + "make": "SONY", + "model": "ILCE-7M3", + "width": 6048, + "height": 4024, + "urlVerified": false + }, + { + "name": "fujifilm-x-t2.raf", + "url": "https://raw.pixls.us/data/Fujifilm/X-T2/DSCF0001.RAF", + "sha256": null, + "size": null, + "format": "Raf", + "make": "FUJIFILM", + "model": "X-T2", + "width": 6000, + "height": 4000, + "urlVerified": false + }, + { + "name": "pentax-k-1.pef", + "url": "https://raw.pixls.us/data/Pentax/K-1/IMGP0001.PEF", + "sha256": null, + "size": null, + "format": "Pef", + "make": "RICOH IMAGING COMPANY, LTD.", + "model": "PENTAX K-1", + "width": 7392, + "height": 4950, + "urlVerified": false + } + ] +}