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

+11
View File
@@ -37,6 +37,7 @@ internal static class DngReader
var photometric = r.GetUInt(raw, TiffTags.PhotometricInterpretation);
var compression = r.GetUInt(raw, TiffTags.Compression) ?? 1;
if (compression is not (1 or 7 or 8 or 34892)) throw new InvalidDataException($"DNG: compression {compression} is not supported");
if (compression is 1 or 7) RequirePayload(r, raw, (long)width * height * spp);
var floating = (r.GetUInt(raw, TiffTags.SampleFormat) ?? 1) == 3;
var bits = (int)(r.GetUInt(raw, TiffTags.BitsPerSample) ?? 16);
@@ -86,6 +87,16 @@ internal static class DngReader
return frame;
}
/// <summary>Uncompressed and lossless-JPEG data hold at least one bit per sample; checked before the raster is allocated
/// so a corrupted size cannot trigger a huge allocation.</summary>
private static void RequirePayload(TiffReader r, Ifd raw, long samples)
{
var counts = r.GetUInts(raw, raw.Has(TiffTags.TileByteCounts) ? TiffTags.TileByteCounts : TiffTags.StripByteCounts);
long total = 0;
foreach (var c in counts ?? []) total += c;
if (total * 8 < samples) throw new InvalidDataException("DNG: image data too short for the raster");
}
/// <summary>First IFD with NewSubfileType 0 and a CFA/LinearRaw photometric: IFD0, its SubIFDs (two levels), the chain.</summary>
private static Ifd? FindRawIfd(TiffReader r, IReadOnlyList<Ifd> chain)
{
+136
View File
@@ -0,0 +1,136 @@
using System.Buffers.Binary;
using Titano.Core.Metadata.Tiff;
namespace Titano.Core.Codecs.Raw;
/// <summary>
/// Sony ARW: the CFA SubIFD holds uncompressed 16-bit words (or packed samples) or ARW2 "cRAW" (Compression 32767 with
/// one byte per pixel): 16-pixel blocks of one colour storing an 11-bit maximum and minimum with their positions and
/// fourteen 7-bit deltas scaled by a shift, expanded through the tone curve of tag 0x7010 (dcraw
/// <c>sony_arw2_load_raw</c>). Black and white come from tags 0x7310/0x787F when present (14-bit scale), otherwise
/// 512 and the curve's top (cRAW) or 16383. As-shot white balance sits in the encrypted SR2 block, which is not read: ARW files develop with daylight
/// balance. ARW1 (DSLR-A100) and the lossless ARW 5.0 codec are not supported.
/// </summary>
internal static class ArwReader
{
private const uint CompressionSony = 32767;
private const ushort ToneCurveTag = 0x7010;
private const ushort BlackLevelTag = 0x7310;
private const ushort WhiteLevelTag = 0x787F;
public static RawFrame Read(Stream stream)
{
var r = TiffReader.Open(stream) ?? throw new InvalidDataException("ARW: missing TIFF header");
var chain = r.ReadChain();
if (chain.Count == 0) throw new InvalidDataException("ARW: no image directory");
var ifd0 = chain[0];
var raw = NefReader.FindCfa(r, ifd0) ?? throw new InvalidDataException("ARW: CFA image missing");
var (width, height) = NativeRaw.Size(r.GetUInt(raw, TiffTags.ImageWidth) ?? 0, r.GetUInt(raw, TiffTags.ImageLength) ?? 0);
var bits = (int)(r.GetUInt(raw, TiffTags.BitsPerSample) ?? 0);
var compression = r.GetUInt(raw, TiffTags.Compression) ?? 1;
var (offset, length) = NefReader.Strip(r, raw);
var warnings = new List<string>();
var cfa = NativeRaw.TiffEpCfa(r, raw);
if (cfa is null)
{
cfa = CfaPattern.Bayer("RGGB");
warnings.Add("ARW: CFA pattern missing; RGGB assumed");
}
if (!cfa.IsBayer) throw new InvalidDataException("ARW: only Bayer sensors are supported");
RawLoader loader;
double black, white;
if (compression == CompressionSony && length == (long)width * height)
{
if (width < 32 || width % 32 != 0) throw new InvalidDataException("ARW: cRAW width must be a multiple of 32");
var curve = ToneCurve(r, raw, ifd0);
loader = new Arw2Loader(offset, width, height, curve);
// Sensor saturation is the largest 11-bit code through the curve.
(black, white) = (512, curve[0x7FF << 1]);
}
else if (length >= (long)width * height * 2)
{
loader = new PlainRawLoader(offset, length, width, height, 16, words: true, littleEndian: r.LittleEndian);
(black, white) = (512, 16383);
}
else if (compression == 1 && bits is 12 or 14 && length >= ((long)width * bits + 7) / 8 * height)
{
loader = new PlainRawLoader(offset, length, width, height, bits, words: false, littleEndian: false);
(black, white) = (128 << (bits - 12), (1 << bits) - 1);
}
else
{
throw new InvalidDataException($"ARW: compression {compression} with {length} bytes is not supported");
}
var blackLevel = IfdValue.UInts(r, raw, BlackLevelTag) is { Length: 4 } b
? NativeRaw.Black2x2(cfa, [b[0], b[1], b[2], b[3]])
: NativeRaw.Black(black);
if (IfdValue.UInts(r, raw, WhiteLevelTag) is { Length: > 0 } w) white = w.Max();
return NativeRaw.Frame("managed-arw", width, height, cfa, new RawRect(0, 0, width, height), blackLevel, white, null,
(int)(r.GetUInt(ifd0, TiffTags.Orientation) ?? 1), r.GetString(ifd0, TiffTags.Make), r.GetString(ifd0, TiffTags.Model), loader, warnings);
}
/// <summary>Tone curve from the four breakpoints of tag 0x7010: slopes 1, 2, 4, 8, 16 between 0, the breakpoints and 4095.</summary>
private static ushort[] ToneCurve(TiffReader r, Ifd raw, Ifd ifd0)
{
Span<int> knots = [0, 0, 0, 0, 0, 4095];
if ((IfdValue.UInts(r, raw, ToneCurveTag) ?? IfdValue.UInts(r, ifd0, ToneCurveTag)) is { Length: >= 4 } t)
{
for (var i = 0; i < 4; i++) knots[i + 1] = (int)(t[i] >> 2 & 0xFFF);
}
var curve = new ushort[0x1000];
for (var i = 0; i < curve.Length; i++) curve[i] = (ushort)i;
for (var i = 0; i < 5; i++)
{
for (var j = knots[i] + 1; j <= knots[i + 1] && j < curve.Length; j++) curve[j] = (ushort)(curve[j - 1] + (1 << i));
}
return curve;
}
private sealed class Arw2Loader(long offset, int width, int height, ushort[] curve) : RawLoader
{
public override RawSamples Load(Stream stream, int maxDegreeOfParallelism, CancellationToken cancellationToken)
{
var data = NativeRaw.ReadPayload(stream, offset, (long)width * height);
var samples = new ushort[width * height];
Parallel.For(0, height, new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism, CancellationToken = cancellationToken }, row =>
{
// One spare byte: the last delta of a block is read as a 16-bit word that reaches one byte past it.
Span<byte> line = stackalloc byte[width + 2];
line.Clear();
data.AsSpan(row * width, width).CopyTo(line);
Span<int> pix = stackalloc int[16];
var dst = samples.AsSpan(row * width, width);
var dp = 0;
for (var col = 0; col < width - 30; dp += 16)
{
var val = BinaryPrimitives.ReadUInt32LittleEndian(line[dp..]);
int max = (int)(val & 0x7FF), min = (int)(val >> 11 & 0x7FF), imax = (int)(val >> 22 & 0xF), imin = (int)(val >> 26 & 0xF);
var sh = 0;
while (sh < 4 && 0x80 << sh <= max - min) sh++;
var bit = 30;
for (var i = 0; i < 16; i++)
{
if (i == imax)
{
pix[i] = max;
}
else if (i == imin)
{
pix[i] = min;
}
else
{
pix[i] = int.Min(((BinaryPrimitives.ReadUInt16LittleEndian(line[(dp + (bit >> 3))..]) >> (bit & 7) & 0x7F) << sh) + min, 0x7FF);
bit += 7;
}
}
for (var i = 0; i < 16; i++, col += 2) dst[col] = curve[pix[i] << 1];
col -= (col & 1) != 0 ? 1 : 31;
}
});
return new RawSamples(width, height, 1, samples, null);
}
}
}
@@ -0,0 +1,114 @@
using Titano.Core.Mathematics;
namespace Titano.Core.Codecs.Raw;
/// <summary>
/// Camera colour matrices (XYZ → camera, D65 calibration, values × 10 000) for bodies whose native RAW files Titano
/// develops, keyed by the normalised "Brand Model" name. The numbers are the public adobe_coeff table of Dave Coffin's
/// dcraw (public-domain origin, also carried by LibRaw), which reproduces the ColorMatrix2 values Adobe DNG Converter
/// writes for these cameras; they are data, not code. A body missing here is decoded through its embedded preview.
/// </summary>
internal static class CameraMatrices
{
private static readonly Dictionary<string, short[]> Table = Build();
/// <summary>Matrix for a normalised camera name (see <see cref="NativeRaw.CameraName"/>), or null.</summary>
public static Matrix3? Find(string? cameraName)
{
if (cameraName is null || !Table.TryGetValue(cameraName, out var m)) return null;
return new Matrix3(m[0] / 1e4, m[1] / 1e4, m[2] / 1e4, m[3] / 1e4, m[4] / 1e4, m[5] / 1e4, m[6] / 1e4, m[7] / 1e4, m[8] / 1e4);
}
/// <summary>Normalised names with an entry (for diagnostics and tests).</summary>
public static IEnumerable<string> Names => Table.Keys;
private static Dictionary<string, short[]> Build()
{
var t = new Dictionary<string, short[]>(StringComparer.OrdinalIgnoreCase);
void Add(short[] m, params string[] names)
{
foreach (var n in names) t[n] = m;
}
// Canon (CR2).
Add([6347, -479, -972, -8297, 15954, 2480, -1968, 2131, 7649], "Canon EOS 5D");
Add([4716, 603, -830, -7798, 15474, 2480, -1496, 1937, 6651], "Canon EOS 5D Mark II");
Add([6722, -635, -963, -4287, 12460, 2028, -908, 2162, 5668], "Canon EOS 5D Mark III");
Add([6446, -366, -864, -4436, 12204, 2513, -952, 2496, 6348], "Canon EOS 5D Mark IV");
Add([6250, -711, -808, -5153, 12794, 2636, -1249, 2198, 5610], "Canon EOS 5DS", "Canon EOS 5DS R");
Add([7034, -804, -1014, -4420, 12564, 2058, -851, 1994, 5758], "Canon EOS 6D", "Canon EOS 70D");
Add([6875, -970, -932, -4691, 12459, 2501, -874, 1953, 5809], "Canon EOS 6D Mark II");
Add([6844, -996, -856, -3876, 11761, 2396, -593, 1772, 6198], "Canon EOS 7D");
Add([7268, -1082, -969, -4186, 11839, 2663, -825, 2029, 5839], "Canon EOS 7D Mark II");
Add([6071, -747, -856, -7653, 15365, 2441, -2025, 2553, 7315], "Canon EOS 40D");
Add([4920, 616, -593, -6493, 13964, 2784, -1774, 3178, 7005], "Canon EOS 50D");
Add([6719, -994, -925, -4408, 12426, 2211, -887, 2129, 6051], "Canon EOS 60D");
Add([7457, -671, -937, -4849, 12495, 2643, -1213, 2354, 5492], "Canon EOS 80D");
Add([6941, -1164, -857, -3825, 11597, 2534, -416, 1540, 6039], "Canon EOS 550D", "Canon EOS Rebel T2i", "Canon EOS Kiss X4");
Add([6461, -907, -882, -4300, 12184, 2378, -819, 1944, 5931], "Canon EOS 600D", "Canon EOS Rebel T3i", "Canon EOS Kiss X5",
"Canon EOS 1200D", "Canon EOS Rebel T5", "Canon EOS Kiss X70");
Add([6602, -841, -939, -4472, 12458, 2247, -975, 2039, 6148], "Canon EOS 650D", "Canon EOS Rebel T4i", "Canon EOS Kiss X6i",
"Canon EOS 700D", "Canon EOS Rebel T5i", "Canon EOS Kiss X7i", "Canon EOS 100D", "Canon EOS Rebel SL1", "Canon EOS Kiss X7", "Canon EOS M");
Add([6362, -823, -847, -4426, 12109, 2616, -743, 1857, 5635], "Canon EOS 750D", "Canon EOS Rebel T6i", "Canon EOS Kiss X8i",
"Canon EOS 760D", "Canon EOS Rebel T6s", "Canon EOS 8000D");
Add([7377, -742, -998, -4235, 11981, 2549, -673, 1918, 5538], "Canon EOS 77D", "Canon EOS 9000D", "Canon EOS 800D", "Canon EOS Rebel T7i", "Canon EOS Kiss X9i");
Add([6847, -614, -1014, -4669, 12737, 2139, -1197, 2488, 6846], "Canon EOS-1D X");
Add([7596, -978, -967, -4808, 12571, 2503, -1398, 2567, 5752], "Canon EOS-1D X Mark II");
// Nikon (NEF).
Add([7309, -1403, -519, -8474, 16008, 2622, -2434, 2826, 8064], "Nikon D90");
Add([9030, -1992, -715, -8465, 16302, 2255, -2689, 3217, 8069], "Nikon D300", "Nikon D300S");
Add([8139, -2171, -663, -8747, 16541, 2295, -1925, 2008, 8093], "Nikon D3", "Nikon D700");
Add([8828, -2406, -694, -4874, 12603, 2541, -660, 1509, 7587], "Nikon D3S");
Add([7171, -1986, -648, -8085, 15555, 2718, -2170, 2512, 7457], "Nikon D3X");
Add([8598, -2848, -857, -5618, 13606, 2195, -1002, 1773, 7137], "Nikon D4", "Nikon Df");
Add([9200, -3522, -992, -5755, 13803, 2117, -753, 1486, 6338], "Nikon D5");
Add([7911, -2167, -813, -5327, 13150, 2408, -1288, 2483, 7968], "Nikon D3100");
Add([7013, -1408, -635, -5268, 12902, 2640, -1470, 2801, 7379], "Nikon D3200");
Add([6988, -1384, -714, -5631, 13410, 2447, -1485, 2204, 7318], "Nikon D3300", "Nikon D3400", "Nikon D5300");
Add([8198, -2239, -724, -4871, 12389, 2798, -1043, 2050, 7181], "Nikon D5100", "Nikon D7000");
Add([8322, -3112, -1047, -6367, 14342, 2179, -988, 1638, 6394], "Nikon D5200", "Nikon D7100", "Nikon D7200");
Add([8821, -2938, -785, -4178, 12142, 2287, -824, 1651, 6860], "Nikon D5500", "Nikon D5600");
Add([8813, -3210, -1036, -4703, 12868, 2021, -1054, 1940, 6129], "Nikon D500", "Nikon D7500");
Add([8178, -2245, -609, -4857, 12394, 2776, -1207, 2086, 7298], "Nikon D600", "Nikon D610");
Add([9020, -2890, -715, -4535, 12436, 2348, -934, 1919, 7086], "Nikon D750");
Add([7866, -2108, -555, -4869, 12483, 2681, -1176, 2069, 7501], "Nikon D800", "Nikon D800E");
Add([9369, -3195, -791, -4488, 12430, 2301, -893, 1796, 6872], "Nikon D810");
Add([10405, -3755, -1270, -5461, 13787, 1793, -1040, 2015, 6785], "Nikon D850", "Nikon Z 7");
Add([8210, -2534, -683, -5355, 13338, 2212, -1143, 1929, 6464], "Nikon Z 6");
// Sony (ARW).
Add([5271, -712, -347, -6153, 13653, 2763, -1601, 2366, 7242], "Sony ILCE-7", "Sony ILCE-7M2");
Add([7374, -2389, -551, -5435, 13162, 2519, -1006, 1795, 6552], "Sony ILCE-7M3");
Add([4913, -541, -202, -6130, 13513, 2906, -1564, 2151, 7183], "Sony ILCE-7R");
Add([6629, -1900, -483, -4618, 12349, 2550, -622, 1381, 6514], "Sony ILCE-7RM2");
Add([6640, -1847, -503, -5238, 13010, 2474, -993, 1673, 6527], "Sony ILCE-7RM3");
Add([5838, -1430, -246, -3497, 11477, 2297, -748, 1885, 5778], "Sony ILCE-7S", "Sony ILCE-7SM2");
Add([5991, -1456, -455, -4764, 12135, 2980, -707, 1425, 6701], "Sony ILCE-6000", "Sony ILCE-5100", "Sony SLT-A57");
Add([5973, -1695, -419, -3826, 11797, 2293, -639, 1398, 5789], "Sony ILCE-6300", "Sony ILCE-6500", "Sony ILCE-6400");
Add([5491, -1192, -363, -4951, 12342, 2948, -911, 1722, 7192], "Sony NEX-7", "Sony SLT-A65", "Sony SLT-A77");
Add([8651, -2754, -1057, -3464, 12207, 1373, -568, 1398, 4434], "Sony DSC-RX100");
Add([6596, -2079, -562, -4782, 13016, 1933, -970, 1581, 5181], "Sony DSC-RX100M2");
// Pentax (PEF).
Add([9566, -2863, -803, -7170, 15172, 2112, -818, 803, 9705], "Pentax K10D");
Add([9427, -2714, -868, -7493, 16092, 1373, -2199, 3264, 7180], "Pentax K20D");
Add([9142, -2947, -678, -8648, 16967, 1663, -2224, 2898, 8615], "Pentax K-7");
Add([8713, -2833, -743, -4342, 11900, 2772, -722, 1543, 6247], "Pentax K-5");
Add([8170, -2725, -639, -4440, 12017, 2744, -771, 1465, 6599], "Pentax K-5 II", "Pentax K-5 II s");
Add([8843, -2837, -625, -5025, 12644, 2668, -411, 1234, 7410], "Pentax K-x");
Add([9895, -3077, -850, -5304, 13035, 2521, -883, 1768, 6936], "Pentax K-r");
Add([8710, -2632, -1167, -3995, 12301, 1881, -981, 1719, 6535], "Pentax K-30");
Add([7415, -2052, -721, -5186, 12788, 2682, -1446, 2157, 6773], "Pentax K-3");
Add([8596, -2981, -639, -4202, 12046, 2431, -685, 1424, 6122], "Pentax K-1");
// Fujifilm (RAF).
Add([10413, -3996, -993, -3721, 11640, 2361, -733, 1540, 6011], "Fujifilm X-E1", "Fujifilm X-Pro1", "Fujifilm X-M1");
Add([10592, -4262, -1008, -3514, 11355, 2465, -870, 2025, 6386], "Fujifilm X100S", "Fujifilm X20");
Add([8458, -2451, -855, -4597, 12447, 2407, -1475, 2482, 6526], "Fujifilm X-T1", "Fujifilm X-E2", "Fujifilm X-T10", "Fujifilm X100T", "Fujifilm X30");
Add([11434, -4948, -1210, -3746, 12042, 1903, -666, 1479, 5235], "Fujifilm X-T2", "Fujifilm X-Pro2", "Fujifilm X-T20", "Fujifilm X100F", "Fujifilm X-E3");
Add([13426, -6334, -1177, -4244, 12136, 2371, -580, 1303, 5980], "Fujifilm X-T3", "Fujifilm X-T30");
Add([11086, -4555, -839, -3512, 11310, 2517, -815, 1341, 5940], "Fujifilm X-A1");
return t;
}
}
+161
View File
@@ -0,0 +1,161 @@
using Titano.Core.Codecs.Jpeg;
using Titano.Core.Mathematics;
using Titano.Core.Metadata.Tiff;
namespace Titano.Core.Codecs.Raw;
/// <summary>
/// Canon CR2: the fourth IFD holds one lossless-JPEG (SOF3) strip whose samples are laid out in vertical slices
/// (tag 0xC640 cr2_slice = count, slice width, last slice width; dcraw <c>lossless_jpeg_load_raw</c>). The MakerNote
/// supplies SensorInfo (0x00E0: sensor borders, i.e. the active area) and ColorData (0x4001: as-shot RGGB levels at a
/// version-dependent offset). Black is measured on the masked left columns. sRAW/mRAW (subsampled YCbCr) is not
/// supported and falls back to the preview.
/// </summary>
internal static class Cr2Reader
{
private const ushort SliceTag = 0xC640;
private const ushort SensorInfoTag = 0x00E0;
private const ushort ColorDataTag = 0x4001;
private const int RawIfdIndex = 3;
private const int HeaderProbeBytes = 64 * 1024;
public static RawFrame Read(Stream stream)
{
var r = TiffReader.Open(stream) ?? throw new InvalidDataException("CR2: missing TIFF header");
var chain = r.ReadChain();
if (chain.Count <= RawIfdIndex) throw new InvalidDataException("CR2: raw IFD missing");
var ifd0 = chain[0];
var raw = chain[RawIfdIndex];
var offset = r.GetUInt(raw, TiffTags.StripOffsets) ?? throw new InvalidDataException("CR2: raw strip missing");
var length = r.GetUInt(raw, TiffTags.StripByteCounts) ?? throw new InvalidDataException("CR2: raw strip length missing");
if (offset + (long)length > stream.Length || length < 16) throw new InvalidDataException("CR2: raw strip outside the file");
var head = NativeRaw.ReadPayload(stream, offset, long.Min(length, HeaderProbeBytes));
JpegInfo info;
try
{
info = JpegDecoder.ReadInfo(head);
}
catch (InvalidDataException ex)
{
throw new InvalidDataException("CR2: raw strip is not a JPEG: " + ex.Message);
}
if (info.Process != JpegProcess.Lossless) throw new InvalidDataException("CR2: raw strip is not lossless JPEG");
if (info.Sampling.Any(s => s != (1, 1))) throw new InvalidDataException("CR2: sRAW/mRAW is not supported");
var rawWidth = (long)info.Width * info.Components;
var slices = IfdValue.UInts(r, raw, SliceTag);
if (slices is [var count, var sliceWidth, var lastWidth, ..] && count + sliceWidth + lastWidth > 0)
{
if (count * (long)sliceWidth + lastWidth != rawWidth || lastWidth == 0 || count > 0 && sliceWidth == 0)
throw new InvalidDataException("CR2: slice layout does not match the JPEG frame");
}
else
{
slices = null;
}
var (width, height) = NativeRaw.Size(rawWidth, info.Height);
NativeRaw.RequireBits(length, (long)width * height);
var make = r.GetString(ifd0, TiffTags.Make);
var model = r.GetString(ifd0, TiffTags.Model);
var warnings = new List<string>();
var makerNote = MakerNote(r, ifd0);
var active = new RawRect(0, 0, width, height);
if (makerNote is not null && IfdValue.UInts(r, makerNote, SensorInfoTag) is { Length: >= 9 } sensor)
{
// SensorInfo borders are inclusive pixel indices.
var area = new RawRect((int)sensor[5], (int)sensor[6], (int)sensor[7] + 1, (int)sensor[8] + 1).Intersect(active);
if (!area.IsEmpty && sensor[7] > sensor[5] && sensor[8] > sensor[6]) active = area;
else warnings.Add("CR2: SensorInfo borders ignored");
}
else
{
warnings.Add("CR2: SensorInfo missing; full raster used");
}
var cfa = CfaPattern.Bayer("RGGB").Shift(active.Left, active.Top);
var white = (1 << info.Precision) - 1;
var loader = new Cr2Loader(offset, length, width, height, slices, active);
return NativeRaw.Frame("managed-cr2", width, height, cfa, active, NativeRaw.Black(0), white,
Multipliers(r, makerNote), (int)(r.GetUInt(ifd0, TiffTags.Orientation) ?? 1), make, model, loader, warnings);
}
/// <summary>Canon MakerNote: a plain IFD inside the Exif IFD, offsets relative to the TIFF header.</summary>
private static Ifd? MakerNote(TiffReader r, Ifd ifd0)
{
if (NativeRaw.ExifIfd(r, ifd0) is not { } exif || !exif.Entries.TryGetValue(TiffTags.MakerNote, out var note) || note.IsInline) return null;
return r.ReadIfd(note.ValueOffset);
}
/// <summary>As-shot RGGB levels from ColorData; the offset (in 16-bit words) depends on the record's length (dcraw).</summary>
private static Vector3D? Multipliers(TiffReader r, Ifd? makerNote)
{
if (makerNote is null || !makerNote.Entries.TryGetValue(ColorDataTag, out var e) || e.Count <= 500 || e.Count > 8192) return null;
var data = r.GetUInts(e);
if (data is null) return null;
var at = data.Length switch { 582 => 25, 653 => 34, 5120 => 71, _ => 63 };
if (at + 4 > data.Length) return null;
double red = data[at], green = (data[at + 1] + data[at + 2]) / 2.0, blue = data[at + 3];
return red > 0 && green > 0 && blue > 0 ? new Vector3D(red / green, 1, blue / green) : null;
}
/// <summary>Decodes the strip, places samples by slice, and measures black on the masked columns left of the active area.</summary>
private sealed class Cr2Loader(long offset, long length, int width, int height, uint[]? slices, RawRect active) : RawLoader
{
public override RawSamples Load(Stream stream, int maxDegreeOfParallelism, CancellationToken cancellationToken)
{
var data = NativeRaw.ReadPayload(stream, offset, length);
var jpeg = JpegLosslessDecoder.Decode(data);
var src = jpeg.Samples;
if ((long)jpeg.Width * jpeg.Components != width || jpeg.Height != height) throw new InvalidDataException("CR2: JPEG frame changed size");
cancellationToken.ThrowIfCancellationRequested();
ushort[] samples;
if (slices is null)
{
samples = src;
}
else
{
samples = new ushort[src.Length];
int count = (int)slices[0], sliceWidth = (int)slices[1], lastWidth = (int)slices[2];
var perSlice = (long)sliceWidth * height;
Parallel.For(0, count + 1, new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism, CancellationToken = cancellationToken }, s =>
{
var w = s < count ? sliceWidth : lastWidth;
var start = s * perSlice;
for (var row = 0; row < height; row++)
{
src.AsSpan((int)(start + (long)row * w), w).CopyTo(samples.AsSpan(row * width + s * sliceWidth, w));
}
});
}
var result = new RawSamples(width, height, 1, samples, null);
result.MeasuredBlack = MaskedBlack(samples, result.Warnings);
return result;
}
/// <summary>Per-CFA-colour mean of the masked columns (4 px away from both edges of the mask).</summary>
private RawBlackLevel MaskedBlack(ushort[] samples, List<string> warnings)
{
if (active.Left < 12)
{
warnings.Add("CR2: no masked columns; black level 0 assumed");
return NativeRaw.Black(0);
}
Span<double> sum = stackalloc double[4];
Span<long> n = stackalloc long[4];
for (var y = active.Top; y < active.Bottom; y++)
{
for (var x = 4; x < active.Left - 4; x++)
{
// Cell index relative to the active area's CFA phase.
var cell = ((y - active.Top) & 1) * 2 + ((x - active.Left) & 1);
sum[cell] += samples[y * width + x];
n[cell]++;
}
}
var values = new float[4];
for (var i = 0; i < 4; i++) values[i] = n[i] > 0 ? (float)(sum[i] / n[i]) : 0f;
return new RawBlackLevel(2, 2, values, null, null);
}
}
}
+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;
}
}
+231
View File
@@ -0,0 +1,231 @@
using System.Buffers.Binary;
using Titano.Core.Mathematics;
using Titano.Core.Metadata.Tiff;
namespace Titano.Core.Codecs.Raw;
/// <summary>
/// Nikon NEF: the CFA SubIFD is uncompressed (16-bit words or MSB-packed samples) or Nikon-compressed (Compression
/// 34713): lossy type 1, lossless and lossy type 2 including the "after split" variant, decoded as dcraw's
/// <c>nikon_load_raw</c> does from the MakerNote decode info (0x0096: version, vertical predictors, tone curve, split row)
/// with Nikon's six Huffman trees and two-column horizontal prediction. White balance comes from MakerNote 0x000C, black
/// from 0x003D. Packed 12-bit "uncompressed" variants, YUV NEFs and the high-efficiency (TicoRAW) codec are not supported
/// and fall back to the preview.
/// </summary>
internal static class NefReader
{
private const ushort WbLevelsTag = 0x000C;
private const ushort BlackLevelTag = 0x003D;
private const ushort DecodeInfoTag = 0x0096;
private const uint CompressionNikon = 34713;
/// <summary>Nikon Huffman trees (dcraw <c>nikon_tree</c>): 16 code-length counts, then the symbols (len | shift &lt;&lt; 4).</summary>
internal static readonly byte[][] Trees =
[
[0, 1, 5, 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 5, 4, 3, 6, 2, 7, 1, 0, 8, 9, 11, 10, 12, 0, 0, 0],
[0, 1, 5, 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0x39, 0x5a, 0x38, 0x27, 0x16, 5, 4, 3, 2, 1, 0, 11, 12, 12, 0, 0],
[0, 1, 4, 2, 3, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 4, 6, 3, 7, 2, 8, 1, 9, 0, 10, 11, 12, 0, 0, 0],
[0, 1, 4, 3, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 5, 6, 4, 7, 8, 3, 9, 2, 1, 0, 10, 11, 12, 13, 14, 0],
[0, 1, 5, 1, 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 8, 0x5c, 0x4b, 0x3a, 0x29, 7, 6, 5, 4, 3, 2, 1, 0, 13, 14, 0],
[0, 1, 4, 2, 2, 3, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 7, 6, 8, 5, 9, 4, 10, 3, 11, 12, 2, 0, 1, 13, 14, 0],
];
public static RawFrame Read(Stream stream)
{
var r = TiffReader.Open(stream) ?? throw new InvalidDataException("NEF: missing TIFF header");
var chain = r.ReadChain();
if (chain.Count == 0) throw new InvalidDataException("NEF: no image directory");
var ifd0 = chain[0];
var raw = FindCfa(r, ifd0) ?? throw new InvalidDataException("NEF: CFA image missing");
var (width, height) = NativeRaw.Size(r.GetUInt(raw, TiffTags.ImageWidth) ?? 0, r.GetUInt(raw, TiffTags.ImageLength) ?? 0);
var bits = (int)(r.GetUInt(raw, TiffTags.BitsPerSample) ?? 0);
if (bits is not (12 or 14)) throw new InvalidDataException($"NEF: {bits}-bit data is not supported");
var (offset, length) = Strip(r, raw);
var compression = r.GetUInt(raw, TiffTags.Compression) ?? 1;
var note = MakerNote(stream, r, ifd0);
var warnings = new List<string>();
var cfa = NativeRaw.TiffEpCfa(r, raw);
if (cfa is null)
{
cfa = CfaPattern.Bayer("RGGB");
warnings.Add("NEF: CFA pattern missing; RGGB assumed");
}
if (!cfa.IsBayer) throw new InvalidDataException("NEF: only Bayer sensors are supported");
RawLoader loader;
double white = (1 << bits) - 1;
if (compression == CompressionNikon && length != (long)width * height * 2)
{
if (note is not { } mn || !mn.Ifd.Entries.TryGetValue(DecodeInfoTag, out var meta) || mn.Reader.ReadBytes(meta) is not { } info)
throw new InvalidDataException("NEF: compressed data without decode information");
NativeRaw.RequireBits(length, (long)width * height);
var decode = DecodeInfo.Parse(info, mn.Reader.LittleEndian, bits);
white = decode.White;
loader = new NikonHuffmanLoader(offset, length, width, height, decode);
}
else if (compression is CompressionNikon or 1 && length >= (long)width * height * 2)
{
// Nikon writes unpacked "compressed" data big-endian; plain uncompressed follows the TIFF byte order.
loader = new PlainRawLoader(offset, length, width, height, bits, words: true, littleEndian: compression == 1 && r.LittleEndian);
}
else if (compression == 1 && length >= ((long)width * bits + 7) / 8 * height)
{
loader = new PlainRawLoader(offset, length, width, height, bits, words: false, littleEndian: false);
}
else
{
throw new InvalidDataException($"NEF: compression {compression} with {length} bytes is not supported");
}
var black = NativeRaw.Black(0);
if (note is { } n && IfdValue.UInts(n.Reader, n.Ifd, BlackLevelTag) is { Length: 4 } b)
{
// Stored on a 14-bit scale whatever the bit depth.
black = NativeRaw.Black2x2(cfa, [b[0] >> (14 - bits), b[1] >> (14 - bits), b[2] >> (14 - bits), b[3] >> (14 - bits)]);
}
Vector3D? multipliers = note is { } w && IfdValue.Doubles(w.Reader, w.Ifd, WbLevelsTag, 2) is [var red, var blue, ..] ? new Vector3D(red, 1, blue) : null;
return NativeRaw.Frame("managed-nef", width, height, cfa, new RawRect(0, 0, width, height), black, white, multipliers,
(int)(r.GetUInt(ifd0, TiffTags.Orientation) ?? 1), r.GetString(ifd0, TiffTags.Make), r.GetString(ifd0, TiffTags.Model), loader, warnings);
}
/// <summary>The full-resolution CFA IFD: IFD0 or one of its SubIFDs.</summary>
internal static Ifd? FindCfa(TiffReader r, Ifd ifd0)
{
bool IsRaw(Ifd ifd) => (r.GetUInt(ifd, TiffTags.NewSubfileType) ?? 0) == 0 && r.GetUInt(ifd, TiffTags.PhotometricInterpretation) == 32803;
if (IsRaw(ifd0)) return ifd0;
if (!ifd0.Entries.TryGetValue(TiffTags.SubIfds, out var sub) || sub.Count > 16) return null;
foreach (var offset in r.GetUInts(sub) ?? [])
{
if (r.ReadIfd(offset) is { } child && IsRaw(child)) return child;
}
return null;
}
/// <summary>Absolute position and length of the image data (several strips must be contiguous).</summary>
internal static (long Offset, long Length) Strip(TiffReader r, Ifd ifd)
{
var offsets = IfdValue.UInts(r, ifd, TiffTags.StripOffsets) ?? r.GetUInts(ifd, TiffTags.StripOffsets);
var counts = IfdValue.UInts(r, ifd, TiffTags.StripByteCounts) ?? r.GetUInts(ifd, TiffTags.StripByteCounts);
if (offsets is not { Length: > 0 } || counts is null || counts.Length < offsets.Length) throw new InvalidDataException("RAW: strip offsets missing");
long length = 0;
for (var i = 0; i < offsets.Length; i++)
{
if (offsets[i] != offsets[0] + length) throw new InvalidDataException("RAW: non-contiguous strips are not supported");
length += counts[i];
}
var offset = r.Base + offsets[0];
if (offset + length > r.StreamLength) throw new InvalidDataException("RAW: image data outside the file");
return (offset, length);
}
/// <summary>Nikon type-3 MakerNote: "Nikon\0", version, then an embedded TIFF header 10 bytes in.</summary>
private static (TiffReader Reader, Ifd Ifd)? MakerNote(Stream stream, TiffReader r, Ifd ifd0)
{
if (NativeRaw.ExifIfd(r, ifd0) is not { } exif || !exif.Entries.TryGetValue(TiffTags.MakerNote, out var e) || e.IsInline || e.Count < 18) return null;
var start = e.ValuePosition(r.Base);
var head = r.ReadRelative(start - r.Base, 6);
if (head is null || !head.AsSpan().SequenceEqual("Nikon\0"u8)) return null;
var reader = TiffReader.Open(stream, start + 10);
return reader?.ReadIfd(reader.FirstIfdOffset) is { } ifd ? (reader, ifd) : null;
}
/// <summary>Parsed MakerNote 0x0096: Huffman tree, initial vertical predictors, tone curve and split row.</summary>
internal sealed record DecodeInfo(int Tree, ushort[] Predictors, ushort[] Curve, int Split, double White)
{
public static DecodeInfo Parse(byte[] meta, bool littleEndian, int bits)
{
if (meta.Length < 12) throw new InvalidDataException("NEF: decode information truncated");
var pos = 0;
ushort U16()
{
if (pos + 2 > meta.Length) throw new InvalidDataException("NEF: decode information truncated");
var v = littleEndian ? BinaryPrimitives.ReadUInt16LittleEndian(meta.AsSpan(pos)) : BinaryPrimitives.ReadUInt16BigEndian(meta.AsSpan(pos));
pos += 2;
return v;
}
int ver0 = meta[0], ver1 = meta[1];
pos = ver0 == 0x49 || ver1 == 0x58 ? 2 + 2110 : 2;
var tree = (ver0 == 0x46 ? 2 : 0) + (bits == 14 ? 3 : 0);
ushort[] predictors = [U16(), U16(), U16(), U16()];
var max = 1 << bits & 0x7FFF;
var curve = new ushort[0x10000];
for (var i = 0; i < curve.Length; i++) curve[i] = (ushort)i;
var size = U16();
var step = size > 1 ? max / (size - 1) : 0;
var split = 0;
var top = max;
if (ver0 == 0x44 && ver1 == 0x20 && step > 0)
{
// Lossy type 2: curve sampled every 'step' codes, linearly interpolated; split row at offset 562.
for (var i = 0; i < size; i++) curve[int.Min(i * step, curve.Length - 1)] = U16();
for (var i = 0; i < max; i++)
{
var k = i % step;
var a = i - k;
curve[i] = (ushort)((curve[a] * (step - k) + curve[int.Min(a + step, curve.Length - 1)] * k) / step);
}
if (meta.Length >= 564)
{
pos = 562;
split = U16();
}
}
else if (ver0 != 0x46 && size <= 0x4001)
{
// Lossy type 1: the curve is stored in full.
for (var i = 0; i < size; i++) curve[i] = U16();
top = size;
}
double white = 0;
for (var i = 0; i < int.Max(top, 1); i++) white = double.Max(white, curve[i]);
return new DecodeInfo(tree, predictors, curve, split, white);
}
}
/// <summary>dcraw <c>nikon_load_raw</c>.</summary>
private sealed class NikonHuffmanLoader(long offset, long length, int width, int height, DecodeInfo info) : RawLoader
{
public override RawSamples Load(Stream stream, int maxDegreeOfParallelism, CancellationToken cancellationToken)
{
var data = NativeRaw.ReadPayload(stream, offset, length);
var samples = new ushort[width * height];
var huff = new HuffmanTable(Trees[info.Tree]);
var reader = new MsbBitReader(data);
Span<ushort> vpred = stackalloc ushort[4];
info.Predictors.CopyTo(vpred);
Span<ushort> hpred = stackalloc ushort[2];
var curve = info.Curve;
for (var row = 0; row < height; row++)
{
if (info.Split > 0 && row == info.Split && info.Tree + 1 < Trees.Length) huff = new HuffmanTable(Trees[info.Tree + 1]);
if ((row & 63) == 0) cancellationToken.ThrowIfCancellationRequested();
var dst = samples.AsSpan(row * width, width);
for (var col = 0; col < width; col++)
{
var symbol = huff.Decode(ref reader);
int len = symbol & 15, shl = symbol >> 4;
var diff = 0;
if (len > 0)
{
diff = (int)(((reader.Read(int.Max(len - shl, 0)) << 1) + 1) << shl >> 1);
if ((diff & 1 << (len - 1)) == 0) diff -= (1 << len) - (shl == 0 ? 1 : 0);
}
ushort predicted;
if (col < 2)
{
ref var v = ref vpred[(row & 1) * 2 + col];
v = (ushort)(v + diff);
predicted = hpred[col] = v;
}
else
{
predicted = hpred[col & 1] = (ushort)(hpred[col & 1] + diff);
}
dst[col] = curve[int.Clamp((short)predicted, 0, 0x3FFF)];
}
if (reader.Overrun) throw new InvalidDataException($"NEF: compressed data ends at row {row}");
}
return new RawSamples(width, height, 1, samples, null);
}
}
}
+149
View File
@@ -0,0 +1,149 @@
using System.Buffers.Binary;
using Titano.Core.Mathematics;
using Titano.Core.Metadata.Tiff;
namespace Titano.Core.Codecs.Raw;
/// <summary>
/// Pentax PEF: the CFA image in IFD0 is uncompressed or Pentax-Huffman coded (Compression 65535): a code table from
/// MakerNote 0x0220 (code values left-aligned in 12 bits and their lengths, dcraw <c>pentax_load_raw</c>), JPEG-style
/// difference magnitudes, and two-column horizontal prediction seeded per row parity. Black (0x0200) and as-shot RGGB
/// levels (0x0201) come from the "AOC" MakerNote, whose offsets are relative to the TIFF header.
/// </summary>
internal static class PefReader
{
private const uint CompressionPentax = 65535;
private const ushort BlackPointTag = 0x0200;
private const ushort WhiteBalanceTag = 0x0201;
private const ushort HuffmanTag = 0x0220;
public static RawFrame Read(Stream stream)
{
var r = TiffReader.Open(stream) ?? throw new InvalidDataException("PEF: missing TIFF header");
var chain = r.ReadChain();
if (chain.Count == 0) throw new InvalidDataException("PEF: no image directory");
var ifd0 = chain[0];
var raw = NefReader.FindCfa(r, ifd0) ?? throw new InvalidDataException("PEF: CFA image missing");
var (width, height) = NativeRaw.Size(r.GetUInt(raw, TiffTags.ImageWidth) ?? 0, r.GetUInt(raw, TiffTags.ImageLength) ?? 0);
var bits = (int)(r.GetUInt(raw, TiffTags.BitsPerSample) ?? 0);
if (bits is not (12 or 14)) throw new InvalidDataException($"PEF: {bits}-bit data is not supported");
var compression = r.GetUInt(raw, TiffTags.Compression) ?? 1;
var (offset, length) = NefReader.Strip(r, raw);
var note = MakerNote(stream, r, ifd0);
var warnings = new List<string>();
var cfa = NativeRaw.TiffEpCfa(r, raw);
if (cfa is null)
{
cfa = CfaPattern.Bayer("RGGB");
warnings.Add("PEF: CFA pattern missing; RGGB assumed");
}
if (!cfa.IsBayer) throw new InvalidDataException("PEF: only Bayer sensors are supported");
RawLoader loader;
if (compression == CompressionPentax)
{
if (note is not { } mn || !mn.Ifd.Entries.TryGetValue(HuffmanTag, out var e) || mn.Reader.ReadBytes(e) is not { } table)
throw new InvalidDataException("PEF: Huffman table missing");
NativeRaw.RequireBits(length, (long)width * height);
loader = new PentaxLoader(offset, length, width, height, bits, Table(table, mn.Reader.LittleEndian));
}
else if (compression == 1 && length >= (long)width * height * 2)
{
loader = new PlainRawLoader(offset, length, width, height, bits, words: true, littleEndian: r.LittleEndian);
}
else if (compression == 1 && length >= ((long)width * bits + 7) / 8 * height)
{
loader = new PlainRawLoader(offset, length, width, height, bits, words: false, littleEndian: false);
}
else
{
throw new InvalidDataException($"PEF: compression {compression} is not supported");
}
var black = note is { } n && IfdValue.UInts(n.Reader, n.Ifd, BlackPointTag) is { Length: 4 } b
? NativeRaw.Black2x2(cfa, [b[0], b[1], b[2], b[3]])
: NativeRaw.Black(0);
Vector3D? multipliers = note is { } w && IfdValue.UInts(w.Reader, w.Ifd, WhiteBalanceTag) is { Length: 4 } wb && wb[1] + wb[2] > 0
? new Vector3D(wb[0] * 2.0 / (wb[1] + wb[2]), 1, wb[3] * 2.0 / (wb[1] + wb[2]))
: null;
return NativeRaw.Frame("managed-pef", width, height, cfa, new RawRect(0, 0, width, height), black, (1 << bits) - 1, multipliers,
(int)(r.GetUInt(ifd0, TiffTags.Orientation) ?? 1), r.GetString(ifd0, TiffTags.Make), r.GetString(ifd0, TiffTags.Model), loader, warnings);
}
/// <summary>"AOC\0" + byte order ("MM"/"II", else the file's) + IFD; offsets relative to the file's TIFF header.</summary>
private static (TiffReader Reader, Ifd Ifd)? MakerNote(Stream stream, TiffReader r, Ifd ifd0)
{
if (NativeRaw.ExifIfd(r, ifd0) is not { } exif || !exif.Entries.TryGetValue(TiffTags.MakerNote, out var e) || e.IsInline || e.Count < 8) return null;
var start = e.ValuePosition(r.Base);
var head = r.ReadRelative(start - r.Base, 6);
if (head is null || !head.AsSpan(0, 4).SequenceEqual("AOC\0"u8)) return null;
var littleEndian = head[4] == 'I' && head[5] == 'I' || !(head[4] == 'M' && head[5] == 'M') && r.LittleEndian;
var reader = TiffReader.ForEmbedded(stream, r.Base, littleEndian);
return reader.ReadIfd((uint)(start + 6 - r.Base)) is { } ifd ? (reader, ifd) : null;
}
/// <summary>12-bit lookup from MakerNote 0x0220: a code count, 12 skipped bytes, left-aligned code values (16-bit) and
/// code lengths (8-bit); every 12-bit prefix of a code maps to (length, difference magnitude).</summary>
internal static HuffmanTable Table(byte[] table, bool littleEndian)
{
if (table.Length < 14) throw new InvalidDataException("PEF: Huffman table truncated");
ushort U16(int at) => littleEndian ? BinaryPrimitives.ReadUInt16LittleEndian(table.AsSpan(at)) : BinaryPrimitives.ReadUInt16BigEndian(table.AsSpan(at));
var depth = (U16(0) + 12) & 15;
if (table.Length < 14 + depth * 3) throw new InvalidDataException("PEF: Huffman table truncated");
var lut = new ushort[4096];
for (var c = 0; c < depth; c++)
{
int code = U16(14 + 2 * c), len = table[14 + 2 * depth + c];
if (code > 4095 || len > 12) continue;
var end = (code + (4096 >> len) - 1) & 4095;
for (var i = code; i <= end; i++) lut[i] = (ushort)(len << 8 | c);
}
return new HuffmanTable(lut, 12);
}
private sealed class PentaxLoader(long offset, long length, int width, int height, int bits, HuffmanTable huff) : RawLoader
{
public override RawSamples Load(Stream stream, int maxDegreeOfParallelism, CancellationToken cancellationToken)
{
var data = NativeRaw.ReadPayload(stream, offset, length);
var samples = new ushort[width * height];
var reader = new MsbBitReader(data);
Span<int> vpred = stackalloc int[4];
Span<int> hpred = stackalloc int[2];
var limit = (1 << bits) - 1;
var outOfRange = false;
for (var row = 0; row < height; row++)
{
if ((row & 63) == 0) cancellationToken.ThrowIfCancellationRequested();
var dst = samples.AsSpan(row * width, width);
for (var col = 0; col < width; col++)
{
var len = huff.Decode(ref reader);
var diff = 0;
if (len == 16)
{
diff = -32768;
}
else if (len > 0)
{
diff = (int)reader.Read(len);
if ((diff & 1 << (len - 1)) == 0) diff -= (1 << len) - 1;
}
int value;
if (col < 2) value = hpred[col] = vpred[(row & 1) * 2 + col] += diff;
else value = hpred[col & 1] += diff;
if ((uint)value > (uint)limit)
{
outOfRange = true;
value = int.Clamp(value, 0, limit);
}
dst[col] = (ushort)value;
}
if (reader.Overrun) throw new InvalidDataException($"PEF: compressed data ends at row {row}");
}
var result = new RawSamples(width, height, 1, samples, null);
if (outOfRange) result.Warnings.Add("PEF: predicted values outside the bit depth were clamped");
return result;
}
}
}
+114
View File
@@ -0,0 +1,114 @@
using System.Buffers.Binary;
using Titano.Core.Mathematics;
using Titano.Core.Metadata;
using Titano.Core.Metadata.Tiff;
namespace Titano.Core.Codecs.Raw;
/// <summary>
/// Fujifilm RAF, uncompressed: the header's CFA offset (0x64) points to a big-endian TIFF block whose FujiIFD (0xF000)
/// gives the raster size (0xF001/0xF002), bit depth (0xF003), the data offset relative to that block (0xF007), its length
/// (0xF008) and black levels (0xF00A); the RAF directory supplies the crop (0x0110 top-left, 0x0111 size), the 6×6
/// colour layout (0x0131, stored reversed, anchored at the raster origin; Bayer bodies describe their 2×2 pattern the same
/// way) and the as-shot GRGB levels (0x2FF0). Samples are 16-bit words in the block's byte order. Compressed RAFs and
/// SuperCCD (45°) layouts are not supported and fall back to the preview.
/// </summary>
internal static class RafReader
{
private const int HeaderLength = 0x6C;
private const int MaxDirectoryBytes = 1 << 20;
private const ushort FullSizeRecord = 0x0100, CropTopLeftRecord = 0x0110, CropSizeRecord = 0x0111;
private const ushort LayoutRecord = 0x0130, CfaRecord = 0x0131, WhiteBalanceRecord = 0x2FF0;
private const ushort FujiIfdTag = 0xF000, WidthTag = 0xF001, HeightTag = 0xF002, BitsTag = 0xF003;
private const ushort OffsetTag = 0xF007, LengthTag = 0xF008, BlackTag = 0xF00A;
public static RawFrame Read(Stream stream)
{
var header = new byte[HeaderLength];
stream.Position = 0;
if (stream.ReadAtLeast(header, HeaderLength, throwOnEndOfStream: false) < HeaderLength || !header.AsSpan().StartsWith("FUJIFILMCCD-RAW "u8))
throw new InvalidDataException("RAF: header missing");
var records = Directory(stream, BinaryPrimitives.ReadUInt32BigEndian(header.AsSpan(0x5C)), BinaryPrimitives.ReadUInt32BigEndian(header.AsSpan(0x60)));
if (records.TryGetValue(LayoutRecord, out var layout) && layout.Length >= 2 && (layout[1] & 8) == 0)
throw new InvalidDataException("RAF: SuperCCD layouts are not supported");
if (!records.TryGetValue(CfaRecord, out var pattern) || pattern.Length < 36) throw new InvalidDataException("RAF: colour layout record missing");
var blockBase = BinaryPrimitives.ReadUInt32BigEndian(header.AsSpan(0x64));
var r = TiffReader.Open(stream, blockBase) ?? throw new InvalidDataException("RAF: raw block is not TIFF-structured");
var fuji = r.ReadIfd(r.FirstIfdOffset) is { } top && r.GetUInt(top, FujiIfdTag) is { } sub ? r.ReadIfd(sub) : null;
if (fuji is null) throw new InvalidDataException("RAF: FujiIFD missing");
var (width, height) = NativeRaw.Size(r.GetUInt(fuji, WidthTag) ?? 0, r.GetUInt(fuji, HeightTag) ?? 0);
var bits = (int)(r.GetUInt(fuji, BitsTag) ?? 0);
if (bits is < 12 or > 16) throw new InvalidDataException($"RAF: {bits}-bit data is not supported");
var offset = blockBase + (long)(r.GetUInt(fuji, OffsetTag) ?? 0);
var length = (long)(r.GetUInt(fuji, LengthTag) ?? 0);
if (length < (long)width * height * 2) throw new InvalidDataException("RAF: compressed RAF is not supported");
var warnings = new List<string>();
var active = new RawRect(0, 0, width, height);
if (records.TryGetValue(CropTopLeftRecord, out var tl) && tl.Length >= 4 && records.TryGetValue(CropSizeRecord, out var size) && size.Length >= 4)
{
int top0 = BinaryPrimitives.ReadUInt16BigEndian(tl), left0 = BinaryPrimitives.ReadUInt16BigEndian(tl.AsSpan(2));
int h = BinaryPrimitives.ReadUInt16BigEndian(size), w = BinaryPrimitives.ReadUInt16BigEndian(size.AsSpan(2));
var crop = new RawRect(left0, top0, left0 + w, top0 + h).Intersect(active);
if (!crop.IsEmpty) active = crop;
}
var colors = new byte[36];
for (var i = 0; i < 36; i++) colors[35 - i] = (byte)(pattern[i] & 3);
var cfa = new CfaPattern(6, 6, colors).Shift(active.Left, active.Top);
var black = NativeRaw.Black(1024 >> (14 - int.Min(bits, 14)));
if (fuji.Entries.TryGetValue(BlackTag, out var be) && be.Count is > 0 and <= 64 && r.GetUInts(be) is { } levels)
{
if (levels.Length >= 36)
{
// A 6×6 grid anchored at the raster origin, re-anchored at the active area.
var grid = new float[36];
for (var y = 0; y < 6; y++)
{
for (var x = 0; x < 6; x++) grid[y * 6 + x] = levels[(y + active.Top) % 6 * 6 + (x + active.Left) % 6];
}
black = new RawBlackLevel(6, 6, grid, null, null);
}
else
{
black = NativeRaw.Black(levels.Average(v => (double)v));
}
}
else
{
warnings.Add("RAF: black level missing; default used");
}
Vector3D? multipliers = null;
if (records.TryGetValue(WhiteBalanceRecord, out var wb) && wb.Length >= 8)
{
double g = BinaryPrimitives.ReadUInt16BigEndian(wb), red = BinaryPrimitives.ReadUInt16BigEndian(wb.AsSpan(2)), blue = BinaryPrimitives.ReadUInt16BigEndian(wb.AsSpan(6));
if (g > 0) multipliers = new Vector3D(red / g, 1, blue / g);
}
var metadata = MetadataReader.Read(stream, "frame.raf");
var loader = new PlainRawLoader(offset, length, width, height, 16, words: true, littleEndian: r.LittleEndian);
return NativeRaw.Frame("managed-raf", width, height, cfa, active, black, (1 << bits) - 1, multipliers, metadata.Orientation,
metadata.Make ?? "FUJIFILM", metadata.Model, loader, warnings);
}
/// <summary>RAF directory: u32 record count, then (u16 tag, u16 size, data) records, big-endian.</summary>
private static Dictionary<ushort, byte[]> Directory(Stream stream, uint offset, uint length)
{
var result = new Dictionary<ushort, byte[]>();
if (offset < HeaderLength || length < 4 || length > MaxDirectoryBytes || offset > stream.Length - length) throw new InvalidDataException("RAF: directory missing");
var data = NativeRaw.ReadPayload(stream, offset, length);
var count = BinaryPrimitives.ReadUInt32BigEndian(data);
var pos = 4;
for (var i = 0; i < count && pos + 4 <= data.Length; i++)
{
var tag = BinaryPrimitives.ReadUInt16BigEndian(data.AsSpan(pos));
var size = BinaryPrimitives.ReadUInt16BigEndian(data.AsSpan(pos + 2));
pos += 4;
if (pos + size > data.Length) break;
result.TryAdd(tag, data.AsSpan(pos, size).ToArray());
pos += size;
}
return result;
}
}
+8 -3
View File
@@ -58,15 +58,15 @@ public sealed class RawDecodeResult(ImageBuffer image, RawImageInfo info, IReadO
}
/// <summary>
/// Managed RAW decoding (BRIEF §8.2): DNG (CFA and LinearRaw) through <see cref="DngReader"/>,
/// developed by the shared pipeline. Bodies or encodings
/// Managed RAW decoding (BRIEF §8.2): DNG (CFA and LinearRaw) through <see cref="DngReader"/>, and the native containers
/// Canon CR2, Nikon NEF, Sony ARW, Pentax PEF and Fujifilm RAF, all developed by the same pipeline. Bodies or encodings
/// without support are reported by <see cref="Probe"/> so callers can fall back to the embedded preview.
/// </summary>
public static class RawDecoder
{
/// <summary>Formats with a managed decoder (individual files may still be unsupported).</summary>
public static IReadOnlyList<ImageFileFormat> Formats { get; } =
[ImageFileFormat.Dng];
[ImageFileFormat.Dng, ImageFileFormat.Cr2, ImageFileFormat.Nef, ImageFileFormat.Arw, ImageFileFormat.Pef, ImageFileFormat.Raf];
/// <summary>Parses the container and decides whether it can be developed natively; never reads the pixel payload.</summary>
public static RawProbe Probe(Stream stream, string? fileName = null)
@@ -112,6 +112,11 @@ public static class RawDecoder
var frame = format switch
{
ImageFileFormat.Dng => DngReader.Read(stream),
ImageFileFormat.Cr2 => Cr2Reader.Read(stream),
ImageFileFormat.Nef => NefReader.Read(stream),
ImageFileFormat.Arw => ArwReader.Read(stream),
ImageFileFormat.Pef => PefReader.Read(stream),
ImageFileFormat.Raf => RafReader.Read(stream),
_ => throw new InvalidDataException($"RAW: no managed decoder for {format}"),
};
return (format, frame);
+36 -18
View File
@@ -17,8 +17,11 @@ internal static class RawDeveloper
/// <summary>Normalised level at or above which a sample counts as clipped.</summary>
private const float ClipThreshold = 0.999f;
/// <summary>LibRaw's adjust_maximum_thr: the data maximum replaces the nominal white level above this fraction of it.</summary>
private const double AdjustMaximumThreshold = 0.75;
/// <summary>A pile-up of samples at the data maximum counts as sensor saturation only above this fraction of the white level.</summary>
private const double SaturationFloor = 0.75;
/// <summary>Samples that must sit at the data maximum for it to count as saturation rather than the brightest detail.</summary>
private const int SaturationPileUp = 16;
public static ImageBuffer Develop(RawFrame frame, RawSamples samples, RawDecodeOptions options, ICollection<string> warnings, CancellationToken cancellationToken)
{
@@ -28,8 +31,8 @@ internal static class RawDeveloper
var spp = frame.SamplesPerPixel;
var active = frame.ActiveArea;
var black = samples.MeasuredBlack ?? frame.Black;
var white = (double[])frame.White.Clone();
if (frame.AdjustWhiteToData) AdjustWhite(samples, active, white, warnings);
var white = frame.White;
var saturation = frame.DetectSaturation && frame.Linearization is null && spp == 1 ? Saturation(samples, active, black, white[0]) : 1f;
var list1 = DngOpcodes.Parse(frame.OpcodeList1, 1, warnings);
ImageBuffer? stage1 = null;
@@ -50,7 +53,7 @@ internal static class RawDeveloper
byte[]? clip;
try
{
(linear, clip) = Linearize(frame, samples, stage1, black, white, dop, cancellationToken);
(linear, clip) = Linearize(frame, samples, stage1, black, white, saturation * ClipThreshold, dop, cancellationToken);
}
finally
{
@@ -63,7 +66,7 @@ internal static class RawDeveloper
DngOpcodes.Apply(DngOpcodes.Parse(frame.OpcodeList2, 2, warnings), stage, warnings, cancellationToken);
return frame.Cfa is null && spp == 1
? DevelopMonochrome(frame, stage, options, warnings, cancellationToken)
: DevelopColour(frame, stage, options, warnings, cancellationToken);
: DevelopColour(frame, stage, saturation, options, warnings, cancellationToken);
}
finally
{
@@ -71,7 +74,7 @@ internal static class RawDeveloper
}
}
private static ImageBuffer DevelopColour(RawFrame frame, OpcodeImage stage, RawDecodeOptions options, ICollection<string> warnings, CancellationToken cancellationToken)
private static ImageBuffer DevelopColour(RawFrame frame, OpcodeImage stage, float saturation, RawDecodeOptions options, ICollection<string> warnings, CancellationToken cancellationToken)
{
var dop = options.MaxDegreeOfParallelism;
var colour = frame.Color.Solve(warnings);
@@ -109,7 +112,7 @@ internal static class RawDeveloper
var image = new OpcodeImage(rgb, stage.Left, stage.Top, step, null);
try
{
if (bits is not null) HighlightReconstruction.Apply(rgb, bits, wb, dop, cancellationToken);
if (bits is not null) HighlightReconstruction.Apply(rgb, bits, [wb[0] * saturation, wb[1] * saturation, wb[2] * saturation], dop, cancellationToken);
var list3 = DngOpcodes.Parse(frame.OpcodeList3, 3, warnings);
if (list3.Count > 0)
{
@@ -190,7 +193,7 @@ internal static class RawDeveloper
/// <summary>Linear reference values over the active area plus a clip map (bit = CFA colour or sample plane), or null
/// when nothing clipped.</summary>
private static (ImageBuffer Image, byte[]? Clip) Linearize(RawFrame frame, RawSamples samples, ImageBuffer? stage1, RawBlackLevel black, double[] white, int dop, CancellationToken cancellationToken)
private static (ImageBuffer Image, byte[]? Clip) Linearize(RawFrame frame, RawSamples samples, ImageBuffer? stage1, RawBlackLevel black, double[] white, float clipLevel, int dop, CancellationToken cancellationToken)
{
var active = frame.ActiveArea;
int w = active.Width, h = active.Height, spp = frame.SamplesPerPixel;
@@ -227,7 +230,7 @@ internal static class RawDeveloper
+ (black.DeltaH is { } dh && x < dh.Length ? dh[x] : 0f);
var value = (v - b) * scale[s];
row[x] = value;
if (value >= ClipThreshold)
if (value >= clipLevel)
{
clip[y * w + x] |= (byte)(1 << (cfa is null ? s : cfa.ColorAt(x, y)));
rowClip = 1;
@@ -239,19 +242,34 @@ internal static class RawDeveloper
return (image, anyClip != 0 ? clip : null);
}
private static void AdjustWhite(RawSamples samples, RawRect active, double[] white, ICollection<string> warnings)
/// <summary>Normalised saturation level of a native RAW: the data maximum when at least <see cref="SaturationPileUp"/>
/// samples reach it and it lies between <see cref="SaturationFloor"/> and 1 of the white level, else 1. It only moves
/// the clip threshold; scaling stays tied to the white level so brightness never depends on frame content.</summary>
private static float Saturation(RawSamples samples, RawRect active, RawBlackLevel black, double white)
{
var max = 0f;
var count = 0;
for (var y = active.Top; y < active.Bottom; y++)
{
var o = (long)y * samples.Width * samples.SamplesPerPixel;
for (var x = active.Left * samples.SamplesPerPixel; x < active.Right * samples.SamplesPerPixel; x++) max = float.Max(max, samples[o + x]);
var o = (long)y * samples.Width;
for (var x = active.Left; x < active.Right; x++)
{
var v = samples[o + x];
if (v > max + 1)
{
max = v;
count = 1;
}
else if (v >= max - 1)
{
count++;
max = float.Max(max, v);
}
}
}
for (var s = 0; s < white.Length; s++)
{
if (max > 0 && max < white[s] && max > white[s] * AdjustMaximumThreshold) white[s] = max;
}
if (max > white.Max()) warnings.Add($"RAW: samples exceed the white level ({max} > {white.Max()})");
if (count < SaturationPileUp || max >= white || max < white * SaturationFloor) return 1f;
var floor = black.Max(0, 1);
return (float)double.Clamp((max - 1 - floor) / (white - floor), 0.5, 1);
}
private static void ScaleMosaic(ImageBuffer mosaic, CfaPattern cfa, float[] wb, int dop, CancellationToken cancellationToken)
+3 -3
View File
@@ -112,9 +112,9 @@ internal sealed class RawFrame
/// <summary>White level per sample plane, in linearised units.</summary>
public required double[] White { get; init; }
/// <summary>Lower the white level to the data maximum when that exceeds 75 % of it (LibRaw adjust_maximum_thr): native
/// RAWs state a nominal bit-depth maximum while sensors saturate earlier.</summary>
public bool AdjustWhiteToData { get; init; }
/// <summary>Find the clip threshold in the data: native RAWs state a nominal bit-depth maximum while sensors saturate
/// earlier. Only clip detection uses it; the white level keeps scaling every frame the same (no flicker).</summary>
public bool DetectSaturation { get; init; }
public required DngColorProfile Color { get; init; }
public byte[]? OpcodeList1 { get; init; }