Skip to main content

PixelBuffer

Struct PixelBuffer 

Source
#[non_exhaustive]
pub struct PixelBuffer<P = ()> { /* private fields */ }
Expand description

Owned pixel buffer with format metadata.

Wraps a Vec<u8> with an optional alignment offset so that pixel rows start at the correct alignment for the channel type. The backing vec can be recovered with into_vec for pool reuse.

The type parameter P tracks pixel format at compile time, same as PixelSlice.

Implementations§

Source§

impl PixelBuffer

Source

pub fn new(width: u32, height: u32, descriptor: PixelDescriptor) -> PixelBuffer

Allocate a zero-filled buffer for the given dimensions and format.

The default path is infallible for speed — see the allocation policy for the rationale and for guidance on when to reach for the try_* sibling.

§Panics

Panics if allocation fails. Use try_new for fallible allocation when handling untrusted sizes or when OOM must be recoverable.

Source

pub fn try_new( width: u32, height: u32, descriptor: PixelDescriptor, ) -> Result<PixelBuffer, At<BufferError>>

Try to allocate a zero-filled buffer for the given dimensions and format.

Returns BufferError::InvalidDimensions if the total size overflows, or BufferError::AllocationFailed if allocation fails.

Source

pub fn new_simd_aligned( width: u32, height: u32, descriptor: PixelDescriptor, simd_align: usize, ) -> PixelBuffer

Allocate a SIMD-aligned buffer for the given dimensions and format.

Row stride is a multiple of lcm(bpp, simd_align), ensuring every row start is both pixel-aligned and SIMD-aligned when the buffer itself starts at a SIMD-aligned address.

simd_align must be a power of 2 (e.g. 16, 32, 64).

The default path is infallible for speed — see the allocation policy for the rationale and for guidance on when to reach for the try_* sibling.

§Panics

Panics if allocation fails. Use try_new_simd_aligned for fallible allocation when handling untrusted sizes or when OOM must be recoverable.

Source

pub fn try_new_simd_aligned( width: u32, height: u32, descriptor: PixelDescriptor, simd_align: usize, ) -> Result<PixelBuffer, At<BufferError>>

Try to allocate a SIMD-aligned buffer for the given dimensions and format.

Returns BufferError::InvalidDimensions if the total size overflows, or BufferError::AllocationFailed if allocation fails.

Source

pub fn from_vec( data: Vec<u8>, width: u32, height: u32, descriptor: PixelDescriptor, ) -> Result<PixelBuffer, At<BufferError>>

Wrap an existing Vec<u8> as a pixel buffer.

The vec must be large enough to hold aligned_stride(width) * height bytes (plus any alignment offset). Stride is computed from the descriptor – rows are assumed tightly packed.

§Errors

Returns BufferError::InsufficientData if the vec is too small.

Source§

impl<P> PixelBuffer<P>
where P: Pixel,

Source

pub fn new_typed(width: u32, height: u32) -> PixelBuffer<P>

Allocate a typed zero-filled buffer for the given dimensions.

The descriptor is derived from P::DESCRIPTOR.

The default path is infallible for speed — see the allocation policy for the rationale and for guidance on when to reach for the try_* sibling.

§Panics

Panics if allocation fails. Use try_new_typed for fallible allocation when handling untrusted sizes or when OOM must be recoverable.

Source

pub fn try_new_typed( width: u32, height: u32, ) -> Result<PixelBuffer<P>, At<BufferError>>

Try to allocate a typed zero-filled buffer for the given dimensions.

Returns BufferError::InvalidDimensions if the total size overflows, or BufferError::AllocationFailed if allocation fails.

Source§

impl PixelBuffer

Source

pub fn transform_in_place<F>(&mut self, f: F)
where F: for<'a> FnOnce(InPlacePixels<'a>) -> PixelSliceMut<'a>,

Run a layout-changing in-place transform over this buffer and atomically adopt the transform’s output geometry, descriptor, and color context.

