Repository navigation
Bmp: streaming row reader and streaming encoder session - #19
Merged
Merged
Conversation
BmpImageDecoder implements IStreamingImageDecoder. Uncompressed bitmaps (BI_RGB, BI_BITFIELDS, BI_ALPHABITFIELDS, every depth and palette) stream to the 8-bit basic formats with the span decoder's row kernels: top-down from any stream; bottom-up from a seekable (or retained) input by one forward pass, then a seek back per ~256 KiB band of stored rows. A 32-bit BI_RGB bitmap read with alpha scans its fourth bytes in that forward pass (seekable only). RLE, Huffman 1D, BI_PNG, other formats or options, a pixel offset into the headers, and a V5 profile after the pixels fall back. Rows equal TryDecodeInto's, Info equals TryReadInfo's. BmpImageEncoder.Create returns a StreamingEncoderSession for top-down, explicit direct-colour, non-RLE options: headers at AddFrame, rows as they arrive, the profile at Commit; bytes equal the buffered session's. The benchmark gains a streaming-reader decode contestant, an encoder session contestant and two top-down encode cases. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ead-whole-stream path Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The per-row call cost ~25% on 256x256 icon images (BDN, 5800X). 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.
What
BMP gets true forward-only streaming read and streaming write.
Decode.
BmpImageDecodernow implementsIStreamingImageDecoderand setsRegistration.CreateRowReader. The reader isBmpRowReader : StreamingRowReader. It reuses the span decoder's row kernels (BmpPixels.PlanRows/DecodeRowwere factored out ofDecodeUncompressed), so its rows equalTryDecodeInto's byte for byte. ItsInfoequalsTryReadInfo's, and the header is parsed with the sameBmpHeader.Parsein a new prefix mode.retainConsumedinput)BI_RGB/BI_BITFIELDS/BI_ALPHABITFIELDS, every depth and palette), top-downBI_RGBread with alpha (the "alpha unless all 0" rule)AlphaOr, Vector with a byte-at-a-time head and tail)BI_PNG, non-basic-8 formats, non-full options, offBits into the headers or > 1 MiB, V5 profile after the pixelsThe bottom-up forward pass exists because
StreamInput.Rewindonly goes back. Reaching the last band means reading up to it, so the pixels are read twice. Abstractions need: a forward seek onStreamInput(aRewindthat accepts a position pastPositionwhen the stream can seek) would remove that pass for bottom-up files without the alpha scan.Encode.
BmpImageEncoder.Createreturns aStreamingEncoderSessionwhenTopDownis set,Formatis an explicit direct-colour layout (Rgb555,Rgb565,Rgb24,Rgb32,Argb32) andRleis off. The headers go out atAddFrame, rows go out as they are written, and the profile goes out atCommit. Header building was factored intoBmpEncoder.PlanFile/WriteHeaders, whichWritealso uses, so the bytes equal the buffered session's under any chunking or stride. Everything else keeps today's buffered session.Tests (
BmpStreamingTests)TryDecodeIntofor 13 layouts × top-down / bottom-up × 7 formats. Each case runs the seekableWholefeed (expects streaming) and the OneByte / RandomChunks / AsyncOnly feeds (expecttopDown && !alphaAmbiguous).Infomatches too.InvalidDataExceptionunder every feed.AlphaOris checked against a byte-at-a-time loop.MemoryStream. A streaming encode of the same size allocates < 4 MB.BmpEncoder.Encode(what the buffered session runs) for 6 layouts × 7 input formats, both whole and in random bands with padded strides.EncoderEmitsBeforeCommitpasses, and the non-streaming options reportIsStreaming == false.FileStreamstreams, and from an async-onlyChunkedStreamit falls back. Both give the same rows asTryDecodeInto. A top-down file round-trips through the streaming encoder and aFileStreamreader.BMP_STREAM_FUZZ_ITERATIONS=300000 … -method Sdcb.SimdCodecs.Tests.BmpStreamingTests.Nothing_but_invalid_data_from_malformed_streamspasses. It checks that streaming andTryDecodeIntoagree on success and rows, and that nothing butInvalidDataExceptionis thrown.BMP_FUZZ_ITERATIONS=300000 … BmpRobustnessalso passes.REQUIRE_ORACLES=1, and also withDOTNET_EnableAVX2=0andDOTNET_EnableHWIntrinsic=0. No new warnings.AlphaOroffset makes the tests fail.Benchmark (AMD Ryzen 7 5800X, Windows 11 26200, .NET 10.0.15, AVX2, 1920×1080, shared machine)
New contestants:
current rows ← stream(the reader over aMemoryStream, 64 rows per call),current ← read whole stream(read the stream into a buffer, thenTryDecodeInto), andcurrent session → writer. New encode cases:rgb24-topdownandargb32-topdown. The run is recorded inresults/streaming-2026-11-23-amd-ryzen-7-5800x.md.DecodeRowis nowAggressiveInlining. BDN after the fix: 7.95 µs vs 7.87 µs.Encode(…, Stream)on the top-down cases: 0.69 to 0.90×.Notes for the coordinator
BMP_STREAM_FUZZ_ITERATIONS=300000 … --filter "FullyQualifiedName~BmpStreaming".StreamInput(see above).🤖 Generated with Claude Code