Repository navigation
QOI: streaming row reader and streaming encoder session - #14
Merged
Merged
Conversation
QoiImageDecoder implements IStreamingImageDecoder: CreateRowReader decodes a forward-only stream a row at a time (sync and async). A row decodes on a copy of the reader state and is kept only when all its chunks are buffered; a failed attempt waits for twice the bytes (capped at 5 bytes a pixel), so a byte-at-a-time stream costs a few decodes per row. Non-full-image options take the buffered fallback. Rows come from the same per-row step as TryDecodeInto (QoiDecoder.TryDecodeRow), so they are the same bytes for every PixelFormat. QoiImageEncoder.Create returns a StreamingEncoderSession: header at AddFrame, each band encoded and written as it arrives with the encoder state (index, previous pixel, open run) carried across bands, end marker at Commit. Bytes equal QoiEncoder.Encode for any banding (shared WriteHeader/WriteRow/WriteEnd). Tests: QoiStreamingTests (rows vs TryDecodeInto over every feed and format, ImageMagick-written and hand-assembled streams, every cut, banded encode bytes, emits before commit, 8192x4096 decode/encode under 1 MiB, async-only pipe into the encoder) and QoiStreamingRobustnessTests (QOI_STREAM_FUZZ_ITERATIONS). The QoiPnm benchmark gains a QOI `streaming` contestant. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The 2.0 TryDecodeInto takes options and a token; the four-parameter lookup found nothing, so decode had no baseline column. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
TryRead and Read share one loop instantiated over a struct marker; returning false from the end checks in the shared loop cost the span decode about 8%. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d two rows The throwing instantiation is the pre-streaming loop; TryRead marks running out with position -1. The row reader asks for more input while fewer than twice the previous row's bytes are buffered, so it seldom decodes a partial row twice. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
QOI gets true forward-only streaming read and streaming write on top of the streaming foundation (84b58f2). Abstractions, the facade and AGENTS.md are untouched.
Decoder
QoiImageDecoderimplementsIStreamingImageDecoder;Registration.CreateRowReader = CreateRowReader(concrete method group).InvalidDataException(avoids the base'scheckedrow-size overflow on hostile widths).QoiDecoder.TryDecodeRow(now also the span decoder's per-row step, so the rows are the same bytes for everyPixelFormat) runs on a copy of the reader state over the buffered window, and the copy is kept only when the whole row decoded. A failed attempt waits for twice the bytes (capped at 5 bytes a pixel, the most a row can take), so a byte-at-a-time stream costs a few decodes of each row. Before a row it asks for more input while fewer than twice the previous row's bytes are buffered.QoiCodec.Reader:Read(throws) andTryRead(returns false) share one loop instantiated over a struct marker (typeoftest, Mono-AOT-safe). The throwing instantiation is the old loop: void return, cold throw blocks. A shared bool-returning loop cost the span decode 4-8% (measured), which is why it is shaped like this.UseBufferedFallback(IsStreaming == false).Encoder
QoiImageEncoder.Createreturns aStreamingEncoderSession.AddFramewrites the header, eachWriteRowsband is encoded and written as it arrives with the encoder state (index, previous pixel, open run) carried across bands, andCommitwrites the end marker.QoiEncoderis split intoWriteHeader/WriteRow/WriteEnd, shared with the whole-image path, so the bytes are identical for any banding.Tests
QoiStreamingTests: rows equalTryDecodeIntofor 9 widths (1..1100) × alpha/opaque × 10 pixel formats under every feed; ImageMagick-written and hand-assembled streams; runs across rows and a run past the last pixel; fallback options; every cut of a file fails exactly where the span decoder fails (one-byte, random, async-only feeds); malformed headers; read-ahead bound; streaming-encode bytes equalQoiEncoder.Encodefor 5 sizes × 5 formats × sRGB/linear × 6 bandings/strides;EncoderEmitsBeforeCommit; 8192×4096 encode and decode each allocate < 1 MiB; an async-onlyChunkedStreampiped throughReadRowsAsyncinto the streaming encoder gives back the source file byte for byte.QoiStreamingRobustnessTests(new soak variable): mutated files fed in random pieces, sync and async-only, give exactlyTryDecodeInto's rows or throwInvalidDataExceptionexactly when it returns false.Soaks (all pass):
For AGENTS.md (I did not edit it): please add the
QOI_STREAM_FUZZ_ITERATIONSsoak next to the QOI/Netpbm one.Validation: full suite 7054 tests, 0 failures (559 oracle skips, the same as main). The QOI classes pass under
DOTNET_EnableAVX2=0andDOTNET_EnableHWIntrinsic=0.SimdCodecs.slnxbuilds with 0 warnings. The wasm smoke test does not exercise QOI, so I did not run it.Benchmark
QoiPnm.Benchmarksgains a QOIstreamingcontestant (row reader over aMemoryStream, 16 rows a call / session in 16-row bands). This PR also fixes the baseline's decode binding: 2.0'sTryDecodeIntotakes 6 parameters, so the old 4-parameter lookup found nothing and decode had no baseline column.baseline 533b03e+matrix all --cases qoi --rounds 31, run from a detached worktree outside the repo. An A/A run (baseline = HEAD) measured 1.00×.Machine: AMD Ryzen 7 5800X, Windows 10.0.26200 x64, .NET 10.0.15, AVX2 (no AVX-512), 1920×1080 synthetic sources. Other jobs were running on the machine, so run-to-run spread was several percent.
PerRow) is as slow as the streaming reader, and copying from the stream costs under 2%. Batching tight RGBA / BGRA rows took streaming decode from 1.15× to 1.08×.🤖 Generated with Claude Code