This is the only supported way to rewrite a buffer into a different byte layout in place (lane drops, channel reorders, orientation bakes, load-bearing reductions — the zenpixels-convert in-place family is built on it). The closure receives the buffer’s backing bytes (from the aligned base, including stride padding) plus the current description, rewrites the pixels, and returns a PixelSliceMut re-describing the same memory (built with PixelSliceMut::new, so the new geometry is validated). On return, this buffer’s own width / height / stride / descriptor / color context are updated in the same call — the buffer and its bytes can never disagree, which is the staleness hazard this method exists to prevent.

§Panics

Panics if the returned view does not start at this buffer’s aligned base or does not fit inside the original allocation — both indicate a bug in the transform, not a recoverable condition (the bytes may already be partially rewritten).

Source§

impl<P> PixelBuffer<P>

Source

pub fn erase(self) -> PixelBuffer

Erase the pixel type, returning a type-erased buffer.

Source

pub fn try_typed<Q>(self) -> Option<PixelBuffer<Q>>
where Q: Pixel,

Try to reinterpret as a typed pixel buffer.

Succeeds if the descriptors are layout-compatible.

Source

pub fn with_descriptor(self, descriptor: PixelDescriptor) -> PixelBuffer<P>

Replace the descriptor with a layout-compatible one.

See PixelSlice::with_descriptor() for details.

Source

pub fn reinterpret( self, descriptor: PixelDescriptor, ) -> Result<PixelBuffer<P>, At<BufferError>>

Reinterpret the buffer with a different physical layout.

See PixelSlice::reinterpret() for details.

Source

pub fn with_transfer(self, tf: TransferFunction) -> PixelBuffer<P>

Return a copy with a different transfer function.

Source

pub fn with_primaries(self, cp: ColorPrimaries) -> PixelBuffer<P>

Return a copy with different color primaries.

Source

pub fn with_signal_range(self, sr: SignalRange) -> PixelBuffer<P>

Return a copy with a different signal range.

Source

pub fn with_alpha_mode(self, am: Option<AlphaMode>) -> PixelBuffer<P>

Return a copy with a different alpha mode.

Source

pub fn has_alpha(&self) -> bool

Whether this buffer carries meaningful alpha data.

Source

pub fn is_grayscale(&self) -> bool

Whether this buffer is grayscale (Gray or GrayAlpha layout).

Source

pub fn into_vec(self) -> Vec<u8>

Consume the buffer and return the backing Vec<u8> for pool reuse.

Source

pub fn as_contiguous_bytes(&self) -> Option<&[u8]>

Zero-copy access to the raw pixel bytes when rows are tightly packed.

Returns Some(&[u8]) if stride == width * bpp (no padding), None if rows have stride padding.

Source

pub fn copy_to_contiguous_bytes(&self) -> Vec<u8>

Copy pixel data to a new contiguous byte Vec without stride padding.

Returns exactly width * height * bytes_per_pixel bytes in row-major order. For buffers already tightly packed (stride == width * bpp), this is a single memcpy. For padded buffers, this strips the padding row by row.

Source

pub fn width(&self) -> u32

Image width in pixels.

Source

pub fn height(&self) -> u32

Image height in pixels.

Source

pub fn stride(&self) -> usize

Byte stride between row starts.

Source

pub fn descriptor(&self) -> PixelDescriptor

Pixel format descriptor.

Source

pub fn color_context(&self) -> Option<&Arc<ColorContext>>

Source color context (ICC/CICP metadata), if set.

Source

pub fn with_color_context(self, ctx: Arc<ColorContext>) -> PixelBuffer<P>

Set the color context on this buffer.

Source

pub fn as_slice(&self) -> PixelSlice<'_, P>

Borrow the full buffer as an immutable PixelSlice.

Source

pub fn as_slice_mut(&mut self) -> PixelSliceMut<'_, P>

