chore: bootstrap repository foundation

Solution layout, build rules with zero-package guard, in-house test
framework, localization runtime, brand mark rasteriser with ICO/PNG
generation, Core contracts (imaging, video, domain model, TIFF reader),
Titano.Tools (make-icon, i18n, xaml-lint, package-guard) and docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 08:59:40 +00:00
1 parent def3fadb6e
commit 83a54fb57b
109 files changed
+7523

No files matched your search

+71
View File
@@ -0,0 +1,71 @@
using Titano.Core.Imaging;
using Titano.Core.Model;
namespace Titano.Core.Metadata;
public enum PreviewKind
{
Thumbnail,
Preview,
FullSize,
}
/// <summary>An embedded JPEG inside a RAW/TIFF/HEIF/JPEG(MPF) container.</summary>
public sealed record EmbeddedPreview(long Offset, long Length, int Width, int Height, PreviewKind Kind);
public sealed record GpsPosition(double Latitude, double Longitude, double? Altitude, DateTime? UtcTimestamp);
/// <summary>Everything Titano reads from one photo's metadata (BRIEF §9). Never throws while parsing:
/// malformed input yields partial data plus <see cref="Warnings"/>.</summary>
public sealed record FrameMetadata
{
public ImageFileFormat Format { get; init; }
public string? Make { get; init; }
public string? Model { get; init; }
public string? BodySerial { get; init; }
public string? LensModel { get; init; }
public string? Software { get; init; }
public string? Artist { get; init; }
public string? Copyright { get; init; }
/// <summary>Camera wall-clock time including sub-seconds (DateTimeKind.Unspecified).</summary>
public DateTime? CapturedAt { get; init; }
public int? UtcOffsetMinutes { get; init; }
public TimestampSource TimestampSource { get; init; }
public double? ExposureTime { get; init; }
public double? FNumber { get; init; }
public int? Iso { get; init; }
public double? FocalLength { get; init; }
public double? FocalLength35 { get; init; }
public double? ExposureBias { get; init; }
public int? ExposureProgram { get; init; }
public int? MeteringMode { get; init; }
public int? WhiteBalanceMode { get; init; }
public int? ColorTemperature { get; init; }
public int? Flash { get; init; }
/// <summary>Exif orientation 1–8.</summary>
public int Orientation { get; init; } = 1;
/// <summary>Dimensions of the main (largest) image as stored, before orientation.</summary>
public int Width { get; init; }
public int Height { get; init; }
public int? BitsPerSample { get; init; }
public GpsPosition? Gps { get; init; }
public int? Rating { get; init; }
public string? Label { get; init; }
public long? ShutterCount { get; init; }
public bool? ImageStabilization { get; init; }
/// <summary>Embedded JPEGs, largest first.</summary>
public IReadOnlyList<EmbeddedPreview> Previews { get; init; } = [];
public IReadOnlyList<string> Warnings { get; init; } = [];
/// <summary>Width/height after applying orientation 5–8 (90° rotations).</summary>
public (int Width, int Height) OrientedSize => Orientation is >= 5 and <= 8 ? (Height, Width) : (Width, Height);
public EmbeddedPreview? LargestPreview => Previews.Count > 0 ? Previews[0] : null;
}
+251
View File
@@ -0,0 +1,251 @@
using System.Buffers.Binary;
using System.Text;
namespace Titano.Core.Metadata.Tiff;
/// <summary>One IFD entry. Small values (≤ 4 bytes) are inline; larger ones live at <see cref="ValueOffset"/>.</summary>
public readonly record struct IfdEntry(ushort Tag, TiffType Type, uint Count, uint ValueOffset, long EntryPosition)
{
public long ByteLength => (long)Count * TiffTypeInfo.Size(Type);
public bool IsInline => ByteLength <= 4;
/// <summary>Absolute file position of the value bytes (inline values sit inside the entry).</summary>
public long ValuePosition(long tiffBase) => IsInline ? EntryPosition + 8 : tiffBase + ValueOffset;
}
/// <summary>An image file directory: its entries and the offset of the next IFD (0 = none).</summary>
public sealed class Ifd(long position, IReadOnlyDictionary<ushort, IfdEntry> entries, uint nextOffset)
{
public long Position { get; } = position;
public IReadOnlyDictionary<ushort, IfdEntry> Entries { get; } = entries;
public uint NextOffset { get; } = nextOffset;
public bool Has(ushort tag) => Entries.ContainsKey(tag);
}
/// <summary>
/// Fuzz-safe TIFF structure reader over a seekable stream. Reads only the bytes it needs (never the whole
/// file), validates every offset against the stream length, caps entry counts and detects IFD loops.
/// Also used for TIFF structures embedded at a base offset (Exif APP1, RAF, CR3 CMT boxes, MakerNotes).
/// </summary>
public sealed class TiffReader
{
public const int MaxEntriesPerIfd = 4096;
public const int MaxIfdChain = 64;
public const long MaxValueBytes = 64L * 1024 * 1024;
private readonly Stream _stream;
private readonly List<string> _warnings = [];
private TiffReader(Stream stream, long tiffBase, bool littleEndian, uint firstIfdOffset)
{
_stream = stream;
Base = tiffBase;
LittleEndian = littleEndian;
FirstIfdOffset = firstIfdOffset;
}
/// <summary>Absolute position of the TIFF header ("II*\0"/"MM\0*"); IFD offsets are relative to it.</summary>
public long Base { get; }
public bool LittleEndian { get; }
public uint FirstIfdOffset { get; }
public IReadOnlyList<string> Warnings => _warnings;
public long StreamLength => _stream.Length;
/// <summary>Opens a TIFF header at <paramref name="tiffBase"/>; null when the bytes are not a TIFF header.
/// Accepts the ORF ("IIRO"/"IIRS") and RW2 ("IIU\0") header variants.</summary>
public static TiffReader? Open(Stream stream, long tiffBase = 0)
{
if (!stream.CanSeek || tiffBase < 0 || tiffBase + 8 > stream.Length) return null;
Span<byte> h = stackalloc byte[8];
stream.Position = tiffBase;
if (stream.ReadAtLeast(h, 8, throwOnEndOfStream: false) < 8) return null;
bool le;
if (h[0] == 'I' && h[1] == 'I') le = true;
else if (h[0] == 'M' && h[1] == 'M') le = false;
else return null;
var magic = le ? BinaryPrimitives.ReadUInt16LittleEndian(h[2..]) : BinaryPrimitives.ReadUInt16BigEndian(h[2..]);
if (magic is not (42 or 0x4F52 or 0x5352 or 0x0055)) return null;
var first = le ? BinaryPrimitives.ReadUInt32LittleEndian(h[4..]) : BinaryPrimitives.ReadUInt32BigEndian(h[4..]);
return new TiffReader(stream, tiffBase, le, first);
}
/// <summary>Creates a reader for an IFD structure without a header (e.g. maker notes) with a known byte order.</summary>
public static TiffReader ForEmbedded(Stream stream, long tiffBase, bool littleEndian) => new(stream, tiffBase, littleEndian, 0);
/// <summary>Reads the chain of top-level IFDs (IFD0, IFD1, …).</summary>
public IReadOnlyList<Ifd> ReadChain()
{
var list = new List<Ifd>();
var seen = new HashSet<long>();
var offset = FirstIfdOffset;
while (offset != 0 && list.Count < MaxIfdChain)
{
var ifd = ReadIfd(offset, seen);
if (ifd is null) break;
list.Add(ifd);
offset = ifd.NextOffset;
}
return list;
}
/// <summary>Reads one IFD at a base-relative offset; null if out of bounds or already visited.</summary>
public Ifd? ReadIfd(uint offset, HashSet<long>? visited = null)
{
var pos = Base + offset;
if (pos + 2 > _stream.Length || pos < 0)
{
Warn($"IFD offset {offset} beyond end of file");
return null;
}
if (visited is not null && !visited.Add(pos))
{
Warn($"IFD loop at {offset}");
return null;
}
Span<byte> two = stackalloc byte[2];
if (!ReadAt(pos, two)) return null;
var count = U16(two);
if (count == 0 || count > MaxEntriesPerIfd)
{
Warn($"IFD at {offset} has {count} entries");
return null;
}
var bytes = new byte[count * 12 + 4];
var available = (int)Math.Min(bytes.Length, _stream.Length - (pos + 2));
if (!ReadAt(pos + 2, bytes.AsSpan(0, available))) return null;
var entries = new Dictionary<ushort, IfdEntry>(count);
var complete = available / 12;
for (var i = 0; i < Math.Min(count, complete); i++)
{
var e = bytes.AsSpan(i * 12, 12);
var tag = U16(e);
var type = (TiffType)U16(e[2..]);
var n = U32(e[4..]);
var value = U32(e[8..]);
if (TiffTypeInfo.Size(type) == 0) continue;
entries.TryAdd(tag, new IfdEntry(tag, type, n, value, pos + 2 + i * 12));
}
if (complete < count) Warn($"IFD at {offset} truncated");
var next = available >= count * 12 + 4 ? U32(bytes.AsSpan(count * 12)) : 0u;
return new Ifd(pos, entries, next);
}
/// <summary>Raw value bytes of an entry (bounded by <see cref="MaxValueBytes"/>); null when invalid.</summary>
public byte[]? ReadBytes(IfdEntry e, long maxBytes = MaxValueBytes)
{
var length = e.ByteLength;
if (length <= 0 || length > maxBytes) return null;
var pos = e.ValuePosition(Base);
if (pos < 0 || pos + length > _stream.Length)
{
Warn($"Tag 0x{e.Tag:X4} value outside file");
return null;
}
var buffer = new byte[length];
return ReadAt(pos, buffer) ? buffer : null;
}
public uint? GetUInt(Ifd ifd, ushort tag) => ifd.Entries.TryGetValue(tag, out var e) ? GetUInts(e) is { Length: > 0 } v ? v[0] : null : null;
public uint[]? GetUInts(Ifd ifd, ushort tag) => ifd.Entries.TryGetValue(tag, out var e) ? GetUInts(e) : null;
/// <summary>Integer values of BYTE/SHORT/LONG/IFD (and signed variants) entries.</summary>
public uint[]? GetUInts(IfdEntry e)
{
var raw = ReadBytes(e);
if (raw is null) return null;
var size = TiffTypeInfo.Size(e.Type);
var result = new uint[e.Count];
for (var i = 0; i < e.Count; i++)
{
var s = raw.AsSpan(i * size);
result[i] = e.Type switch
{
TiffType.Byte or TiffType.Undefined or TiffType.Ascii => s[0],
TiffType.SByte => (uint)(sbyte)s[0],
TiffType.Short => U16(s),
TiffType.SShort => (uint)(short)U16(s),
TiffType.Long or TiffType.Ifd or TiffType.SLong => U32(s),
_ => 0,
};
}
return result;
}
public double? GetDouble(Ifd ifd, ushort tag) => GetDoubles(ifd, tag) is { Length: > 0 } v ? v[0] : null;
public double[]? GetDoubles(Ifd ifd, ushort tag) => ifd.Entries.TryGetValue(tag, out var e) ? GetDoubles(e) : null;
/// <summary>Numeric values of any numeric type as doubles (rationals divided; 0-denominator → NaN skipped as 0).</summary>
public double[]? GetDoubles(IfdEntry e)
{
var raw = ReadBytes(e);
if (raw is null) return null;
var size = TiffTypeInfo.Size(e.Type);
var result = new double[e.Count];
for (var i = 0; i < e.Count; i++)
{
var s = raw.AsSpan(i * size);
result[i] = e.Type switch
{
TiffType.Rational => U32(s[4..]) == 0 ? 0 : (double)U32(s) / U32(s[4..]),
TiffType.SRational => (int)U32(s[4..]) == 0 ? 0 : (double)(int)U32(s) / (int)U32(s[4..]),
TiffType.Float => BitConverter.Int32BitsToSingle((int)U32(s)),
TiffType.Double => BitConverter.Int64BitsToDouble((long)U64(s)),
TiffType.Byte or TiffType.Undefined => s[0],
TiffType.SByte => (sbyte)s[0],
TiffType.Short => U16(s),
TiffType.SShort => (short)U16(s),
TiffType.Long or TiffType.Ifd => U32(s),
TiffType.SLong => (int)U32(s),
_ => 0,
};
}
return result;
}
/// <summary>ASCII/UTF-8 string value with trailing NULs and spaces trimmed; null when empty.</summary>
public string? GetString(Ifd ifd, ushort tag)
{
if (!ifd.Entries.TryGetValue(tag, out var e)) return null;
var raw = ReadBytes(e, 1 << 20);
if (raw is null) return null;
var end = Array.IndexOf(raw, (byte)0);
var s = Encoding.UTF8.GetString(raw, 0, end < 0 ? raw.Length : end).Trim();
return s.Length == 0 ? null : s;
}
/// <summary>Reads arbitrary bytes at a base-relative offset (bounds-checked).</summary>
public byte[]? ReadRelative(long offset, int length)
{
if (length <= 0 || length > MaxValueBytes) return null;
var pos = Base + offset;
if (pos < 0 || pos + length > _stream.Length) return null;
var buffer = new byte[length];
return ReadAt(pos, buffer) ? buffer : null;
}
public ushort U16(ReadOnlySpan<byte> s) => LittleEndian ? BinaryPrimitives.ReadUInt16LittleEndian(s) : BinaryPrimitives.ReadUInt16BigEndian(s);
public uint U32(ReadOnlySpan<byte> s) => LittleEndian ? BinaryPrimitives.ReadUInt32LittleEndian(s) : BinaryPrimitives.ReadUInt32BigEndian(s);
public ulong U64(ReadOnlySpan<byte> s) => LittleEndian ? BinaryPrimitives.ReadUInt64LittleEndian(s) : BinaryPrimitives.ReadUInt64BigEndian(s);
public void Warn(string message)
{
if (_warnings.Count < 32) _warnings.Add(message);
}
private bool ReadAt(long position, Span<byte> buffer)
{
try
{
_stream.Position = position;
return _stream.ReadAtLeast(buffer, buffer.Length, throwOnEndOfStream: false) == buffer.Length;
}
catch (IOException ex)
{
Warn("I/O error: " + ex.Message);
return false;
}
}
}
+126
View File
@@ -0,0 +1,126 @@
namespace Titano.Core.Metadata.Tiff;
/// <summary>Tag numbers used across TIFF, Exif, GPS and DNG.</summary>
public static class TiffTags
{
// Baseline TIFF
public const ushort NewSubfileType = 0x00FE;
public const ushort ImageWidth = 0x0100;
public const ushort ImageLength = 0x0101;
public const ushort BitsPerSample = 0x0102;
public const ushort Compression = 0x0103;
public const ushort PhotometricInterpretation = 0x0106;
public const ushort ImageDescription = 0x010E;
public const ushort Make = 0x010F;
public const ushort Model = 0x0110;
public const ushort StripOffsets = 0x0111;
public const ushort Orientation = 0x0112;
public const ushort SamplesPerPixel = 0x0115;
public const ushort RowsPerStrip = 0x0116;
public const ushort StripByteCounts = 0x0117;
public const ushort XResolution = 0x011A;
public const ushort YResolution = 0x011B;
public const ushort PlanarConfiguration = 0x011C;
public const ushort ResolutionUnit = 0x0128;
public const ushort Software = 0x0131;
public const ushort DateTime = 0x0132;
public const ushort Artist = 0x013B;
public const ushort Predictor = 0x013D;
public const ushort TileWidth = 0x0142;
public const ushort TileLength = 0x0143;
public const ushort TileOffsets = 0x0144;
public const ushort TileByteCounts = 0x0145;
public const ushort SubIfds = 0x014A;
public const ushort ExtraSamples = 0x0152;
public const ushort SampleFormat = 0x0153;
public const ushort JpegTables = 0x015B;
public const ushort JpegInterchangeFormat = 0x0201;
public const ushort JpegInterchangeFormatLength = 0x0202;
public const ushort YCbCrSubSampling = 0x0212;
public const ushort Rating = 0x4746;
public const ushort Xmp = 0x02BC;
public const ushort Copyright = 0x8298;
public const ushort ExifIfd = 0x8769;
public const ushort GpsIfd = 0x8825;
public const ushort InteropIfd = 0xA005;
// Exif
public const ushort ExposureTime = 0x829A;
public const ushort FNumber = 0x829D;
public const ushort ExposureProgram = 0x8822;
public const ushort IsoSpeedRatings = 0x8827;
public const ushort SensitivityType = 0x8830;
public const ushort RecommendedExposureIndex = 0x8832;
public const ushort ExifVersion = 0x9000;
public const ushort DateTimeOriginal = 0x9003;
public const ushort DateTimeDigitized = 0x9004;
public const ushort OffsetTime = 0x9010;
public const ushort OffsetTimeOriginal = 0x9011;
public const ushort OffsetTimeDigitized = 0x9012;
public const ushort ShutterSpeedValue = 0x9201;
public const ushort ApertureValue = 0x9202;
public const ushort ExposureBiasValue = 0x9204;
public const ushort MeteringMode = 0x9207;
public const ushort Flash = 0x9209;
public const ushort FocalLength = 0x920A;
public const ushort MakerNote = 0x927C;
public const ushort SubSecTime = 0x9290;
public const ushort SubSecTimeOriginal = 0x9291;
public const ushort SubSecTimeDigitized = 0x9292;
public const ushort ColorSpace = 0xA001;
public const ushort PixelXDimension = 0xA002;
public const ushort PixelYDimension = 0xA003;
public const ushort WhiteBalance = 0xA403;
public const ushort FocalLengthIn35mmFilm = 0xA405;
public const ushort BodySerialNumber = 0xA431;
public const ushort LensSpecification = 0xA432;
public const ushort LensMake = 0xA433;
public const ushort LensModel = 0xA434;
// GPS
public const ushort GpsLatitudeRef = 0x0001;
public const ushort GpsLatitude = 0x0002;
public const ushort GpsLongitudeRef = 0x0003;
public const ushort GpsLongitude = 0x0004;
public const ushort GpsAltitudeRef = 0x0005;
public const ushort GpsAltitude = 0x0006;
public const ushort GpsTimeStamp = 0x0007;
public const ushort GpsDateStamp = 0x001D;
// DNG
public const ushort CfaRepeatPatternDim = 0x828D;
public const ushort CfaPattern = 0x828E;
public const ushort DngVersion = 0xC612;
public const ushort DngBackwardVersion = 0xC613;
public const ushort UniqueCameraModel = 0xC614;
public const ushort CfaPlaneColor = 0xC616;
public const ushort CfaLayout = 0xC617;
public const ushort LinearizationTable = 0xC618;
public const ushort BlackLevelRepeatDim = 0xC619;
public const ushort BlackLevel = 0xC61A;
public const ushort BlackLevelDeltaH = 0xC61B;
public const ushort BlackLevelDeltaV = 0xC61C;
public const ushort WhiteLevel = 0xC61D;
public const ushort DefaultScale = 0xC61E;
public const ushort DefaultCropOrigin = 0xC61F;
public const ushort DefaultCropSize = 0xC620;
public const ushort ColorMatrix1 = 0xC621;
public const ushort ColorMatrix2 = 0xC622;
public const ushort CameraCalibration1 = 0xC623;
public const ushort CameraCalibration2 = 0xC624;
public const ushort AnalogBalance = 0xC627;
public const ushort AsShotNeutral = 0xC628;
public const ushort AsShotWhiteXy = 0xC629;
public const ushort BaselineExposure = 0xC62A;
public const ushort CameraSerialNumber = 0xC62F;
public const ushort LensInfo = 0xC630;
public const ushort ActiveArea = 0xC68D;
public const ushort CalibrationIlluminant1 = 0xC65A;
public const ushort CalibrationIlluminant2 = 0xC65B;
public const ushort OpcodeList1 = 0xC740;
public const ushort OpcodeList2 = 0xC741;
public const ushort OpcodeList3 = 0xC74E;
public const ushort ForwardMatrix1 = 0xC714;
public const ushort ForwardMatrix2 = 0xC715;
public const ushort NewRawImageDigest = 0xC7A7;
}
+32
View File
@@ -0,0 +1,32 @@
namespace Titano.Core.Metadata.Tiff;
/// <summary>TIFF 6.0 field types (plus IFD = 13).</summary>
public enum TiffType : ushort
{
Byte = 1,
Ascii = 2,
Short = 3,
Long = 4,
Rational = 5,
SByte = 6,
Undefined = 7,
SShort = 8,
SLong = 9,
SRational = 10,
Float = 11,
Double = 12,
Ifd = 13,
}
public static class TiffTypeInfo
{
/// <summary>Size in bytes of one value; 0 for unknown types.</summary>
public static int Size(TiffType type) => type switch
{
TiffType.Byte or TiffType.Ascii or TiffType.SByte or TiffType.Undefined => 1,
TiffType.Short or TiffType.SShort => 2,
TiffType.Long or TiffType.SLong or TiffType.Float or TiffType.Ifd => 4,
TiffType.Rational or TiffType.SRational or TiffType.Double => 8,
_ => 0,
};
}