Table of Contents

Class Buffer2D<T>

Namespace
SixLabors.ImageSharp.Memory
Assembly
SixLabors.ImageSharp.dll

Represents a buffer of value type objects interpreted as a 2D region of Width x Height elements.

public sealed class Buffer2D<T> : IDisposable where T : struct

Type Parameters

T

The value type.

Inheritance
Buffer2D<T>
Implements
Inherited Members
Extension Methods

Properties

Bounds

Gets the bounds of the buffer.

public Rectangle Bounds { get; }

Property Value

Rectangle

The Rectangle

Height

Gets the height.

public int Height { get; }

Property Value

int

this[int, int]

Gets a reference to the element at the specified position.

public ref T this[int x, int y] { get; }

Parameters

x int

The x coordinate (row)

y int

The y coordinate (position at row)

Property Value

T

A reference to the element.

Exceptions

IndexOutOfRangeException

When index is out of range of the buffer.

MemoryGroup

Gets the backing IMemoryGroup<T>.

public IMemoryGroup<T> MemoryGroup { get; }

Property Value

IMemoryGroup<T>

The MemoryGroup.

RowStride

Gets the number of elements between row starts in the backing memory.

public int RowStride { get; }

Property Value

int

Size

Gets the size of the buffer.

public Size Size { get; }

Property Value

Size

Width

Gets the width.

public int Width { get; }

Property Value

int

Methods

DangerousGetRowSpan(int)

Gets a Span<T> to the row 'y' beginning from the pixel at the first pixel on that row.

public Span<T> DangerousGetRowSpan(int y)

Parameters

y int

The row index.

Returns

Span<T>

The Span<T> of the pixels in the row.

Remarks

This method does not validate the y argument for performance reason, ArgumentOutOfRangeException is being propagated from lower levels.

Exceptions

ArgumentOutOfRangeException

Thrown when row index is out of range.

DangerousTryGetSingleMemory(out Memory<T>)

Gets the representation of the values as a single contiguous Memory<T> when the backing group is a single tightly packed segment.

public bool DangerousTryGetSingleMemory(out Memory<T> memory)

Parameters

memory Memory<T>

The Memory<T> referencing the buffer.

Returns

bool

true when the buffer can be copied as one contiguous block without per-row handling; otherwise false.

Dispose()

Disposes the Buffer2D<T> instance

public void Dispose()

WrapMemory(Memory<T>, int, int)

Wraps an existing memory area as a Buffer2D<T> with tightly packed rows.

public static Buffer2D<T> WrapMemory(Memory<T> memory, int width, int height)

Parameters

memory Memory<T>

The source memory.

width int

The number of elements in each row.

height int

The number of rows.

Returns

Buffer2D<T>

The wrapped Buffer2D<T> instance.

Remarks

This method does not transfer ownership of memory to the returned Buffer2D<T>. The caller is responsible for ensuring that the memory remains valid for the entire lifetime of the returned buffer. If memory originates from an IMemoryOwner<T> (for example from MemoryPool<T>), do not dispose that owner while the returned buffer is still in use.

Exceptions

ArgumentOutOfRangeException

Thrown when width or height is not positive.

ArgumentException

Thrown when memory is shorter than width * height.

WrapMemory(Memory<T>, int, int, int)

Wraps an existing memory area as a Buffer2D<T> using the specified row stride.

public static Buffer2D<T> WrapMemory(Memory<T> memory, int width, int height, int stride)

Parameters

memory Memory<T>

The source memory.

width int

The number of elements in each row.

height int

The number of rows.

stride int

The number of elements between row starts in the source memory.

Returns

Buffer2D<T>

The wrapped Buffer2D<T> instance.

Remarks

This method does not transfer ownership of memory to the returned Buffer2D<T>. The caller is responsible for ensuring that the memory remains valid for the entire lifetime of the returned buffer. If memory originates from an IMemoryOwner<T> (for example from MemoryPool<T>), do not dispose that owner while the returned buffer is still in use. The minimum required length is ((height - 1) * stride) + width elements.

Exceptions

ArgumentOutOfRangeException

Thrown when width or height is not positive, or when stride is less than width.

ArgumentException

Thrown when memory is shorter than the required buffer size.