using Chronos.Core.Diagnostics; using Chronos.Core.Imaging; using Chronos.Core.Metadata.Bmff; using Chronos.Core.Metadata.Jpeg; using Chronos.Core.Metadata.Png; using Chronos.Core.Metadata.Raf; using Chronos.Core.Metadata.Riff; using Chronos.Core.Metadata.Tiff; namespace Chronos.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; } } }