Repository navigation
Pnm: streaming row reader (P1–P7) and streaming encoder session - #13
Merged
Merged
Conversation
PnmImageDecoder implements IStreamingImageDecoder. The header is scanned as it arrives (resumable, at most 1 MiB) and then parsed by PnmHeader.Read as from the whole file; raw rows decode once their bytes are buffered, through the same chunk kernels as TryDecodeInto; plain rasters are read token by token with the number / comment state carried across reads. Non-full options take the buffered fallback. Rows equal TryDecodeInto's, with the same acceptance (trailing data ignored, short rasters refused). PnmImageEncoder.Create now returns a StreamingEncoderSession for every option: the header at AddFrame, each band's raster as it is written; the bytes equal the whole-image encode for any split. Tests: PnmStreamingTests (every variant x PixelFormat x feed, Magick.NET files, truncations, malformed input, memory bounds, encoder bytes, emits-before-commit, async end to end) and PnmStreamingRobustnessTests (PNM_STREAM_FUZZ_ITERATIONS). QoiPnm benchmark: row reader and streaming session contestants. 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.
What
PnmImageDecoderimplementsIStreamingImageDecoder(Registration.CreateRowReaderset by method group). It covers P1–P7: plain and raw, 8 and 16 bits, everyPixelFormat.PnmHeader.Readparses the header exactly as it does from the whole file. Streaming caps the header at 1 MiB, comments included; a longer header throwsInvalidDataException.Source8/ chunk kernels asTryDecodeInto(newPnmPixels.DecodeRawRow). 16-bit samples into a 16-bit layout go through native samples andConvertRow, as the span path's fallback does.InvalidDataException, and a trailing number at end of stream is accepted.UseBufferedFallback. PFM has no streaming reader: it is a separate registration, so the facade buffers it.PnmImageEncoder.Createalways returns aStreamingEncoderSession. The header is written atAddFrameand each band's raster as it is written. The bytes equal the whole-image encode for any split, because every Netpbm row is encoded on its own. A consequence: an option the input cannot be written with now throws atAddFrameinstead of atCommit.PnmImageDecoder.CreateRowReader(inPublicAPI.Unshipped.txt). The README has a new Streaming section.Tests
PnmStreamingTests:TryDecodeIntofor every P1–P7 variant at maxima 255 / 7 / 300 / 1000 / 4095 / 65535, for 8- and 16-bit input, acrossPixelFormat.Alland everyStreamingAssertfeed.InvalidDataException.InvalidDataException, notOverflowException.PixelFormatwith random strided bands.EncoderEmitsBeforeCommitpasses.ChunkedStreamis decoded withReadRowsAsyncand piped into the streaming encoder, which reproduces the source file byte for byte (P6, 16-bit P5, P3).PnmStreamingRobustnessTestsadds a streaming fuzz with its own soak variable. For each mutation, the streamed rows must equal the span decoder's, or the stream must throwInvalidDataExceptionwhere the span decoder refuses.Runs on an AMD Ryzen 7 5800X, Windows 11, .NET 10.0.15:
DOTNET_EnableAVX2=0andDOTNET_EnableHWIntrinsic=0: all 164 PNM tests pass.PNM_STREAM_FUZZ_ITERATIONS=300000 QOI_FUZZ_ITERATIONS=300000 PNM_FUZZ_ITERATIONS=300000 … --filter "FullyQualifiedName~PnmStreamingRobustness|FullyQualifiedName~QoiPnmRobustness"passes.Please add to AGENTS.md (I did not edit it): after touching the PNM streaming reader, soak
PNM_STREAM_FUZZ_ITERATIONS=300000 … --filter "FullyQualifiedName~PnmStreamingRobustness".Benchmark
QoiPnm
matrixagainst a freshly builtbaseline 533b03e, run from a detached worktree outside the repo. Same machine, 1920×1080, medians of 15 interleaved rounds, ms. New contestants: "PNM row reader" (CreateRowReaderover aMemoryStream, 16 rows per call, into RGBA) and "PNM streaming session" (16-row bands into a reusedArrayBufferWriter).TryDecodeIntoThe streaming decode does not meet the 1.05× target on the raw formats that are pure copies. The added cost is one copy of the file from the
Streaminto theStreamInputbuffer, which a span decode of bytes already in memory skips. About 0.18 ms of the PAM case is that copy (8 MB at memory bandwidth). PBM, PGM and plain rasters are within 1.05–1.08×.Encode→ streamThe session matches the
Streamoverload, and is faster for 16-bit because it skips the whole-image conversion buffer.Regression check:
currentagainst baseline (a new array per call) is within run-to-run noise. Over 31 rounds: photo-ppm 0.755 vs 0.719, screen-pgm 0.277 vs 0.270, screen-pam 0.807 vs 0.819.🤖 Generated with Claude Code