using System.IO.Compression; namespace Titano.Core.Codecs.Tiff; /// /// Strip/tile decompressors. Each fills exactly output.Length bytes when the data allows and returns the number /// of bytes produced; damaged data ends early instead of throwing out of range. /// internal static class TiffDecompressors { /// TIFF LZW (MSB-first codes, "early change"). Old-style streams (pre-6.0 libtiff, LSB-first codes, /// no early change) are recognised by their first byte as libtiff does. public static int Lzw(ReadOnlySpan input, Span output) { const int clear = 256, eoi = 257, maxCodes = 4096; var oldStyle = input.Length >= 2 && input[0] == 0 && (input[1] & 1) != 0; Span prefix = stackalloc ushort[maxCodes]; Span suffix = stackalloc byte[maxCodes]; Span first = stackalloc byte[maxCodes]; Span length = stackalloc ushort[maxCodes]; for (var i = 0; i < 256; i++) { suffix[i] = first[i] = (byte)i; length[i] = 1; } int next = 258, width = 9, old = -1, written = 0, inPos = 0, bits = 0; uint acc = 0; var early = oldStyle ? 0 : 1; while (written < output.Length) { while (bits < width && inPos < input.Length) { if (oldStyle) acc |= (uint)input[inPos++] << bits; else acc = acc << 8 | input[inPos++]; bits += 8; } if (bits < width) break; int code; if (oldStyle) { code = (int)(acc & ((1u << width) - 1)); acc >>= width; } else { code = (int)(acc >> (bits - width)) & ((1 << width) - 1); } bits -= width; if (code == eoi) break; if (code == clear) { next = 258; width = 9; old = -1; continue; } if (old < 0) { if (code > 255) break; // the first code after a clear must be a literal } else { if (code > next || next >= maxCodes && code == next) break; // corrupt: code beyond the table if (next < maxCodes) { // New entry: old string + first byte of the current string (KwKwK when code == next). prefix[next] = (ushort)old; suffix[next] = code < next ? first[code] : first[old]; first[next] = first[old]; length[next] = (ushort)Math.Min(ushort.MaxValue, length[old] + 1); next++; } } // Strings are prefix chains: write back to front, clipped to the output. var len = length[code]; var end = Math.Min(written + len, output.Length); var c = code; for (var p = written + len - 1; p >= written; p--) { if (p < end) output[p] = suffix[c]; c = prefix[c]; } written = end; old = code; if (next + early >= 1 << width && width < 12) width++; } return written; } /// Macintosh PackBits run-length decoding. public static int PackBits(ReadOnlySpan input, Span output) { int i = 0, o = 0; while (i < input.Length && o < output.Length) { var n = (sbyte)input[i++]; if (n >= 0) { var count = Math.Min(n + 1, Math.Min(input.Length - i, output.Length - o)); input.Slice(i, count).CopyTo(output[o..]); i += n + 1; o += count; } else if (n != -128) { if (i >= input.Length) break; var count = Math.Min(1 - n, output.Length - o); output.Slice(o, count).Fill(input[i++]); o += count; } } return o; } /// zlib (Adobe Deflate, TIFF compression 8/32946). public static int Deflate(byte[] input, Span output) { using var z = new ZLibStream(new MemoryStream(input, writable: false), CompressionMode.Decompress); return z.ReadAtLeast(output, output.Length, throwOnEndOfStream: false); } }