diff --git a/src/Titano.Core/Codecs/Jpeg/JpegDecoder.cs b/src/Titano.Core/Codecs/Jpeg/JpegDecoder.cs new file mode 100644 index 00000000..00f32884 --- /dev/null +++ b/src/Titano.Core/Codecs/Jpeg/JpegDecoder.cs @@ -0,0 +1,74 @@ +using Titano.Core.Imaging; + +namespace Titano.Core.Codecs.Jpeg; + +/// +/// Managed JPEG decoder: baseline, extended (12-bit), progressive and lossless Huffman streams; 1, 3 or 4 components +/// (CMYK/YCCK via Adobe APP14); any integral sampling factors; restart intervals with resynchronisation on corrupt +/// data; DCT-domain scaling for thumbnails. Exif orientation is not applied. Malformed headers throw +/// ; damaged entropy-coded data yields a partial image plus warnings. +/// +public static class JpegDecoder +{ + private static readonly float[]?[] LinearLuts = new float[17][]; + + /// Reads the frame header only (no pixel decoding). + public static JpegInfo ReadInfo(ReadOnlySpan data) + { + var parser = new JpegParser(JpegDecodeOptions.Default); + parser.Parse(data, headerOnly: true); + return parser.Info(); + } + + /// Decodes to interleaved gray or RGB samples (8-bit, or 16-bit containers for higher precisions). + public static JpegImage Decode(ReadOnlySpan data, JpegDecodeOptions? options = null) + { + var parser = Parse(data, options); + if (parser.Frame!.Process == JpegProcess.Lossless) return LosslessToImage(parser); + return new JpegRenderer(parser).RenderInterleaved(parser.Info()); + } + + /// Decodes into a linear-light (samples are taken as sRGB-encoded). + /// Return gray images as three identical channels instead of one. + public static ImageBuffer DecodeToLinear(ReadOnlySpan data, JpegDecodeOptions? options = null, bool expandGray = false) + { + var parser = Parse(data, options); + if (parser.Frame!.Process == JpegProcess.Lossless) + { + var image = LosslessToImage(parser); + var channels = image.Components == 1 && expandGray ? 3 : image.Components; + return image.Pixels8 is { } p8 + ? PixelConvert.FromInterleaved8(p8, image.Width, image.Height, image.Components, outputChannels: channels) + : PixelConvert.FromInterleaved16(image.Pixels16, image.Width, image.Height, image.Components, maxValue: (1 << image.BitDepth) - 1, outputChannels: channels); + } + return new JpegRenderer(parser).RenderLinear(expandGray); + } + + private static JpegParser Parse(ReadOnlySpan data, JpegDecodeOptions? options) + { + var parser = new JpegParser(options ?? JpegDecodeOptions.Default); + parser.Parse(data); + return parser; + } + + private static JpegImage LosslessToImage(JpegParser parser) + { + var raw = JpegLosslessDecoder.Finish(parser); + if (raw.Components is not (1 or 3)) throw new InvalidDataException($"JPEG: {raw.Components}-component lossless images have no colour interpretation"); + if (raw.Precision > 8) + return new JpegImage(parser.Info(), raw.Width, raw.Height, raw.Components, raw.Precision, null, raw.Samples, [.. parser.Warnings]); + var bytes = new byte[raw.Samples.Length]; + for (var i = 0; i < bytes.Length; i++) bytes[i] = (byte)raw.Samples[i]; + return new JpegImage(parser.Info(), raw.Width, raw.Height, raw.Components, 8, bytes, null, [.. parser.Warnings]); + } + + /// sRGB-decoding table for codes 0..2^precision−1. + internal static float[] LinearLut(int precision) + { + if (LinearLuts[precision] is { } cached) return cached; + var size = 1 << precision; + var lut = new float[size]; + for (var i = 0; i < size; i++) lut[i] = (float)ColorSpace.SrgbToLinear(i / (double)(size - 1)); + return LinearLuts[precision] = lut; + } +} diff --git a/src/Titano.Core/Codecs/Jpeg/JpegHuffman.cs b/src/Titano.Core/Codecs/Jpeg/JpegHuffman.cs new file mode 100644 index 00000000..5a41440f --- /dev/null +++ b/src/Titano.Core/Codecs/Jpeg/JpegHuffman.cs @@ -0,0 +1,241 @@ +using System.Runtime.CompilerServices; + +namespace Titano.Core.Codecs.Jpeg; + +/// Canonical Huffman decoding table with a 9-bit lookahead (and a combined run/value table for AC). +internal sealed class JpegHuffmanTable +{ + public const int FastBits = 9; + + /// (length << 8) | symbol for codes of ≤ bits; 0 = longer code. + public readonly ushort[] Fast = new ushort[1 << FastBits]; + + /// Exclusive upper bound of each code length's codes, left-aligned to 16 bits. + public readonly int[] MaxCode = new int[18]; + + /// Symbol index = (code >> (16 − length)) + Delta[length]. + public readonly int[] Delta = new int[17]; + + public readonly byte[] Values = new byte[256]; + + /// For AC tables: (value << 16) | (run << 8) | totalBits when code and magnitude fit in the lookahead. + public readonly int[] FastAc = new int[1 << FastBits]; + + /// Builds a table from the 16 code-length counts and the symbols (DHT layout). + /// The lengths oversubscribe the code space. + public JpegHuffmanTable(ReadOnlySpan counts, ReadOnlySpan values) + { + int code = 0, k = 0; + for (var len = 1; len <= 16; len++) + { + Delta[len] = k - code; + for (var i = 0; i < counts[len - 1]; i++, code++, k++) + { + if (len > FastBits) continue; + var first = code << (FastBits - len); + Fast.AsSpan(first, 1 << (FastBits - len)).Fill((ushort)(len << 8 | values[k])); + } + if (code > 1 << len) throw new InvalidDataException("JPEG: Huffman table oversubscribed"); + MaxCode[len] = code << (16 - len); + code <<= 1; + } + MaxCode[17] = int.MaxValue; + values[..k].CopyTo(Values); + BuildFastAc(); + } + + private void BuildFastAc() + { + for (var i = 0; i < Fast.Length; i++) + { + int e = Fast[i]; + if (e == 0) continue; + var len = e >> 8; + var rs = e & 0xFF; + int run = rs >> 4, s = rs & 15; + if (s == 0 || len + s > FastBits) continue; + var m = (i >> (FastBits - len - s)) & ((1 << s) - 1); + var v = m < 1 << (s - 1) ? m - (1 << s) + 1 : m; + FastAc[i] = v << 16 | run << 8 | (len + s); + } + } +} + +/// Thrown inside a scan when entropy-coded data is corrupt; the scan decoder resynchronises. +internal sealed class JpegEntropyException(string message) : Exception(message); + +/// +/// MSB-first bit reader over entropy-coded JPEG data: removes 0xFF00 stuffing, stops at markers and then +/// feeds zero bits, counting them so that consumption past the real data is detectable (). +/// +internal ref struct JpegBitReader +{ + private readonly ReadOnlySpan _data; + private int _pos; + private ulong _bits; + private int _count; + private int _padBits; + private int _marker; + private int _markerPos; + + public JpegBitReader(ReadOnlySpan data, int start) + { + _data = data; + _pos = start; + } + + /// Marker code that stopped the reader (0 = none, -1 = end of data). + public readonly int Marker => _marker; + + /// True once bits beyond the entropy-coded segment have been consumed. + public readonly bool Overrun => _padBits > _count; + + /// Where marker parsing continues after the scan. + public readonly int Position => _marker > 0 ? _markerPos : _pos; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Ensure(int n) + { + if (_count < n) Fill(); + } + + /// Next bits (1..32) without consuming; call first. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly int Peek(int n) => (int)(_bits >> (64 - n)); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Consume(int n) + { + _bits <<= n; + _count -= n; + } + + /// Reads 1..16 bits. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public int ReadBits(int n) + { + if (_count < n) Fill(); + var v = (int)(_bits >> (64 - n)); + _bits <<= n; + _count -= n; + return v; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public int ReadBit() + { + if (_count < 1) Fill(); + var v = (int)(_bits >> 63); + _bits <<= 1; + _count--; + return v; + } + + /// Reads an -bit magnitude (1..16) and sign-extends it (T.81 F.2.2.1 EXTEND). + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public int Receive(int s) + { + var v = ReadBits(s); + return v + ((((v >> (s - 1)) & 1) - 1) & (1 - (1 << s))); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public int Decode(JpegHuffmanTable t) + { + if (_count < 16) Fill(); + int e = t.Fast[(int)(_bits >> (64 - JpegHuffmanTable.FastBits))]; + if (e != 0) + { + var len = e >> 8; + _bits <<= len; + _count -= len; + return e & 0xFF; + } + return DecodeSlow(t); + } + + private int DecodeSlow(JpegHuffmanTable t) + { + var code = (int)(_bits >> 48); + for (var len = JpegHuffmanTable.FastBits + 1; len <= 16; len++) + { + if (code < t.MaxCode[len]) + { + Consume(len); + return t.Values[(code >> (16 - len)) + t.Delta[len]]; + } + } + throw new JpegEntropyException("JPEG: invalid Huffman code"); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + public void Fill() + { + while (_count <= 56) + { + if (_marker != 0 || _pos >= _data.Length) + { + if (_marker == 0) _marker = -1; + _padBits += 8; + _count += 8; + continue; + } + int b = _data[_pos]; + if (b == 0xFF) + { + var p = _pos + 1; + while (p < _data.Length && _data[p] == 0xFF) p++; + if (p >= _data.Length) + { + _pos = p; + continue; + } + if (_data[p] != 0) + { + _marker = _data[p]; + _markerPos = p - 1; + continue; + } + _pos = p + 1; + } + else + { + _pos++; + } + _bits |= (ulong)(uint)b << (56 - _count); + _count += 8; + } + } + + /// Discards buffered bits and locates the next marker (for restart processing). Returns its code + /// (-1 when the data ends) without consuming it; steps over it. + public int FindMarker() + { + _bits = 0; + _count = 0; + _padBits = 0; + if (_marker > 0) return _marker; + var p = _pos; + while (p + 1 < _data.Length) + { + if (_data[p] == 0xFF && _data[p + 1] is not (0x00 or 0xFF)) + { + _marker = _data[p + 1]; + _markerPos = p; + return _marker; + } + p++; + } + _pos = _data.Length; + _marker = -1; + return -1; + } + + /// Consumes the marker found by and resumes reading after it. + public void AcceptMarker() + { + if (_marker <= 0) return; + _pos = _markerPos + 2; + _marker = 0; + } +} diff --git a/src/Titano.Core/Codecs/Jpeg/JpegIdct.cs b/src/Titano.Core/Codecs/Jpeg/JpegIdct.cs new file mode 100644 index 00000000..41c8c826 --- /dev/null +++ b/src/Titano.Core/Codecs/Jpeg/JpegIdct.cs @@ -0,0 +1,169 @@ +namespace Titano.Core.Codecs.Jpeg; + +/// +/// Inverse DCT. Full-size blocks use libjpeg's accurate integer algorithm (jidctint ISLOW: Loeffler–Ligtenberg– +/// Moschytz with 13-bit constants), so output is bit-identical to libjpeg/djpeg. Reduced sizes (4, 2, 1) evaluate +/// the low-frequency n×n coefficients on an n-point grid in float (DCT-domain scaling for thumbnails). +/// +internal static class JpegIdct +{ + private const int ConstBits = 13; + private const int Fix0298631336 = 2446, Fix0390180644 = 3196, Fix0541196100 = 4433, Fix0765366865 = 6270; + private const int Fix0899976223 = 7373, Fix1175875602 = 9633, Fix1501321110 = 12299, Fix1847759065 = 15137; + private const int Fix1961570560 = 16069, Fix2053119869 = 16819, Fix2562915447 = 20995, Fix3072711026 = 25172; + + private static readonly float[] Basis4 = JpegTables.BuildBasis(4); + private static readonly float[] Basis2 = JpegTables.BuildBasis(2); + + /// Transforms one block into n·n samples, level-shifted and clamped to 0..2^precision−1. + /// 64 quantised coefficients, natural order. + /// 64 dequantisation factors, natural order. + /// Output block size: 8, 4, 2 or 1. + /// Sample precision (8 or 12). + public static void Transform(ReadOnlySpan coefficients, ReadOnlySpan quant, int size, int precision, Span output) + { + var max = (1 << precision) - 1; + var center = 1 << (precision - 1); + if (size == 8) + { + Islow(coefficients, quant, precision == 8 ? 2 : 1, center, max, output); + return; + } + if (size == 1) + { + output[0] = Math.Clamp((int)MathF.Round(coefficients[0] * quant[0] * 0.125f) + center, 0, max); + return; + } + Reduced(coefficients, quant, size, size == 4 ? Basis4 : Basis2, center, max, output); + } + + /// Port of libjpeg-turbo jpeg_idct_islow (columns, then rows; PASS1_BITS 2 for 8-bit, 1 for 12-bit). + private static void Islow(ReadOnlySpan c, ReadOnlySpan q, int pass1Bits, int center, int max, Span output) + { + Span ws = stackalloc int[64]; + _ = c[63]; + _ = q[63]; + for (var col = 0; col < 8; col++) + { + if (c[8 + col] == 0 && c[16 + col] == 0 && c[24 + col] == 0 && c[32 + col] == 0 && + c[40 + col] == 0 && c[48 + col] == 0 && c[56 + col] == 0) + { + var dc = c[col] * q[col] << pass1Bits; + for (var r = 0; r < 8; r++) ws[r * 8 + col] = dc; + continue; + } + var shift = ConstBits - pass1Bits; + Butterfly( + c[col] * q[col], c[8 + col] * q[8 + col], c[16 + col] * q[16 + col], c[24 + col] * q[24 + col], + c[32 + col] * q[32 + col], c[40 + col] * q[40 + col], c[48 + col] * q[48 + col], c[56 + col] * q[56 + col], + out var o0, out var o1, out var o2, out var o3, out var o4, out var o5, out var o6, out var o7); + var round = 1 << (shift - 1); + ws[col] = (o0 + round) >> shift; + ws[8 + col] = (o1 + round) >> shift; + ws[16 + col] = (o2 + round) >> shift; + ws[24 + col] = (o3 + round) >> shift; + ws[32 + col] = (o4 + round) >> shift; + ws[40 + col] = (o5 + round) >> shift; + ws[48 + col] = (o6 + round) >> shift; + ws[56 + col] = (o7 + round) >> shift; + } + for (var row = 0; row < 8; row++) + { + var w = ws.Slice(row * 8, 8); + var o = output.Slice(row * 8, 8); + if (w[1] == 0 && w[2] == 0 && w[3] == 0 && w[4] == 0 && w[5] == 0 && w[6] == 0 && w[7] == 0) + { + var dcShift = pass1Bits + 3; + o.Fill(Math.Clamp(((w[0] + (1 << (dcShift - 1))) >> dcShift) + center, 0, max)); + continue; + } + var shift = ConstBits + pass1Bits + 3; + var round = 1 << (shift - 1); + Butterfly(w[0], w[1], w[2], w[3], w[4], w[5], w[6], w[7], + out var o0, out var o1, out var o2, out var o3, out var o4, out var o5, out var o6, out var o7); + o[0] = Math.Clamp(((o0 + round) >> shift) + center, 0, max); + o[1] = Math.Clamp(((o1 + round) >> shift) + center, 0, max); + o[2] = Math.Clamp(((o2 + round) >> shift) + center, 0, max); + o[3] = Math.Clamp(((o3 + round) >> shift) + center, 0, max); + o[4] = Math.Clamp(((o4 + round) >> shift) + center, 0, max); + o[5] = Math.Clamp(((o5 + round) >> shift) + center, 0, max); + o[6] = Math.Clamp(((o6 + round) >> shift) + center, 0, max); + o[7] = Math.Clamp(((o7 + round) >> shift) + center, 0, max); + } + } + + /// One 8-point ISLOW pass; outputs are scaled by 2^13 (CONST_BITS) before descaling. + private static void Butterfly(int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7, + out int o0, out int o1, out int o2, out int o3, out int o4, out int o5, out int o6, out int o7) + { + // Even part: the rotator is sqrt(2)·c(−6). + var z1 = (i2 + i6) * Fix0541196100; + var tmp2 = z1 - i6 * Fix1847759065; + var tmp3 = z1 + i2 * Fix0765366865; + var tmp0 = (i0 + i4) << ConstBits; + var tmp1 = (i0 - i4) << ConstBits; + int tmp10 = tmp0 + tmp3, tmp13 = tmp0 - tmp3, tmp11 = tmp1 + tmp2, tmp12 = tmp1 - tmp2; + + // Odd part (figure 8 of the LL&M paper). + tmp0 = i7; + tmp1 = i5; + tmp2 = i3; + tmp3 = i1; + z1 = tmp0 + tmp3; + var z2 = tmp1 + tmp2; + var z3 = tmp0 + tmp2; + var z4 = tmp1 + tmp3; + var z5 = (z3 + z4) * Fix1175875602; + tmp0 *= Fix0298631336; + tmp1 *= Fix2053119869; + tmp2 *= Fix3072711026; + tmp3 *= Fix1501321110; + z1 *= -Fix0899976223; + z2 *= -Fix2562915447; + z3 = z3 * -Fix1961570560 + z5; + z4 = z4 * -Fix0390180644 + z5; + tmp0 += z1 + z3; + tmp1 += z2 + z4; + tmp2 += z2 + z3; + tmp3 += z1 + z4; + + o0 = tmp10 + tmp3; + o7 = tmp10 - tmp3; + o1 = tmp11 + tmp2; + o6 = tmp11 - tmp2; + o2 = tmp12 + tmp1; + o5 = tmp12 - tmp1; + o3 = tmp13 + tmp0; + o4 = tmp13 - tmp0; + } + + /// n-point IDCT of the low-frequency n×n coefficients, evaluated at the centres of n output samples. + private static void Reduced(ReadOnlySpan coefficients, ReadOnlySpan quant, int n, float[] basis, int center, int max, Span output) + { + Span g = stackalloc float[n * n]; + Span f = stackalloc float[n]; + g.Clear(); + for (var v = 0; v < n; v++) + { + var row = g.Slice(v * n, n); + for (var u = 0; u < n; u++) + { + int coef = coefficients[v * 8 + u]; + if (coef == 0) continue; + var value = (float)(coef * quant[v * 8 + u]); + for (var x = 0; x < n; x++) row[x] += value * basis[u * n + x]; + } + } + for (var y = 0; y < n; y++) + { + f.Clear(); + for (var v = 0; v < n; v++) + { + var w = basis[v * n + y]; + var row = g.Slice(v * n, n); + for (var x = 0; x < n; x++) f[x] += w * row[x]; + } + for (var x = 0; x < n; x++) output[y * n + x] = Math.Clamp((int)MathF.Round(f[x]) + center, 0, max); + } + } +} diff --git a/src/Titano.Core/Codecs/Jpeg/JpegLosslessDecoder.cs b/src/Titano.Core/Codecs/Jpeg/JpegLosslessDecoder.cs new file mode 100644 index 00000000..3ae36db9 --- /dev/null +++ b/src/Titano.Core/Codecs/Jpeg/JpegLosslessDecoder.cs @@ -0,0 +1,136 @@ +namespace Titano.Core.Codecs.Jpeg; + +/// +/// Lossless JPEG (SOF3, ITU T.81 Annex H) as used by DNG raw tiles and CR2: predictors 1–7, point transform, +/// 2–16 bit precision, one or more components (interleaved or in separate scans), restart intervals. +/// +public static class JpegLosslessDecoder +{ + /// Decodes a lossless stream to interleaved samples with the frame's own width and component count + /// (DNG tiles often encode e.g. two components at half the tile width; callers re-map the sample order). + /// The stream is malformed or not lossless. + public static JpegLosslessImage Decode(ReadOnlySpan data) + { + var parser = new JpegParser(JpegDecodeOptions.Default); + parser.Parse(data); + if (parser.Frame!.Process != JpegProcess.Lossless) throw new InvalidDataException("JPEG: not a lossless (SOF3) stream"); + return Finish(parser); + } + + /// Applies each component's point transform and packages the samples. + internal static JpegLosslessImage Finish(JpegParser parser) + { + var f = parser.Frame!; + var n = f.Components.Length; + var samples = parser.LosslessSamples ?? new ushort[checked(f.Width * f.Height * n)]; + foreach (var c in f.Components) + { + if (c.PointTransform == 0) continue; + for (var i = c.Index; i < samples.Length; i += n) samples[i] = (ushort)(samples[i] << c.PointTransform); + c.PointTransform = 0; + } + parser.LosslessSamples = samples; + return new JpegLosslessImage(samples, f.Width, f.Height, n, f.Precision); + } + + internal static int DecodeScan(JpegParser parser, ReadOnlySpan data, int pos, JpegScan scan) + { + var f = parser.Frame!; + int width = f.Width, nf = f.Components.Length, stride = width * nf; + var samples = parser.LosslessSamples ??= new ushort[checked(stride * f.Height)]; + var comps = scan.Components; + var tables = scan.DcTables; + var ns = comps.Length; + var index = new int[ns]; + for (var i = 0; i < ns; i++) + { + index[i] = comps[i].Index; + comps[i].PointTransform = scan.Al; + } + var predictor = scan.Ss; + var initial = 1 << (f.Precision - scan.Al - 1); + var interval = parser.RestartInterval; + var total = (long)width * f.Height; + var reader = new JpegBitReader(data, pos); + + long mcu = 0; + int x = 0, y = 0, restartRow = 0, expectedRst = 0; + var first = true; + while (mcu < total) + { + var end = interval > 0 ? Math.Min(total, mcu + interval) : total; + try + { + for (; mcu < end; mcu++) + { + var at = y * stride + x * nf; + for (var i = 0; i < ns; i++) + { + var s = reader.Decode(tables[i]!); + var diff = s switch + { + 0 => 0, + 16 => 32768, + > 16 => throw new JpegEntropyException("JPEG: bad lossless difference magnitude"), + _ => reader.Receive(s), + }; + var idx = at + index[i]; + int pred; + if (first) pred = initial; + else if (y == restartRow) pred = samples[idx - nf]; + else if (x == 0) pred = samples[idx - stride]; + else pred = Predict(predictor, samples[idx - nf], samples[idx - stride], samples[idx - stride - nf]); + samples[idx] = (ushort)(pred + diff); + } + first = false; + if (++x == width) + { + x = 0; + y++; + if (reader.Overrun) throw new JpegEntropyException("JPEG: premature end of entropy-coded data"); + } + } + if (reader.Overrun) throw new JpegEntropyException("JPEG: premature end of entropy-coded data"); + } + catch (JpegEntropyException ex) + { + parser.Warn(ex.Message); + if (interval == 0) break; + mcu = end; + } + if (mcu >= total || interval == 0) break; + + var marker = reader.FindMarker(); + if (marker is < JpegTables.Rst0 or > JpegTables.Rst0 + 7) + { + parser.Warn("JPEG: restart marker missing; scan truncated"); + break; + } + var found = marker - JpegTables.Rst0; + if (found != expectedRst) + { + var lost = (found - expectedRst) & 7; + parser.Warn($"JPEG: {lost} restart interval(s) lost"); + mcu += (long)lost * interval; + } + reader.AcceptMarker(); + expectedRst = (found + 1) & 7; + x = (int)(mcu % width); + y = (int)(mcu / width); + restartRow = y; + first = true; + } + return reader.Position; + } + + private static int Predict(int predictor, int ra, int rb, int rc) => predictor switch + { + 1 => ra, + 2 => rb, + 3 => rc, + 4 => ra + rb - rc, + 5 => ra + ((rb - rc) >> 1), + 6 => rb + ((ra - rc) >> 1), + _ => (ra + rb) >> 1, + }; +} diff --git a/src/Titano.Core/Codecs/Jpeg/JpegParser.cs b/src/Titano.Core/Codecs/Jpeg/JpegParser.cs new file mode 100644 index 00000000..ec660afd --- /dev/null +++ b/src/Titano.Core/Codecs/Jpeg/JpegParser.cs @@ -0,0 +1,400 @@ +namespace Titano.Core.Codecs.Jpeg; + +/// One frame component and its coefficient (DCT) storage. +internal sealed class JpegComponent +{ + public int Id, Index, H, V, QuantTable; + + /// Component size in samples: ceil(X·H/Hmax) × ceil(Y·V/Vmax). + public int Width, Height; + + /// Blocks covering the component (non-interleaved scan extent). + public int WidthInBlocks, HeightInBlocks; + + /// Allocated block grid, padded to whole MCUs. + public int BlocksPerLine, BlocksPerColumn; + + /// Quantised coefficients, 64 per block in natural order, blocks row-major. + public short[]? Coefficients; + + /// Quantisation table latched when the component's first scan starts (natural order). + public ushort[]? Quant; + + /// Point transform of the lossless scan that carried this component. + public int PointTransform; + + public int DcPred; +} + +/// Frame header (SOFn) with derived MCU geometry. +internal sealed class JpegFrame +{ + public required JpegProcess Process { get; init; } + public required int Precision { get; init; } + public required int Width { get; init; } + public required int Height { get; init; } + public required JpegComponent[] Components { get; init; } + public int HMax { get; init; } + public int VMax { get; init; } + public int McusX { get; init; } + public int McusY { get; init; } +} + +/// Scan header (SOS) resolved against the frame. +internal sealed class JpegScan +{ + public required JpegComponent[] Components { get; init; } + public required JpegHuffmanTable?[] DcTables { get; init; } + public required JpegHuffmanTable?[] AcTables { get; init; } + public int Ss { get; init; } + public int Se { get; init; } + public int Ah { get; init; } + public int Al { get; init; } +} + +/// +/// Marker-level JPEG parser: tables, frame and scan headers; dispatches entropy-coded segments to the DCT or +/// lossless scan decoders. Structural errors throw ; once a scan has been +/// decoded, later errors only end parsing with a warning so that partial images survive. +/// +internal sealed class JpegParser(JpegDecodeOptions options) +{ + private readonly ushort[]?[] _quant = new ushort[4][]; + private readonly JpegHuffmanTable?[] _dc = new JpegHuffmanTable[4]; + private readonly JpegHuffmanTable?[] _ac = new JpegHuffmanTable[4]; + private readonly List _warnings = []; + private static JpegHuffmanTable? _stdDcLuma, _stdDcChroma, _stdAcLuma, _stdAcChroma; + + public JpegDecodeOptions Options { get; } = options; + public JpegFrame? Frame { get; private set; } + public int RestartInterval { get; private set; } + public int AdobeTransform { get; private set; } = -1; + public bool Jfif { get; private set; } + public int ScansDecoded { get; private set; } + public IReadOnlyList Warnings => _warnings; + + /// Interleaved samples of a lossless frame (point transform not yet applied). + public ushort[]? LosslessSamples { get; set; } + + public void Warn(string message) + { + if (_warnings.Count < 64) _warnings.Add(message); + } + + /// Parses a full stream. With it stops at the first scan header. + public void Parse(ReadOnlySpan data, bool headerOnly = false) + { + if (Options.Tables is { Length: > 0 } tables) ParseCore(tables, tablesOnly: true, headerOnly: false); + try + { + ParseCore(data, tablesOnly: false, headerOnly); + } + catch (InvalidDataException ex) when (ScansDecoded > 0) + { + Warn(ex.Message); + } + if (Frame is null) throw new InvalidDataException("JPEG: no frame header"); + if (!headerOnly && ScansDecoded == 0) throw new InvalidDataException("JPEG: no scan data"); + } + + public JpegInfo Info() + { + var f = Frame ?? throw new InvalidDataException("JPEG: no frame header"); + return new JpegInfo( + f.Width, f.Height, f.Components.Length, f.Precision, f.Process, ColorSpace(), + f.Components.Select(c => (c.H, c.V)).ToArray(), RestartInterval); + } + + /// Colour space after applying the option override and libjpeg's marker/component-id heuristics. + public JpegColorSpace ColorSpace() + { + var f = Frame!; + var n = f.Components.Length; + if (n == 1) return JpegColorSpace.Gray; + if (f.Process == JpegProcess.Lossless && Options.ColorTransform == JpegColorTransform.Auto) + return n switch { 3 => JpegColorSpace.Rgb, 4 => JpegColorSpace.Cmyk, _ => JpegColorSpace.Other }; + var transformed = Options.ColorTransform switch + { + JpegColorTransform.None => false, + JpegColorTransform.YCbCr => true, + _ => n == 3 + ? Jfif || (AdobeTransform < 0 ? !(f.Components[0].Id == 'R' && f.Components[1].Id == 'G' && f.Components[2].Id == 'B') : AdobeTransform != 0) + : AdobeTransform == 2, + }; + return n switch + { + 3 => transformed ? JpegColorSpace.YCbCr : JpegColorSpace.Rgb, + 4 => transformed ? JpegColorSpace.Ycck : JpegColorSpace.Cmyk, + _ => JpegColorSpace.Other, + }; + } + + private void ParseCore(ReadOnlySpan data, bool tablesOnly, bool headerOnly) + { + if (data.Length < 4 || data[0] != 0xFF || data[1] != JpegTables.Soi) throw new InvalidDataException("JPEG: missing SOI marker"); + var pos = 2; + while (true) + { + var marker = NextMarker(data, ref pos); + if (marker < 0) + { + if (!tablesOnly) Warn("JPEG: missing EOI marker"); + return; + } + if (marker == JpegTables.Eoi) return; + if (marker is JpegTables.Soi or (>= JpegTables.Rst0 and <= JpegTables.Rst0 + 7) or 0x01) continue; + if (pos + 2 > data.Length) throw new InvalidDataException("JPEG: truncated marker segment"); + var length = data[pos] << 8 | data[pos + 1]; + if (length < 2 || pos + length > data.Length) throw new InvalidDataException($"JPEG: bad length for marker 0x{marker:X2}"); + var segment = data.Slice(pos + 2, length - 2); + pos += length; + switch (marker) + { + case JpegTables.Dqt: + ParseDqt(segment); + break; + case JpegTables.Dht: + ParseDht(segment); + break; + case JpegTables.Dri: + if (segment.Length < 2) throw new InvalidDataException("JPEG: bad DRI"); + RestartInterval = segment[0] << 8 | segment[1]; + break; + case JpegTables.App0: + if (segment.StartsWith("JFIF\0"u8)) Jfif = true; + break; + case JpegTables.App14: + if (segment.Length >= 12 && segment.StartsWith("Adobe"u8)) AdobeTransform = segment[11]; + break; + case >= JpegTables.Sof0 and <= JpegTables.Sof3: + if (tablesOnly) throw new InvalidDataException("JPEG: frame header in a tables-only stream"); + if (Frame is not null) throw new InvalidDataException("JPEG: more than one frame"); + ParseSof(marker, segment); + break; + case JpegTables.Sos: + if (tablesOnly || Frame is null) throw new InvalidDataException("JPEG: scan without frame header"); + if (headerOnly) return; + pos = DecodeScan(data, segment, pos); + ScansDecoded++; + break; + case JpegTables.Dac: + throw new InvalidDataException("JPEG: arithmetic coding is not supported"); + case >= 0xC5 and <= 0xCF: + throw new InvalidDataException($"JPEG: unsupported coding process (SOF 0x{marker:X2})"); + } + } + } + + /// Finds the next marker at or after , skipping garbage; returns its code or -1. + private int NextMarker(ReadOnlySpan data, ref int pos) + { + var start = pos; + while (pos + 1 < data.Length) + { + if (data[pos] != 0xFF) + { + pos++; + continue; + } + int m = data[pos + 1]; + if (m == 0xFF) + { + pos++; + continue; + } + if (m == 0) + { + pos += 2; + continue; + } + if (pos != start) Warn($"JPEG: {pos - start} extraneous bytes before marker 0x{m:X2}"); + pos += 2; + return m; + } + pos = data.Length; + return -1; + } + + private void ParseDqt(ReadOnlySpan s) + { + while (s.Length > 0) + { + int pq = s[0] >> 4, tq = s[0] & 15; + var size = pq == 0 ? 64 : 128; + if (pq > 1 || tq > 3 || s.Length < 1 + size) throw new InvalidDataException("JPEG: bad DQT"); + var table = new ushort[64]; + for (var k = 0; k < 64; k++) + { + table[JpegTables.NaturalOrder[k]] = pq == 0 ? s[1 + k] : (ushort)(s[1 + 2 * k] << 8 | s[2 + 2 * k]); + } + _quant[tq] = table; + s = s[(1 + size)..]; + } + } + + private void ParseDht(ReadOnlySpan s) + { + while (s.Length > 0) + { + if (s.Length < 17) throw new InvalidDataException("JPEG: bad DHT"); + int tc = s[0] >> 4, th = s[0] & 15; + if (tc > 1 || th > 3) throw new InvalidDataException("JPEG: bad DHT class/id"); + var counts = s.Slice(1, 16); + var total = 0; + foreach (var c in counts) total += c; + if (total > 256 || s.Length < 17 + total) throw new InvalidDataException("JPEG: bad DHT counts"); + var table = new JpegHuffmanTable(counts, s.Slice(17, total)); + (tc == 0 ? _dc : _ac)[th] = table; + s = s[(17 + total)..]; + } + } + + private void ParseSof(int marker, ReadOnlySpan s) + { + if (s.Length < 6) throw new InvalidDataException("JPEG: bad SOF"); + var process = marker switch + { + JpegTables.Sof0 => JpegProcess.Baseline, + JpegTables.Sof1 => JpegProcess.ExtendedSequential, + JpegTables.Sof2 => JpegProcess.Progressive, + _ => JpegProcess.Lossless, + }; + int precision = s[0], height = s[1] << 8 | s[2], width = s[3] << 8 | s[4], n = s[5]; + if (process == JpegProcess.Lossless ? precision is < 2 or > 16 : precision is not (8 or 12)) + throw new InvalidDataException($"JPEG: unsupported precision {precision}"); + if (width == 0 || height == 0) throw new InvalidDataException("JPEG: zero image size (DNL is not supported)"); + if ((long)width * height > Options.MaxPixels) throw new InvalidDataException($"JPEG: {width}x{height} exceeds the pixel limit"); + if (n is < 1 or > 4 || s.Length < 6 + 3 * n) throw new InvalidDataException("JPEG: bad component count"); + var components = new JpegComponent[n]; + int hMax = 1, vMax = 1; + for (var i = 0; i < n; i++) + { + var c = s.Slice(6 + 3 * i, 3); + int h = c[1] >> 4, v = c[1] & 15; + if (h is < 1 or > 4 || v is < 1 or > 4 || c[2] > 3) throw new InvalidDataException("JPEG: bad sampling factors"); + components[i] = new JpegComponent { Id = c[0], Index = i, H = h, V = v, QuantTable = c[2] }; + hMax = Math.Max(hMax, h); + vMax = Math.Max(vMax, v); + } + if (n == 1) + { + // A single component is always coded non-interleaved: its own factors are irrelevant. + components[0].H = components[0].V = hMax = vMax = 1; + } + foreach (var c in components) + { + if (hMax % c.H != 0 || vMax % c.V != 0) throw new InvalidDataException("JPEG: fractional sampling ratios are not supported"); + if (process == JpegProcess.Lossless && (c.H != 1 || c.V != 1)) + throw new InvalidDataException("JPEG: subsampled lossless streams are not supported"); + } + var unit = process == JpegProcess.Lossless ? 1 : 8; + var mcusX = DivUp(width, unit * hMax); + var mcusY = DivUp(height, unit * vMax); + foreach (var c in components) + { + c.Width = DivUp(width * c.H, hMax); + c.Height = DivUp(height * c.V, vMax); + c.WidthInBlocks = DivUp(c.Width, 8); + c.HeightInBlocks = DivUp(c.Height, 8); + c.BlocksPerLine = mcusX * c.H; + c.BlocksPerColumn = mcusY * c.V; + } + Frame = new JpegFrame + { + Process = process, Precision = precision, Width = width, Height = height, Components = components, + HMax = hMax, VMax = vMax, McusX = mcusX, McusY = mcusY, + }; + } + + private int DecodeScan(ReadOnlySpan data, ReadOnlySpan s, int pos) + { + var frame = Frame!; + if (s.Length < 1) throw new InvalidDataException("JPEG: bad SOS"); + int n = s[0]; + if (n is < 1 or > 4 || s.Length < 4 + 2 * n) throw new InvalidDataException("JPEG: bad SOS component count"); + var comps = new JpegComponent[n]; + var dc = new JpegHuffmanTable?[n]; + var ac = new JpegHuffmanTable?[n]; + var tdTa = new int[n]; + for (var i = 0; i < n; i++) + { + int id = s[1 + 2 * i]; + JpegComponent? match = null; + foreach (var c in frame.Components) + { + if (c.Id == id && Array.IndexOf(comps, c) < 0) + { + match = c; + break; + } + } + comps[i] = match ?? throw new InvalidDataException($"JPEG: scan references unknown component {id}"); + tdTa[i] = s[2 + 2 * i]; + } + var p = 1 + 2 * n; + int ss = s[p], se = s[p + 1], ah = s[p + 2] >> 4, al = s[p + 2] & 15; + + if (frame.Process == JpegProcess.Lossless) + { + if (ss is < 1 or > 7) throw new InvalidDataException($"JPEG: bad lossless predictor {ss}"); + if (al >= frame.Precision) throw new InvalidDataException("JPEG: bad point transform"); + for (var i = 0; i < n; i++) dc[i] = _dc[tdTa[i] >> 4 & 3] ?? throw new InvalidDataException("JPEG: missing Huffman table"); + var losslessScan = new JpegScan { Components = comps, DcTables = dc, AcTables = ac, Ss = ss, Al = al }; + return JpegLosslessDecoder.DecodeScan(this, data, pos, losslessScan); + } + + var progressive = frame.Process == JpegProcess.Progressive; + if (progressive) + { + if (ss > se || se > 63 || (ss == 0 && se != 0) || (ss > 0 && n != 1) || al > 13 || ah > 13) + throw new InvalidDataException("JPEG: bad progressive scan parameters"); + } + else + { + ss = 0; + se = 63; + ah = al = 0; + } + if (n > 1) + { + var blocks = 0; + foreach (var c in comps) blocks += c.H * c.V; + if (blocks > 10) throw new InvalidDataException("JPEG: too many blocks per MCU"); + } + var needDc = ss == 0 && ah == 0; + var needAc = se > 0; + for (var i = 0; i < n; i++) + { + int td = tdTa[i] >> 4, ta = tdTa[i] & 15; + if (td > 3 || ta > 3) throw new InvalidDataException("JPEG: bad Huffman table selector"); + if (needDc) dc[i] = _dc[td] ?? StandardTable(dc: true, td); + if (needAc) ac[i] = _ac[ta] ?? StandardTable(dc: false, ta); + var c = comps[i]; + if (c.Quant is null) + { + c.Quant = _quant[c.QuantTable] is { } q ? (ushort[])q.Clone() : throw new InvalidDataException("JPEG: missing quantisation table"); + } + c.Coefficients ??= new short[checked(c.BlocksPerLine * c.BlocksPerColumn * 64)]; + } + var scan = new JpegScan { Components = comps, DcTables = dc, AcTables = ac, Ss = ss, Se = se, Ah = ah, Al = al }; + return JpegScanDecoder.Decode(this, data, pos, scan); + } + + /// Quantisation table for a component that never appeared in a scan (rendered flat). + public ushort[] QuantFor(JpegComponent c) => c.Quant ?? _quant[c.QuantTable] ?? new ushort[64]; + + /// Annex K tables for streams that omit DHT (Motion-JPEG), as libjpeg does. + private static JpegHuffmanTable StandardTable(bool dc, int id) + { + if (id > 1) throw new InvalidDataException("JPEG: missing Huffman table"); + return (dc, id) switch + { + (true, 0) => _stdDcLuma ??= new JpegHuffmanTable(JpegTables.DcLumaBits, JpegTables.DcLumaValues), + (true, _) => _stdDcChroma ??= new JpegHuffmanTable(JpegTables.DcChromaBits, JpegTables.DcChromaValues), + (false, 0) => _stdAcLuma ??= new JpegHuffmanTable(JpegTables.AcLumaBits, JpegTables.AcLumaValues), + _ => _stdAcChroma ??= new JpegHuffmanTable(JpegTables.AcChromaBits, JpegTables.AcChromaValues), + }; + } + + internal static int DivUp(int a, int b) => (int)(((long)a + b - 1) / b); +} diff --git a/src/Titano.Core/Codecs/Jpeg/JpegRenderer.cs b/src/Titano.Core/Codecs/Jpeg/JpegRenderer.cs new file mode 100644 index 00000000..1140772f --- /dev/null +++ b/src/Titano.Core/Codecs/Jpeg/JpegRenderer.cs @@ -0,0 +1,378 @@ +using System.Buffers; +using System.Numerics; +using Titano.Core.Imaging; + +namespace Titano.Core.Codecs.Jpeg; + +/// +/// Turns decoded coefficients into pixels: (A) parallel IDCT of every block into per-component sample planes, +/// (B) parallel bands of rows doing chroma upsampling (libjpeg "fancy" triangle filters for 2× factors, +/// replication otherwise) and colour conversion (libjpeg's fixed-point YCbCr tables, CMYK/YCCK → RGB). +/// +internal sealed class JpegRenderer +{ + private const int BandRows = 16; + + private readonly JpegParser _parser; + private readonly JpegFrame _frame; + private readonly int _n; + private readonly bool _fancy; + private readonly int _max; + private readonly JpegColorSpace _colorSpace; + private readonly bool _invertedCmyk; + private readonly ParallelOptions _parallel; + private readonly byte[]?[] _planes8; + private readonly ushort[]?[] _planes16; + private readonly int[] _stride, _validW, _validH, _hf, _vf; + + public JpegRenderer(JpegParser parser) + { + _parser = parser; + _frame = parser.Frame!; + var options = parser.Options; + _n = BlockSize(_frame, options.MaxLongEdge); + _fancy = options.FancyUpsampling && _n > 1; + _max = (1 << _frame.Precision) - 1; + _colorSpace = parser.ColorSpace(); + _invertedCmyk = parser.AdobeTransform >= 0 || _colorSpace == JpegColorSpace.Ycck; + _parallel = new ParallelOptions { MaxDegreeOfParallelism = options.MaxDegreeOfParallelism }; + Width = JpegParser.DivUp(_frame.Width * _n, 8); + Height = JpegParser.DivUp(_frame.Height * _n, 8); + Channels = _colorSpace == JpegColorSpace.Gray ? 1 : 3; + if (_colorSpace == JpegColorSpace.Other) throw new InvalidDataException("JPEG: component count has no colour interpretation"); + + var count = _frame.Components.Length; + _planes8 = new byte[count][]; + _planes16 = new ushort[count][]; + _stride = new int[count]; + _validW = new int[count]; + _validH = new int[count]; + _hf = new int[count]; + _vf = new int[count]; + for (var i = 0; i < count; i++) + { + var c = _frame.Components[i]; + _hf[i] = _frame.HMax / c.H; + _vf[i] = _frame.VMax / c.V; + _validW[i] = JpegParser.DivUp(_frame.Width * c.H * _n, _frame.HMax * 8); + _validH[i] = JpegParser.DivUp(_frame.Height * c.V * _n, _frame.VMax * 8); + } + } + + public int Width { get; } + public int Height { get; } + + /// Output channels: 1 for gray, 3 otherwise. + public int Channels { get; } + + /// IDCT output size per block (8 = full size, 4/2/1 = DCT-domain scaling) for a requested long edge. + public static int BlockSize(JpegFrame frame, int maxLongEdge) + { + if (maxLongEdge <= 0) return 8; + var longEdge = Math.Max(frame.Width, frame.Height); + foreach (var n in (ReadOnlySpan)[1, 2, 4]) + { + if (JpegParser.DivUp(longEdge * n, 8) >= maxLongEdge) return n; + } + return 8; + } + + public JpegImage RenderInterleaved(JpegInfo info) + { + RunIdct(); + var length = checked(Width * Height * Channels); + if (_frame.Precision <= 8) + { + var pixels = new byte[length]; + Render((y, rows) => + { + var dst = pixels.AsSpan(y * Width * Channels, Width * Channels); + for (var c = 0; c < Channels; c++) + { + var src = rows[c]; + for (var x = 0; x < Width; x++) dst[x * Channels + c] = (byte)src[x]; + } + }); + return new JpegImage(info, Width, Height, Channels, 8, pixels, null, [.. _parser.Warnings]); + } + var pixels16 = new ushort[length]; + Render((y, rows) => + { + var dst = pixels16.AsSpan(y * Width * Channels, Width * Channels); + for (var c = 0; c < Channels; c++) + { + var src = rows[c]; + for (var x = 0; x < Width; x++) dst[x * Channels + c] = (ushort)src[x]; + } + }); + return new JpegImage(info, Width, Height, Channels, _frame.Precision, null, pixels16, [.. _parser.Warnings]); + } + + /// Renders straight into linear-light float planes (sRGB transfer removed by table lookup). + public ImageBuffer RenderLinear(bool expandGray) + { + RunIdct(); + var lut = _frame.Precision == 8 ? null : JpegDecoder.LinearLut(_frame.Precision); + var outChannels = Channels == 1 && !expandGray ? 1 : 3; + var image = new ImageBuffer(Width, Height, outChannels, clear: false); + var storage = image.Storage; + var plane = image.PlaneLength; + Render((y, rows) => + { + for (var c = 0; c < outChannels; c++) + { + var src = rows[Channels == 1 ? 0 : c]; + var dst = storage.AsSpan(c * plane + y * Width, Width); + if (lut is null) + { + for (var x = 0; x < dst.Length; x++) dst[x] = ColorSpace.Linear8((byte)src[x]); + } + else + { + for (var x = 0; x < dst.Length; x++) dst[x] = lut[src[x]]; + } + } + }); + return image; + } + + private void RunIdct() + { + var comps = _frame.Components; + var center = 1 << (_frame.Precision - 1); + var jobs = new List<(int Component, int BlockRow)>(); + for (var i = 0; i < comps.Length; i++) + { + var c = comps[i]; + _stride[i] = c.BlocksPerLine * _n; + var length = checked(_stride[i] * c.BlocksPerColumn * _n); + if (_frame.Precision <= 8) _planes8[i] = new byte[length]; + else _planes16[i] = new ushort[length]; + var blockRows = JpegParser.DivUp(_validH[i], _n); + for (var by = 0; by < blockRows; by++) jobs.Add((i, by)); + } + var quant = new int[comps.Length][]; + for (var i = 0; i < comps.Length; i++) quant[i] = Array.ConvertAll(_parser.QuantFor(comps[i]), q => (int)q); + + Parallel.For(0, jobs.Count, _parallel, () => new int[64], (j, _, block) => + { + var (i, by) = jobs[j]; + var c = comps[i]; + var stride = _stride[i]; + var rowOffset = by * _n * stride; + if (c.Coefficients is not { } coefficients) + { + // Component never scanned: flat mid-grey, like zero coefficients. + if (_planes8[i] is { } flat8) flat8.AsSpan(rowOffset, _n * stride).Fill((byte)center); + else _planes16[i].AsSpan(rowOffset, _n * stride).Fill((ushort)center); + return block; + } + var blocks = JpegParser.DivUp(_validW[i], _n); + for (var bx = 0; bx < blocks; bx++) + { + JpegIdct.Transform(coefficients.AsSpan((by * c.BlocksPerLine + bx) * 64, 64), quant[i], _n, _frame.Precision, block); + var at = rowOffset + bx * _n; + for (var y = 0; y < _n; y++, at += stride) + { + var src = block.AsSpan(y * _n, _n); + if (_planes8[i] is { } p8) + { + var dst = p8.AsSpan(at, _n); + for (var x = 0; x < _n; x++) dst[x] = (byte)src[x]; + } + else + { + var dst = _planes16[i].AsSpan(at, _n); + for (var x = 0; x < _n; x++) dst[x] = (ushort)src[x]; + } + } + } + return block; + }, _ => { }); + foreach (var c in comps) c.Coefficients = null; + } + + private delegate void RowSink(int y, int[][] rows); + + private void Render(RowSink sink) + { + var bands = JpegParser.DivUp(Height, BandRows); + var count = _frame.Components.Length; + var ycc = _colorSpace is JpegColorSpace.YCbCr or JpegColorSpace.Ycck ? YccTables.For(_frame.Precision) : null; + Parallel.For(0, bands, _parallel, band => + { + var rows = new int[count][]; + for (var c = 0; c < count; c++) rows[c] = ArrayPool.Shared.Rent(Width + 1); + try + { + var end = Math.Min(Height, (band + 1) * BandRows); + for (var y = band * BandRows; y < end; y++) + { + for (var c = 0; c < count; c++) + { + if (_planes8[c] is { } p8) Upsample(p8, c, y, rows[c]); + else Upsample(_planes16[c]!, c, y, rows[c]); + } + ConvertColor(rows, ycc); + sink(y, rows); + } + } + finally + { + foreach (var r in rows) ArrayPool.Shared.Return(r); + } + }); + } + + /// Writes one output row of component at full output width. + /// holds at least Width + 1 values, enough for the 2·w samples a 2× upsampler produces. + private void Upsample(T[] plane, int c, int y, int[] dst) where T : unmanaged, IBinaryInteger + { + int hf = _hf[c], vf = _vf[c], stride = _stride[c], w = _validW[c], h = _validH[c]; + var cy = Math.Min(y / vf, h - 1); + var near = plane.AsSpan(cy * stride, w); + var width = Width; + if (hf == 1 && vf == 1) + { + for (var x = 0; x < width; x++) dst[x] = int.CreateTruncating(near[x]); + return; + } + if (_fancy && vf == 2 && hf <= 2) + { + var upper = (y & 1) == 0; + var far = plane.AsSpan(Math.Clamp(upper ? cy - 1 : cy + 1, 0, h - 1) * stride, w); + if (hf == 1) + { + var bias = upper ? 1 : 2; + for (var x = 0; x < width; x++) dst[x] = (3 * int.CreateTruncating(near[x]) + int.CreateTruncating(far[x]) + bias) >> 2; + return; + } + if (w > 2) + { + H2V2(near, far, w, dst); + return; + } + } + if (_fancy && hf == 2 && vf == 1 && w > 2) + { + H2V1(near, w, dst); + return; + } + for (var x = 0; x < width; x++) dst[x] = int.CreateTruncating(near[x / hf]); + } + + /// libjpeg h2v1_fancy_upsample: 3/4·nearer + 1/4·further, alternating rounding bias. + private static void H2V1(ReadOnlySpan s, int w, Span o) where T : unmanaged, IBinaryInteger + { + var first = int.CreateTruncating(s[0]); + o[0] = first; + o[1] = (3 * first + int.CreateTruncating(s[1]) + 2) >> 2; + for (var i = 1; i < w - 1; i++) + { + var v = 3 * int.CreateTruncating(s[i]); + o[2 * i] = (v + int.CreateTruncating(s[i - 1]) + 1) >> 2; + o[2 * i + 1] = (v + int.CreateTruncating(s[i + 1]) + 2) >> 2; + } + var last = int.CreateTruncating(s[w - 1]); + o[2 * w - 2] = (3 * last + int.CreateTruncating(s[w - 2]) + 1) >> 2; + o[2 * w - 1] = last; + } + + /// libjpeg h2v2_fancy_upsample: triangle filter in both directions (9/3/3/1 weights). + private static void H2V2(ReadOnlySpan near, ReadOnlySpan far, int w, Span o) where T : unmanaged, IBinaryInteger + { + var thisSum = 3 * int.CreateTruncating(near[0]) + int.CreateTruncating(far[0]); + var nextSum = 3 * int.CreateTruncating(near[1]) + int.CreateTruncating(far[1]); + o[0] = (thisSum * 4 + 8) >> 4; + o[1] = (thisSum * 3 + nextSum + 7) >> 4; + var lastSum = thisSum; + thisSum = nextSum; + for (var i = 1; i < w - 1; i++) + { + nextSum = 3 * int.CreateTruncating(near[i + 1]) + int.CreateTruncating(far[i + 1]); + o[2 * i] = (thisSum * 3 + lastSum + 8) >> 4; + o[2 * i + 1] = (thisSum * 3 + nextSum + 7) >> 4; + lastSum = thisSum; + thisSum = nextSum; + } + o[2 * w - 2] = (thisSum * 3 + lastSum + 8) >> 4; + o[2 * w - 1] = (thisSum * 4 + 7) >> 4; + } + + private void ConvertColor(int[][] rows, YccTables? ycc) + { + var w = Width; + var max = _max; + switch (_colorSpace) + { + case JpegColorSpace.YCbCr: + YccToRgb(rows, ycc!, w, max); + break; + case JpegColorSpace.Cmyk or JpegColorSpace.Ycck: + { + if (_colorSpace == JpegColorSpace.Ycck) + { + YccToRgb(rows, ycc!, w, max); + for (var c = 0; c < 3; c++) + { + var r = rows[c]; + for (var x = 0; x < w; x++) r[x] = max - r[x]; + } + } + var k = rows[3]; + var half = max / 2; + for (var c = 0; c < 3; c++) + { + var r = rows[c]; + for (var x = 0; x < w; x++) + { + r[x] = _invertedCmyk ? (r[x] * k[x] + half) / max : ((max - r[x]) * (max - k[x]) + half) / max; + } + } + break; + } + } + } + + private static void YccToRgb(int[][] rows, YccTables t, int w, int max) + { + int[] y = rows[0], cb = rows[1], cr = rows[2]; + for (var x = 0; x < w; x++) + { + int yy = y[x], b = cb[x], r = cr[x]; + y[x] = Math.Clamp(yy + t.CrR[r], 0, max); + cb[x] = Math.Clamp(yy + ((t.CbG[b] + t.CrG[r]) >> 16), 0, max); + cr[x] = Math.Clamp(yy + t.CbB[b], 0, max); + } + } + + /// libjpeg's fixed-point (16 fractional bits) YCbCr → RGB tables for a sample precision. + private sealed class YccTables + { + private static readonly YccTables?[] Cache = new YccTables?[17]; + public readonly int[] CrR, CbB, CrG, CbG; + + private YccTables(int precision) + { + var size = 1 << precision; + var center = size / 2; + CrR = new int[size]; + CbB = new int[size]; + CrG = new int[size]; + CbG = new int[size]; + const int half = 1 << 15; + for (var i = 0; i < size; i++) + { + var x = i - center; + CrR[i] = (Fix(1.40200) * x + half) >> 16; + CbB[i] = (Fix(1.77200) * x + half) >> 16; + CrG[i] = -Fix(0.71414) * x; + CbG[i] = -Fix(0.34414) * x + half; + } + } + + public static YccTables For(int precision) => Cache[precision] ??= new YccTables(precision); + + private static int Fix(double v) => (int)(v * 65536 + 0.5); + } +} diff --git a/src/Titano.Core/Codecs/Jpeg/JpegScanDecoder.cs b/src/Titano.Core/Codecs/Jpeg/JpegScanDecoder.cs new file mode 100644 index 00000000..9e90a42b --- /dev/null +++ b/src/Titano.Core/Codecs/Jpeg/JpegScanDecoder.cs @@ -0,0 +1,260 @@ +namespace Titano.Core.Codecs.Jpeg; + +/// +/// Entropy decoding of DCT scans into the components' coefficient arrays: sequential (baseline/extended) and the +/// four progressive scan kinds (DC first/refine, AC first/refine). Corrupt intervals are skipped by resynchronising +/// on the next RSTn marker; without restart markers the rest of the scan is abandoned with a warning. +/// +internal static class JpegScanDecoder +{ + private enum Kind + { + Sequential, + DcFirst, + DcRefine, + AcFirst, + AcRefine, + } + + public static int Decode(JpegParser parser, ReadOnlySpan data, int pos, JpegScan scan) + { + var frame = parser.Frame!; + var comps = scan.Components; + var kind = frame.Process != JpegProcess.Progressive ? Kind.Sequential + : scan.Ss == 0 ? (scan.Ah == 0 ? Kind.DcFirst : Kind.DcRefine) + : (scan.Ah == 0 ? Kind.AcFirst : Kind.AcRefine); + + var single = comps.Length == 1; + var mcusX = single ? comps[0].WidthInBlocks : frame.McusX; + var mcusY = single ? comps[0].HeightInBlocks : frame.McusY; + var total = (long)mcusX * mcusY; + var interval = parser.RestartInterval; + var reader = new JpegBitReader(data, pos); + var state = new ScanState(scan); + var expectedRst = 0; + foreach (var c in comps) c.DcPred = 0; + + long mcu = 0; + while (mcu < total) + { + var end = interval > 0 ? Math.Min(total, mcu + interval) : total; + try + { + for (; mcu < end; mcu++) + { + if (single) + { + var c = comps[0]; + var bx = (int)(mcu % mcusX); + var by = (int)(mcu / mcusX); + DecodeBlock(ref reader, kind, c, (by * c.BlocksPerLine + bx) * 64, 0, ref state); + } + else + { + var mx = (int)(mcu % mcusX); + var my = (int)(mcu / mcusX); + for (var i = 0; i < comps.Length; i++) + { + var c = comps[i]; + for (var v = 0; v < c.V; v++) + { + var row = (my * c.V + v) * c.BlocksPerLine + mx * c.H; + for (var h = 0; h < c.H; h++) DecodeBlock(ref reader, kind, c, (row + h) * 64, i, ref state); + } + } + } + if (reader.Overrun) throw new JpegEntropyException("JPEG: premature end of entropy-coded data"); + } + } + catch (JpegEntropyException ex) + { + parser.Warn(ex.Message); + if (interval == 0) break; + mcu = end; + } + if (mcu >= total || interval == 0) break; + + var marker = reader.FindMarker(); + if (marker is < JpegTables.Rst0 or > JpegTables.Rst0 + 7) + { + parser.Warn("JPEG: restart marker missing; scan truncated"); + break; + } + var found = marker - JpegTables.Rst0; + if (found != expectedRst) + { + // Intervals between the expected and the found marker were lost: leave their blocks untouched. + var lost = (found - expectedRst) & 7; + parser.Warn($"JPEG: {lost} restart interval(s) lost"); + mcu += (long)lost * interval; + } + reader.AcceptMarker(); + expectedRst = (found + 1) & 7; + state.EobRun = 0; + foreach (var c in comps) c.DcPred = 0; + } + return reader.Position; + } + + private struct ScanState(JpegScan scan) + { + public readonly JpegScan Scan = scan; + public int EobRun; + } + + private static void DecodeBlock(ref JpegBitReader r, Kind kind, JpegComponent c, int offset, int index, ref ScanState state) + { + var block = c.Coefficients.AsSpan(offset, 64); + var scan = state.Scan; + switch (kind) + { + case Kind.Sequential: + DecodeSequential(ref r, block, scan.DcTables[index]!, scan.AcTables[index]!, ref c.DcPred); + break; + case Kind.DcFirst: + { + var t = r.Decode(scan.DcTables[index]!); + if (t > 16) throw new JpegEntropyException("JPEG: bad DC magnitude"); + c.DcPred += t == 0 ? 0 : r.Receive(t); + block[0] = (short)(c.DcPred * (1 << scan.Al)); + break; + } + case Kind.DcRefine: + if (r.ReadBit() != 0) block[0] |= (short)(1 << scan.Al); + break; + case Kind.AcFirst: + DecodeAcFirst(ref r, block, scan.AcTables[index]!, scan.Ss, scan.Se, scan.Al, ref state.EobRun); + break; + default: + DecodeAcRefine(ref r, block, scan.AcTables[index]!, scan.Ss, scan.Se, scan.Al, ref state.EobRun); + break; + } + } + + private static void DecodeSequential(ref JpegBitReader r, Span block, JpegHuffmanTable dc, JpegHuffmanTable ac, ref int pred) + { + var t = r.Decode(dc); + if (t > 16) throw new JpegEntropyException("JPEG: bad DC magnitude"); + pred += t == 0 ? 0 : r.Receive(t); + block[0] = (short)pred; + var order = JpegTables.NaturalOrder; + var fastAc = ac.FastAc; + var k = 1; + while (k < 64) + { + r.Ensure(16); + var fast = fastAc[r.Peek(JpegHuffmanTable.FastBits)]; + if (fast != 0) + { + k += (fast >> 8) & 15; + r.Consume(fast & 0xFF); + if (k > 63) throw new JpegEntropyException("JPEG: coefficient index out of range"); + block[order[k++]] = (short)(fast >> 16); + continue; + } + var rs = r.Decode(ac); + int run = rs >> 4, s = rs & 15; + if (s == 0) + { + if (run != 15) break; + k += 16; + continue; + } + k += run; + if (k > 63) throw new JpegEntropyException("JPEG: coefficient index out of range"); + block[order[k++]] = (short)r.Receive(s); + } + } + + private static void DecodeAcFirst(ref JpegBitReader r, Span block, JpegHuffmanTable ac, int ss, int se, int al, ref int eobRun) + { + if (eobRun > 0) + { + eobRun--; + return; + } + var order = JpegTables.NaturalOrder; + for (var k = ss; k <= se;) + { + var rs = r.Decode(ac); + int run = rs >> 4, s = rs & 15; + if (s == 0) + { + if (run < 15) + { + eobRun = (1 << run) - 1; + if (run > 0) eobRun += r.ReadBits(run); + return; + } + k += 16; + continue; + } + k += run; + if (k > 63) throw new JpegEntropyException("JPEG: coefficient index out of range"); + block[order[k++]] = (short)(r.Receive(s) * (1 << al)); + } + } + + /// AC successive-approximation refinement (T.81 G.1.2.3), following libjpeg's decode_mcu_AC_refine. + private static void DecodeAcRefine(ref JpegBitReader r, Span block, JpegHuffmanTable ac, int ss, int se, int al, ref int eobRun) + { + var order = JpegTables.NaturalOrder; + var p1 = (short)(1 << al); + var m1 = (short)(-1 << al); + var k = ss; + if (eobRun == 0) + { + for (; k <= se; k++) + { + var rs = r.Decode(ac); + int run = rs >> 4, s = rs & 15; + short value = 0; + if (s != 0) + { + if (s != 1) throw new JpegEntropyException("JPEG: bad refinement magnitude"); + value = r.ReadBit() != 0 ? p1 : m1; + } + else if (run != 15) + { + eobRun = 1 << run; + if (run > 0) eobRun += r.ReadBits(run); + break; + } + // Skip `run` still-zero coefficients, refining the non-zero ones passed on the way. + for (; k <= se; k++) + { + ref var coef = ref block[order[k]]; + if (coef != 0) + { + RefineBit(ref r, ref coef, p1, m1); + } + else + { + if (run == 0) break; + run--; + } + } + if (value != 0) + { + if (k > se) throw new JpegEntropyException("JPEG: refinement beyond spectral band"); + block[order[k]] = value; + } + } + } + if (eobRun > 0) + { + for (; k <= se; k++) + { + ref var coef = ref block[order[k]]; + if (coef != 0) RefineBit(ref r, ref coef, p1, m1); + } + eobRun--; + } + } + + private static void RefineBit(ref JpegBitReader r, ref short coef, short p1, short m1) + { + if (r.ReadBit() == 0 || (coef & p1) != 0) return; + coef += coef >= 0 ? p1 : m1; + } +} diff --git a/src/Titano.Core/Codecs/Jpeg/JpegTables.cs b/src/Titano.Core/Codecs/Jpeg/JpegTables.cs new file mode 100644 index 00000000..6e3d6f0a --- /dev/null +++ b/src/Titano.Core/Codecs/Jpeg/JpegTables.cs @@ -0,0 +1,96 @@ +namespace Titano.Core.Codecs.Jpeg; + +/// Constants shared by the JPEG encoder and decoders (ITU T.81 Annex K). +internal static class JpegTables +{ + public const byte Soi = 0xD8, Eoi = 0xD9, Sos = 0xDA, Dqt = 0xDB, Dnl = 0xDC, Dri = 0xDD, Dht = 0xC4, Dac = 0xCC; + public const byte Sof0 = 0xC0, Sof1 = 0xC1, Sof2 = 0xC2, Sof3 = 0xC3, Rst0 = 0xD0, App0 = 0xE0, App1 = 0xE1, App14 = 0xEE, Com = 0xFE; + + /// Natural (row-major) index of the coefficient at each zig-zag position. + public static ReadOnlySpan NaturalOrder => + [ + 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, + 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, + 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, + 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63, + ]; + + /// Annex K.1 luminance quantisation table, natural order. + public static ReadOnlySpan LumaQuant => + [ + 16, 11, 10, 16, 24, 40, 51, 61, + 12, 12, 14, 19, 26, 58, 60, 55, + 14, 13, 16, 24, 40, 57, 69, 56, + 14, 17, 22, 29, 51, 87, 80, 62, + 18, 22, 37, 56, 68, 109, 103, 77, + 24, 35, 55, 64, 81, 104, 113, 92, + 49, 64, 78, 87, 103, 121, 120, 101, + 72, 92, 95, 98, 112, 100, 103, 99, + ]; + + /// Annex K.1 chrominance quantisation table, natural order. + public static ReadOnlySpan ChromaQuant => + [ + 17, 18, 24, 47, 99, 99, 99, 99, + 18, 21, 26, 66, 99, 99, 99, 99, + 24, 26, 56, 99, 99, 99, 99, 99, + 47, 66, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99, + ]; + + public static ReadOnlySpan DcLumaBits => [0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0]; + public static ReadOnlySpan DcLumaValues => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]; + public static ReadOnlySpan DcChromaBits => [0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0]; + public static ReadOnlySpan DcChromaValues => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]; + public static ReadOnlySpan AcLumaBits => [0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d]; + + public static ReadOnlySpan AcLumaValues => + [ + 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, + 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, + 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, + 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, + 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, + 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, + 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, + 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, + 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, + 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, + 0xf9, 0xfa, + ]; + + public static ReadOnlySpan AcChromaBits => [0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77]; + + public static ReadOnlySpan AcChromaValues => + [ + 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, + 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, + 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, + 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, + 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, + 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, + 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, + 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, + 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, + 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, + 0xf9, 0xfa, + ]; + + /// Orthonormal 8-point DCT basis: Basis[u * 8 + x] = c(u)/2 · cos((2x+1)uπ/16). + public static readonly float[] Basis = BuildBasis(8); + + /// DCT basis of an n-point reduced IDCT evaluated with 8-point normalisation (DCT-domain scaling). + public static float[] BuildBasis(int n) + { + var b = new float[n * n]; + for (var u = 0; u < n; u++) + { + var cu = u == 0 ? Math.Sqrt(0.5) : 1.0; + for (var x = 0; x < n; x++) b[u * n + x] = (float)(cu / 2 * Math.Cos((2 * x + 1) * u * Math.PI / (2 * n))); + } + return b; + } +} diff --git a/src/Titano.Core/Codecs/Jpeg/JpegTypes.cs b/src/Titano.Core/Codecs/Jpeg/JpegTypes.cs new file mode 100644 index 00000000..a8514b3b --- /dev/null +++ b/src/Titano.Core/Codecs/Jpeg/JpegTypes.cs @@ -0,0 +1,153 @@ +namespace Titano.Core.Codecs.Jpeg; + +/// JPEG coding process declared by the SOF marker. +public enum JpegProcess +{ + /// SOF0: baseline sequential DCT, 8-bit, at most two Huffman tables per class. + Baseline, + + /// SOF1: extended sequential DCT (8 or 12-bit). + ExtendedSequential, + + /// SOF2: progressive DCT (spectral selection and successive approximation). + Progressive, + + /// SOF3: lossless predictive coding (2–16 bit). + Lossless, +} + +/// Colour space of the encoded components. +public enum JpegColorSpace +{ + Gray, + YCbCr, + Rgb, + Cmyk, + Ycck, + + /// No defined colour interpretation (e.g. two-component lossless raw tiles). + Other, +} + +/// Overrides the colour interpretation of a 3- or 4-component stream (TIFF knows it out of band). +public enum JpegColorTransform +{ + /// JFIF/Adobe markers and component ids decide (libjpeg rules). + Auto, + + /// Components are stored as-is (RGB or CMYK). + None, + + /// Components are YCbCr (or YCCK for four components). + YCbCr, +} + +/// Frame facts from the headers, available without decoding pixels. +/// Full image width in pixels. +/// Full image height in pixels. +/// Number of encoded components (1–4). +/// Sample precision in bits (8 or 12 for DCT, 2–16 for lossless). +/// Coding process. +/// Colour space the decoder will assume. +/// Horizontal and vertical sampling factors per component. +/// Restart interval in MCUs declared before the first scan (0 = none). +public sealed record JpegInfo( + int Width, + int Height, + int Components, + int Precision, + JpegProcess Process, + JpegColorSpace ColorSpace, + IReadOnlyList<(int H, int V)> Sampling, + int RestartInterval) +{ + /// Chroma subsampling in J:a:b notation for three-component images ("4:2:0"), otherwise a factor list. + public string SamplingNotation + { + get + { + if (Sampling.Count == 3 && Sampling[1] == Sampling[2] && Sampling[1] == (1, 1)) + { + return Sampling[0] switch + { + (1, 1) => "4:4:4", + (2, 1) => "4:2:2", + (2, 2) => "4:2:0", + (1, 2) => "4:4:0", + (4, 1) => "4:1:1", + (4, 2) => "4:1:0", + _ => Factors(), + }; + } + return Factors(); + } + } + + private string Factors() => string.Join(",", Sampling.Select(s => $"{s.H}x{s.V}")); +} + +/// Options for . +public sealed record JpegDecodeOptions +{ + /// Default options: full size, fancy upsampling, automatic colour transform. + public static JpegDecodeOptions Default { get; } = new(); + + /// When > 0, decode with the smallest DCT-domain reduction (1/2, 1/4, 1/8) whose long edge is still + /// ≥ this value. 0 decodes at full size. Ignored for lossless streams. + public int MaxLongEdge { get; init; } + + /// Triangle-filter chroma upsampling (libjpeg "fancy"); false replicates chroma samples. + public bool FancyUpsampling { get; init; } = true; + + /// Colour interpretation override (TIFF photometric). + public JpegColorTransform ColorTransform { get; init; } = JpegColorTransform.Auto; + + /// Abbreviated table-specification stream (TIFF JPEGTables) parsed before the image stream. + public byte[]? Tables { get; init; } + + /// Upper bound on width × height; larger frames are rejected before any allocation. + public long MaxPixels { get; init; } = 1L << 28; + + /// Worker limit for the IDCT and colour stages (-1 = all cores, 1 = single-threaded). + public int MaxDegreeOfParallelism { get; init; } = -1; +} + +/// Decoded JPEG pixels, interleaved, top-down, Exif orientation not applied. +public sealed class JpegImage +{ + internal JpegImage(JpegInfo info, int width, int height, int components, int bitDepth, byte[]? pixels8, ushort[]? pixels16, IReadOnlyList warnings) + { + Info = info; + Width = width; + Height = height; + Components = components; + BitDepth = bitDepth; + Pixels8 = pixels8; + Pixels16 = pixels16; + Warnings = warnings; + } + + /// Header facts of the source stream (full-size dimensions). + public JpegInfo Info { get; } + + /// Output width (smaller than when DCT scaling was used). + public int Width { get; } + + public int Height { get; } + + /// Output channels: 1 (gray) or 3 (RGB; CMYK/YCCK are converted). + public int Components { get; } + + /// Significant bits per sample: 8 → ; more → with values 0..2^BitDepth−1. + public int BitDepth { get; } + + public byte[]? Pixels8 { get; } + + public ushort[]? Pixels16 { get; } + + /// Recoverable problems (corrupt entropy data, missing restart markers, truncation). + public IReadOnlyList Warnings { get; } +} + +/// Raw samples of a lossless (SOF3) stream as encoded: interleaved, as in the frame header. +public readonly record struct JpegLosslessImage(ushort[] Samples, int Width, int Height, int Components, int Precision); diff --git a/src/Titano.Core/Imaging/PixelConvert.cs b/src/Titano.Core/Imaging/PixelConvert.cs new file mode 100644 index 00000000..4a416d6a --- /dev/null +++ b/src/Titano.Core/Imaging/PixelConvert.cs @@ -0,0 +1,390 @@ +using System.Numerics; +using System.Runtime.CompilerServices; + +namespace Titano.Core.Imaging; + +/// How integer samples relate to light: sRGB-encoded (display referred) or proportional to linear light. +public enum TransferFunction +{ + Srgb, + Linear, +} + +/// +/// Conversions between interleaved integer pixels and planar linear-light s, gray ↔ RGB, +/// Exif orientation, and clamping. Interleaved layouts: 1 = gray, 2 = gray+alpha, 3 = RGB, 4 = RGBA. Alpha is +/// never transfer-encoded. Row loops are parallel for large images. +/// +public static class PixelConvert +{ + private const int ParallelThreshold = 1 << 16; + + /// 8×8 Bayer thresholds in code units, centred on zero (ordered dither). + private static readonly float[] Bayer = BuildBayer(); + + /// Interleaved 8-bit samples → linear buffer. + /// 0 = natural (1/2 → 1, 3 → 3, 4 → 3); 3 expands gray; 4 keeps alpha. + public static ImageBuffer FromInterleaved8(ReadOnlySpan pixels, int width, int height, int channels, TransferFunction transfer = TransferFunction.Srgb, int outputChannels = 0) + { + var outCh = OutputChannels(channels, outputChannels); + CheckLength(pixels.Length, width, height, channels); + var lut = new float[256]; + for (var i = 0; i < 256; i++) lut[i] = transfer == TransferFunction.Srgb ? ColorSpace.Linear8((byte)i) : i / 255f; + var image = new ImageBuffer(width, height, outCh, clear: false); + unsafe + { + fixed (byte* p = pixels) + { + var ptr = (nint)p; + ForRows(height, width, y => + { + var row = new ReadOnlySpan((byte*)ptr + (long)y * width * channels, width * channels); + Scatter(row, image, y, channels, lut, 1f / 255f); + }); + } + } + return image; + } + + /// Interleaved 16-bit container samples (values 0..) → linear buffer. + public static ImageBuffer FromInterleaved16(ReadOnlySpan pixels, int width, int height, int channels, TransferFunction transfer = TransferFunction.Srgb, int maxValue = 65535, int outputChannels = 0) + { + if (maxValue is < 1 or > 65535) throw new ArgumentOutOfRangeException(nameof(maxValue)); + var outCh = OutputChannels(channels, outputChannels); + CheckLength(pixels.Length, width, height, channels); + var lut = new float[maxValue + 1]; + for (var i = 0; i <= maxValue; i++) + { + lut[i] = transfer == TransferFunction.Linear ? i / (float)maxValue + : maxValue == 65535 ? ColorSpace.Linear16((ushort)i) : (float)ColorSpace.SrgbToLinear(i / (double)maxValue); + } + var alphaScale = 1f / maxValue; + var image = new ImageBuffer(width, height, outCh, clear: false); + unsafe + { + fixed (ushort* p = pixels) + { + var ptr = (nint)p; + ForRows(height, width, y => + { + var row = new ReadOnlySpan((ushort*)ptr + (long)y * width * channels, width * channels); + Scatter(row, image, y, channels, lut, alphaScale); + }); + } + } + return image; + } + + /// Linear buffer → interleaved 8-bit samples with per pixel + /// (1 from RGB takes Rec.709 luminance; missing alpha is opaque). + /// Add 8×8 ordered dither before rounding (breaks banding in smooth gradients). + public static void ToInterleaved8(ImageBuffer image, Span destination, int channels, TransferFunction transfer = TransferFunction.Srgb, bool dither = false) + { + CheckLength(destination.Length, image.Width, image.Height, channels); + unsafe + { + fixed (byte* p = destination) + { + var ptr = (nint)p; + ForRows(image.Height, image.Width, y => + { + var row = new Span((byte*)ptr + (long)y * image.Width * channels, image.Width * channels); + EncodeRow8(image, y, row, channels, transfer, dither); + }); + } + } + } + + /// Allocating overload of . + public static byte[] ToInterleaved8(ImageBuffer image, int channels, TransferFunction transfer = TransferFunction.Srgb, bool dither = false) + { + var result = new byte[checked(image.Width * image.Height * channels)]; + ToInterleaved8(image, result, channels, transfer, dither); + return result; + } + + /// Linear buffer → interleaved 16-bit samples (0..65535). + public static void ToInterleaved16(ImageBuffer image, Span destination, int channels, TransferFunction transfer = TransferFunction.Srgb) + { + CheckLength(destination.Length, image.Width, image.Height, channels); + unsafe + { + fixed (ushort* p = destination) + { + var ptr = (nint)p; + ForRows(image.Height, image.Width, y => + { + var row = new Span((ushort*)ptr + (long)y * image.Width * channels, image.Width * channels); + EncodeRow16(image, y, row, channels, transfer); + }); + } + } + } + + /// Allocating overload of . + public static ushort[] ToInterleaved16(ImageBuffer image, int channels, TransferFunction transfer = TransferFunction.Srgb) + { + var result = new ushort[checked(image.Width * image.Height * channels)]; + ToInterleaved16(image, result, channels, transfer); + return result; + } + + /// Encodes row into interleaved 8-bit samples (codec edge helper). + internal static void EncodeRow8(ImageBuffer image, int y, Span row, int channels, TransferFunction transfer, bool dither) + { + var w = image.Width; + Span offsets = stackalloc float[8]; + if (dither) + { + for (var i = 0; i < 8; i++) offsets[i] = 0.5f + Bayer[(y & 7) * 8 + i]; + } + else + { + offsets.Fill(0.5f); + } + var srgb = transfer == TransferFunction.Srgb; + var colour = channels is 1 or 2 ? 1 : 3; + for (var c = 0; c < channels; c++) + { + if (c == colour) + { + var a = image.Channels == 4 ? image.Row(3, y) : default; + for (var x = 0; x < w; x++) row[x * channels + c] = a.IsEmpty ? (byte)255 : (byte)(Math.Clamp(a[x], 0f, 1f) * 255f + 0.5f); + continue; + } + var src = SourceRow(image, y, c, colour, out var luminance); + for (var x = 0; x < w; x++) + { + var v = luminance is null ? src[x] : luminance[x]; + var e = srgb ? ColorSpace.EncodeFast(v) : Math.Clamp(v, 0f, 1f); + row[x * channels + c] = (byte)Math.Clamp(e * 255f + offsets[x & 7], 0f, 255f); + } + } + } + + /// Encodes row into interleaved 16-bit samples (codec edge helper). + internal static void EncodeRow16(ImageBuffer image, int y, Span row, int channels, TransferFunction transfer) + { + var w = image.Width; + var srgb = transfer == TransferFunction.Srgb; + var colour = channels is 1 or 2 ? 1 : 3; + for (var c = 0; c < channels; c++) + { + if (c == colour) + { + var a = image.Channels == 4 ? image.Row(3, y) : default; + for (var x = 0; x < w; x++) row[x * channels + c] = a.IsEmpty ? (ushort)65535 : (ushort)(Math.Clamp(a[x], 0f, 1f) * 65535f + 0.5f); + continue; + } + var src = SourceRow(image, y, c, colour, out var luminance); + for (var x = 0; x < w; x++) + { + var v = luminance is null ? src[x] : luminance[x]; + row[x * channels + c] = srgb ? ColorSpace.Encode16(v) : (ushort)(Math.Clamp(v, 0f, 1f) * 65535f + 0.5f); + } + } + } + + /// Single-channel buffer replicated into three channels. + public static ImageBuffer GrayToRgb(ImageBuffer gray) + { + if (gray.Channels != 1) throw new ArgumentException("Expected one channel", nameof(gray)); + var rgb = new ImageBuffer(gray.Width, gray.Height, 3, clear: false); + for (var c = 0; c < 3; c++) gray.Plane(0).CopyTo(rgb.Plane(c)); + return rgb; + } + + /// Rec.709 luminance of an RGB(A) buffer. + public static ImageBuffer RgbToGray(ImageBuffer rgb) + { + if (rgb.Channels < 3) throw new ArgumentException("Expected RGB", nameof(rgb)); + var gray = new ImageBuffer(rgb.Width, rgb.Height, 1, clear: false); + var r = rgb.Plane(0); + var g = rgb.Plane(1); + var b = rgb.Plane(2); + var d = gray.Plane(0); + var i = 0; + if (Vector.IsHardwareAccelerated) + { + var wr = new Vector(ColorSpace.LumaR); + var wg = new Vector(ColorSpace.LumaG); + var wb = new Vector(ColorSpace.LumaB); + for (; i <= d.Length - Vector.Count; i += Vector.Count) + { + (new Vector(r[i..]) * wr + new Vector(g[i..]) * wg + new Vector(b[i..]) * wb).CopyTo(d[i..]); + } + } + for (; i < d.Length; i++) d[i] = ColorSpace.Luminance(r[i], g[i], b[i]); + return gray; + } + + /// Clamps every value into [, ] (NaN becomes ). + public static void Clamp(Span values, float min = 0f, float max = 1f) + { + var i = 0; + if (Vector.IsHardwareAccelerated) + { + var lo = new Vector(min); + var hi = new Vector(max); + for (; i <= values.Length - Vector.Count; i += Vector.Count) + { + var v = new Vector(values[i..]); + // Max(v, lo) with v as the first operand maps NaN to lo on x86 (maxps returns the second operand). + Vector.Min(Vector.ConditionalSelect(Vector.Equals(v, v), Vector.Max(v, lo), lo), hi).CopyTo(values[i..]); + } + } + for (; i < values.Length; i++) + { + var v = values[i]; + values[i] = float.IsNaN(v) ? min : Math.Clamp(v, min, max); + } + } + + /// Size after applying an Exif orientation (5–8 swap width and height). + public static (int Width, int Height) OrientedSize(int width, int height, int orientation) => + orientation is >= 5 and <= 8 ? (height, width) : (width, height); + + /// Applies an Exif orientation (1–8) into a new buffer; other values are treated as 1 (copy). + public static ImageBuffer ApplyOrientation(ImageBuffer source, int orientation) + { + int w = source.Width, h = source.Height; + var (ow, oh) = OrientedSize(w, h, orientation); + var result = new ImageBuffer(ow, oh, source.Channels, clear: false); + if (orientation is < 2 or > 8) + { + source.CopyTo(result); + return result; + } + for (var c = 0; c < source.Channels; c++) + { + var src = source.Storage.AsMemory(c * source.PlaneLength, source.PlaneLength); + var dst = result.Storage.AsMemory(c * result.PlaneLength, result.PlaneLength); + if (orientation <= 4) + { + ForRows(oh, ow, y => + { + var sy = orientation is 3 or 4 ? h - 1 - y : y; + var s = src.Span.Slice(sy * w, w); + var d = dst.Span.Slice(y * ow, ow); + s.CopyTo(d); + if (orientation is 2 or 3) d.Reverse(); + }); + continue; + } + // Transposing orientations: copy in 32×32 tiles so both sides stay cache-friendly. + const int tile = 32; + var tilesY = (oh + tile - 1) / tile; + ForRows(tilesY, ow * tile, ty => + { + var s = src.Span; + var d = dst.Span; + var y1 = Math.Min(oh, (ty + 1) * tile); + for (var x0 = 0; x0 < ow; x0 += tile) + { + var x1 = Math.Min(ow, x0 + tile); + for (var y = ty * tile; y < y1; y++) + { + for (var x = x0; x < x1; x++) + { + var (sx, sy) = orientation switch + { + 5 => (y, x), + 6 => (y, h - 1 - x), + 7 => (w - 1 - y, h - 1 - x), + _ => (w - 1 - y, x), + }; + d[y * ow + x] = s[sy * w + sx]; + } + } + } + }); + } + return result; + } + + private static ReadOnlySpan SourceRow(ImageBuffer image, int y, int c, int colourChannels, out float[]? luminance) + { + luminance = null; + if (image.Channels == 1) return image.Row(0, y); + if (colourChannels == 3) return image.Row(c, y); + var r = image.Row(0, y); + var g = image.Row(1, y); + var b = image.Row(2, y); + luminance = new float[image.Width]; + for (var x = 0; x < luminance.Length; x++) luminance[x] = ColorSpace.Luminance(r[x], g[x], b[x]); + return default; + } + + /// De-interleaves one row: colour through (indices clamped to its range), alpha scaled. + private static void Scatter(ReadOnlySpan row, ImageBuffer image, int y, int channels, float[] lut, float alphaScale) + where T : unmanaged, IBinaryInteger + { + var w = image.Width; + var top = lut.Length - 1; + var colour = channels is 1 or 2 ? 1 : 3; + for (var c = 0; c < image.Channels; c++) + { + var dst = image.Row(c, y); + if (c == 3) + { + if (channels is 2 or 4) + { + var a = channels - 1; + for (var x = 0; x < w; x++) dst[x] = Math.Min(int.CreateTruncating(row[x * channels + a]), top) * alphaScale; + } + else + { + dst.Fill(1f); + } + continue; + } + var s = colour == 1 ? 0 : c; + for (var x = 0; x < w; x++) dst[x] = lut[Math.Min(int.CreateTruncating(row[x * channels + s]), top)]; + } + } + + private static int OutputChannels(int channels, int requested) + { + if (channels is < 1 or > 4) throw new ArgumentOutOfRangeException(nameof(channels)); + if (requested is not (0 or 1 or 3 or 4)) throw new ArgumentOutOfRangeException(nameof(requested)); + if (requested == 1 && channels > 2) throw new ArgumentException("Use RgbToGray to reduce colour to one channel", nameof(requested)); + return requested != 0 ? requested : channels <= 2 ? 1 : 3; + } + + private static void CheckLength(int length, int width, int height, int channels) + { + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(width); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(height); + if (channels is < 1 or > 4) throw new ArgumentOutOfRangeException(nameof(channels)); + if (length < (long)width * height * channels) throw new ArgumentException("Pixel buffer too small"); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static void ForRows(int rows, int rowWork, Action body) + { + if ((long)rows * rowWork < ParallelThreshold) + { + for (var y = 0; y < rows; y++) body(y); + } + else + { + Parallel.For(0, rows, body); + } + } + + private static float[] BuildBayer() + { + // Recursive Bayer matrix: interleave bits of (x ^ y) and y, least significant coordinate bits first. + var m = new float[64]; + for (var y = 0; y < 8; y++) + { + for (var x = 0; x < 8; x++) + { + var v = 0; + for (var bit = 0; bit < 3; bit++) v = v << 2 | (((x ^ y) >> bit) & 1) << 1 | ((y >> bit) & 1); + m[y * 8 + x] = (v + 0.5f) / 64f - 0.5f; + } + } + return m; + } +} diff --git a/tests/Titano.Core.Tests/Codecs/CodecTestData.cs b/tests/Titano.Core.Tests/Codecs/CodecTestData.cs new file mode 100644 index 00000000..3f637bd4 --- /dev/null +++ b/tests/Titano.Core.Tests/Codecs/CodecTestData.cs @@ -0,0 +1,167 @@ +using System.Text; +using Titano.Testing; + +namespace Titano.Core.Tests.Codecs; + +/// Shared fixtures, synthetic images and image metrics for codec tests. +internal static class CodecTestData +{ + public static byte[] Fixture(string relativePath) => + File.ReadAllBytes(Path.Combine(TestContext.FixturesDirectory, "codecs", relativePath)); + + /// Netpbm P5/P6/P7 (PAM) reader for reference images; samples are big-endian when maxval > 255. + public static Pnm ReadPnm(byte[] data) + { + var pos = 0; + string Token() + { + while (pos < data.Length && (char.IsWhiteSpace((char)data[pos]) || data[pos] == '#')) + { + if (data[pos] == '#') while (pos < data.Length && data[pos] != '\n') pos++; + else pos++; + } + var start = pos; + while (pos < data.Length && !char.IsWhiteSpace((char)data[pos])) pos++; + return Encoding.ASCII.GetString(data, start, pos - start); + } + var magic = Token(); + int width, height, channels, maxVal; + if (magic == "P7") + { + width = height = channels = maxVal = 0; + for (var t = Token(); t != "ENDHDR"; t = Token()) + { + switch (t) + { + case "WIDTH": width = int.Parse(Token()); break; + case "HEIGHT": height = int.Parse(Token()); break; + case "DEPTH": channels = int.Parse(Token()); break; + case "MAXVAL": maxVal = int.Parse(Token()); break; + case "TUPLTYPE": Token(); break; + } + } + } + else + { + channels = magic switch { "P5" => 1, "P6" => 3, _ => throw new InvalidDataException(magic) }; + width = int.Parse(Token()); + height = int.Parse(Token()); + maxVal = int.Parse(Token()); + } + pos++; + var samples = new int[width * height * channels]; + for (var i = 0; i < samples.Length; i++) + { + samples[i] = maxVal > 255 ? data[pos + 2 * i] << 8 | data[pos + 2 * i + 1] : data[pos + i]; + } + return new Pnm(width, height, channels, maxVal, samples); + } + + public sealed record Pnm(int Width, int Height, int Channels, int MaxVal, int[] Samples) + { + public byte[] Bytes => Samples.Select(s => (byte)s).ToArray(); + public ushort[] Words => Samples.Select(s => (ushort)s).ToArray(); + } + + /// Deterministic RGB test card: smooth gradients, saturated colour bars, seeded noise and + /// text-like one-pixel strokes (the four content classes codecs struggle with). + public static byte[] TestCard(int width, int height, int seed = 1) + { + var rng = new Random(seed); + var rgb = new byte[width * height * 3]; + for (var y = 0; y < height; y++) + { + for (var x = 0; x < width; x++) + { + var band = y * 4 / height; + int r, g, b; + switch (band) + { + case 0: + r = x * 255 / Math.Max(1, width - 1); + g = y * 4 * 255 / Math.Max(1, height - 1) / 4 * 4 % 256; + b = 255 - r; + break; + case 1: + var bar = x * 8 / width; + r = (bar & 4) != 0 ? 230 : 25; + g = (bar & 2) != 0 ? 230 : 25; + b = (bar & 1) != 0 ? 230 : 25; + break; + case 2: + r = rng.Next(256); + g = rng.Next(256); + b = rng.Next(256); + break; + default: + var stroke = x % 7 == 0 || y % 9 == 0 || (x + y) % 13 == 0; + r = g = b = stroke ? 20 : 235; + break; + } + var i = (y * width + x) * 3; + rgb[i] = (byte)r; + rgb[i + 1] = (byte)g; + rgb[i + 2] = (byte)b; + } + } + return rgb; + } + + /// Smooth photographic-like content (low-frequency gradients); compresses like real photos. + public static byte[] SmoothImage(int width, int height) + { + var rgb = new byte[width * height * 3]; + for (var y = 0; y < height; y++) + { + for (var x = 0; x < width; x++) + { + double u = x / (double)width, v = y / (double)height; + var i = (y * width + x) * 3; + rgb[i] = (byte)(127.5 + 120 * Math.Sin(6.1 * u + 2.3 * v)); + rgb[i + 1] = (byte)(127.5 + 120 * Math.Cos(4.7 * u * v + 3.1 * v)); + rgb[i + 2] = (byte)(127.5 + 120 * Math.Sin(9.3 * v - 1.7 * u + 0.5)); + } + } + return rgb; + } + + public static double Psnr(ReadOnlySpan a, ReadOnlySpan b) => Psnr(a, b, 255); + + public static double Psnr(ReadOnlySpan a, ReadOnlySpan b, double peak) where T : struct, IConvertible + { + Assert.Equal(a.Length, b.Length, "sample count"); + double sum = 0; + for (var i = 0; i < a.Length; i++) + { + var d = a[i].ToDouble(null) - b[i].ToDouble(null); + sum += d * d; + } + var mse = sum / a.Length; + return mse == 0 ? 99 : 10 * Math.Log10(peak * peak / mse); + } + + public static int MaxAbsDiff(ReadOnlySpan a, ReadOnlySpan b) + { + Assert.Equal(a.Length, b.Length, "sample count"); + var max = 0; + for (var i = 0; i < a.Length; i++) max = Math.Max(max, Math.Abs(a[i] - b[i])); + return max; + } + + /// Seeded corruption: the input truncated at points plus single-byte flips. + public static IEnumerable Corruptions(byte[] valid, int cases, int seed) + { + var rng = new Random(seed); + for (var i = 0; i < cases; i++) + { + yield return valid.AsSpan(0, rng.Next(valid.Length)).ToArray(); + var flipped = (byte[])valid.Clone(); + var flips = 1 + rng.Next(4); + for (var f = 0; f < flips; f++) flipped[rng.Next(flipped.Length)] ^= (byte)(1 << rng.Next(8)); + yield return flipped; + var garbage = (byte[])valid.Clone(); + garbage[rng.Next(garbage.Length)] = (byte)rng.Next(256); + yield return garbage; + } + } +} diff --git a/tests/Titano.Core.Tests/Codecs/JpegDecoderTests.cs b/tests/Titano.Core.Tests/Codecs/JpegDecoderTests.cs new file mode 100644 index 00000000..d161083c --- /dev/null +++ b/tests/Titano.Core.Tests/Codecs/JpegDecoderTests.cs @@ -0,0 +1,79 @@ +using Titano.Core.Codecs.Jpeg; +using Titano.Testing; +using static Titano.Core.Tests.Codecs.CodecTestData; + +namespace Titano.Core.Tests.Codecs; + +/// Decoder output against libjpeg-turbo's djpeg (accurate integer IDCT) on cjpeg-produced fixtures. +public sealed class JpegDecoderTests(TestContext context) +{ + [Theory] + [Case("b444", "4:4:4")] + [Case("b422", "4:2:2")] + [Case("b420", "4:2:0")] + [Case("b440", "4:4:0")] + [Case("b411", "4:1:1")] + [Case("p420", "4:2:0")] + [Case("p444", "4:4:4")] + [Case("r420", "4:2:0")] + [Case("r444", "4:4:4")] + public void MatchesDjpeg(string name, string sampling) + { + var data = Fixture($"jpeg/{name}.jpg"); + var reference = ReadPnm(Fixture($"jpeg/{name}.ppm")); + var image = JpegDecoder.Decode(data); + Assert.Equal(reference.Width, image.Width); + Assert.Equal(reference.Height, image.Height); + Assert.Equal(3, image.Components); + Assert.Equal(sampling, image.Info.SamplingNotation); + Assert.Empty(image.Warnings); + var diff = MaxAbsDiff(image.Pixels8, reference.Samples); + context.Log($"{name}: max |diff| = {diff}"); + Assert.LessOrEqual(diff, 2); + } + + [Theory] + [Case("gray")] + [Case("pgray")] + public void GrayMatchesDjpeg(string name) + { + var image = JpegDecoder.Decode(Fixture($"jpeg/{name}.jpg")); + var reference = ReadPnm(Fixture($"jpeg/{name}.pgm")); + Assert.Equal(1, image.Components); + Assert.Equal(JpegColorSpace.Gray, image.Info.ColorSpace); + Assert.LessOrEqual(MaxAbsDiff(image.Pixels8, reference.Samples), 2); + } + + [Test] + public void ReplicatedUpsamplingMatchesDjpegNoSmooth() + { + var image = JpegDecoder.Decode(Fixture("jpeg/b420.jpg"), new JpegDecodeOptions { FancyUpsampling = false }); + Assert.LessOrEqual(MaxAbsDiff(image.Pixels8, ReadPnm(Fixture("jpeg/b420-nosmooth.ppm")).Samples), 2); + } + + [Test] + public void ReadInfoParsesHeadersOnly() + { + var info = JpegDecoder.ReadInfo(Fixture("jpeg/p420.jpg")); + Assert.Equal(61, info.Width); + Assert.Equal(45, info.Height); + Assert.Equal(JpegProcess.Progressive, info.Process); + Assert.Equal(JpegColorSpace.YCbCr, info.ColorSpace); + Assert.Equal(8, info.Precision); + var restart = JpegDecoder.ReadInfo(Fixture("jpeg/r444.jpg")); + Assert.Equal(5, restart.RestartInterval); + } + + [Test] + public void AdobeYcckIsConvertedToRgb() + { + var image = JpegDecoder.Decode(Fixture("jpeg/cmyk.jpg")); + Assert.Equal(JpegColorSpace.Ycck, image.Info.ColorSpace); + Assert.Equal(4, image.Info.Components); + Assert.Equal(3, image.Components); + var source = ReadPnm(Fixture("src.ppm")); + var psnr = Psnr(image.Pixels8, source.Bytes); + context.Log($"YCCK round trip PSNR {psnr:F1} dB"); + Assert.Greater(psnr, 30); + } +} diff --git a/tests/fixtures/codecs/jpeg/b411.jpg b/tests/fixtures/codecs/jpeg/b411.jpg new file mode 100644 index 00000000..7331b1e5 Binary files /dev/null and b/tests/fixtures/codecs/jpeg/b411.jpg differ diff --git a/tests/fixtures/codecs/jpeg/b411.ppm b/tests/fixtures/codecs/jpeg/b411.ppm new file mode 100644 index 00000000..e05451f9 Binary files /dev/null and b/tests/fixtures/codecs/jpeg/b411.ppm differ diff --git a/tests/fixtures/codecs/jpeg/b420-nosmooth.ppm b/tests/fixtures/codecs/jpeg/b420-nosmooth.ppm new file mode 100644 index 00000000..f29d6a77 Binary files /dev/null and b/tests/fixtures/codecs/jpeg/b420-nosmooth.ppm differ diff --git a/tests/fixtures/codecs/jpeg/b420.jpg b/tests/fixtures/codecs/jpeg/b420.jpg new file mode 100644 index 00000000..0181451d Binary files /dev/null and b/tests/fixtures/codecs/jpeg/b420.jpg differ diff --git a/tests/fixtures/codecs/jpeg/b420.ppm b/tests/fixtures/codecs/jpeg/b420.ppm new file mode 100644 index 00000000..4fada5ab Binary files /dev/null and b/tests/fixtures/codecs/jpeg/b420.ppm differ diff --git a/tests/fixtures/codecs/jpeg/b422.jpg b/tests/fixtures/codecs/jpeg/b422.jpg new file mode 100644 index 00000000..0624bc3a Binary files /dev/null and b/tests/fixtures/codecs/jpeg/b422.jpg differ diff --git a/tests/fixtures/codecs/jpeg/b422.ppm b/tests/fixtures/codecs/jpeg/b422.ppm new file mode 100644 index 00000000..37088094 Binary files /dev/null and b/tests/fixtures/codecs/jpeg/b422.ppm differ diff --git a/tests/fixtures/codecs/jpeg/b440.jpg b/tests/fixtures/codecs/jpeg/b440.jpg new file mode 100644 index 00000000..e7294bbd Binary files /dev/null and b/tests/fixtures/codecs/jpeg/b440.jpg differ diff --git a/tests/fixtures/codecs/jpeg/b440.ppm b/tests/fixtures/codecs/jpeg/b440.ppm new file mode 100644 index 00000000..edb57f27 Binary files /dev/null and b/tests/fixtures/codecs/jpeg/b440.ppm differ diff --git a/tests/fixtures/codecs/jpeg/b444.jpg b/tests/fixtures/codecs/jpeg/b444.jpg new file mode 100644 index 00000000..315fd5f5 Binary files /dev/null and b/tests/fixtures/codecs/jpeg/b444.jpg differ diff --git a/tests/fixtures/codecs/jpeg/b444.ppm b/tests/fixtures/codecs/jpeg/b444.ppm new file mode 100644 index 00000000..2d0ce918 Binary files /dev/null and b/tests/fixtures/codecs/jpeg/b444.ppm differ diff --git a/tests/fixtures/codecs/jpeg/cmyk.jpg b/tests/fixtures/codecs/jpeg/cmyk.jpg new file mode 100644 index 00000000..a5fce73c Binary files /dev/null and b/tests/fixtures/codecs/jpeg/cmyk.jpg differ diff --git a/tests/fixtures/codecs/jpeg/gray.jpg b/tests/fixtures/codecs/jpeg/gray.jpg new file mode 100644 index 00000000..86533a0f Binary files /dev/null and b/tests/fixtures/codecs/jpeg/gray.jpg differ diff --git a/tests/fixtures/codecs/jpeg/gray.pgm b/tests/fixtures/codecs/jpeg/gray.pgm new file mode 100644 index 00000000..2a3b7f1d Binary files /dev/null and b/tests/fixtures/codecs/jpeg/gray.pgm differ diff --git a/tests/fixtures/codecs/jpeg/p420.jpg b/tests/fixtures/codecs/jpeg/p420.jpg new file mode 100644 index 00000000..cd78ae91 Binary files /dev/null and b/tests/fixtures/codecs/jpeg/p420.jpg differ diff --git a/tests/fixtures/codecs/jpeg/p420.ppm b/tests/fixtures/codecs/jpeg/p420.ppm new file mode 100644 index 00000000..0faf5be8 Binary files /dev/null and b/tests/fixtures/codecs/jpeg/p420.ppm differ diff --git a/tests/fixtures/codecs/jpeg/p444.jpg b/tests/fixtures/codecs/jpeg/p444.jpg new file mode 100644 index 00000000..064da6eb Binary files /dev/null and b/tests/fixtures/codecs/jpeg/p444.jpg differ diff --git a/tests/fixtures/codecs/jpeg/p444.ppm b/tests/fixtures/codecs/jpeg/p444.ppm new file mode 100644 index 00000000..d4864ccd Binary files /dev/null and b/tests/fixtures/codecs/jpeg/p444.ppm differ diff --git a/tests/fixtures/codecs/jpeg/pgray.jpg b/tests/fixtures/codecs/jpeg/pgray.jpg new file mode 100644 index 00000000..06b705b4 Binary files /dev/null and b/tests/fixtures/codecs/jpeg/pgray.jpg differ diff --git a/tests/fixtures/codecs/jpeg/pgray.pgm b/tests/fixtures/codecs/jpeg/pgray.pgm new file mode 100644 index 00000000..2a3b7f1d Binary files /dev/null and b/tests/fixtures/codecs/jpeg/pgray.pgm differ diff --git a/tests/fixtures/codecs/jpeg/r420.jpg b/tests/fixtures/codecs/jpeg/r420.jpg new file mode 100644 index 00000000..d831c18d Binary files /dev/null and b/tests/fixtures/codecs/jpeg/r420.jpg differ diff --git a/tests/fixtures/codecs/jpeg/r420.ppm b/tests/fixtures/codecs/jpeg/r420.ppm new file mode 100644 index 00000000..86af7fd9 Binary files /dev/null and b/tests/fixtures/codecs/jpeg/r420.ppm differ diff --git a/tests/fixtures/codecs/jpeg/r444.jpg b/tests/fixtures/codecs/jpeg/r444.jpg new file mode 100644 index 00000000..cebd1513 Binary files /dev/null and b/tests/fixtures/codecs/jpeg/r444.jpg differ diff --git a/tests/fixtures/codecs/jpeg/r444.ppm b/tests/fixtures/codecs/jpeg/r444.ppm new file mode 100644 index 00000000..84db23e9 Binary files /dev/null and b/tests/fixtures/codecs/jpeg/r444.ppm differ diff --git a/tests/fixtures/codecs/make-fixtures.sh b/tests/fixtures/codecs/make-fixtures.sh new file mode 100755 index 00000000..a2aea85c --- /dev/null +++ b/tests/fixtures/codecs/make-fixtures.sh @@ -0,0 +1,75 @@ +#!/usr/bin/env bash +# Regenerates the codec reference fixtures (dev-only tools: python3, libjpeg-turbo-progs, imagemagick, libtiff-tools). +# Every output is < 50 KB. Run from this directory: ./make-fixtures.sh +set -euo pipefail +cd "$(dirname "$0")" +mkdir -p jpeg png tiff + +# 61x45 source (odd size exercises MCU/tile padding): gradients, colour bars, seeded noise and hard edges. +python3 - <<'EOF' +import random +W, H = 61, 45 +random.seed(7) +px8, px16 = [], [] +for y in range(H): + for x in range(W): + if y < 15: + r, g, b = x / (W - 1), y / 14, 1 - x / (W - 1) + elif y < 30: + bar = (x * 8) // W + r, g, b = (bar >> 2) & 1, (bar >> 1) & 1, bar & 1 + r, g, b = 0.1 + 0.8 * r, 0.1 + 0.8 * g, 0.1 + 0.8 * b + else: + n = random.random() + edge = 1.0 if ((x // 3) + (y // 3)) % 2 == 0 else 0.0 + r, g, b = 0.6 * edge + 0.4 * n, 0.5 * n, 0.8 * edge + px8 += [round(v * 255) for v in (r, g, b)] + px16 += [round(v * 65535) for v in (r, g, b)] +with open("src.ppm", "wb") as f: + f.write(b"P6\n%d %d\n255\n" % (W, H)); f.write(bytes(px8)) +with open("src16.ppm", "wb") as f: + f.write(b"P6\n%d %d\n65535\n" % (W, H)) + f.write(b"".join(v.to_bytes(2, "big") for v in px16)) +EOF + +# JPEG: cjpeg encodes, djpeg (accurate integer IDCT, fancy upsampling) provides the reference pixels. +for s in "444:1x1" "422:2x1" "420:2x2" "440:1x2" "411:4x1"; do + name=${s%%:*}; f=${s##*:} + cjpeg -quality 88 -sample "$f" src.ppm > "jpeg/b$name.jpg" + djpeg -ppm "jpeg/b$name.jpg" > "jpeg/b$name.ppm" +done +djpeg -nosmooth -ppm jpeg/b420.jpg > jpeg/b420-nosmooth.ppm +cjpeg -quality 85 -sample 2x2 -progressive src.ppm > jpeg/p420.jpg && djpeg -ppm jpeg/p420.jpg > jpeg/p420.ppm +cjpeg -quality 92 -sample 1x1 -progressive src.ppm > jpeg/p444.jpg && djpeg -ppm jpeg/p444.jpg > jpeg/p444.ppm +cjpeg -quality 90 -sample 2x2 -restart 1 src.ppm > jpeg/r420.jpg && djpeg -ppm jpeg/r420.jpg > jpeg/r420.ppm +cjpeg -quality 90 -sample 1x1 -restart 5B -optimize src.ppm > jpeg/r444.jpg && djpeg -ppm jpeg/r444.jpg > jpeg/r444.ppm +cjpeg -quality 90 -grayscale src.ppm > jpeg/gray.jpg && djpeg -pnm jpeg/gray.jpg > jpeg/gray.pgm +cjpeg -quality 90 -grayscale -progressive src.ppm > jpeg/pgray.jpg && djpeg -pnm jpeg/pgray.jpg > jpeg/pgray.pgm +convert src.ppm -quality 95 -colorspace CMYK jpeg/cmyk.jpg + +# PNG: ImageMagick/libpng writers; pixels compare against src.ppm / src16.ppm. +convert src.ppm -interlace PNG png24:png/adam7.png +convert src16.ppm -define png:bit-depth=16 png48:png/rgb16.png +convert src16.ppm -interlace PNG -define png:bit-depth=16 png48:png/adam7-16.png +convert src.ppm -colorspace Gray -define png:color-type=0 -define png:bit-depth=8 png/gray8.png +convert src.ppm -colorspace Gray -depth 4 -define png:color-type=0 -define png:bit-depth=4 png/gray4.png +convert src.ppm -colorspace Gray -depth 2 -define png:color-type=0 -define png:bit-depth=2 png/gray2.png +convert src.ppm -colorspace Gray -threshold 50% -define png:color-type=0 -define png:bit-depth=1 png/gray1.png +convert src.ppm -colors 16 -transparent "$(convert src.ppm -colors 16 -format '%[pixel:p{0,40}]' info:)" png8:png/palette-trns.png +convert src.ppm -colors 16 -interlace PNG png8:png/palette-adam7.png +convert src.ppm -colors 12 -define png:color-type=3 -define png:bit-depth=4 png/palette4.png +convert src.ppm \( src.ppm -colorspace Gray \) -alpha off -compose CopyOpacity -composite png32:png/rgba.png +convert src.ppm -colorspace Gray \( src.ppm -channel R -separate \) -alpha off -compose CopyOpacity -composite -define png:color-type=4 png/graya.png +for f in gray1 gray2 gray4 gray8 graya palette-trns palette-adam7 palette4 rgba; do convert "png/$f.png" -depth 8 "png/$f.pam"; done + +# TIFF: ImageMagick/libtiff writers in every supported layout and compression. +convert src.ppm -compress LZW -define tiff:predictor=2 tiff/lzw-pred.tif +convert src.ppm -compress RLE tiff/packbits.tif +convert src.ppm -compress None -define tiff:rows-per-strip=7 tiff/none-strips.tif +convert src16.ppm -depth 16 -compress Zip -define tiff:predictor=2 tiff/deflate16-pred.tif +convert src.ppm -compress LZW -define tiff:tile-geometry=16x16 tiff/lzw-tiled.tif +tiffcp -p separate -c zip src.ppm.tmp.tif tiff/planar-zip.tif 2>/dev/null || { convert src.ppm -compress None tiff/_tmp.tif; tiffcp -p separate -c zip tiff/_tmp.tif tiff/planar-zip.tif; rm tiff/_tmp.tif; } +convert src.ppm -compress None tiff/_tmp.tif && tiffcp -c jpeg:90 -r 16 tiff/_tmp.tif tiff/jpeg-ycbcr.tif && tiffcp -c jpeg:r:90 -r 16 tiff/_tmp.tif tiff/jpeg-rgb.tif && rm tiff/_tmp.tif +convert src16.ppm -define quantum:format=floating-point -depth 32 -compress Zip -define tiff:predictor=3 tiff/float32-pred3.tif +convert src.ppm -colorspace Gray -define tiff:photometric=min-is-white -compress None tiff/gray-miniswhite.tif +ls -l jpeg png tiff diff --git a/tests/fixtures/codecs/png/adam7-16.png b/tests/fixtures/codecs/png/adam7-16.png new file mode 100644 index 00000000..cf5c5c4e Binary files /dev/null and b/tests/fixtures/codecs/png/adam7-16.png differ diff --git a/tests/fixtures/codecs/png/adam7.png b/tests/fixtures/codecs/png/adam7.png new file mode 100644 index 00000000..0c03608f Binary files /dev/null and b/tests/fixtures/codecs/png/adam7.png differ diff --git a/tests/fixtures/codecs/png/gray1.pam b/tests/fixtures/codecs/png/gray1.pam new file mode 100644 index 00000000..5927d797 Binary files /dev/null and b/tests/fixtures/codecs/png/gray1.pam differ diff --git a/tests/fixtures/codecs/png/gray1.png b/tests/fixtures/codecs/png/gray1.png new file mode 100644 index 00000000..c60c4723 Binary files /dev/null and b/tests/fixtures/codecs/png/gray1.png differ diff --git a/tests/fixtures/codecs/png/gray2.pam b/tests/fixtures/codecs/png/gray2.pam new file mode 100644 index 00000000..e5ea0dd9 Binary files /dev/null and b/tests/fixtures/codecs/png/gray2.pam differ diff --git a/tests/fixtures/codecs/png/gray2.png b/tests/fixtures/codecs/png/gray2.png new file mode 100644 index 00000000..2b998fd7 Binary files /dev/null and b/tests/fixtures/codecs/png/gray2.png differ diff --git a/tests/fixtures/codecs/png/gray4.pam b/tests/fixtures/codecs/png/gray4.pam new file mode 100644 index 00000000..3ddb45bd Binary files /dev/null and b/tests/fixtures/codecs/png/gray4.pam differ diff --git a/tests/fixtures/codecs/png/gray4.png b/tests/fixtures/codecs/png/gray4.png new file mode 100644 index 00000000..e1d927f9 Binary files /dev/null and b/tests/fixtures/codecs/png/gray4.png differ diff --git a/tests/fixtures/codecs/png/gray8.pam b/tests/fixtures/codecs/png/gray8.pam new file mode 100644 index 00000000..83a4c570 Binary files /dev/null and b/tests/fixtures/codecs/png/gray8.pam differ diff --git a/tests/fixtures/codecs/png/gray8.png b/tests/fixtures/codecs/png/gray8.png new file mode 100644 index 00000000..45b3a890 Binary files /dev/null and b/tests/fixtures/codecs/png/gray8.png differ diff --git a/tests/fixtures/codecs/png/graya.pam b/tests/fixtures/codecs/png/graya.pam new file mode 100644 index 00000000..d12dfabe Binary files /dev/null and b/tests/fixtures/codecs/png/graya.pam differ diff --git a/tests/fixtures/codecs/png/graya.png b/tests/fixtures/codecs/png/graya.png new file mode 100644 index 00000000..273e65c7 Binary files /dev/null and b/tests/fixtures/codecs/png/graya.png differ diff --git a/tests/fixtures/codecs/png/palette-adam7.pam b/tests/fixtures/codecs/png/palette-adam7.pam new file mode 100644 index 00000000..4ef76b4c Binary files /dev/null and b/tests/fixtures/codecs/png/palette-adam7.pam differ diff --git a/tests/fixtures/codecs/png/palette-adam7.png b/tests/fixtures/codecs/png/palette-adam7.png new file mode 100644 index 00000000..6f333e4d Binary files /dev/null and b/tests/fixtures/codecs/png/palette-adam7.png differ diff --git a/tests/fixtures/codecs/png/palette-trns.pam b/tests/fixtures/codecs/png/palette-trns.pam new file mode 100644 index 00000000..50bd0431 Binary files /dev/null and b/tests/fixtures/codecs/png/palette-trns.pam differ diff --git a/tests/fixtures/codecs/png/palette-trns.png b/tests/fixtures/codecs/png/palette-trns.png new file mode 100644 index 00000000..93807bb3 Binary files /dev/null and b/tests/fixtures/codecs/png/palette-trns.png differ diff --git a/tests/fixtures/codecs/png/palette4.pam b/tests/fixtures/codecs/png/palette4.pam new file mode 100644 index 00000000..0595ac69 Binary files /dev/null and b/tests/fixtures/codecs/png/palette4.pam differ diff --git a/tests/fixtures/codecs/png/palette4.png b/tests/fixtures/codecs/png/palette4.png new file mode 100644 index 00000000..38f588a6 Binary files /dev/null and b/tests/fixtures/codecs/png/palette4.png differ diff --git a/tests/fixtures/codecs/png/rgb16.png b/tests/fixtures/codecs/png/rgb16.png new file mode 100644 index 00000000..3b1269cc Binary files /dev/null and b/tests/fixtures/codecs/png/rgb16.png differ diff --git a/tests/fixtures/codecs/png/rgba.pam b/tests/fixtures/codecs/png/rgba.pam new file mode 100644 index 00000000..9b59332c Binary files /dev/null and b/tests/fixtures/codecs/png/rgba.pam differ diff --git a/tests/fixtures/codecs/png/rgba.png b/tests/fixtures/codecs/png/rgba.png new file mode 100644 index 00000000..2cf5a36f Binary files /dev/null and b/tests/fixtures/codecs/png/rgba.png differ diff --git a/tests/fixtures/codecs/src.ppm b/tests/fixtures/codecs/src.ppm new file mode 100644 index 00000000..d0c8f458 Binary files /dev/null and b/tests/fixtures/codecs/src.ppm differ diff --git a/tests/fixtures/codecs/src16.ppm b/tests/fixtures/codecs/src16.ppm new file mode 100644 index 00000000..bf7e7d73 Binary files /dev/null and b/tests/fixtures/codecs/src16.ppm differ diff --git a/tests/fixtures/codecs/tiff/deflate16-pred.tif b/tests/fixtures/codecs/tiff/deflate16-pred.tif new file mode 100644 index 00000000..03855b81 Binary files /dev/null and b/tests/fixtures/codecs/tiff/deflate16-pred.tif differ diff --git a/tests/fixtures/codecs/tiff/float32-pred3.tif b/tests/fixtures/codecs/tiff/float32-pred3.tif new file mode 100644 index 00000000..85ab48d6 Binary files /dev/null and b/tests/fixtures/codecs/tiff/float32-pred3.tif differ diff --git a/tests/fixtures/codecs/tiff/gray-miniswhite.tif b/tests/fixtures/codecs/tiff/gray-miniswhite.tif new file mode 100644 index 00000000..adb090f9 Binary files /dev/null and b/tests/fixtures/codecs/tiff/gray-miniswhite.tif differ diff --git a/tests/fixtures/codecs/tiff/jpeg-rgb.tif b/tests/fixtures/codecs/tiff/jpeg-rgb.tif new file mode 100644 index 00000000..36dc1b3a Binary files /dev/null and b/tests/fixtures/codecs/tiff/jpeg-rgb.tif differ diff --git a/tests/fixtures/codecs/tiff/jpeg-ycbcr.tif b/tests/fixtures/codecs/tiff/jpeg-ycbcr.tif new file mode 100644 index 00000000..389a331f Binary files /dev/null and b/tests/fixtures/codecs/tiff/jpeg-ycbcr.tif differ diff --git a/tests/fixtures/codecs/tiff/lzw-pred.tif b/tests/fixtures/codecs/tiff/lzw-pred.tif new file mode 100644 index 00000000..5825b301 Binary files /dev/null and b/tests/fixtures/codecs/tiff/lzw-pred.tif differ diff --git a/tests/fixtures/codecs/tiff/lzw-tiled.tif b/tests/fixtures/codecs/tiff/lzw-tiled.tif new file mode 100644 index 00000000..6a00cd59 Binary files /dev/null and b/tests/fixtures/codecs/tiff/lzw-tiled.tif differ diff --git a/tests/fixtures/codecs/tiff/none-strips.tif b/tests/fixtures/codecs/tiff/none-strips.tif new file mode 100644 index 00000000..557a5155 Binary files /dev/null and b/tests/fixtures/codecs/tiff/none-strips.tif differ diff --git a/tests/fixtures/codecs/tiff/packbits.tif b/tests/fixtures/codecs/tiff/packbits.tif new file mode 100644 index 00000000..ebd9b5eb Binary files /dev/null and b/tests/fixtures/codecs/tiff/packbits.tif differ diff --git a/tests/fixtures/codecs/tiff/planar-zip.tif b/tests/fixtures/codecs/tiff/planar-zip.tif new file mode 100644 index 00000000..15390b00 Binary files /dev/null and b/tests/fixtures/codecs/tiff/planar-zip.tif differ