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
impl MaskData
Sourcepub fn from_png(png: Vec<u8>) -> Self
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.
Sourcepub fn from_png_checked(png: Vec<u8>) -> Result<Self, Error>
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.
Sourcepub fn is_valid(&self) -> bool
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.
Sourcepub fn into_bytes(self) -> Vec<u8> ⓘ
pub fn into_bytes(self) -> Vec<u8> ⓘ
Consumes the MaskData and returns the underlying PNG bytes.
Sourcepub fn width(&self) -> u32
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.
Sourcepub fn height(&self) -> u32
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.
Sourcepub fn bit_depth(&self) -> u8
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.
Sourcepub fn encode(
pixels: &[u8],
width: u32,
height: u32,
bit_depth: u8,
) -> Result<Self, Error>
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.
Sourcepub fn encode_16bit(
pixels: &[u16],
width: u32,
height: u32,
) -> Result<Self, Error>
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.
Sourcepub fn decode(&self) -> Result<Vec<u8>, Error>
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§
Auto Trait Implementations§
impl Freeze for MaskData
impl RefUnwindSafe for MaskData
impl Send for MaskData
impl Sync for MaskData
impl Unpin for MaskData
impl UnsafeUnpin for MaskData
impl UnwindSafe for MaskData
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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