Skip to content
Mask

Mask

Mojo struct 🡭

Mask

@memory_only
struct Mask

A width x height grid of 0-255 coverage, row-major, one byte per pixel: 255 lets everything through, 0 nothing.

Fields

  • width (Int)
  • height (Int)
  • coverage (List[UInt8])

Implemented traits

AnyType, Copyable, Deinitable, Movable

Methods

__init__

fn def __init__(out self, width: Int, height: Int, fill: UInt8 = UInt8(0))

A mask of uniform coverage.

Args:

  • width (Int): Mask width in pixels.
  • height (Int): Mask height in pixels.
  • fill (UInt8): Coverage of every pixel, 0 (the default) for a mask that lets nothing through until it is drawn into.
  • self (Self)

Returns:

Self

fn def __init__(out self, width: Int, height: Int, var coverage: List[UInt8])

A mask over existing coverage bytes, taken by value.

Args:

  • width (Int): Mask width in pixels.
  • height (Int): Mask height in pixels.
  • coverage (List[UInt8]): width * height bytes, row-major.
  • self (Self)

Returns:

Self

Raises:

If coverage is not width * height long.

from_path

@staticmethod
fn def from_path(path: Path, width: Int, height: Int, fill_rule: FillRule = FillRule.EVEN_ODD, supersample: Int = Int(4), curve_steps: Int = Int(0)) -> Self

path’s anti-aliased interior over a width x height grid: the coverage push_clip_path would keep, and the coverage fill_path_aa would blend by, so a mask’s edge lands where the fill’s does.

Args:

  • path (Path): Shape whose interior is the mask.
  • width (Int): Mask width in pixels.
  • height (Int): Mask height in pixels.
  • fill_rule (FillRule): EVEN_ODD (default) or NONZERO – see FillRule.
  • supersample (Int): Sub-pixel grid side length for the edge coverage.
  • curve_steps (Int): Straight-line segments per quad/cubic Bezier; 0 (the default) picks a count from the curvature.

Returns:

Self: The mask.

from_alpha

@staticmethod
fn def from_alpha(canvas: Canvas) -> Self

A canvas’s alpha channel as a mask of its size: an opaque pixel is 255, a transparent one 0. Draw a shape onto a transparent canvas and blur it, and this is a soft-edged stencil of it.

Args:

  • canvas (Canvas): Canvas whose alpha is read. Unchanged.

Returns:

Self: The mask.

from_luminance

@staticmethod
fn def from_luminance(canvas: Canvas) -> Self

A canvas’s brightness as a mask of its size, scaled by its alpha: opaque white is 255, black or transparent 0. The luminance is 0.30 R + 0.59 G + 0.11 B, the weighting canvas/blend.mojo’s non-separable modes use.

Args:

  • canvas (Canvas): Canvas whose pixels are read. Unchanged.

Returns:

Self: The mask.

coverage_at

fn def coverage_at(self, x: Int, y: Int) -> UInt8

The coverage at (x, y), 0 outside the mask.

Args:

  • self (Self)
  • x (Int): Column to query.
  • y (Int): Row to query.

Returns:

UInt8: Coverage 0-255.

inverted

fn def inverted(self) -> Self

The complement: 255 - coverage everywhere, so what this mask hides the result shows.

Args:

  • self (Self)

Returns:

Self: The inverted mask.