diff --git a/src/Titano.Core/Metadata/ImageSignature.cs b/src/Titano.Core/Metadata/ImageSignature.cs new file mode 100644 index 00000000..1b5becc2 --- /dev/null +++ b/src/Titano.Core/Metadata/ImageSignature.cs @@ -0,0 +1,149 @@ +using System.Buffers.Binary; +using Titano.Core.Imaging; +using Titano.Core.Metadata.Tiff; + +namespace Titano.Core.Metadata; + +/// +/// Identifies a file's format from its leading bytes. TIFF-based files are refined by the CR2 marker, IFD0 +/// DNGVersion and Make; ISO-BMFF files by their ftyp brands. The file-name extension only breaks +/// ties between formats that share a container (NEF/NRW, ARW/SRF/SR2, 3FR/FFF, RW2/RWL/RAW, TIFF vs camera RAW). +/// +public static class ImageSignature +{ + private const int HeadBytes = 32; + private const int MaxFtypBytes = 256; + + private static ReadOnlySpan PngSignature => [0x89, (byte)'P', (byte)'N', (byte)'G', 0x0D, 0x0A, 0x1A, 0x0A]; + private static ReadOnlySpan JpegXrSignature => [(byte)'I', (byte)'I', 0xBC]; + private static ReadOnlySpan JpegXlBox => [0, 0, 0, 0x0C, (byte)'J', (byte)'X', (byte)'L', (byte)' ', 0x0D, 0x0A, 0x87, 0x0A]; + + private static readonly HashSet TiffBasedRaws = + [ + ImageFileFormat.Dng, ImageFileFormat.Cr2, ImageFileFormat.Nef, ImageFileFormat.Nrw, ImageFileFormat.Arw, + ImageFileFormat.Srf, ImageFileFormat.Sr2, ImageFileFormat.Orf, ImageFileFormat.Rw2, ImageFileFormat.Raw, + ImageFileFormat.Rwl, ImageFileFormat.Pef, ImageFileFormat.Srw, ImageFileFormat.ThreeFr, ImageFileFormat.Fff, + ImageFileFormat.Iiq, ImageFileFormat.Gpr, + ]; + + /// Detects the format of the bytes in (read from offset 0; the position is restored). + /// Optional name or path whose extension breaks ties; never overrides the signature. + /// when no known signature matches. + public static ImageFileFormat Detect(Stream stream, string? fileName) + { + ArgumentNullException.ThrowIfNull(stream); + if (!stream.CanSeek || !stream.CanRead) throw new ArgumentException("The stream must be readable and seekable.", nameof(stream)); + var position = stream.Position; + try + { + var extension = string.IsNullOrEmpty(fileName) ? ImageFileFormat.Unknown : ImageFormatInfo.FromPath(fileName).Format; + return DetectCore(stream, extension); + } + catch (IOException) + { + return ImageFileFormat.Unknown; + } + finally + { + stream.Position = position; + } + } + + private static ImageFileFormat DetectCore(Stream stream, ImageFileFormat extension) + { + Span buffer = stackalloc byte[HeadBytes]; + var h = buffer[..stream.ReadUpTo(0, buffer)]; + if (h.StartsWith((ReadOnlySpan)[0xFF, 0xD8, 0xFF])) return ImageFileFormat.Jpeg; + if (h.StartsWith(PngSignature)) return ImageFileFormat.Png; + if (h.StartsWith("GIF87a"u8) || h.StartsWith("GIF89a"u8)) return ImageFileFormat.Gif; + if (h.StartsWith("FUJIFILMCCD-RAW "u8)) return ImageFileFormat.Raf; + if (h.Length >= 12 && h.StartsWith("RIFF"u8) && h[8..12].SequenceEqual("WEBP"u8)) return ImageFileFormat.WebP; + if (h.StartsWith((ReadOnlySpan)[0xFF, 0x0A]) || h.StartsWith(JpegXlBox)) return ImageFileFormat.JpegXl; + if (h.Length >= 12 && h[4..8].SequenceEqual("ftyp"u8)) return FromBrands(stream, BinaryPrimitives.ReadUInt32BigEndian(h)); + if (h.Length >= 4 && h.StartsWith(JpegXrSignature) && h[3] <= 1) return ImageFileFormat.JpegXr; + if (h.Length >= 14 && h[6..14].SequenceEqual("HEAPCCDR"u8)) return ImageFileFormat.Crw; + if (h.StartsWith("IIRO"u8) || h.StartsWith("IIRS"u8) || h.StartsWith("MMOR"u8)) return ImageFileFormat.Orf; + if (h.StartsWith("IIU\0"u8)) return Panasonic(stream, extension); + if (h.StartsWith("II*\0"u8) || h.StartsWith("MM\0*"u8)) return Tiff(stream, h, extension); + if (h.Length >= 18 && h.StartsWith("BM"u8) && BinaryPrimitives.ReadUInt32LittleEndian(h[14..]) is 12 or 16 or 40 or 52 or 56 or 64 or 108 or 124) + return ImageFileFormat.Bmp; + return ImageFileFormat.Unknown; + } + + /// CR3 (crx), AVIF (avif/avis) or HEIF (heic, heix, hevc, hevx, heim, heis, mif1, msf1); major brand first. + private static ImageFileFormat FromBrands(Stream stream, uint ftypSize) + { + Span buffer = stackalloc byte[MaxFtypBytes]; + var n = stream.ReadUpTo(0, buffer[..(int)Math.Clamp(ftypSize, 16, MaxFtypBytes)]); + if (n < 12) return ImageFileFormat.Unknown; + var major = Brand(buffer[8..12]); + if (major == BrandKind.Cr3) return ImageFileFormat.Cr3; + if (major == BrandKind.Avif) return ImageFileFormat.Avif; + if (major == BrandKind.Heif) return ImageFileFormat.Heif; + var heif = major == BrandKind.Image; + for (var i = 16; i + 4 <= n; i += 4) + { + var brand = Brand(buffer.Slice(i, 4)); + if (brand == BrandKind.Avif) return ImageFileFormat.Avif; + heif |= brand is BrandKind.Heif or BrandKind.Image; + } + return heif ? ImageFileFormat.Heif : ImageFileFormat.Unknown; + } + + private enum BrandKind + { + Other, + Cr3, + Avif, + Heif, + Image, + } + + private static BrandKind Brand(ReadOnlySpan b) => + b.SequenceEqual("crx "u8) ? BrandKind.Cr3 + : b.SequenceEqual("avif"u8) || b.SequenceEqual("avis"u8) ? BrandKind.Avif + : b.SequenceEqual("heic"u8) || b.SequenceEqual("heix"u8) || b.SequenceEqual("hevc"u8) || b.SequenceEqual("hevx"u8) + || b.SequenceEqual("heim"u8) || b.SequenceEqual("heis"u8) ? BrandKind.Heif + : b.SequenceEqual("mif1"u8) || b.SequenceEqual("msf1"u8) ? BrandKind.Image + : BrandKind.Other; + + private static ImageFileFormat Panasonic(Stream stream, ImageFileFormat extension) + { + if (extension is ImageFileFormat.Rwl or ImageFileFormat.Raw) return extension; + return Make(stream)?.StartsWith("LEICA", StringComparison.OrdinalIgnoreCase) == true ? ImageFileFormat.Rwl : ImageFileFormat.Rw2; + } + + private static ImageFileFormat Tiff(Stream stream, ReadOnlySpan h, ImageFileFormat extension) + { + if (h.Length >= 10 && h[8..10].SequenceEqual("CR"u8)) return ImageFileFormat.Cr2; + var reader = TiffReader.Open(stream); + var ifd0 = reader?.ReadIfd(reader.FirstIfdOffset); + var make = reader is not null && ifd0 is not null ? IfdValue.String(reader, ifd0, TiffTags.Make) : null; + if (ifd0?.Has(TiffTags.DngVersion) == true) + return extension == ImageFileFormat.Gpr || make?.StartsWith("GoPro", StringComparison.OrdinalIgnoreCase) == true ? ImageFileFormat.Gpr : ImageFileFormat.Dng; + if (extension == ImageFileFormat.Tiff) return ImageFileFormat.Tiff; + var byMake = ByMake(make, extension); + if (byMake != ImageFileFormat.Unknown) return byMake; + return TiffBasedRaws.Contains(extension) ? extension : ImageFileFormat.Tiff; + } + + private static ImageFileFormat ByMake(string? make, ImageFileFormat extension) + { + if (make is null) return ImageFileFormat.Unknown; + bool Is(string prefix) => make.StartsWith(prefix, StringComparison.OrdinalIgnoreCase); + if (Is("NIKON")) return extension == ImageFileFormat.Nrw ? ImageFileFormat.Nrw : ImageFileFormat.Nef; + if (Is("SONY")) return extension is ImageFileFormat.Srf or ImageFileFormat.Sr2 ? extension : ImageFileFormat.Arw; + if (Is("PENTAX") || Is("RICOH")) return ImageFileFormat.Pef; + if (Is("SAMSUNG")) return ImageFileFormat.Srw; + if (Is("Hasselblad")) return extension == ImageFileFormat.Fff ? ImageFileFormat.Fff : ImageFileFormat.ThreeFr; + if (Is("Imacon")) return ImageFileFormat.Fff; + if (Is("Phase One") || Is("Leaf")) return ImageFileFormat.Iiq; + return ImageFileFormat.Unknown; + } + + private static string? Make(Stream stream) + { + var reader = TiffReader.Open(stream); + return reader?.ReadIfd(reader.FirstIfdOffset) is { } ifd ? IfdValue.String(reader, ifd, TiffTags.Make) : null; + } +} diff --git a/src/Titano.Core/Metadata/MetadataReader.cs b/src/Titano.Core/Metadata/MetadataReader.cs new file mode 100644 index 00000000..74bca4df --- /dev/null +++ b/src/Titano.Core/Metadata/MetadataReader.cs @@ -0,0 +1,110 @@ +using Titano.Core.Diagnostics; +using Titano.Core.Imaging; +using Titano.Core.Metadata.Bmff; +using Titano.Core.Metadata.Jpeg; +using Titano.Core.Metadata.Png; +using Titano.Core.Metadata.Raf; +using Titano.Core.Metadata.Riff; +using Titano.Core.Metadata.Tiff; + +namespace Titano.Core.Metadata; + +/// +/// Reads from any supported photo (BRIEF ยง9). Detects the container by signature, then +/// reads only the structures it needs through bounded seeks (a RAW's image data is never read). Malformed input never +/// throws: the result carries whatever was readable plus . +/// +public static class MetadataReader +{ + private const string LogCategory = "Metadata"; + private const int FileBufferSize = 4096; + + /// Reads the file at ; its modification time is the last-resort capture time. + /// The file cannot be opened (missing or locked). + /// Access to the file is denied. + public static FrameMetadata Read(string path, CancellationToken cancellationToken = default) + { + ArgumentException.ThrowIfNullOrEmpty(path); + using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, FileBufferSize, FileOptions.RandomAccess); + return Read(stream, Path.GetFileName(path), File.GetLastWriteTime(path), cancellationToken); + } + + /// Reads metadata from a readable, seekable stream (the position is left undefined). + /// Name used only to break signature ties (see ). + /// File time used when no capture time is recorded (); + /// its UTC offset is taken from the local zone for values. + /// Cancelled through . + public static FrameMetadata Read(Stream stream, string? fileName, DateTime? fileModifiedLocal = null, CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(stream); + if (!stream.CanSeek || !stream.CanRead) throw new ArgumentException("The stream must be readable and seekable.", nameof(stream)); + cancellationToken.ThrowIfCancellationRequested(); + var m = new MetadataBuilder(stream, cancellationToken); + try + { + m.Format = ImageSignature.Detect(stream, fileName); + Parse(m); + } + catch (OperationCanceledException) + { + throw; + } + catch (IOException ex) + { + m.Warn("I/O error: " + ex.Message); + } + catch (Exception ex) + { + // Safety net: parsers are written not to throw, but a defect must cost one file's metadata, not the import. + m.Warn($"Parser fault: {ex.GetType().Name}: {ex.Message}"); + Log.Warning(LogCategory, $"Metadata parser fault in {fileName ?? "stream"}", ex); + } + return m.Build(fileModifiedLocal); + } + + private static void Parse(MetadataBuilder m) + { + switch (m.Format) + { + case ImageFileFormat.Jpeg: + JpegParser.Parse(m, 0, m.Length, primary: true); + break; + case ImageFileFormat.Png: + PngParser.Parse(m); + break; + case ImageFileFormat.WebP: + WebPParser.Parse(m); + break; + case ImageFileFormat.Heif or ImageFileFormat.Avif: + HeifParser.Parse(m); + break; + case ImageFileFormat.Cr3: + Cr3Parser.Parse(m); + break; + case ImageFileFormat.Raf: + RafParser.Parse(m); + break; + case ImageFileFormat.Gif: + MinimalFormatParser.ParseGif(m); + break; + case ImageFileFormat.Bmp: + MinimalFormatParser.ParseBmp(m); + break; + case ImageFileFormat.JpegXr: + MinimalFormatParser.ParseJpegXr(m); + break; + case ImageFileFormat.JpegXl: + MinimalFormatParser.ParseJpegXl(m); + break; + case ImageFileFormat.Crw: + m.Warn("CRW (CIFF) metadata is not supported"); + break; + case ImageFileFormat.Unknown: + m.Warn("Unrecognised file signature"); + break; + default: + ExifParser.ParseFile(m); + break; + } + } +}