Struct Mask

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

A mask.

During drawing over Pixmap, mask’s black (0) “pixels” would block rendering and white (255) will allow it. Anything in between is used for gradual masking and anti-aliasing.

Unlike Skia, we’re using just a simple 8bit alpha mask. It’s way slower, but easier to implement.

Implementations§

Source§

impl Mask

Source

pub fn new(width: u32, height: u32) -> Option<Self>

Creates a new mask by taking ownership over a mask buffer.

The size needs to match the data provided.

Examples found in repository?
examples/mask.rs (line 15)
3fn main() {
4    let clip_path = {
5        let mut pb = PathBuilder::new();
6        pb.push_circle(250.0, 250.0, 200.0);
7        pb.push_circle(250.0, 250.0, 100.0);
8        pb.finish().unwrap()
9    };
10
11    let clip_path = clip_path
12        .transform(Transform::from_row(1.0, -0.3, 0.0, 1.0, 0.0, 75.0))
13        .unwrap();
14
15    let mut mask = Mask::new(500, 500).unwrap();
16    mask.fill_path(&clip_path, FillRule::EvenOdd, true, Transform::default());
17
18    let mut paint = Paint::default();
19    paint.anti_alias = false;
20    paint.set_color_rgba8(50, 127, 150, 200);
21
22    let mut pixmap = Pixmap::new(500, 500).unwrap();
23    pixmap.fill_rect(
24        Rect::from_xywh(0.0, 0.0, 500.0, 500.0).unwrap(),
25        &paint,
26        Transform::identity(),
27        Some(&mask),
28    );
29    pixmap.save_png("image.png").unwrap();
30}
More examples
Hide additional examples
examples/large_image.rs (line 35)
6fn main() {
7    let path1 = {
8        let mut pb = PathBuilder::new();
9        pb.move_to(1200.0, 1200.0);
10        pb.line_to(3200.0, 18800.0);
11        pb.cubic_to(7600.0, 16800.0, 13200.0, 16000.0, 18800.0, 16000.0);
12        pb.cubic_to(14800.0, 9200.0, 8800.0, 3200.0, 1200.0, 1200.0);
13        pb.close();
14        pb.finish().unwrap()
15    };
16
17    let path2 = {
18        let mut pb = PathBuilder::new();
19        pb.move_to(18800.0, 1200.0);
20        pb.line_to(16800.0, 18800.0);
21        pb.cubic_to(12400.0, 16800.0, 6800.0, 16000.0, 1200.0, 16000.0);
22        pb.cubic_to(5200.0, 9200.0, 11200.0, 3200.0, 18800.0, 1200.0);
23        pb.close();
24        pb.finish().unwrap()
25    };
26
27    let mut pixmap = Pixmap::new(20000, 20000).unwrap();
28
29    let clip_path = {
30        let mut pb = PathBuilder::new();
31        pb.push_circle(10000.0, 10000.0, 7000.0);
32        pb.finish().unwrap()
33    };
34
35    let mut mask = Mask::new(20000, 20000).unwrap();
36    mask.fill_path(&clip_path, FillRule::Winding, true, Transform::default());
37
38    let mut paint = Paint::default();
39    paint.set_color_rgba8(90, 175, 100, 150);
40    paint.anti_alias = true;
41    let large_rect = Rect::from_xywh(500.0, 500.0, 19000.0, 19000.0).unwrap();
42    pixmap.fill_rect(large_rect, &paint, Transform::identity(), None);
43
44    paint.set_color_rgba8(50, 127, 150, 200);
45    paint.anti_alias = true;
46    pixmap.fill_path(
47        &path1,
48        &paint,
49        FillRule::Winding,
50        Transform::default(),
51        Some(&mask),
52    );
53
54    paint.set_color_rgba8(220, 140, 75, 180);
55    paint.anti_alias = false;
56    pixmap.fill_path(
57        &path2,
58        &paint,
59        FillRule::Winding,
60        Transform::default(),
61        None,
62    );
63
64    paint.set_color_rgba8(255, 10, 15, 180);
65    paint.anti_alias = true;
66    let mut stroke = Stroke::default();
67    stroke.width = 0.8; // hairline
68    pixmap.stroke_path(&path2, &paint, &stroke, Transform::default(), None);
69
70    pixmap.save_png("image.png").unwrap();
71}
Source

