Repository navigation
ImageDecoder: true streaming Open/OpenAsync(Stream), DecodeAsync/CopyPixelsAsync - #15
Merged
Merged
Conversation
…frame 0 Open(Stream) no longer copies the stream whole. It sniffs 4 KiB, reads the header through the codec's row reader (ImageDecoderRegistration.CreateRowReader) and, for a still file, decodes frame 0's first full-size Decode / CopyPixels straight from the stream. Every other operation reads the whole file first (seeking, or replaying the bytes a non-seekable stream kept) and takes the span path, so results equal Open(byte[]). Codecs without a row reader are read whole at open, as before. - Open(Stream, leaveOpen, forwardOnly) / OpenAsync(..., forwardOnly, ct): forwardOnly keeps nothing a streamed decode reads; later revisits throw InvalidOperationException. - OpenAsync reads the header asynchronously, then prefetches the rest in the background. New ImageFrame.DecodeAsync / CopyPixelsAsync stream alongside the prefetch; sync Decode / CopyPixels wait for it and never read synchronously. - StreamTap: one hold on the stream, by offset, with a recorded prefix/whole for replay; prefetch and readers serialised by a semaphore. - Tests: ImageDecoderStreamingTests (RawStreamCodec + real codecs); the AOT and wasm smoke tests cover the new entry points. 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.
Summary
ImageDecoder.Open(Stream)/OpenAsync(Stream)no longer copy the stream whole first (unit 12 of the streaming work).ImageDecoderRegistration.CreateRowReader).Formatand frame 0'sInfoare available right away.Decode/CopyPixelsof frame 0 reads its rows straight from the stream.Decodebuilds aRasterImagein the codec's own layout (PixelFormat.ForChannels(info.Channels, info.SampleFormat), plus the header's bit depth and colour encoding); a test checks that this equalsTryDecodefor PNG and JPEG.Open(byte[]). A seekable stream gets the file by seeking; a non-seekable one replays the bytes it kept as it read them.Open(Stream, leaveOpen, forwardOnly)/OpenAsync(Stream, leaveOpen, forwardOnly, ct). WithforwardOnly, the bytes a streamed decode reads are not kept; a later revisit throwsInvalidOperationException.OpenAsyncreads the header asynchronously, then prefetches the rest of a non-seekable stream in the background. NewImageFrame.DecodeAsync/CopyPixelsAsyncstream truly, starting from wherever the prefetch has got to. SyncDecode/CopyPixelsblock on the asynchronous reads and never read the stream synchronously (Kestrel-safe), and their token cancels that wait. Dispose cancels the prefetch; withleaveOpenit also waits for the cancelled read to finish before returning the stream to the caller.StreamTap(new, internal) is the session's only handle on the stream. It reads by offset and keeps the bytes it has read for replay. ASemaphoreSlimlets one stream read run at a time, and a lock guards the kept bytes and counters.OpenAsyncreads the whole stream at open, because a synchronous call cannot block there.Tests
tests/Sdcb.SimdCodecs.Tests/Streaming/ImageDecoderStreamingTests.cscovers: every operation sequence equalsOpen(byte[])over a 1-byte stream, random chunks,MemoryStreamandFileStream, for both the streaming and the fallback variant ofRawStreamCodec; real PNG/JPEG on every source, with and without forwardOnly; a request body (async-onlyChunkedStream) withDecodeAsyncand with legacyDecode; prefetch racingDecodeAsync; dispose during the prefetch; cancellation (async, and sync-over-async); truncated streams throwInvalidDataException; leaveOpen; pooled decode.AllocatesLessThanchecks that seekable and forward-onlyOpen+CopyPixelsof a 4 MB image allocate less than 1/8 of the file.DOTNET_EnableAVX2=0andDOTNET_EnableHWIntrinsic=0.OpenAsync+DecodeAsync,CopyPixelsAsyncand forward-onlyOpen+CopyPixels. NativeAOT and Mono AOT (browser-wasm) output is identical to the JIT's.Notes for the coordinator
PublicAPI.Unshipped.txt: 4 contiguous lines appended, so a line conflict with PR Facade: OpenRowReader(Async) and CreateEncoder(Stream) streaming entry points #12 is likely.ImageCodecs*.cs: the codec sniff is private (ImageDecoder.Probe). When two registrations share a format (GainMap before plain JPEG), the session does not stream and reads the whole file.ImageFrame.Infofor frame 0 of a stream session comes from the header reader, which may not see values stored after the pixels (for example a trailing eXIf orientation). Region / scale / orientation copies size the destination from the whole file's frame info. A plain full-size copy uses the header's size, which is all a streamed decode can know. This is documented onImageFrame.Info.UseBufferedFallback,Openlets the row reader decode the file whole and then discards that result before the span path decodes it again. This costs double for unstreamable variants (progressive JPEG, Adam7) once codecs add row readers. Avoiding it needs an Abstractions hook (header-only initialise) that this unit may not add.Decodeputs only the metadata found before the pixels into the image.🤖 Generated with Claude Code