Skip to main content

MaskData

Struct MaskData 

Source
pub struct MaskData { /* private fields */ }
Expand description

A raster mask stored as PNG-encoded bytes.

MaskData provides zero-copy access to PNG metadata (width, height, bit_depth) by reading the IHDR chunk directly, and full encode/decode for pixel data at 1-bit, 8-bit, and 16-bit depths.

§PNG layout reference

[0..8]   PNG signature
[8..12]  IHDR chunk length (always 13)
[12..16] IHDR chunk type ("IHDR")
[16..20] width  (big-endian u32)
[20..24] height (big-endian u32)
[24]     bit_depth
[25]     color_type
...

Implementations§

Source§

impl MaskData

Source

pub fn from_png(png: Vec<u8>) -> Self

Creates a MaskData from raw PNG bytes.

The caller is responsible for ensuring the bytes represent a valid PNG. For validated construction, use from_png_checked.

Source

pub fn from_png_checked(png: Vec<u8>) -> Result<Self, Error>

Creates a MaskData from raw PNG bytes with validation.

Validates that the bytes represent a valid grayscale PNG:

  • Length >= 33 bytes (signature + IHDR chunk including CRC)
  • PNG magic bytes at offset 0
  • Color type byte (offset 25) is 0 (grayscale)
§Errors

Returns an error if the bytes are not a valid grayscale PNG.

Source

pub fn is_valid(&self) -> bool

Returns true if the underlying bytes contain a valid PNG signature and are long enough to have an IHDR chunk.

Source

pub fn as_bytes(&self) -> &[u8]

Returns a reference to the underlying PNG bytes.

Source

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

Consumes the MaskData and returns the underlying PNG bytes.

Source

pub fn width(&self) -> u32

Returns the image width by reading the PNG IHDR chunk (bytes 16..20).

Returns 0 if the PNG data is too short or invalid.

Source

pub fn height(&self) -> u32

Returns the image height by reading the PNG IHDR chunk (bytes 20..24).

Returns 0 if the PNG data is too short or invalid.

Source

pub fn bit_depth(&self) -> u8

Returns the bit depth by reading the PNG IHDR chunk (byte 24).

Returns 0 if the PNG data is too short or invalid.

Source

pub fn encode( pixels: &[u8], width: u32, height: u32, bit_depth: u8, ) -> Result<Self, Error>

Encodes raw 8-bit grayscale pixels into a PNG.

For bit_depth == 1, pixel values must be 0 or 1 and will be packed into 1-bit-per-pixel PNG rows (MSB first, 8 pixels per byte, with zero-padding on the last byte if width is not a multiple of 8).

For bit_depth == 8, pixels are encoded directly as 8-bit grayscale.

§Errors

Returns an error if bit_depth is not 1 or 8, or if pixels.len() does not equal width * height.

Source

pub fn encode_16bit( pixels: &[u16], width: u32, height: u32, ) -> Result<Self, Error>

Encodes raw 16-bit grayscale pixels into a PNG.

Each u16 value is written as two bytes in big-endian order, matching the PNG 16-bit grayscale format.

§Errors

Returns an error if pixels.len() does not equal width * height.

Source

pub fn decode(&self) -> Result<Vec<u8>, Error>

Decodes the PNG image to raw pixel bytes.

For 1-bit PNGs, each pixel is unpacked to a single byte (0 or 1). For 8-bit PNGs, pixel bytes are returned directly. For 16-bit PNGs, each pixel yields two bytes in big-endian order.

§Errors

Returns an error if the PNG data is malformed or cannot be decoded.

Trait Implementations§

Source§

impl Clone for MaskData

Source§

fn clone(&self) -> MaskData

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MaskData

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Key for T
where T: Clone,

Source§

fn align() -> usize

The alignment necessary for the key. Must return a power of two.
Source§

fn size(&self) -> usize

The size of the key in bytes.
Source§

unsafe fn init(&self, ptr: *mut u8)

Initialize the key in the given memory location. Read more
Source§

unsafe fn get<'a>(ptr: *const u8) -> &'a T

Get a reference to the key from the given memory location. Read more
Source§

unsafe fn drop_in_place(ptr: *mut u8)

Drop the key in place. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more