pub fn from_pixmap(pixmap: PixmapRef<'_>, mask_type: MaskType) -> Self

Creates a new mask from a PixmapRef.

Source

pub fn from_vec(data: Vec<u8>, size: IntSize) -> Option<Self>

Creates a new mask by taking ownership over a mask buffer.

The size needs to match the data provided.

Source

pub fn width(&self) -> u32

Returns mask’s width.

Source

pub fn height(&self) -> u32

Returns mask’s height.

Source

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

Returns the internal data.

Source

pub fn data_mut(&mut self) -> &mut [u8]

Returns the mutable internal data.

Source

pub fn decode_png(data: &[u8]) -> Result<Self, DecodingError>

Loads a PNG file into a Mask.

Only grayscale images are supported.

Source

pub fn load_png<P: AsRef<Path>>(path: P) -> Result<Self, DecodingError>

Loads a PNG file into a Mask.

Only grayscale images are supported.

Source

pub fn encode_png(&self) -> Result<Vec<u8>, EncodingError>

Encodes mask into a PNG data.

Source

pub fn save_png<P: AsRef<Path>>(&self, path: P) -> Result<(), EncodingError>

Saves mask as a PNG file.

Source

pub fn fill_path( &mut self, path: &Path, fill_rule: FillRule, anti_alias: bool, transform: Transform, )

Draws a filled path onto the mask.

In terms of RGB (no alpha) image, draws a white path on top of black mask.

Doesn’t reset the existing mask content and draws the path on top of existing data.

If the above behavior is undesired, Mask::clear() should be called first.

This method is intended to be used for simple cases. For more complex masks prefer Mask::from_pixmap().

Examples found in repository?
examples/mask.rs (line 16)
3fn main() {
4    let clip_path = {
5        let mut pb = PathBuilder::new();
6        pb.push_circle(250.0, 250.0, 200.0);
7        pb.push_circle(250.0, 250.0, 100.0);
8        pb.finish().unwrap()
9    };
10
11    let clip_path = clip_path
12        .transform(Transform::from_row(1.0, -0.3, 0.0, 1.0, 0.0, 75.0))
13        .unwrap();
14
15    let mut mask = Mask::new(500, 500).unwrap();
16    mask.fill_path(&clip_path, FillRule::EvenOdd, true, Transform::default());
17
18    let mut paint = Paint::default();
19    paint.anti_alias = false;
20    paint.set_color_rgba8(50, 127, 150, 200);
21
22    let mut pixmap = Pixmap::new(500, 500).unwrap();
23    pixmap.fill_rect(
24        Rect::from_xywh(0.0, 0.0, 500.0, 500.0).unwrap(),
25        &paint,
26        Transform::identity(),
27        Some(&mask),
28    );
29    pixmap.save_png("image.png").unwrap();
30}
More examples
Hide additional examples
examples/large_image.rs (line 36)
6fn main() {
7    let path1 = {
8        let mut pb = PathBuilder::new();
9        pb.move_to(1200.0, 1200.0);
10        pb.line_to(3200.0, 18800.0);
11        pb.cubic_to(7600.0, 16800.0, 13200.0, 16000.0, 18800.0, 16000.0);
12        pb.cubic_to(14800.0, 9200.0, 8800.0, 3200.0, 1200.0, 1200.0);
13        pb.close();
14        pb.finish().unwrap()
15    };
16
17    let path2 = {
18        let mut pb = PathBuilder::new();
19        pb.move_to(18800.0, 1200.0);
20        pb.line_to(16800.0, 18800.0);
21        pb.cubic_to(12400.0, 16800.0, 6800.0, 16000.0, 1200.0, 16000.0);
22        pb.cubic_to(5200.0, 9200.0, 11200.0, 3200.0, 18800.0, 1200.0);
23        pb.close();
24        pb.finish().unwrap()
25    };
26
27    let mut pixmap = Pixmap::new(20000, 20000).unwrap();
28
29    let clip_path = {
30        let mut pb = PathBuilder::new();
31        pb.push_circle(10000.0, 10000.0, 7000.0);
32        pb.finish().unwrap()
33    };
34
35    let mut mask = Mask::new(20000, 20000).unwrap();
36    mask.fill_path(&clip_path, FillRule::Winding, true, Transform::default());
37
38    let mut paint = Paint::default();
39    paint.set_color_rgba8(90, 175, 100, 150);
40    paint.anti_alias = true;
41    let large_rect = Rect::from_xywh(500.0, 500.0, 19000.0, 19000.0).unwrap();
42    pixmap.fill_rect(large_rect, &paint, Transform::identity(), None);
43
44    paint.set_color_rgba8(50, 127, 150, 200);
45    paint.anti_alias = true;
46    pixmap.fill_path(
47        &path1,
48        &paint,
49        FillRule::Winding,
50        Transform::default(),
51        Some(&mask),
52    );
53
54    paint.set_color_rgba8(220, 140, 75, 180);
55    paint.anti_alias = false;
56    pixmap.fill_path(
57        &path2,
58        &paint,
59        FillRule::Winding,
60        Transform::default(),
61        None,
62    );
63
64    paint.set_color_rgba8(255, 10, 15, 180);
65    paint.anti_alias = true;
66    let mut stroke = Stroke::default();
67    stroke.width = 0.8; // hairline
68    pixmap.stroke_path(&path2, &paint, &stroke, Transform::default(), None);
69
70    pixmap.save_png("image.png").unwrap();
71}
Source

pub fn intersect_path( &mut self, path: &Path, fill_rule: FillRule, anti_alias: bool, transform: Transform, )

Intersects the provided path with the current clipping path.

A temporary mask with the same size as the current one will be created.

Source

pub fn invert(&mut self)

Inverts the mask.

Source

pub fn clear(&mut self)

Clears the mask.

Zero-fills the internal data buffer.

Trait Implementations§

Source§

impl Clone for Mask

Source§

fn clone(&self) -> Mask

Returns a copy 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 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 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.