Files
Chronos/src/Titano.Core/Codecs/Tiff/TiffDecompressors.cs
T
Claude 6d9ede0c47 feat(codecs): add TIFF raster reader and TIFF writer
TiffImageReader decodes strips and tiles (chunky or planar) with none,
LZW (old-style bit order tolerated), Deflate, PackBits and JPEG
compression (JPEGTables, YCbCr or RGB, lossless SOF3 tiles reshaped as
DNG writes them), predictors 2/3 and the DNG x2/x4 variants, 1-16 bit
integer and 16/32-bit float samples; tiles decode in parallel. Output
is a raw TiffRaster or a linear ImageBuffer. JPEG strips match libtiff
bit for bit.

TiffWriter writes 8/16-bit gray/RGB/RGBA, uncompressed or Deflate with
horizontal predictor, and embeds capture metadata from an Exif block.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 10:02:27 +00:00

122 lines
4.4 KiB
C#

using System.IO.Compression;
namespace Titano.Core.Codecs.Tiff;
/// <summary>
/// Strip/tile decompressors. Each fills exactly <c>output.Length</c> bytes when the data allows and returns the number
/// of bytes produced; damaged data ends early instead of throwing out of range.
/// </summary>
internal static class TiffDecompressors
{
/// <summary>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.</summary>
public static int Lzw(ReadOnlySpan<byte> input, Span<byte> output)
{
const int clear = 256, eoi = 257, maxCodes = 4096;
var oldStyle = input.Length >= 2 && input[0] == 0 && (input[1] & 1) != 0;
Span<ushort> prefix = stackalloc ushort[maxCodes];
Span<byte> suffix = stackalloc byte[maxCodes];
Span<byte> first = stackalloc byte[maxCodes];
Span<ushort> 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;
}
/// <summary>Macintosh PackBits run-length decoding.</summary>
public static int PackBits(ReadOnlySpan<byte> input, Span<byte> 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;
}
/// <summary>zlib (Adobe Deflate, TIFF compression 8/32946).</summary>
public static int Deflate(byte[] input, Span<byte> output)
{
using var z = new ZLibStream(new MemoryStream(input, writable: false), CompressionMode.Decompress);
return z.ReadAtLeast(output, output.Length, throwOnEndOfStream: false);
}
}