Borrow the full buffer as a mutable PixelSliceMut.

Note: this is a view; transforms that change the buffer’s layout cannot be expressed through it (a re-described view would leave this buffer’s own descriptor stale). For layout-changing in-place transforms, use PixelBuffer::transform_in_place, which adopts the new description atomically.

Source

pub fn rows(&self, y: u32, count: u32) -> PixelSlice<'_, P>

Borrow a range of rows as an immutable PixelSlice.

§Panics

Panics if y + count > height.

Source

pub fn rows_mut(&mut self, y: u32, count: u32) -> PixelSliceMut<'_, P>

Borrow a range of rows as a mutable PixelSliceMut.

§Panics

Panics if y + count > height.

Source

pub fn crop_view(&self, x: u32, y: u32, w: u32, h: u32) -> PixelSlice<'_, P>

Zero-copy sub-region view (immutable).

§Panics

Panics if the crop region is out of bounds.

Source

pub fn crop_copy(&self, x: u32, y: u32, w: u32, h: u32) -> PixelBuffer<P>

Copy a sub-region into a new, tightly-packed PixelBuffer.

§Panics

Panics if the crop region is out of bounds, or if the destination allocation size (aligned_stride(w) * h + min_alignment - 1) overflows usize. On 64-bit targets the overflow case is unreachable for u32 dimensions; on 32-bit targets it can be reached with adversarial inputs and panics eagerly here rather than producing a deferred slice-bounds panic.

Trait Implementations§

Source§

impl<P> Debug for PixelBuffer<P>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<P> From<PixelBuffer<P>> for PixelBuffer
where P: Pixel,

Source§

fn from(typed: PixelBuffer<P>) -> PixelBuffer

Converts to this type from the input type.
Source§

impl PixelBufferConvertExt for PixelBuffer

Source§

fn convert_to( &self, target: PixelDescriptor, ) -> Result<PixelBuffer, At<ConvertError>>

Convert pixel data to a different layout and depth. Read more
Source§

fn try_add_alpha(&self) -> Result<PixelBuffer, At<ConvertError>>

Add an alpha channel. Allocates a new PixelBuffer. Read more
Source§

fn try_widen_to_u16(&self) -> Result<PixelBuffer, At<ConvertError>>

Widen to U16 depth (lossless, ×257). Allocates a new PixelBuffer.
Source§

fn try_narrow_to_u8(&self) -> Result<PixelBuffer, At<ConvertError>>

Narrow to U8 depth (lossy, rounded). Allocates a new PixelBuffer.
Source§

fn linearize(&self) -> Result<PixelBuffer, At<ConvertError>>

Convert to linear-light F32, preserving channel layout and primaries. Read more
Source§

fn delinearize( &self, transfer: TransferFunction, ) -> Result<PixelBuffer, At<ConvertError>>

Apply a transfer function to a linear-light buffer. Read more
Source§

impl PixelBufferLoadBearingExt for PixelBuffer

Source§

fn reduce_to_load_bearing_format_in_place( &mut self, force_alpha_restructuring: bool, )

Run the load-bearing analysis and rewrite this buffer in place to the narrowest justified format, adopting the narrowed descriptor and tight row stride (width * bytes_per_pixel) in the same call. When no reduction applies the buffer is unchanged – compare PixelBuffer::descriptor before/after to detect it. Read more

Auto Trait Implementations§

§

impl<P> Freeze for PixelBuffer<P>

§

impl<P> RefUnwindSafe for PixelBuffer<P>
where P: RefUnwindSafe,

§

impl<P> Send for PixelBuffer<P>
where P: Send,

§

impl<P> Sync for PixelBuffer<P>
where P: Sync,

§

impl<P> Unpin for PixelBuffer<P>
where P: Unpin,

§

impl<P> UnsafeUnpin for PixelBuffer<P>

§

impl<P> UnwindSafe for PixelBuffer<P>
where P: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.