diff --git a/src/Titano.Core/Codecs/Dng/DngReader.cs b/src/Titano.Core/Codecs/Dng/DngReader.cs index 786793d5..a56db315 100644 --- a/src/Titano.Core/Codecs/Dng/DngReader.cs +++ b/src/Titano.Core/Codecs/Dng/DngReader.cs @@ -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; } + /// 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. + 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"); + } + /// First IFD with NewSubfileType 0 and a CFA/LinearRaw photometric: IFD0, its SubIFDs (two levels), the chain. private static Ifd? FindRawIfd(TiffReader r, IReadOnlyList chain) { diff --git a/src/Titano.Core/Codecs/Raw/ArwReader.cs b/src/Titano.Core/Codecs/Raw/ArwReader.cs new file mode 100644 index 00000000..f055db04 --- /dev/null +++ b/src/Titano.Core/Codecs/Raw/ArwReader.cs @@ -0,0 +1,136 @@ +using System.Buffers.Binary; +using Titano.Core.Metadata.Tiff; + +namespace Titano.Core.Codecs.Raw; + +/// +/// 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 +/// sony_arw2_load_raw). 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. +/// +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(); + 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); + } + + /// Tone curve from the four breakpoints of tag 0x7010: slopes 1, 2, 4, 8, 16 between 0, the breakpoints and 4095. + private static ushort[] ToneCurve(TiffReader r, Ifd raw, Ifd ifd0) + { + Span 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 line = stackalloc byte[width + 2]; + line.Clear(); + data.AsSpan(row * width, width).CopyTo(line); + Span 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); + } + } +} diff --git a/src/Titano.Core/Codecs/Raw/CameraMatrices.cs b/src/Titano.Core/Codecs/Raw/CameraMatrices.cs new file mode 100644 index 00000000..bc1967f1 --- /dev/null +++ b/src/Titano.Core/Codecs/Raw/CameraMatrices.cs @@ -0,0 +1,114 @@ +using Titano.Core.Mathematics; + +namespace Titano.Core.Codecs.Raw; + +/// +/// 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. +/// +internal static class CameraMatrices +{ + private static readonly Dictionary Table = Build(); + + /// Matrix for a normalised camera name (see ), or null. + 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); + } + + /// Normalised names with an entry (for diagnostics and tests). + public static IEnumerable Names => Table.Keys; + + private static Dictionary Build() + { + var t = new Dictionary(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; + } +} diff --git a/src/Titano.Core/Codecs/Raw/Cr2Reader.cs b/src/Titano.Core/Codecs/Raw/Cr2Reader.cs new file mode 100644 index 00000000..25684416 --- /dev/null +++ b/src/Titano.Core/Codecs/Raw/Cr2Reader.cs @@ -0,0 +1,161 @@ +using Titano.Core.Codecs.Jpeg; +using Titano.Core.Mathematics; +using Titano.Core.Metadata.Tiff; + +namespace Titano.Core.Codecs.Raw; + +/// +/// 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 lossless_jpeg_load_raw). 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. +/// +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(); + 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); + } + + /// Canon MakerNote: a plain IFD inside the Exif IFD, offsets relative to the TIFF header. + 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); + } + + /// As-shot RGGB levels from ColorData; the offset (in 16-bit words) depends on the record's length (dcraw). + 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; + } + + /// Decodes the strip, places samples by slice, and measures black on the masked columns left of the active area. + 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; + } + + /// Per-CFA-colour mean of the masked columns (4 px away from both edges of the mask). + private RawBlackLevel MaskedBlack(ushort[] samples, List warnings) + { + if (active.Left < 12) + { + warnings.Add("CR2: no masked columns; black level 0 assumed"); + return NativeRaw.Black(0); + } + Span sum = stackalloc double[4]; + Span 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); + } + } +} diff --git a/src/Titano.Core/Codecs/Raw/NativeRaw.cs b/src/Titano.Core/Codecs/Raw/NativeRaw.cs new file mode 100644 index 00000000..342d110b --- /dev/null +++ b/src/Titano.Core/Codecs/Raw/NativeRaw.cs @@ -0,0 +1,251 @@ +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; + } +} diff --git a/src/Titano.Core/Codecs/Raw/NefReader.cs b/src/Titano.Core/Codecs/Raw/NefReader.cs new file mode 100644 index 00000000..e42d8fc5 --- /dev/null +++ b/src/Titano.Core/Codecs/Raw/NefReader.cs @@ -0,0 +1,231 @@ +using System.Buffers.Binary; +using Titano.Core.Mathematics; +using Titano.Core.Metadata.Tiff; + +namespace Titano.Core.Codecs.Raw; + +/// +/// 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 +/// nikon_load_raw 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. +/// +internal static class NefReader +{ + private const ushort WbLevelsTag = 0x000C; + private const ushort BlackLevelTag = 0x003D; + private const ushort DecodeInfoTag = 0x0096; + private const uint CompressionNikon = 34713; + + /// Nikon Huffman trees (dcraw nikon_tree): 16 code-length counts, then the symbols (len | shift << 4). + 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(); + 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); + } + + /// The full-resolution CFA IFD: IFD0 or one of its SubIFDs. + 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; + } + + /// Absolute position and length of the image data (several strips must be contiguous). + 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); + } + + /// Nikon type-3 MakerNote: "Nikon\0", version, then an embedded TIFF header 10 bytes in. + 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; + } + + /// Parsed MakerNote 0x0096: Huffman tree, initial vertical predictors, tone curve and split row. + 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); + } + } + + /// dcraw nikon_load_raw. + 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 vpred = stackalloc ushort[4]; + info.Predictors.CopyTo(vpred); + Span 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); + } + } +} diff --git a/src/Titano.Core/Codecs/Raw/PefReader.cs b/src/Titano.Core/Codecs/Raw/PefReader.cs new file mode 100644 index 00000000..7c8f73b2 --- /dev/null +++ b/src/Titano.Core/Codecs/Raw/PefReader.cs @@ -0,0 +1,149 @@ +using System.Buffers.Binary; +using Titano.Core.Mathematics; +using Titano.Core.Metadata.Tiff; + +namespace Titano.Core.Codecs.Raw; + +/// +/// 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 pentax_load_raw), 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. +/// +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(); + 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); + } + + /// "AOC\0" + byte order ("MM"/"II", else the file's) + IFD; offsets relative to the file's TIFF header. + 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; + } + + /// 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). + 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 vpred = stackalloc int[4]; + Span 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; + } + } +} diff --git a/src/Titano.Core/Codecs/Raw/RafReader.cs b/src/Titano.Core/Codecs/Raw/RafReader.cs new file mode 100644 index 00000000..2219b7ee --- /dev/null +++ b/src/Titano.Core/Codecs/Raw/RafReader.cs @@ -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; + +/// +/// 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. +/// +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(); + 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); + } + + /// RAF directory: u32 record count, then (u16 tag, u16 size, data) records, big-endian. + private static Dictionary Directory(Stream stream, uint offset, uint length) + { + var result = new Dictionary(); + 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; + } +} diff --git a/src/Titano.Core/Codecs/Raw/RawDecoder.cs b/src/Titano.Core/Codecs/Raw/RawDecoder.cs index 9e1dff47..51094fb1 100644 --- a/src/Titano.Core/Codecs/Raw/RawDecoder.cs +++ b/src/Titano.Core/Codecs/Raw/RawDecoder.cs @@ -58,15 +58,15 @@ public sealed class RawDecodeResult(ImageBuffer image, RawImageInfo info, IReadO } /// -/// Managed RAW decoding (BRIEF §8.2): DNG (CFA and LinearRaw) through , -/// developed by the shared pipeline. Bodies or encodings +/// Managed RAW decoding (BRIEF §8.2): DNG (CFA and LinearRaw) through , 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 so callers can fall back to the embedded preview. /// public static class RawDecoder { /// Formats with a managed decoder (individual files may still be unsupported). public static IReadOnlyList Formats { get; } = - [ImageFileFormat.Dng]; + [ImageFileFormat.Dng, ImageFileFormat.Cr2, ImageFileFormat.Nef, ImageFileFormat.Arw, ImageFileFormat.Pef, ImageFileFormat.Raf]; /// Parses the container and decides whether it can be developed natively; never reads the pixel payload. 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); diff --git a/src/Titano.Core/Codecs/Raw/RawDeveloper.cs b/src/Titano.Core/Codecs/Raw/RawDeveloper.cs index 94e0253d..fcd185a1 100644 --- a/src/Titano.Core/Codecs/Raw/RawDeveloper.cs +++ b/src/Titano.Core/Codecs/Raw/RawDeveloper.cs @@ -17,8 +17,11 @@ internal static class RawDeveloper /// Normalised level at or above which a sample counts as clipped. private const float ClipThreshold = 0.999f; - /// LibRaw's adjust_maximum_thr: the data maximum replaces the nominal white level above this fraction of it. - private const double AdjustMaximumThreshold = 0.75; + /// A pile-up of samples at the data maximum counts as sensor saturation only above this fraction of the white level. + private const double SaturationFloor = 0.75; + + /// Samples that must sit at the data maximum for it to count as saturation rather than the brightest detail. + private const int SaturationPileUp = 16; public static ImageBuffer Develop(RawFrame frame, RawSamples samples, RawDecodeOptions options, ICollection 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 warnings, CancellationToken cancellationToken) + private static ImageBuffer DevelopColour(RawFrame frame, OpcodeImage stage, float saturation, RawDecodeOptions options, ICollection 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 /// Linear reference values over the active area plus a clip map (bit = CFA colour or sample plane), or null /// when nothing clipped. - 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 warnings) + /// Normalised saturation level of a native RAW: the data maximum when at least + /// samples reach it and it lies between 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. + 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) diff --git a/src/Titano.Core/Codecs/Raw/RawFrame.cs b/src/Titano.Core/Codecs/Raw/RawFrame.cs index 9d5e940b..3fc24b4d 100644 --- a/src/Titano.Core/Codecs/Raw/RawFrame.cs +++ b/src/Titano.Core/Codecs/Raw/RawFrame.cs @@ -112,9 +112,9 @@ internal sealed class RawFrame /// White level per sample plane, in linearised units. public required double[] White { get; init; } - /// 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. - public bool AdjustWhiteToData { get; init; } + /// 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). + public bool DetectSaturation { get; init; } public required DngColorProfile Color { get; init; } public byte[]? OpcodeList1 { get; init; } diff --git a/tests/Titano.Core.Tests/Raw/NativeRawTests.cs b/tests/Titano.Core.Tests/Raw/NativeRawTests.cs new file mode 100644 index 00000000..6166181a --- /dev/null +++ b/tests/Titano.Core.Tests/Raw/NativeRawTests.cs @@ -0,0 +1,183 @@ +using Titano.Core.Codecs.Raw; +using Titano.Core.Imaging; +using Titano.Testing; +using static Titano.Core.Tests.Raw.NativeRawWriters; + +namespace Titano.Core.Tests.Raw; + +/// Native RAW decoders on synthetic files written in each container's layout: the decoded image must match the +/// scene the sensor data was generated from (camera matrix, as-shot white balance, black/white levels, crop). +public sealed class NativeRawTests(TestContext context) +{ + private const int W = 96, H = 64; + + private RawDecodeResult Decode(byte[] file, string name, RawDecodeOptions? options = null) + { + var result = RawDecoder.Decode(new MemoryStream(file), name, options); + context.Log($"{name}: {result.Info} warnings: {string.Join("; ", result.Warnings)}"); + return result; + } + + private static void AssertScene(ImageBuffer scene, ImageBuffer image, double minimum, TestContext context) + { + Assert.Equal(scene.Width, image.Width); + Assert.Equal(scene.Height, image.Height); + var psnr = SyntheticRaw.Psnr(scene, image, 4); + context.Log($"PSNR {psnr:F1} dB"); + Assert.GreaterOrEqual(psnr, minimum); + } + + [Test] + public void Cr2SlicedLosslessJpegWithMaskedBlack() + { + using var scene = SyntheticRaw.Smooth(W, H); + // 24 masked columns left and 4 rows on top; Canon's CFA is RGGB at the raster origin. + var active = new RawRect(24, 4, 24 + W, 4 + H); + var sensor = Sensor(scene, "Canon EOS 5D Mark III", CfaPattern.Bayer("RGGB"), 128, 72, active.Left, active.Top, 2048, 16383); + using var result = Decode(Cr2(sensor, active), "IMG_0001.CR2"); + Assert.Equal("managed-cr2", result.Info.Decoder); + Assert.Equal("RGGB", result.Info.Layout); + AssertScene(scene, result.Image, 38, context); + } + + [Theory] + [Case(NefEncoding.Lossless, 14)] + [Case(NefEncoding.Lossless, 12)] + [Case(NefEncoding.Unpacked, 14)] + [Case(NefEncoding.Packed, 12)] + public void NefEncodings(NefEncoding encoding, int bits) + { + using var scene = SyntheticRaw.Smooth(W, H); + var sensor = Sensor(scene, "Nikon D850", CfaPattern.Bayer("RGGB"), W, H, 0, 0, 600 >> (14 - bits), (1 << bits) - 1); + using var result = Decode(Nef(sensor, bits, encoding), "DSC_0001.NEF"); + Assert.Equal("managed-nef", result.Info.Decoder); + AssertScene(scene, result.Image, 38, context); + } + + [Theory] + [Case(NefEncoding.LossyType1, 12, 0)] + [Case(NefEncoding.LossyType2, 14, 0)] + [Case(NefEncoding.LossyType2, 14, 40)] + public void NefLossyCurves(NefEncoding encoding, int bits, int split) + { + // A square-law curve: dense codes in the shadows, coarse in the highlights. + var size = encoding == NefEncoding.LossyType1 ? 683 : 1 << bits; + var curve = Enumerable.Range(0, size).Select(i => (ushort)double.Round(((1 << bits) - 1) * (i / (double)(size - 1)) * (i / (double)(size - 1)))).ToArray(); + var white = encoding == NefEncoding.LossyType1 ? curve[^1] : InterpolatedCurve(curve, bits).Max(); + // After the split row the tree has no unshifted codes for 9–12-bit differences: keep that scene gentle. + using var scene = split > 0 ? Gentle() : SyntheticRaw.Smooth(W, H); + var sensor = Sensor(scene, "Nikon D850", CfaPattern.Bayer("RGGB"), W, H, 0, 0, 600 >> (14 - bits), white); + using var result = Decode(Nef(sensor, bits, encoding, curve: curve, split: split), "DSC_0002.NEF"); + AssertScene(scene, result.Image, 36, context); + } + + [Theory] + [Case(true)] + [Case(false)] + public void ArwCompressedAndUncompressed(bool compressed) + { + ushort[] knots = [8000, 10400, 12900, 14100]; + var curve = ToneCurve(knots); + double white = compressed ? curve[0x7FF << 1] : 16383; + using var scene = SyntheticRaw.Smooth(W, H); + var sensor = Sensor(scene, "Sony ILCE-7M3", CfaPattern.Bayer("RGGB"), W, H, 0, 0, 512, white); + using var result = Decode(Arw(sensor, knots, compressed), "DSC00001.ARW"); + Assert.Equal("managed-arw", result.Info.Decoder); + Assert.Contains(result.Warnings, w => w.Contains("daylight")); + AssertScene(scene, result.Image, compressed ? 36 : 38, context); + } + + [Theory] + [Case(true)] + [Case(false)] + public void PefHuffmanAndUncompressed(bool compressed) + { + using var scene = SyntheticRaw.Smooth(W, H); + var sensor = Sensor(scene, "Pentax K-1", CfaPattern.Bayer("BGGR"), W, H, 0, 0, 512, 16383); + using var result = Decode(Pef(sensor, 14, compressed), "IMGP0001.PEF"); + Assert.Equal("managed-pef", result.Info.Decoder); + Assert.Equal("BGGR", result.Info.Layout); + AssertScene(scene, result.Image, 38, context); + } + + [Theory] + [Case(true)] + [Case(false)] + public void RafXTransAndBayerLayouts(bool xtrans) + { + using var scene = SyntheticRaw.Smooth(W, H); + var rasterCfa = xtrans ? DemosaicTests.XTrans : CfaPattern.Bayer("GRBG"); + // Crop starts at an odd offset so the pattern must be re-anchored. + var crop = new RawRect(5, 3, 5 + W, 3 + H); + var sensor = Sensor(scene, "Fujifilm X-T2", rasterCfa, W + 8, H + 6, crop.Left, crop.Top, 1024, 16383); + using var result = Decode(Raf(sensor, Expand(rasterCfa), crop, orientation: 1), "DSCF0001.RAF"); + Assert.Equal("managed-raf", result.Info.Decoder); + Assert.Equal("X-T2", result.Info.Model); + AssertScene(scene, result.Image, xtrans ? 34 : 38, context); + using var binned = Decode(Raf(sensor, Expand(rasterCfa), crop), "DSCF0001.RAF", new RawDecodeOptions { Binning = true }); + Assert.Equal(W / (xtrans ? 3 : 2), binned.Image.Width); + } + + [Test] + public void OrientationComesFromTheContainer() + { + using var scene = SyntheticRaw.Smooth(W, H); + var sensor = Sensor(scene, "Nikon D850", CfaPattern.Bayer("RGGB"), W, H, 0, 0, 600, 16383); + using var result = Decode(Nef(sensor, 14, NefEncoding.Lossless, orientation: 8), "DSC_0003.NEF"); + Assert.Equal(H, result.Image.Width); + Assert.Equal(W, result.Image.Height); + Assert.Equal(H, result.Info.Width); + } + + [Test] + public void UnknownBodyIsUnsupported() + { + using var scene = SyntheticRaw.Smooth(W, H); + var active = new RawRect(24, 4, 24 + W, 4 + H); + var sensor = Sensor(scene, "Canon EOS 5D Mark III", CfaPattern.Bayer("RGGB"), 128, 72, active.Left, active.Top, 2048, 16383); + var probe = RawDecoder.Probe(new MemoryStream(Cr2(sensor, active, model: "Canon EOS R99")), "x.cr2"); + Assert.False(probe.IsSupported); + Assert.Contains("no colour matrix", probe.Reason); + Assert.Throws(() => RawDecoder.Decode(new MemoryStream(Cr2(sensor, active, model: "Canon EOS R99")), "x.cr2")); + } + + [Test] + public void CameraNamesNormalise() + { + Assert.Equal("Nikon D850", NativeRaw.CameraName("NIKON CORPORATION", "NIKON D850")); + Assert.Equal("Canon EOS 5D Mark III", NativeRaw.CameraName("Canon", "Canon EOS 5D Mark III")); + Assert.Equal("Sony ILCE-7M3", NativeRaw.CameraName("SONY", "ILCE-7M3")); + Assert.Equal("Pentax K-1", NativeRaw.CameraName("RICOH IMAGING COMPANY, LTD.", "PENTAX K-1")); + Assert.Equal("Fujifilm X-T2", NativeRaw.CameraName("FUJIFILM", "X-T2")); + // Every tabulated matrix maps D65 to a positive camera neutral with green the strongest channel. + foreach (var name in CameraMatrices.Names) + { + var m = CameraMatrices.Find(name)!.Value; + var n = m * Titano.Core.Mathematics.Chromaticity.D65.ToXyz(); + Assert.True(n.X > 0 && n.Z > 0 && n.Y > n.X && n.Y > n.Z, name); + } + } + + private static ImageBuffer Gentle() + { + var scene = new ImageBuffer(W, H); + for (var y = 0; y < H; y++) + { + for (var x = 0; x < W; x++) + { + scene[0, x, y] = 0.3f + 0.03f * x / W; + scene[1, x, y] = 0.32f + 0.02f * y / H; + scene[2, x, y] = 0.25f + 0.01f * (x + y) / (W + H); + } + } + return scene; + } + + /// The pattern written into the RAF layout record, expanded to 6×6. + private static CfaPattern Expand(CfaPattern cfa) + { + var colors = new byte[36]; + for (var i = 0; i < 36; i++) colors[i] = (byte)cfa.ColorAt(i % 6, i / 6); + return new CfaPattern(6, 6, colors); + } +} diff --git a/tests/Titano.Core.Tests/Raw/NativeRawWriters.cs b/tests/Titano.Core.Tests/Raw/NativeRawWriters.cs new file mode 100644 index 00000000..9fa3bce0 --- /dev/null +++ b/tests/Titano.Core.Tests/Raw/NativeRawWriters.cs @@ -0,0 +1,485 @@ +using System.Buffers.Binary; +using Titano.Core.Codecs.Jpeg; +using Titano.Core.Codecs.Raw; +using Titano.Core.Imaging; +using Titano.Core.Mathematics; +using Titano.Core.Metadata; +using Titano.Core.Metadata.Tiff; +using Titano.Core.Tests.Metadata.Builders; + +namespace Titano.Core.Tests.Raw; + +/// Payload encodings of the synthetic NEF writer. +public enum NefEncoding +{ + Lossless, + LossyType1, + LossyType2, + Unpacked, + Packed, +} + +/// A synthetic sensor readout: stored samples of a scene through a camera model, plus the as-shot neutral. +internal sealed record SensorData(ushort[] Samples, int Width, int Height, Vector3D Neutral); + +/// +/// Test-side writers for native RAW containers. Each follows the layout its reader documents (and dcraw/LibRaw decode): +/// CR2 sliced lossless JPEG with Canon MakerNote, NEF Nikon Huffman with type-3 MakerNote, ARW2 cRAW blocks with tone +/// curve, PEF Pentax Huffman with AOC MakerNote, RAF with FujiIFD and RAF directory. +/// +internal static class NativeRawWriters +{ + /// Stored sensor values of placed at (left, top) of a raster, CFA anchored at the raster + /// origin, outside pixels at black. + public static SensorData Sensor(ImageBuffer scene, string camera, CfaPattern cfa, int width, int height, int left, int top, double black, double white) + { + var matrix = CameraMatrices.Find(camera) ?? throw new ArgumentException("No matrix for " + camera); + var (rgb, neutral) = SyntheticRaw.ToCamera(scene, matrix, Chromaticity.D65); + var samples = new ushort[width * height]; + for (var y = 0; y < height; y++) + { + for (var x = 0; x < width; x++) + { + int sx = x - left, sy = y - top; + var v = black; + if (sx >= 0 && sy >= 0 && sx < scene.Width && sy < scene.Height) v += rgb[cfa.ColorAt(x, y)][sy * scene.Width + sx] * (white - black); + samples[y * width + x] = (ushort)double.Clamp(double.Round(v), 0, white); + } + } + return new SensorData(samples, width, height, neutral); + } + + /// Real JPEG preview with an Exif block (make, model, orientation). + public static byte[] Preview(string make, string model, int orientation, int width = 64, int height = 48) + { + using var image = new ImageBuffer(width, height); + for (var i = 0; i < image.PlaneLength; i++) + { + image.Plane(0)[i] = 0.5f; + image.Plane(1)[i] = 0.3f; + image.Plane(2)[i] = 0.2f; + } + var exif = ExifWriter.BuildTiff(new FrameMetadata { Make = make, Model = model, Orientation = orientation }); + return JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 85, App1Segments = [[.. "Exif\0\0"u8, .. exif]] }); + } + + /// Canon CR2: "CR" header, IFD0 JPEG preview, two dummy IFDs, IFD3 lossless raw in two slices. + public static byte[] Cr2(SensorData s, RawRect active, int orientation = 1, string model = "Canon EOS 5D Mark III") + { + const int components = 2; + var sliceWidth = s.Width / 2; + var stream = new ushort[s.Samples.Length]; + var k = 0; + foreach (var x0 in new[] { 0, sliceWidth }) + { + for (var y = 0; y < s.Height; y++) + { + for (var x = 0; x < sliceWidth; x++) stream[k++] = s.Samples[y * s.Width + x0 + x]; + } + } + var jpeg = JpegLosslessEncoder.Encode(stream, s.Width / components, s.Height, components, 14); + var sensor = new ushort[17]; + sensor[1] = (ushort)s.Width; + sensor[2] = (ushort)s.Height; + sensor[5] = (ushort)active.Left; + sensor[6] = (ushort)active.Top; + sensor[7] = (ushort)(active.Right - 1); + sensor[8] = (ushort)(active.Bottom - 1); + var colorData = new ushort[1338]; + colorData[63] = (ushort)double.Round(1024 * s.Neutral.Y / s.Neutral.X); + colorData[64] = colorData[65] = 1024; + colorData[66] = (ushort)double.Round(1024 * s.Neutral.Y / s.Neutral.Z); + var makerNote = new IfdSpec().Short(0x00E0, sensor).Short(0x4001, colorData); + var exif = new IfdSpec().Ascii(TiffTags.DateTimeOriginal, "2024:05:01 12:00:00").Embedded(TiffTags.MakerNote, [], makerNote, relativeToTiff: true, littleEndian: true); + var preview = Preview("Canon", model, orientation); + var ifd0 = new IfdSpec().Ascii(TiffTags.Make, "Canon").Ascii(TiffTags.Model, model).Short(TiffTags.Orientation, (ushort)orientation) + .Short(TiffTags.Compression, 6).Strip(preview).Child(TiffTags.ExifIfd, exif); + var ifd1 = new IfdSpec().Short(TiffTags.Compression, 6); + var ifd2 = new IfdSpec().Short(TiffTags.Compression, 1); + var ifd3 = new IfdSpec().Short(TiffTags.Compression, 6).Strip(jpeg).Short(0xC640, 1, (ushort)sliceWidth, (ushort)(s.Width - sliceWidth)); + ifd0.Next = ifd1; + ifd1.Next = ifd2; + ifd2.Next = ifd3; + return TiffFile.Build(ifd0, true, headerExtra: [(byte)'C', (byte)'R', 2, 0, 0, 0, 0, 0]); + } + + /// Canonical codes of a dcraw-layout Huffman specification: symbol → (code, length). + public static Dictionary Codes(byte[] spec) + { + var max = 16; + while (spec[max - 1] == 0) max--; + var codes = new Dictionary(); + var h = 0; + var symbol = 16; + for (var len = 1; len <= max; len++) + { + for (var i = 0; i < spec[len - 1]; i++, symbol++) + { + codes.TryAdd(spec[symbol], (h >> (max - len), len)); + h += 1 << (max - len); + } + } + return codes; + } + + /// JPEG difference category and its bits (negative values one's-complement style). + public static (int Length, int Bits) Category(int diff) + { + if (diff == 0) return (0, 0); + var len = 32 - int.LeadingZeroCount(int.Abs(diff)); + return (len, diff > 0 ? diff : diff + (1 << len) - 1); + } + + /// Two-column horizontal prediction seeded by per-row-parity vertical predictors (Nikon and Pentax). + public static byte[] PredictiveHuffman(ushort[] values, int width, int height, Func> codesForRow, ushort[] initial) + { + var w = new BitWriter(); + var vpred = (int[])[initial[0], initial[1], initial[2], initial[3]]; + var hpred = new int[2]; + for (var row = 0; row < height; row++) + { + var codes = codesForRow(row); + for (var col = 0; col < width; col++) + { + int v = values[row * width + col], predicted; + if (col < 2) + { + predicted = vpred[(row & 1) * 2 + col]; + vpred[(row & 1) * 2 + col] = v; + hpred[col] = v; + } + else + { + predicted = hpred[col & 1]; + hpred[col & 1] = v; + } + var (len, bits) = Category(v - predicted); + var (code, codeLength) = codes[len]; + w.Write(code, codeLength); + w.Write(bits, len); + } + } + return w.ToArray(); + } + + /// Big-endian Nikon NEF: IFD0 thumbnail, SubIFD CFA raw, Exif with a type-3 MakerNote. + public static byte[] Nef(SensorData s, int bits, NefEncoding encoding, int orientation = 1, ushort[]? curve = null, int split = 0) + { + byte[] data; + uint compression = 34713; + var meta = new List { 0x46, 0x30 }; + ushort[] vpred = [100, 200, 300, 400]; + switch (encoding) + { + case NefEncoding.Lossless: + data = PredictiveHuffman(s.Samples, s.Width, s.Height, _ => Codes(NefTrees(bits == 14 ? 5 : 2)), vpred); + break; + case NefEncoding.LossyType1: + { + meta = [0x44, 0x10]; + var indices = Indices(s.Samples, curve!); + data = PredictiveHuffman(indices, s.Width, s.Height, _ => Codes(NefTrees(bits == 14 ? 3 : 0)), vpred); + break; + } + case NefEncoding.LossyType2: + { + meta = [0x44, 0x20]; + var indices = Indices(s.Samples, InterpolatedCurve(curve!, bits)); + var tree = bits == 14 ? 3 : 0; + data = PredictiveHuffman(indices, s.Width, s.Height, row => Codes(NefTrees(split > 0 && row >= split ? tree + 1 : tree)), vpred); + break; + } + case NefEncoding.Unpacked: + data = new byte[s.Samples.Length * 2]; + for (var i = 0; i < s.Samples.Length; i++) BinaryPrimitives.WriteUInt16BigEndian(data.AsSpan(2 * i), s.Samples[i]); + break; + default: + compression = 1; + data = Packed(s.Samples, s.Width, s.Height, bits); + break; + } + foreach (var v in vpred) meta.AddRange([(byte)(v >> 8), (byte)v]); + if (encoding == NefEncoding.LossyType1) + { + meta.AddRange([(byte)(curve!.Length >> 8), (byte)curve.Length]); + foreach (var v in curve) meta.AddRange([(byte)(v >> 8), (byte)v]); + } + else if (encoding == NefEncoding.LossyType2) + { + // Curve sampled every 'step' codes; split row at byte 562. + meta.AddRange([(byte)(CurvePoints >> 8), unchecked((byte)CurvePoints)]); + foreach (var v in CurveSamples(curve!, bits)) meta.AddRange([(byte)(v >> 8), (byte)v]); + while (meta.Count < 562) meta.Add(0); + meta.AddRange([(byte)(split >> 8), (byte)split]); + } + else + { + meta.AddRange([0, 0]); + } + var nikon = new IfdSpec() + .Rational(0x000C, ((uint)double.Round(1e5 * s.Neutral.Y / s.Neutral.X), 100000u), ((uint)double.Round(1e5 * s.Neutral.Y / s.Neutral.Z), 100000u), (1u, 1u), (1u, 1u)) + .Short(0x003D, 600, 600, 600, 600) + .Short(0x0093, (ushort)(encoding == NefEncoding.Lossless ? 3 : encoding == NefEncoding.LossyType1 ? 1 : encoding == NefEncoding.LossyType2 ? 4 : 2)) + .Undefined(0x0096, [.. meta]); + var exif = new IfdSpec().Undefined(TiffTags.MakerNote, [.. "Nikon\0"u8, 2, 0x11, 0, 0, .. TiffFile.Build(nikon, littleEndian: false)]); + var raw = new IfdSpec().Long(TiffTags.NewSubfileType, 0) + .Long(TiffTags.ImageWidth, (uint)s.Width).Long(TiffTags.ImageLength, (uint)s.Height) + .Short(TiffTags.BitsPerSample, (ushort)bits).Short(TiffTags.Compression, (ushort)compression) + .Short(TiffTags.PhotometricInterpretation, 32803) + .Short(TiffTags.CfaRepeatPatternDim, 2, 2).Bytes(TiffTags.CfaPattern, 0, 1, 1, 2) + .Strip(data); + var ifd0 = new IfdSpec().Ascii(TiffTags.Make, "NIKON CORPORATION").Ascii(TiffTags.Model, "NIKON D850") + .Short(TiffTags.Orientation, (ushort)orientation).Long(TiffTags.NewSubfileType, 1) + .Child(TiffTags.SubIfds, raw).Child(TiffTags.ExifIfd, exif); + return TiffFile.Build(ifd0, littleEndian: false); + } + + private static byte[] NefTrees(int index) => NefReader.Trees[index]; + + private const int CurvePoints = 257; + + private static ushort[] CurveSamples(ushort[] curve, int bits) + { + var step = (1 << bits & 0x7FFF) / (CurvePoints - 1); + return [.. Enumerable.Range(0, CurvePoints).Select(i => curve[int.Min(i * step, curve.Length - 1)])]; + } + + /// The curve a lossy type-2 decoder rebuilds: samples every step codes, linearly interpolated. + public static ushort[] InterpolatedCurve(ushort[] curve, int bits) + { + var max = 1 << bits & 0x7FFF; + var step = max / (CurvePoints - 1); + var samples = CurveSamples(curve, bits); + var result = new ushort[max]; + for (var i = 0; i < max; i++) + { + int k = i % step, a = i / step; + result[i] = (ushort)((samples[a] * (step - k) + samples[int.Min(a + 1, samples.Length - 1)] * k) / step); + } + return result; + } + + /// Curve indices whose values are nearest to the samples (monotonic curve). + public static ushort[] Indices(ushort[] samples, ushort[] curve) + { + var result = new ushort[samples.Length]; + for (var i = 0; i < samples.Length; i++) + { + var target = samples[i]; + int lo = 0, hi = curve.Length - 1; + while (lo < hi) + { + var mid = (lo + hi) / 2; + if (curve[mid] < target) lo = mid + 1; + else hi = mid; + } + if (lo > 0 && target - curve[lo - 1] < curve[lo] - target) lo--; + result[i] = (ushort)lo; + } + return result; + } + + /// MSB-first packed samples (TIFF convention), rows padded to whole bytes. + public static byte[] Packed(ushort[] samples, int width, int height, int bits) + { + var w = new BitWriter(); + for (var y = 0; y < height; y++) + { + for (var x = 0; x < width; x++) w.Write(samples[y * width + x], bits); + w.Align(); + } + return w.ToArray(); + } + + /// Sony ARW2 (cRAW): 16-pixel blocks of one colour in 32-column groups, values through the inverse tone curve. + public static byte[] Arw(SensorData s, ushort[] knots, bool compressed, int orientation = 1) + { + byte[] data; + uint compression = 32767; + if (compressed) + { + var curve = ToneCurve(knots); + var even = new ushort[2048]; + for (var i = 0; i < even.Length; i++) even[i] = curve[i << 1]; + var codes = Indices(s.Samples, even); + data = new byte[s.Width * s.Height]; + for (var y = 0; y < s.Height; y++) + { + for (var group = 0; group < s.Width; group += 32) + { + for (var parity = 0; parity < 2; parity++) + { + var block = data.AsSpan(y * s.Width + group + parity * 16, 16); + var pix = new int[16]; + for (var i = 0; i < 16; i++) pix[i] = codes[y * s.Width + group + parity + 2 * i]; + EncodeBlock(pix, block); + } + } + } + } + else + { + data = new byte[s.Samples.Length * 2]; + for (var i = 0; i < s.Samples.Length; i++) BinaryPrimitives.WriteUInt16LittleEndian(data.AsSpan(2 * i), s.Samples[i]); + } + var raw = new IfdSpec().Long(TiffTags.NewSubfileType, 0) + .Long(TiffTags.ImageWidth, (uint)s.Width).Long(TiffTags.ImageLength, (uint)s.Height) + .Short(TiffTags.BitsPerSample, (ushort)(compressed ? 8 : 14)).Short(TiffTags.Compression, (ushort)compression) + .Short(TiffTags.PhotometricInterpretation, 32803) + .Short(TiffTags.CfaRepeatPatternDim, 2, 2).Bytes(TiffTags.CfaPattern, 0, 1, 1, 2) + .Short(0x7010, knots).Short(0x7310, 512, 512, 512, 512) + .Strip(data); + var ifd0 = new IfdSpec().Ascii(TiffTags.Make, "SONY").Ascii(TiffTags.Model, "ILCE-7M3").Short(TiffTags.Orientation, (ushort)orientation) + .Long(TiffTags.NewSubfileType, 1).Child(TiffTags.SubIfds, raw); + return TiffFile.Build(ifd0); + } + + /// The reader's tone curve (slopes 1, 2, 4, 8, 16 between the knots). + public static ushort[] ToneCurve(ushort[] knots) + { + int[] k = [0, knots[0] >> 2 & 0xFFF, knots[1] >> 2 & 0xFFF, knots[2] >> 2 & 0xFFF, knots[3] >> 2 & 0xFFF, 4095]; + var curve = new ushort[4096]; + for (var i = 0; i < curve.Length; i++) curve[i] = (ushort)i; + for (var i = 0; i < 5; i++) + { + for (var j = k[i] + 1; j <= k[i + 1]; j++) curve[j] = (ushort)(curve[j - 1] + (1 << i)); + } + return curve; + } + + private static void EncodeBlock(int[] pix, Span block) + { + int imax = 0, imin = 0; + for (var i = 1; i < 16; i++) + { + if (pix[i] > pix[imax]) imax = i; + if (pix[i] < pix[imin]) imin = i; + } + if (imax == imin) imin = imax == 0 ? 1 : 0; + int max = pix[imax], min = pix[imin]; + var sh = 0; + while (sh < 4 && 0x80 << sh <= max - min) sh++; + var bits = new System.Collections.BitArray(130); + void Put(int value, int at, int count) + { + for (var b = 0; b < count; b++) bits[at + b] = (value >> b & 1) != 0; + } + Put(max, 0, 11); + Put(min, 11, 11); + Put(imax, 22, 4); + Put(imin, 26, 4); + var bit = 30; + for (var i = 0; i < 16; i++) + { + if (i == imax || i == imin) continue; + Put(int.Min((pix[i] - min) >> sh, 0x7F), bit, 7); + bit += 7; + } + for (var i = 0; i < 16; i++) + { + byte v = 0; + for (var b = 0; b < 8; b++) v |= (byte)(bits[i * 8 + b] ? 1 << b : 0); + block[i] = v; + } + } + + /// Pentax PEF: big-endian, raw in IFD0, AOC MakerNote with black, WB and the Huffman table. + public static byte[] Pef(SensorData s, int bits, bool compressed, int orientation = 1) + { + // Prefix code over difference lengths 0..14 (Kraft sum below 1). + int[] lengths = [3, 3, 3, 3, 3, 4, 4, 5, 5, 6, 7, 8, 9, 10, 10]; + var codes = new Dictionary(); + var code = 0; + var previous = lengths[0]; + for (var c = 0; c < lengths.Length; c++) + { + code <<= lengths[c] - previous; + previous = lengths[c]; + codes[c] = (code, lengths[c]); + code++; + } + var table = new List { 0, (byte)((lengths.Length - 12) & 15) }; + table.AddRange(new byte[12]); + for (var c = 0; c < lengths.Length; c++) + { + var aligned = codes[c].Code << (12 - codes[c].Length); + table.AddRange([(byte)(aligned >> 8), (byte)aligned]); + } + foreach (var len in lengths) table.Add((byte)len); + var data = compressed ? PredictiveHuffman(s.Samples, s.Width, s.Height, _ => codes, [0, 0, 0, 0]) : Packed(s.Samples, s.Width, s.Height, bits); + var levels = new ushort[] { (ushort)double.Round(1000 * s.Neutral.Y / s.Neutral.X), 1000, 1000, (ushort)double.Round(1000 * s.Neutral.Y / s.Neutral.Z) }; + var pentax = new IfdSpec().Short(0x0200, 512, 512, 512, 512).Short(0x0201, levels).Undefined(0x0220, [.. table]); + var exif = new IfdSpec().Embedded(TiffTags.MakerNote, [.. "AOC\0MM"u8], pentax, relativeToTiff: true, littleEndian: false); + var ifd0 = new IfdSpec().Ascii(TiffTags.Make, "RICOH IMAGING COMPANY, LTD.").Ascii(TiffTags.Model, "PENTAX K-1") + .Short(TiffTags.Orientation, (ushort)orientation).Long(TiffTags.NewSubfileType, 0) + .Long(TiffTags.ImageWidth, (uint)s.Width).Long(TiffTags.ImageLength, (uint)s.Height) + .Short(TiffTags.BitsPerSample, (ushort)bits).Short(TiffTags.Compression, (ushort)(compressed ? 65535 : 1)) + .Short(TiffTags.PhotometricInterpretation, 32803) + .Short(TiffTags.CfaRepeatPatternDim, 2, 2).Bytes(TiffTags.CfaPattern, 2, 1, 1, 0) + .Strip(data).Child(TiffTags.ExifIfd, exif); + return TiffFile.Build(ifd0, littleEndian: false); + } + + /// Fujifilm RAF: header, JPEG with Exif, RAF directory (crop, layout, WB), FujiIFD block with 16-bit data. + public static byte[] Raf(SensorData s, CfaPattern rasterCfa, RawRect crop, int orientation = 1, string model = "X-T2") + { + var jpeg = Preview("FUJIFILM", model, orientation); + var pattern = new byte[36]; + for (var i = 0; i < 36; i++) pattern[35 - i] = (byte)rasterCfa.ColorAt(i % 6, i / 6); + var records = new List(); + void Record(ushort tag, byte[] payload) => records.AddRange([.. Box.U16(tag), .. Box.U16((ushort)payload.Length), .. payload]); + Record(0x0100, [.. Box.U16((ushort)s.Height), .. Box.U16((ushort)s.Width)]); + Record(0x0110, [.. Box.U16((ushort)crop.Top), .. Box.U16((ushort)crop.Left)]); + Record(0x0111, [.. Box.U16((ushort)crop.Height), .. Box.U16((ushort)crop.Width)]); + Record(0x0130, [0, 8, 0, 0]); + Record(0x0131, pattern); + Record(0x2FF0, [.. Box.U16(302), .. Box.U16((ushort)double.Round(302 * s.Neutral.Y / s.Neutral.X)), .. Box.U16(302), .. Box.U16((ushort)double.Round(302 * s.Neutral.Y / s.Neutral.Z))]); + byte[] directory = [.. Box.U32(6), .. records]; + var data = new byte[s.Samples.Length * 2]; + for (var i = 0; i < s.Samples.Length; i++) BinaryPrimitives.WriteUInt16BigEndian(data.AsSpan(2 * i), s.Samples[i]); + var fuji = new IfdSpec().Long(0xF001, (uint)s.Width).Long(0xF002, (uint)s.Height).Long(0xF003, 14) + .Long(0xF00A, [.. Enumerable.Repeat(1024u, 36)]).Blob(0xF007, 0xF008, data); + var block = TiffFile.Build(new IfdSpec().Child(0xF000, fuji), littleEndian: false); + var jpegOffset = 0x6C; + var directoryOffset = jpegOffset + jpeg.Length; + var blockOffset = directoryOffset + directory.Length; + var header = new byte[0x6C]; + "FUJIFILMCCD-RAW 0201FF129502"u8.CopyTo(header); + System.Text.Encoding.ASCII.GetBytes(model).CopyTo(header.AsSpan(0x1C)); + uint[] fields = [(uint)jpegOffset, (uint)jpeg.Length, (uint)directoryOffset, (uint)directory.Length, (uint)blockOffset, (uint)block.Length]; + for (var i = 0; i < fields.Length; i++) BinaryPrimitives.WriteUInt32BigEndian(header.AsSpan(0x54 + 4 * i), fields[i]); + return [.. header, .. jpeg, .. directory, .. block]; + } + + /// MSB-first bit writer. + internal sealed class BitWriter + { + private readonly List _bytes = []; + private int _accumulator; + private int _count; + + public void Write(int value, int bits) + { + for (var b = bits - 1; b >= 0; b--) + { + _accumulator = _accumulator << 1 | (value >> b & 1); + if (++_count == 8) + { + _bytes.Add((byte)_accumulator); + _accumulator = _count = 0; + } + } + } + + public void Align() + { + if (_count > 0) Write(0, 8 - _count); + } + + public byte[] ToArray() + { + Align(); + return [.. _bytes, 0, 0, 0, 0]; + } + } +} diff --git a/tests/Titano.Core.Tests/Raw/RawFuzzTests.cs b/tests/Titano.Core.Tests/Raw/RawFuzzTests.cs new file mode 100644 index 00000000..06818fdd --- /dev/null +++ b/tests/Titano.Core.Tests/Raw/RawFuzzTests.cs @@ -0,0 +1,92 @@ +using Titano.Core.Codecs.Raw; +using Titano.Core.Imaging; +using Titano.Testing; +using static Titano.Core.Tests.Raw.NativeRawWriters; + +namespace Titano.Core.Tests.Raw; + +/// Truncated and corrupted RAW files may only raise (or decode). +public sealed class RawFuzzTests(TestContext context) +{ + private const int W = 48, H = 32; + + private static byte[] Sample(string format) + { + using var scene = SyntheticRaw.Smooth(W, H); + return format switch + { + "dng" => SyntheticDng.Write(scene, new DngSpec { TileSize = 16, OpcodeList2 = GainMapList(), OpcodeList3 = new OpcodeListBuilder().Add(3, 0, p => p.F64(0.1).F64(0).F64(0).F64(0).F64(0).F64(0.5).F64(0.5)).Build() }), + "linear.dng" => SyntheticDng.Write(scene, new DngSpec { Cfa = null, Compression = 8 }), + "cr2" => Cr2(Sensor(scene, "Canon EOS 5D Mark III", CfaPattern.Bayer("RGGB"), 80, 36, 24, 4, 2048, 16383), new RawRect(24, 4, 24 + W, 4 + H)), + "nef" => Nef(Sensor(scene, "Nikon D850", CfaPattern.Bayer("RGGB"), W, H, 0, 0, 600, 16383), 14, NefEncoding.Lossless), + "arw" => Arw(Sensor(scene, "Sony ILCE-7M3", CfaPattern.Bayer("RGGB"), 64, H, 0, 0, 512, 16000), [8000, 10400, 12900, 14100], compressed: true), + "pef" => Pef(Sensor(scene, "Pentax K-1", CfaPattern.Bayer("BGGR"), W, H, 0, 0, 512, 16383), 14, compressed: true), + _ => Raf(Sensor(scene, "Fujifilm X-T2", DemosaicTests.XTrans, W + 6, H + 6, 3, 3, 1024, 16383), DemosaicTests.XTrans, new RawRect(3, 3, 3 + W, 3 + H)), + }; + } + + private static byte[] GainMapList() => new OpcodeListBuilder().Add(9, 0, p => + { + OpcodeListBuilder.Area(p, 0, 0, H, W); + p.U32(2).U32(2).F64(1).F64(1).F64(0).F64(0).U32(1).F32(1).F32(1.1f).F32(1.2f).F32(1.3f); + }).Build(); + + [Theory] + [Case("dng")] + [Case("linear.dng")] + [Case("cr2")] + [Case("nef")] + [Case("arw")] + [Case("pef")] + [Case("raf")] + public void TruncatedAndCorruptedFilesOnlyRaiseInvalidData(string format) + { + var file = Sample(format); + var name = "fuzz." + format.Split('.').Last(); + var options = new RawDecodeOptions { Quality = DemosaicQuality.Draft, MaxDegreeOfParallelism = 1 }; + using (var clean = RawDecoder.Decode(new MemoryStream(file), name, options)) Assert.Equal(format == "arw" ? 64 : W, clean.Image.Width); + var random = new Random(format.Length * 7919); + var outcomes = new Dictionary(); + for (var i = 0; i < 120; i++) + { + byte[] variant; + if (i < 40) + { + variant = file[..(int)((long)file.Length * i / 40)]; + } + else + { + variant = (byte[])file.Clone(); + var flips = 1 + random.Next(4); + for (var k = 0; k < flips; k++) + { + // Bias corruption toward the headers, where structure lives. + var at = random.Next(2) == 0 ? random.Next(int.Min(variant.Length, 512)) : random.Next(variant.Length); + variant[at] = (byte)random.Next(256); + } + } + var outcome = Run(variant, name, options); + outcomes[outcome] = outcomes.GetValueOrDefault(outcome) + 1; + } + context.Log($"{format}: " + string.Join(", ", outcomes.Select(o => $"{o.Key} {o.Value}"))); + } + + private static string Run(byte[] data, string name, RawDecodeOptions options) + { + try + { + RawDecoder.Probe(new MemoryStream(data), name); + using var result = RawDecoder.Decode(new MemoryStream(data), name, options); + return "decoded"; + } + catch (InvalidDataException) + { + return "invalid"; + } + catch (Exception ex) + { + Assert.Fail($"{ex.GetType().Name}: {ex.Message}\n{ex.StackTrace}"); + return ""; + } + } +}