feat(raw): native CR2, NEF, ARW, PEF and RAF decoders

Each container reader builds a RawFrame for the shared pipeline: CR2
sliced lossless JPEG with SensorInfo crop, masked-column black and
ColorData WB; NEF uncompressed and Nikon Huffman (lossy type 1,
lossless, lossy type 2 incl. split); ARW uncompressed and ARW2 cRAW
with tone curve; PEF uncompressed and Pentax Huffman; RAF uncompressed
Bayer and X-Trans with FujiIFD. Colour comes from a dcraw adobe_coeff
matrix table; bodies without an entry are reported unsupported so the
caller uses the embedded preview. Native RAWs detect the sensor's
saturation level for clipping only, never rescaling per frame.

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 11:13:18 +00:00
1 parent d40fdee35e
commit 7e157cbf72
14 files changed
+1974 -24

No files matched your search

+251
View File
@@ -0,0 +1,251 @@
using Titano.Core.Codecs.Dng;
using Titano.Core.Mathematics;
using Titano.Core.Metadata.Tiff;
namespace Titano.Core.Codecs.Raw;
/// <summary>Shared pieces of the native (non-DNG) RAW readers: camera naming, frame construction, MakerNote access and
/// bounded payload reads.</summary>
internal static class NativeRaw
{
/// <summary>Largest raw raster accepted (256 megapixels).</summary>
public const long MaxPixels = 1L << 28;
/// <summary>"Brand Model" as keyed in <see cref="CameraMatrices"/>: "NIKON CORPORATION" + "NIKON D850" → "Nikon D850".</summary>
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)}";
}
/// <summary>Builds the frame of a native RAW developed with a tabulated D65 matrix.</summary>
/// <param name="multipliers">As-shot white-balance multipliers (R, G, B); null falls back to daylight.</param>
/// <exception cref="InvalidDataException">The body has no colour matrix (the caller falls back to the preview).</exception>
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<string> 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;
}
/// <summary>Validated raster size.</summary>
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);
}
/// <summary>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).</summary>
public static void RequireBits(long payloadBytes, long samples)
{
if (payloadBytes * 8 < samples) throw new InvalidDataException("RAW: compressed data too short for the raster");
}
/// <summary>Uniform black level for every CFA cell.</summary>
public static RawBlackLevel Black(double value) => new(1, 1, [(float)value], null, null);
/// <summary>Black level of a 2×2 CFA from values in R, G1, G2, B order, re-anchored at the active area.</summary>
public static RawBlackLevel Black2x2(CfaPattern cfa, ReadOnlySpan<double> 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);
}
/// <summary>Exif IFD of IFD0, or null.</summary>
public static Ifd? ExifIfd(TiffReader r, Ifd ifd0) => r.GetUInt(ifd0, TiffTags.ExifIfd) is { } offset and > 0 ? r.ReadIfd(offset) : null;
/// <summary>Reads <paramref name="length"/> bytes at an absolute position (bounded by the stream).</summary>
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;
}
/// <summary>CFA pattern from TIFF/EP CFARepeatPatternDim/CFAPattern tags (as written in NEF/ARW/PEF raw IFDs), or null.</summary>
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;
}
}
}
/// <summary>Uncompressed raw payloads: 16-bit words (either byte order) or MSB-first packed samples (TIFF convention).</summary>
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);
}
}
/// <summary>MSB-first bit reader without byte stuffing (Nikon and Pentax Huffman streams). Reading past the end yields
/// zero bits and sets <see cref="Overrun"/>.</summary>
internal ref struct MsbBitReader(ReadOnlySpan<byte> data)
{
private readonly ReadOnlySpan<byte> _data = data;
private int _pos;
private ulong _buffer;
private int _bits;
public readonly bool Overrun => _pos > _data.Length + 8;
/// <summary>Next <paramref name="n"/> bits (n ≤ 32) without consuming them.</summary>
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;
}
}
/// <summary>Huffman lookup in dcraw's <c>make_decoder</c> layout: code-length counts for lengths 1–16 followed by symbols,
/// codes assigned canonically; each table entry is (length &lt;&lt; 8 | symbol) indexed by the next <see cref="Bits"/> bits.</summary>
internal sealed class HuffmanTable
{
private readonly ushort[] _lut;
public HuffmanTable(ReadOnlySpan<byte> 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);
}
}
}
/// <summary>Wraps a prebuilt lookup (Pentax tables).</summary>
public HuffmanTable(ushort[] lut, int bits)
{
_lut = lut;
Bits = bits;
}
public int Bits { get; }
/// <summary>Decodes one symbol.</summary>
public int Decode(ref MsbBitReader reader)
{
var entry = _lut[reader.Peek(Bits)];
reader.Skip(entry >> 8);
return entry & 0xFF;
}
}