Skip to main content

Mask

Struct Mask 

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

A 2-D boolean bitmask for a raster region.

Mask uses bit-packed u64 words (64 pixels per word) for memory efficiency. See the module documentation for the storage and semantic conventions.

§Examples

use oxigdal_core::buffer::Mask;

let mut mask = Mask::new(10, 10);         // all valid (0)
mask.set(3, 3, true);                      // mark (3,3) as nodata
assert!(mask.get(3, 3));
assert_eq!(mask.count_set(), 1);

Implementations§

Source§

impl Mask

Source

pub fn new(width: usize, height: usize) -> Self

Creates a new mask with all bits unset (all pixels valid).

A mask with width == 0 or height == 0 is valid and has no pixels.

Source

pub fn new_filled(width: usize, height: usize) -> Self

Creates a new mask with all bits set (all pixels invalid/nodata).

The tail-word invariant is enforced — high bits of the last word are always zero even after the fill.

Source

pub fn from_slice(width: usize, height: usize, values: &[bool]) -> Result<Self>

Constructs a mask from a bool slice.

The slice is interpreted in row-major order: values[y * width + x] is the mask value for pixel (x, y). true → bit set (nodata).

§Errors

Returns OxiGdalError::InvalidParameter if values.len() != width * height.

Source§

impl Mask

Source

pub const fn width(&self) -> usize

Width of the raster region in pixels.

Source

pub const fn height(&self) -> usize

Height of the raster region in pixels.

Source

pub fn pixel_count(&self) -> usize

Total number of pixels (width * height).

Source

pub fn get(&self, x: usize, y: usize) -> bool

Returns the mask bit for pixel (x, y).

true means the pixel is invalid / nodata.

§Panics

Panics in debug builds if x >= width or y >= height. In release builds the access is clamped silently.

Source

pub fn set(&mut self, x: usize, y: usize, value: bool)

Sets the mask bit for pixel (x, y).

value = true marks the pixel as invalid / nodata.

§Panics

Panics in debug builds if x >= width or y >= height.

Source§

impl Mask

Source

pub fn fill(&mut self, value: bool)

Sets all bits to value.

The tail-word invariant is maintained.

Source

pub fn fill_rect(&mut self, x: usize, y: usize, w: usize, h: usize, value: bool)

Sets all bits in the rectangle [x, x+w) × [y, y+h) to value.

Coordinates that fall outside the mask bounds are silently clamped. The tail-word invariant is maintained.

Source§

impl Mask

Source

pub fn and_assign(&mut self, other: &Mask) -> Result<()>

Applies bitwise AND with other in place.

§Errors

Returns OxiGdalError::InvalidParameter if dimensions differ.

Source

pub fn or_assign(&mut self, other: &Mask) -> Result<()>

Applies bitwise OR with other in place.

§Errors

Returns OxiGdalError::InvalidParameter if dimensions differ.

Source

pub fn not_in_place(&mut self)

Applies bitwise NOT in place.

The tail-word invariant is re-established after inversion.

Source

pub fn xor_assign(&mut self, other: &Mask) -> Result<()>

Applies bitwise XOR with other in place.

§Errors

Returns OxiGdalError::InvalidParameter if dimensions differ.

Source

pub fn and(&self, other: &Mask) -> Result<Mask>

Returns a new mask that is the bitwise AND of self and other.

§Errors

Returns OxiGdalError::InvalidParameter if dimensions differ.

Source

pub fn or(&self, other: &Mask) -> Result<Mask>

Returns a new mask that is the bitwise OR of self and other.

§Errors

Returns OxiGdalError::InvalidParameter if dimensions differ.

Source

pub fn not(&self) -> Mask

Returns a new mask that is the bitwise NOT of self.

The tail-word invariant is preserved.

Source§

impl Mask

Source

pub fn count_set(&self) -> usize

Number of bits that are set (pixels marked invalid/nodata).

Source

pub fn count_unset(&self) -> usize

Number of bits that are unset (pixels that are valid).

Source

pub fn all_set(&self) -> bool

Returns true if all pixels are masked (all bits set).

Source

pub fn all_unset(&self) -> bool

Returns true if no pixels are masked (all bits unset).

Source

pub fn any_set(&self) -> bool

Returns true if at least one pixel is masked.

Source§

impl Mask

Source

pub fn set_positions(&self) -> impl Iterator<Item = (usize, usize)> + '_

Returns an iterator over (x, y) coordinates of all set bits (masked/nodata pixels).

Iterates in row-major order.

Source§

impl Mask

Source

pub fn from_nodata_f32( data: &[f32], width: usize, height: usize, nodata: f32, ) -> Result<Self>

Builds a nodata mask from an f32 slice.

A pixel is marked invalid if its value is NaN or exactly equals nodata (bitwise-exact comparison after handling NaN).

§Errors

Returns OxiGdalError::InvalidParameter if data.len() != width * height.

Source

pub fn from_nodata_f64( data: &[f64], width: usize, height: usize, nodata: f64, ) -> Result<Self>

Builds a nodata mask from an f64 slice.

A pixel is marked invalid if its value is NaN or exactly equals nodata.

§Errors

Returns OxiGdalError::InvalidParameter if data.len() != width * height.

Source

pub fn from_nodata_i32( data: &[i32], width: usize, height: usize, nodata: i32, ) -> Result<Self>

Builds a nodata mask from an i32 slice.

A pixel is marked invalid if its value exactly equals nodata.

§Errors

Returns OxiGdalError::InvalidParameter if data.len() != width * height.

Source

pub fn from_nodata_u8( data: &[u8], width: usize, height: usize, nodata: u8, ) -> Result<Self>

Builds a nodata mask from a u8 slice.

A pixel is marked invalid if its value exactly equals nodata.

§Errors

Returns OxiGdalError::InvalidParameter if data.len() != width * height.

Source§

impl Mask

Source

pub fn to_bool_vec(&self) -> Vec<bool>

Converts the mask to a Vec<bool> in row-major order.

The returned vector has length pixel_count(). true at index y * width + x means pixel (x, y) is invalid / nodata.

Source§

impl Mask

Source

pub fn apply_to_f32(&self, data: &mut [f32], nodata_value: f32)

Replaces every masked pixel in data with nodata_value.

Operates on an f32 slice of the same shape as the mask. Pixels where the mask bit is set (invalid) are written with nodata_value.

Source

pub fn apply_to_f64(&self, data: &mut [f64], nodata_value: f64)

Replaces every masked pixel in data with nodata_value.

Operates on an f64 slice of the same shape as the mask.

Source

pub fn apply_to_u8(&self, data: &mut [u8], nodata_value: u8)

Replaces every masked pixel in data with nodata_value.

Operates on a u8 slice of the same shape as the mask.

Source

pub fn apply_to_i32(&self, data: &mut [i32], nodata_value: i32)

Replaces every masked pixel in data with nodata_value.

Operates on an i32 slice of the same shape as the mask.

Trait Implementations§

Source§

impl Clone for Mask

Source§

fn clone(&self) -> Mask

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Mask

Source§

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

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

impl PartialEq for Mask

Source§

fn eq(&self, other: &Mask) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Mask

Source§

impl StructuralPartialEq for Mask

Auto Trait Implementations§

§

impl Freeze for Mask

§

impl RefUnwindSafe for Mask

§

impl Send for Mask

§

impl Sync for Mask

§

impl Unpin for Mask

§

impl UnsafeUnpin for Mask

§

impl UnwindSafe for Mask

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> 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> 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.