Skip to content

Add row-stride overloads for memory APIs#3066

Merged
JimBobSquarePants merged 3 commits into
mainfrom
js/expose-buffer-helpers
Mar 3, 2026
Merged

Add row-stride overloads for memory APIs#3066
JimBobSquarePants merged 3 commits into
mainfrom
js/expose-buffer-helpers

Conversation

@JimBobSquarePants
Copy link
Copy Markdown
Member

@JimBobSquarePants JimBobSquarePants commented Mar 3, 2026

Prerequisites

  • I have written a descriptive pull-request title
  • I have verified that there are no overlapping pull-requests open
  • I have verified that I am following the existing coding patterns and practice as demonstrated in the repository. These follow strict Stylecop rules 👮.
  • I have provided test coverage for my change (where applicable)

Description

See #2821 and #2932

This pull request adds support for loading and working with images that use a custom row stride, allowing more flexible memory layouts and compatibility with strided pixel buffers. It introduces new overloads for loading pixel data with row stride parameters, updates constructors and methods to handle row stride, and refactors internal buffer handling to consistently use the new stride-aware logic.

Strided Pixel Data Loading and API Extensions

  • Added new overloads to Image.LoadPixelData and ImageFrame.LoadPixelData for loading pixel data from both ReadOnlySpan<TPixel> and ReadOnlySpan<byte> with explicit row stride parameters, including comprehensive documentation and validation. [1] [2] [3] [4] [5] [6]
  • Updated constructors in ImageFrame<TPixel> and ImageFrameCollection<TPixel> to accept and propagate row stride, ensuring correct buffer wrapping and initialization for strided data. [1] [2] [3]

Buffer and Memory Handling Refactor

  • Refactored pixel buffer copying and access methods to use the new stride-aware Buffer2D<TPixel> logic, replacing previous contiguous memory group operations. This includes changes to pixel copying, frame cloning, and pixel data export methods. [1] [2] [3] [4] [5]

Validation and Error Handling

  • Strengthened validation for row stride, pixel size, and buffer length in all relevant loading methods, providing clear exceptions for invalid input and preventing buffer overruns. [1] [2] [3] [4]

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends ImageSharp’s memory APIs to support strided (row-padded) pixel buffers by introducing row-stride overloads for loading/wrapping pixel data and refactoring internal copy/clone logic to be stride-aware via Buffer2D<T>.RowStride.

Changes:

  • Added stride-aware overloads for Image.LoadPixelData, ImageFrame.LoadPixelData, and Image.WrapMemory (pixels + bytes), including validation and updated docs.
  • Introduced Buffer2D<T>.RowStride plus new copy/wrap helpers to correctly handle strided layouts across processors and frame/image APIs.
  • Updated/added tests to cover stride behavior (copy semantics, wrap semantics, swap-or-copy behavior) and adjusted allocator validation expectations.

Reviewed changes

Copilot reviewed 26 out of 26 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tests/ImageSharp.Tests/Memory/DiscontiguousBuffers/MemoryGroupTests.cs Removes older transform-related tests no longer applicable after refactor.
tests/ImageSharp.Tests/Memory/DiscontiguousBuffers/MemoryGroupTests.CopyTo.cs Adds stride-aware MemoryGroup.CopyTo tests ensuring trailing padding isn’t required.
tests/ImageSharp.Tests/Memory/Buffer2DTests.cs Updates allocator behavior expectations + splits invalid vs overflow dimensions tests.
tests/ImageSharp.Tests/Memory/Buffer2DTests.WrapMemory.cs New tests for Buffer2D<T>.WrapMemory (packed/strided) and single-memory behavior.
tests/ImageSharp.Tests/Memory/Buffer2DTests.SwapOrCopyContent.cs Adds coverage for swap/copy behavior with differing layouts and totals.
tests/ImageSharp.Tests/Memory/Buffer2DTests.CopyTo.cs New tests for Buffer2D.CopyTo(Span) behavior with strided vs packed sources.
tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs Adds tests for Image.WrapMemory with strided pixel/byte buffers and copy/export behavior.
tests/ImageSharp.Tests/Image/ImageTests.LoadPixelData.cs Adds tests for new LoadPixelData stride overloads + invalid stride/length cases.
tests/ImageSharp.Tests/Image/ImageFrameCollectionTests.Generic.cs Updates expected exception message type formatting (long vs int).
src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs Uses stride-aware PixelBuffer.CopyTo instead of memory-group copy.
src/ImageSharp/Processing/Processors/Transforms/Linear/RotateProcessor{TPixel}.cs Uses stride-aware PixelBuffer.CopyTo instead of memory-group copy.
src/ImageSharp/Processing/Processors/Transforms/CropProcessor{TPixel}.cs Uses stride-aware PixelBuffer.CopyTo instead of memory-group copy.
src/ImageSharp/Memory/TransformItemsInplaceDelegate.cs Deletes unused delegate after refactor.
src/ImageSharp/Memory/MemoryAllocatorExtensions.cs Adds positive-dimension guards for 2D allocations.
src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroupExtensions.cs Reworks copy logic to support 2D regions with explicit source/target strides.
src/ImageSharp/Memory/Buffer2D{T}.cs Adds RowStride, WrapMemory, stride-aware copy methods, and updates row access logic.
src/ImageSharp/Memory/Buffer2DRegion{T}.cs Updates Stride semantics to reflect backing stride and adjusts clear/fill optimizations.
src/ImageSharp/Memory/Buffer2DExtensions.cs Updates column-copy stride math and clone path to use Buffer2D.CopyTo.
src/ImageSharp/Image{TPixel}.cs Propagates stride through internal constructors and routes pixel export/single-memory queries via frame/buffer.
src/ImageSharp/ImageFrame{TPixel}.cs Adds stride-aware constructors and refactors copy/export/clear logic to use Buffer2D APIs.
src/ImageSharp/ImageFrameCollection{TPixel}.cs Propagates row stride when wrapping external memory groups and updates non-compatible frame copy.
src/ImageSharp/ImageFrame.cs Updates CopyPixelsTo abstraction to target Buffer2D<T> instead of MemoryGroup<T>.
src/ImageSharp/ImageFrame.LoadPixelData.cs Adds stride overloads and performs row-by-row copy from strided sources.
src/ImageSharp/Image.WrapMemory.cs Adds row-stride overloads for wrapping pixel/byte/pointer memory and centralizes stride validation helpers.
src/ImageSharp/Image.LoadPixelData.cs Adds row-stride overloads and routes to stride-aware copy-from logic.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/ImageSharp/Memory/Buffer2D{T}.cs Outdated
Comment thread src/ImageSharp/Image.WrapMemory.cs Outdated
Comment thread src/ImageSharp/Image.WrapMemory.cs Outdated
Comment thread src/ImageSharp/Memory/Buffer2D{T}.cs Outdated
@JimBobSquarePants JimBobSquarePants merged commit 2cf379e into main Mar 3, 2026
12 checks passed
@JimBobSquarePants JimBobSquarePants deleted the js/expose-buffer-helpers branch March 3, 2026 10:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants