Repository navigation
feat(image,stream): zero-copy image adopt-ctor + to_image bridge (Phase 2 PR1) - #516
Merged
Merged
Conversation
…ge (Phase 2 PR1) Foundational H2 piece for the Phase-2 streaming sinks (roadmap STATE_BINARY_STREAMING.md). Lets a stream video frame become a cvc::image with no copy, so a frame's bytes can flow into a GL texture (PR2) zero-copy. cvc::image: - New adopt constructor image(w,h,f,dt,bytes,keepalive) that ALIASES foreign interleaved bytes with no alloc/memcpy. The shared_ptr<void> keepalive is bridged into the boost::shared_array<unsigned char> buffer via a no-op deleter that captures it, so the foreign storage lives exactly as long as the image and any copy-on-write copy / storage() handle. - detach() now also forks an adopted buffer on first mutation (an adopted buffer has use_count()==1 — its keepalive lives inside the deleter — so the plain use_count()>1 test alone would hand a writable pointer into immutable, shared frame bytes). New _adopted flag drives this. storage() documented read-only for adopted images. cvc::stream: - format_desc gains a frame_origin (top_left default / bottom_left) so the bridge can reject un-aliasable row order. - to_image(frame_ptr) aliases a video_raw frame as a cvc::image, co-owning the frame keepalive. Rejects (throws) what it cannot alias losslessly: null/non- video frame, unknown or channel-swapped codec (only matching-order codecs map; never a silent reinterpret), padded row stride (stride==0 treated as dense), bottom-left origin, or a buffer smaller than w*h*bpp. Tests: stream_to_image_test — adopt aliases without copy, mutation forks leaving source untouched, keepalive released exactly when the last alias drops; to_image codec/stride/origin/size/kind gates + pixel round-trip. 12/12 pass, valgrind memcheck clean. Pure cvc::image + cvc::stream; no GL/Python/hardware.
…tests Adversarial review of PR1 (verdict SHIP): fold in the one MED gap + the LOW test-coverage notes. - MED: to_image() now throws on a frame with null data (previously a malformed frame with data==null but a large claimed size passed every check and the adopt ctor's own `bytes` guard silently produced an EMPTY image, violating the "throws on anything it cannot alias losslessly" contract). - Tests: RejectsNullData; AliasesNonRgbaMatchingCodecs (rgb8/gray8/graya8 map + alias); CopyDivergesOnMutationLeavingSibling (b=a; mutate a; assert b still aliases the original source bytes, then b forks on its own mutation). 15/15 pass.
This was referenced Oct 1, 2026
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.
Foundational H2 piece for the Phase-2 streaming sinks (roadmap
STATE_BINARY_STREAMING.md). Lets a stream video frame become acvc::imagewith no copy, so frame bytes can flow into a GL texture (PR2) zero-copy.cvc::image
image(w,h,f,dt,bytes,keepalive)— ALIASES foreign interleaved bytes (no alloc/memcpy). Theshared_ptr<void>keepalive is bridged into theboost::shared_array<unsigned char>buffer via a no-op deleter that captures it, so foreign storage lives exactly as long as the image + any COW copy /storage()handle.detach()now also forks an adopted buffer on first mutation (adopted buffers haveuse_count()==1— the keepalive lives inside the deleter — so the plainuse_count()>1test alone would hand a writable pointer into immutable shared frame bytes). New_adoptedflag;storage()documented read-only for adopted images.cvc::stream
format_descgainsframe_origin(top_left default / bottom_left).to_image(frame_ptr)aliases avideo_rawframe as acvc::image, co-owning the frame keepalive; throws on anything it can't alias losslessly: null/non-video, unknown or channel-swapped codec (matching-order only — never a silent reinterpret), padded stride (stride==0= dense), bottom-left origin, or short buffer.Tests:
stream_to_image_test— alias-without-copy, mutation-forks-leaving-source-untouched, keepalive-released-exactly-when-last-alias-drops;to_imagecodec/stride/origin/size/kind gates + pixel round-trip. 12/12 pass, valgrind memcheck clean. Purecvc::image+cvc::stream; no GL/Python/hardware.First of the 6 Phase-2 PRs; folds in the planning-critique MED/LOW findings for
to_image(origin-aware,stride==0-dense, strict codec map) and the mandatory adopted-buffer copy-on-write. Flags vs the roadmap: theconst void*ctor copied (no adopt ctor existed); line-refs in the roadmap were stale.