using Titano.Core.Codecs.Dng; using Titano.Core.Mathematics; using Titano.Core.Metadata.Tiff; namespace Titano.Core.Codecs.Raw; /// Shared pieces of the native (non-DNG) RAW readers: camera naming, frame construction, MakerNote access and /// bounded payload reads. internal static class NativeRaw { /// Largest raw raster accepted (256 megapixels). public const long MaxPixels = 1L << 28; /// "Brand Model" as keyed in : "NIKON CORPORATION" + "NIKON D850" → "Nikon D850". public static string? CameraName(string? make, string? model) { if (string.IsNullOrWhiteSpace(model)) return null; var m = make?.Trim() ?? string.Empty; string brand; if (m.StartsWith("Canon", StringComparison.OrdinalIgnoreCase)) brand = "Canon"; else if (m.StartsWith("NIKON", StringComparison.OrdinalIgnoreCase)) brand = "Nikon"; else if (m.StartsWith("SONY", StringComparison.OrdinalIgnoreCase)) brand = "Sony"; else if (m.StartsWith("PENTAX", StringComparison.OrdinalIgnoreCase) || m.StartsWith("RICOH", StringComparison.OrdinalIgnoreCase) || m.StartsWith("ASAHI", StringComparison.OrdinalIgnoreCase)) brand = "Pentax"; else if (m.StartsWith("FUJIFILM", StringComparison.OrdinalIgnoreCase)) brand = "Fujifilm"; else brand = m.Split(' ', StringSplitOptions.RemoveEmptyEntries).FirstOrDefault() ?? string.Empty; var words = model.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToList(); if (words.Count > 1 && (words[0].Equals(brand, StringComparison.OrdinalIgnoreCase) || words[0].Equals("PENTAX", StringComparison.OrdinalIgnoreCase))) words.RemoveAt(0); return words.Count == 0 ? null : $"{brand} {string.Join(' ', words)}"; } /// Builds the frame of a native RAW developed with a tabulated D65 matrix. /// As-shot white-balance multipliers (R, G, B); null falls back to daylight. /// The body has no colour matrix (the caller falls back to the preview). public static RawFrame Frame(string decoder, int width, int height, CfaPattern cfa, RawRect active, RawBlackLevel black, double white, Vector3D? multipliers, int orientation, string? make, string? model, RawLoader loader, IEnumerable warnings) { var name = CameraName(make, model); var matrix = CameraMatrices.Find(name) ?? throw new InvalidDataException($"RAW: no colour matrix for {name ?? "unknown camera"}"); if (active.IsEmpty || active.Left < 0 || active.Top < 0 || active.Right > width || active.Bottom > height) throw new InvalidDataException("RAW: sensor area outside the raster"); Vector3D? neutral = multipliers is { } m && m.X > 0 && m.Y > 0 && m.Z > 0 && double.IsFinite(m.X + m.Y + m.Z) ? new Vector3D(1 / m.X, 1 / m.Y, 1 / m.Z) : null; var frame = new RawFrame { Decoder = decoder, Width = width, Height = height, Cfa = cfa, ActiveArea = active, Black = black, White = [white], DetectSaturation = true, Color = new DngColorProfile { ColorMatrix1 = matrix, CalibrationIlluminant1 = 21, AsShotNeutral = neutral, AsShotWhite = neutral is null ? Chromaticity.D65 : null, }, Orientation = orientation is >= 1 and <= 8 ? orientation : 1, Make = make, Model = model, Loader = loader, }; frame.Warnings.AddRange(warnings); if (neutral is null) frame.Warnings.Add("RAW: as-shot white balance unavailable; daylight assumed"); return frame; } /// Validated raster size. public static (int Width, int Height) Size(long width, long height) { if (width <= 0 || height <= 0 || width * height > MaxPixels) throw new InvalidDataException($"RAW: unsupported raster size {width}x{height}"); return ((int)width, (int)height); } /// Rejects entropy-coded payloads too short to hold one bit per sample, before the raster is allocated (a /// corrupted size must not trigger a huge allocation). public static void RequireBits(long payloadBytes, long samples) { if (payloadBytes * 8 < samples) throw new InvalidDataException("RAW: compressed data too short for the raster"); } /// Uniform black level for every CFA cell. public static RawBlackLevel Black(double value) => new(1, 1, [(float)value], null, null); /// Black level of a 2×2 CFA from values in R, G1, G2, B order, re-anchored at the active area. public static RawBlackLevel Black2x2(CfaPattern cfa, ReadOnlySpan rggb) { var values = new float[4]; for (var y = 0; y < 2; y++) { for (var x = 0; x < 2; x++) { var c = cfa.ColorAt(x, y); // The second green of a 2×2 cell is the one sharing a row with blue. var index = c == 0 ? 0 : c == 2 ? 3 : cfa.ColorAt(x ^ 1, y) == 0 ? 1 : 2; values[y * 2 + x] = (float)rggb[index]; } } return new RawBlackLevel(2, 2, values, null, null); } /// Exif IFD of IFD0, or null. public static Ifd? ExifIfd(TiffReader r, Ifd ifd0) => r.GetUInt(ifd0, TiffTags.ExifIfd) is { } offset and > 0 ? r.ReadIfd(offset) : null; /// Reads bytes at an absolute position (bounded by the stream). public static byte[] ReadPayload(Stream stream, long position, long length) { if (position < 0 || length <= 0 || length > int.MaxValue || position > stream.Length - length) throw new InvalidDataException("RAW: image data outside the file"); var data = new byte[length]; stream.Position = position; stream.ReadExactly(data); return data; } /// CFA pattern from TIFF/EP CFARepeatPatternDim/CFAPattern tags (as written in NEF/ARW/PEF raw IFDs), or null. public static CfaPattern? TiffEpCfa(TiffReader r, Ifd ifd) { if (IfdValue.UInts(r, ifd, TiffTags.CfaPattern) is not { } pattern) return null; var dim = IfdValue.UInts(r, ifd, TiffTags.CfaRepeatPatternDim) ?? [2, 2]; if (dim.Length < 2 || dim[0] is < 1 or > CfaPattern.MaxSize || dim[1] is < 1 or > CfaPattern.MaxSize || pattern.Length < dim[0] * dim[1]) return null; var colors = new byte[dim[0] * dim[1]]; for (var i = 0; i < colors.Length; i++) { if (pattern[i] > 2) return null; colors[i] = (byte)pattern[i]; } try { return new CfaPattern((int)dim[1], (int)dim[0], colors); } catch (InvalidDataException) { return null; } } } /// Uncompressed raw payloads: 16-bit words (either byte order) or MSB-first packed samples (TIFF convention). internal sealed class PlainRawLoader(long offset, long length, int width, int height, int bits, bool words, bool littleEndian) : RawLoader { public override RawSamples Load(Stream stream, int maxDegreeOfParallelism, CancellationToken cancellationToken) { var rowBytes = words ? width * 2L : (width * (long)bits + 7) / 8; if (rowBytes * height > length) throw new InvalidDataException("RAW: image data truncated"); var data = NativeRaw.ReadPayload(stream, offset, rowBytes * height); var samples = new ushort[width * height]; var mask = (1 << bits) - 1; Parallel.For(0, height, new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism, CancellationToken = cancellationToken }, y => { var row = data.AsSpan((int)(y * rowBytes), (int)rowBytes); var dst = samples.AsSpan(y * width, width); if (words) { for (var x = 0; x < width; x++) dst[x] = (ushort)((littleEndian ? row[2 * x] | row[2 * x + 1] << 8 : row[2 * x] << 8 | row[2 * x + 1]) & mask); return; } long bit = 0; for (var x = 0; x < width; x++, bit += bits) { var b = (int)(bit >> 3); // Up to 16 bits spanning at most three bytes. var window = row[b] << 16 | (b + 1 < row.Length ? row[b + 1] << 8 : 0) | (b + 2 < row.Length ? row[b + 2] : 0); dst[x] = (ushort)(window >> (24 - (int)(bit & 7) - bits) & mask); } }); return new RawSamples(width, height, 1, samples, null); } } /// MSB-first bit reader without byte stuffing (Nikon and Pentax Huffman streams). Reading past the end yields /// zero bits and sets . internal ref struct MsbBitReader(ReadOnlySpan data) { private readonly ReadOnlySpan _data = data; private int _pos; private ulong _buffer; private int _bits; public readonly bool Overrun => _pos > _data.Length + 8; /// Next bits (n ≤ 32) without consuming them. public uint Peek(int n) { if (n == 0) return 0; while (_bits < n) { _buffer = _buffer << 8 | (_pos < _data.Length ? _data[_pos] : 0u); _pos++; _bits += 8; } return (uint)(_buffer >> (_bits - n)) & (uint)((1UL << n) - 1); } public void Skip(int n) => _bits -= n; public uint Read(int n) { var v = Peek(n); _bits -= n; return v; } } /// Huffman lookup in dcraw's make_decoder layout: code-length counts for lengths 1–16 followed by symbols, /// codes assigned canonically; each table entry is (length << 8 | symbol) indexed by the next bits. internal sealed class HuffmanTable { private readonly ushort[] _lut; public HuffmanTable(ReadOnlySpan countsAndSymbols) { var bits = 16; while (bits > 0 && countsAndSymbols[bits - 1] == 0) bits--; if (bits == 0) throw new InvalidDataException("RAW: empty Huffman table"); Bits = bits; _lut = new ushort[1 << bits]; var h = 0; var symbol = 16; for (var len = 1; len <= bits; len++) { for (var i = 0; i < countsAndSymbols[len - 1]; i++, symbol++) { var value = symbol < countsAndSymbols.Length ? countsAndSymbols[symbol] : (byte)0; for (var j = 0; j < 1 << (bits - len) && h < _lut.Length; j++) _lut[h++] = (ushort)(len << 8 | value); } } } /// Wraps a prebuilt lookup (Pentax tables). public HuffmanTable(ushort[] lut, int bits) { _lut = lut; Bits = bits; } public int Bits { get; } /// Decodes one symbol. public int Decode(ref MsbBitReader reader) { var entry = _lut[reader.Peek(Bits)]; reader.Skip(entry >> 8); return entry & 0xFF; } }