Skip to content

Tiff: streaming row reader for seekable streams; Read(Stream) reads by offset - #22

Merged
sdcb merged 1 commit into
mainfrom
streaming/tiff-decode
Nov 23, 2026
Merged

sdcb merged 1 commit into
mainfrom
streaming/tiff-decode

Conversation

@sdcb

@sdcb sdcb commented Nov 23, 2026

Copy link
Copy Markdown
Owner

TIFF streaming decode (unit 10): TiffImageDecoder implements IStreamingImageDecoder, and TiffReader.Read(Stream) stops copying a seekable stream.

What it does

  • TiffImageDecoder.CreateRowReader (TiffRowReader.cs, registered in Registration). On a seekable stream it reads the header and the first page's directory by offset. It does this by rerunning the span parser (ReadLayoutCore) over the ranges fetched so far, with 4 KB read-ahead and a cap on fetches and bytes. It then walks the IFD chain's entry counts and next pointers for FrameCount, so Info equals TryReadInfo.
  • Rows come one strip, or one row of tiles, at a time. For a planar page that includes every plane's segments for the band. Each band goes through the same DecodeBand + PixelConverter.ConvertRow that TryDecodeInto uses, so the rows are byte-identical. Memory is one decoded band plus its coded bytes.
  • Falls back (UseBufferedFallback, IsStreaming == false, same rows) for: a non-seekable stream; a float page (TryDecodeInto has no integer projection for it, so it returns false either way); any option other than the full image; and a file where streaming would read through more than 2× the file plus 1 MB. The last case covers libtiff's separate planes stored one after another, and IFD chains or directories that jump back and forth.
  • TiffReader.Read(Stream): a seekable stream is read by offset from its current position, and the stream is left at its end even on failure. ReadCore/ReadPage are now generic over ITiffBytes (new View member), so the span path runs the same code. The fuzz test checks that it produces the same document as Read(span).
  • Hardening needed now that streams over 2 GB reach the parser: tag values and the inflate source are bounded by Array.MaxLength (both are no-ops for spans).

Need from Abstractions (not changed here)

StreamInput can only move backwards (Rewind). Forward jumps are done by reading through the bytes in between. Two consequences:

  • When libtiff puts the directory after the pixels, the header reads the whole file once before seeking back to strip 0.
  • Large planar files take the buffered fallback.

A forward Seek (or Rewind to any offset ≤ Length) on seekable streams would remove both. After that, ForwardBytes and the _skipped budget can go.

Tests (TiffStreamingDecodeTests)

  • Layout coverage: every TiffRobustnessTests seed plus extra layouts (big-endian planar 16-bit tiles, reversed or shared strips, short strip list, BigTIFF single strip). Each is checked against 7 PixelFormats via RowsEqualDecodeInto: the Whole feed must stream, the 3 non-seekable feeds must fall back.
  • Seekable chunked feed: a seekable stream with 1..997-byte reads, random bands of 1..17 rows, sync and async, and a 16-byte first buffer.
  • Correctness checks: fallback options, Info equals TryReadInfo, multi-page reads page 0, a stream not at offset 0, and segments or IFD past the end throw InvalidDataException.
  • Fallback triggers: large planar file falls back; a back-and-forth IFD chain is read whole.
  • Read(Stream): equals Read(span).
  • E2E: an 8192×4096 Magick.NET (libtiff) TIFF, zip and uncompressed, with its IFD after the pixels, is read from a FileStream with ReadRowsAsync and matches TryDecodeInto. A synchronous read allocates less than 4 MB.
  • Deliberate-bug checks: I broke the band-row offset and each fallback guard in turn; the tests caught every break.
  • Fuzz: Mutations_stream_like_the_span_decoder checks that streaming succeeds exactly when TryDecodeInto does, with the same rows, and that Read(Stream) matches Read(span).

Runs

  • Build: 0 warnings, 0 errors.
  • Tiff classes (602 tests) pass with REQUIRE_ORACLES=1, with DOTNET_EnableAVX2=0 and with DOTNET_EnableHWIntrinsic=0. The 1 skip is the env-gated local corpus.
  • Full suite: 7052 tests, 0 failed.
  • Soaks passed: TIFF_STREAM_FUZZ_ITERATIONS=300000 … -method …TiffStreamingDecodeTests.Mutations_stream_like_the_span_decoder and TIFF_FUZZ_ITERATIONS=300000 … -class …TiffRobustnessTests.

Timing

Machine: AMD Ryzen 7 5800X, 128 GB RAM, Windows 11, .NET 10.0.15. Throwaway Stopwatch console outside the repo, best of 7, run against a fresh 533b03e baseline build and against this branch in the same session. Input: 8192×4096 RGB, 16 rows per strip, predictor 2 on compressed, decoded to Rgba32.

file span TryDecodeInto base span branch streaming from FileStream (alloc) Read(Stream) base → branch
zip 64.7 MB 415.9 / 416.5 ms 414.4 ms 432.9 ms (0.81 MB) 386 → 381 ms
none 100.7 MB 56.7 / 56.4 ms 56.9 ms 82.2 ms (1.12 MB) 35 → 20 ms
lzw 75.9 MB 758.0 / 756.2 ms 751.6 ms 773.7 ms (5.06 MB) 729 → 721 ms

The span path does not regress. The streaming time includes the file I/O, plus one read-through of the file to reach the trailing IFD.

🤖 Generated with Claude Code

…y offset

TiffImageDecoder implements IStreamingImageDecoder. On a seekable stream the
reader parses the first page's directory by offset (the span parser, rerun over
the ranges fetched so far), walks the IFD chain for FrameCount, then fetches one
strip or row of tiles at a time (every plane's segments) and decodes it with the
same DecodeBand / ConvertRow path as TryDecodeInto. Rewinds seek; forward moves
read through, so files that would read more than twice their length (planes
stored far apart, chains jumping back and forth) are decoded whole instead, as
are non-seekable streams, float pages and non-full-image options.

TiffReader.Read(Stream) on a seekable stream now reads the header, directories
and segments by offset instead of copying the stream; ReadPage/ReadCore are
generic over ITiffBytes, so the span path is the same code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sdcb
sdcb merged commit e856bf0 into main Nov 23, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant