Repository navigation
Tiff: streaming row reader for seekable streams; Read(Stream) reads by offset - #22
Merged
Merged
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TIFF streaming decode (unit 10):
TiffImageDecoderimplementsIStreamingImageDecoder, andTiffReader.Read(Stream)stops copying a seekable stream.What it does
TiffImageDecoder.CreateRowReader(TiffRowReader.cs, registered inRegistration). 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 forFrameCount, soInfoequalsTryReadInfo.DecodeBand+PixelConverter.ConvertRowthatTryDecodeIntouses, so the rows are byte-identical. Memory is one decoded band plus its coded bytes.UseBufferedFallback,IsStreaming == false, same rows) for: a non-seekable stream; a float page (TryDecodeIntohas 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/ReadPageare now generic overITiffBytes(newViewmember), so the span path runs the same code. The fuzz test checks that it produces the same document asRead(span).Array.MaxLength(both are no-ops for spans).Need from Abstractions (not changed here)
StreamInputcan only move backwards (Rewind). Forward jumps are done by reading through the bytes in between. Two consequences:A forward
Seek(orRewindto any offset ≤Length) on seekable streams would remove both. After that,ForwardBytesand the_skippedbudget can go.Tests (
TiffStreamingDecodeTests)TiffRobustnessTestsseed plus extra layouts (big-endian planar 16-bit tiles, reversed or shared strips, short strip list, BigTIFF single strip). Each is checked against 7PixelFormats viaRowsEqualDecodeInto: theWholefeed must stream, the 3 non-seekable feeds must fall back.InfoequalsTryReadInfo, multi-page reads page 0, a stream not at offset 0, and segments or IFD past the end throwInvalidDataException.Read(Stream): equalsRead(span).FileStreamwithReadRowsAsyncand matchesTryDecodeInto. A synchronous read allocates less than 4 MB.Mutations_stream_like_the_span_decoderchecks that streaming succeeds exactly whenTryDecodeIntodoes, with the same rows, and thatRead(Stream)matchesRead(span).Runs
REQUIRE_ORACLES=1, withDOTNET_EnableAVX2=0and withDOTNET_EnableHWIntrinsic=0. The 1 skip is the env-gated local corpus.TIFF_STREAM_FUZZ_ITERATIONS=300000 … -method …TiffStreamingDecodeTests.Mutations_stream_like_the_span_decoderandTIFF_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.
TryDecodeIntobaseRead(Stream)base → branchThe 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