Struct Pixmap

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

A container that owns premultiplied RGBA pixels.

The data is not aligned, therefore width == stride.

Implementations§

Source§

impl Pixmap

Source

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

Allocates a new pixmap.

A pixmap is filled with transparent black by default, aka (0, 0, 0, 0).

Zero size in an error.

Pixmap’s width is limited by i32::MAX/4.

Examples found in repository?
examples/image_on_image.rs (line 6)
3fn main() {
4    let triangle = create_triangle();
5
6    let mut pixmap = Pixmap::new(400, 400).unwrap();
7
8    let now = std::time::Instant::now();
9
10    let mut paint = PixmapPaint::default();
11    paint.quality = FilterQuality::Bicubic;
12
13    pixmap.draw_pixmap(
14        20,
15        20,
16        triangle.as_ref(),
17        &paint,
18        Transform::from_row(1.2, 0.5, 0.5, 1.2, 0.0, 0.0),
19        None,
20    );
21
22    println!(
23        "Rendered in {:.2}ms",
24        now.elapsed().as_micros() as f64 / 1000.0
25    );
26
27    pixmap.save_png("image.png").unwrap();
28}
29
30fn create_triangle() -> Pixmap {
31    let mut paint = Paint::default();
32    paint.set_color_rgba8(50, 127, 150, 200);
33    paint.anti_alias = true;
34
35    let mut pb = PathBuilder::new();
36    pb.move_to(0.0, 200.0);
37    pb.line_to(200.0, 200.0);
38    pb.line_to(100.0, 0.0);
39    pb.close();
40    let path = pb.finish().unwrap();
41
42    let mut pixmap = Pixmap::new(200, 200).unwrap();
43
44    pixmap.fill_path(
45        &path,
46        &paint,
47        FillRule::Winding,
48        Transform::identity(),
49        None,
50    );
51
52    let path = PathBuilder::from_rect(Rect::from_ltrb(0.0, 0.0, 200.0, 200.0).unwrap());
53    let stroke = Stroke::default();
54    paint.set_color_rgba8(200, 0, 0, 220);
55
56    pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None); // TODO: stroke_rect
57
58    pixmap
59}
More examples
Hide additional examples
examples/pattern.rs (line 18)
3fn main() {
4    let triangle = crate_triangle();
5
6    let mut paint = Paint::default();
7    paint.anti_alias = true;
8    paint.shader = Pattern::new(
9        triangle.as_ref(),
10        SpreadMode::Repeat,
11        FilterQuality::Bicubic,
12        1.0,
13        Transform::from_row(1.5, -0.4, 0.0, -0.8, 5.0, 1.0),
14    );
15
16    let path = PathBuilder::from_circle(200.0, 200.0, 180.0).unwrap();
17
18    let mut pixmap = Pixmap::new(400, 400).unwrap();
19    pixmap.fill_path(
20        &path,
21        &paint,
22        FillRule::Winding,
23        Transform::identity(),
24        None,
25    );
26    pixmap.save_png("image.png").unwrap();
27}
28
29fn crate_triangle() -> Pixmap {
30    let mut paint = Paint::default();
31    paint.set_color_rgba8(50, 127, 150, 200);
32    paint.anti_alias = true;
33
34    let mut pb = PathBuilder::new();
35    pb.move_to(0.0, 20.0);
36    pb.line_to(20.0, 20.0);
37    pb.line_to(10.0, 0.0);
38    pb.close();
39    let path = pb.finish().unwrap();
40
41    let mut pixmap = Pixmap::new(20, 20).unwrap();
42    pixmap.fill_path(
43        &path,
44        &paint,
45        FillRule::Winding,
46        Transform::identity(),
47        None,
48    );
49    pixmap
50}
examples/hairline.rs (line 15)
5fn main() {
6    let mut pb = PathBuilder::new();
7    pb.move_to(50.0, 100.0);
8    pb.cubic_to(130.0, 20.0, 390.0, 120.0, 450.0, 30.0);
9    let path = pb.finish().unwrap();
10
11    let mut paint = Paint::default();
12    paint.set_color_rgba8(50, 127, 150, 200);
13    paint.anti_alias = true;
14
15    let mut pixmap = Pixmap::new(500, 500).unwrap();
16    let mut transform = Transform::identity();
17    for i in 0..20 {
18        let mut stroke = Stroke::default();
19        stroke.width = 2.0 - (i as f32 / 10.0);
20        pixmap.stroke_path(&path, &paint, &stroke, transform, None);
21        transform = transform.pre_translate(0.0, 20.0);
22    }
23
24    pixmap.save_png("image.png").unwrap();
25}
examples/mask.rs (line 22)
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}
examples/stroke.rs (line 27)
5fn main() {
6    let mut paint = Paint::default();
7    paint.set_color_rgba8(0, 127, 0, 200);
8    paint.anti_alias = true;
9
10    let path = {
11        let mut pb = PathBuilder::new();
12        const RADIUS: f32 = 250.0;
13        const CENTER: f32 = 250.0;
14        pb.move_to(CENTER + RADIUS, CENTER);
15        for i in 1..8 {
16            let a = 2.6927937 * i as f32;
17            pb.line_to(CENTER + RADIUS * a.cos(), CENTER + RADIUS * a.sin());
18        }
19        pb.finish().unwrap()
20    };
21
22    let mut stroke = Stroke::default();
23    stroke.width = 6.0;
24    stroke.line_cap = LineCap::Round;
25    stroke.dash = StrokeDash::new(vec![20.0, 40.0], 0.0);
26
27    let mut pixmap = Pixmap::new(500, 500).unwrap();
28    pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None);
29    pixmap.save_png("image.png").unwrap();
30}
examples/linear_gradient.rs (line 26)
3fn main() {
4    let mut paint = Paint::default();
5    paint.anti_alias = false;
6    paint.shader = LinearGradient::new(
7        Point::from_xy(100.0, 100.0),
8        Point::from_xy(900.0, 900.0),
9        vec![
10            GradientStop::new(0.0, Color::from_rgba8(50, 127, 150, 200)),
11            GradientStop::new(1.0, Color::from_rgba8(220, 140, 75, 180)),
12        ],
13        SpreadMode::Pad,
14        Transform::identity(),
15    )
16    .unwrap();
17
18    let mut pb = PathBuilder::new();
19    pb.move_to(60.0, 60.0);
20    pb.line_to(160.0, 940.0);
21    pb.cubic_to(380.0, 840.0, 660.0, 800.0, 940.0, 800.0);
22    pb.cubic_to(740.0, 460.0, 440.0, 160.0, 60.0, 60.0);
23    pb.close();
24    let path = pb.finish().unwrap();
25
26    let mut pixmap = Pixmap::new(1000, 1000).unwrap();
27    pixmap.fill_path(
28        &path,
29        &paint,
30        FillRule::Winding,
31        Transform::identity(),
32        None,
33    );
34    pixmap.save_png("image.png").unwrap();
35}
Source

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

Creates a new pixmap by taking ownership over an image buffer (premultiplied RGBA pixels).

The size needs to match the data provided.

Pixmap’s width is limited by i32::MAX/4.

Source

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

Decodes a PNG data into a Pixmap.

Only 8-bit images are supported. Index PNGs are not supported.

Source

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

Loads a PNG file into a Pixmap.

Only 8-bit images are supported. Index PNGs are not supported.

Source

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

Encodes pixmap into a PNG data.

Source

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

Saves pixmap as a PNG file.

Examples found in repository?
examples/image_on_image.rs (line 27)
3fn main() {
4    let triangle = create_triangle();
5
6    let mut pixmap = Pixmap::new(400, 400).unwrap();
7
8    let now = std::time::Instant::now();
9
10    let mut paint = PixmapPaint::default();
11    paint.quality = FilterQuality::Bicubic;
12
13    pixmap.draw_pixmap(
14        20,
15        20,
16        triangle.as_ref(),
17        &paint,
18        Transform::from_row(1.2, 0.5, 0.5, 1.2, 0.0, 0.0),
19        None,
20    );
21
22    println!(
23        "Rendered in {:.2}ms",
24        now.elapsed().as_micros() as f64 / 1000.0
25    );
26
27    pixmap.save_png("image.png").unwrap();
28}
More examples
Hide additional examples
examples/pattern.rs (line 26)
3fn main() {
4    let triangle = crate_triangle();
5
6    let mut paint = Paint::default();
7    paint.anti_alias = true;
8    paint.shader = Pattern::new(
9        triangle.as_ref(),
10        SpreadMode::Repeat,
11        FilterQuality::Bicubic,
12        1.0,
13        Transform::from_row(1.5, -0.4, 0.0, -0.8, 5.0, 1.0),
14    );
15
16    let path = PathBuilder::from_circle(200.0, 200.0, 180.0).unwrap();
17
18    let mut pixmap = Pixmap::new(400, 400).unwrap();
19    pixmap.fill_path(
20        &path,
21        &paint,
22        FillRule::Winding,
23        Transform::identity(),
24        None,
25    );
26    pixmap.save_png("image.png").unwrap();
27}
examples/hairline.rs (line 24)
5fn main() {
6    let mut pb = PathBuilder::new();
7    pb.move_to(50.0, 100.0);
8    pb.cubic_to(130.0, 20.0, 390.0, 120.0, 450.0, 30.0);
9    let path = pb.finish().unwrap();
10
11    let mut paint = Paint::default();
12    paint.set_color_rgba8(50, 127, 150, 200);
13    paint.anti_alias = true;
14
15    let mut pixmap = Pixmap::new(500, 500).unwrap();
16    let mut transform = Transform::identity();
17    for i in 0..20 {
18        let mut stroke = Stroke::default();
19        stroke.width = 2.0 - (i as f32 / 10.0);
20        pixmap.stroke_path(&path, &paint, &stroke, transform, None);
21        transform = transform.pre_translate(0.0, 20.0);
22    }
23
24    pixmap.save_png("image.png").unwrap();
25}
examples/mask.rs (line 29)
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}
examples/stroke.rs (line 29)
5fn main() {
6    let mut paint = Paint::default();
7    paint.set_color_rgba8(0, 127, 0, 200);
8    paint.anti_alias = true;
9
10    let path = {
11        let mut pb = PathBuilder::new();
12        const RADIUS: f32 = 250.0;
13        const CENTER: f32 = 250.0;
14        pb.move_to(CENTER + RADIUS, CENTER);
15        for i in 1..8 {
16            let a = 2.6927937 * i as f32;
17            pb.line_to(CENTER + RADIUS * a.cos(), CENTER + RADIUS * a.sin());
18        }
19        pb.finish().unwrap()
20    };
21
22    let mut stroke = Stroke::default();
23    stroke.width = 6.0;
24    stroke.line_cap = LineCap::Round;
25    stroke.dash = StrokeDash::new(vec![20.0, 40.0], 0.0);
26
27    let mut pixmap = Pixmap::new(500, 500).unwrap();
28    pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None);
29    pixmap.save_png("image.png").unwrap();
30}
examples/linear_gradient.rs (line 34)
3fn main() {
4    let mut paint = Paint::default();
5    paint.anti_alias = false;
6    paint.shader = LinearGradient::new(
7        Point::from_xy(100.0, 100.0),
8        Point::from_xy(900.0, 900.0),
9        vec![
10            GradientStop::new(0.0, Color::from_rgba8(50, 127, 150, 200)),
11            GradientStop::new(1.0, Color::from_rgba8(220, 140, 75, 180)),
12        ],
13        SpreadMode::Pad,
14        Transform::identity(),
15    )
16    .unwrap();
17
18    let mut pb = PathBuilder::new();
19    pb.move_to(60.0, 60.0);
20    pb.line_to(160.0, 940.0);
21    pb.cubic_to(380.0, 840.0, 660.0, 800.0, 940.0, 800.0);
22    pb.cubic_to(740.0, 460.0, 440.0, 160.0, 60.0, 60.0);
23    pb.close();
24    let path = pb.finish().unwrap();
25
26    let mut pixmap = Pixmap::new(1000, 1000).unwrap();
27    pixmap.fill_path(
28        &path,
29        &paint,
30        FillRule::Winding,
31        Transform::identity(),
32        None,
33    );
34    pixmap.save_png("image.png").unwrap();
35}
Source

pub fn as_ref(&self) -> PixmapRef<'_>

Returns a container that references Pixmap’s data.

Examples found in repository?
examples/image_on_image.rs (line 16)
3fn main() {
4    let triangle = create_triangle();
5
6    let mut pixmap = Pixmap::new(400, 400).unwrap();
7
8    let now = std::time::Instant::now();
9
10    let mut paint = PixmapPaint::default();
11    paint.quality = FilterQuality::Bicubic;
12
13    pixmap.draw_pixmap(
14        20,
15        20,
16        triangle.as_ref(),
17        &paint,
18        Transform::from_row(1.2, 0.5, 0.5, 1.2, 0.0, 0.0),
19        None,
20    );
21
22    println!(
23        "Rendered in {:.2}ms",
24        now.elapsed().as_micros() as f64 / 1000.0
25    );
26
27    pixmap.save_png("image.png").unwrap();
28}
More examples
Hide additional examples
examples/pattern.rs (line 9)
3fn main() {
4    let triangle = crate_triangle();
5
6    let mut paint = Paint::default();
7    paint.anti_alias = true;
8    paint.shader = Pattern::new(
9        triangle.as_ref(),
10        SpreadMode::Repeat,
11        FilterQuality::Bicubic,
12        1.0,
13        Transform::from_row(1.5, -0.4, 0.0, -0.8, 5.0, 1.0),
14    );
15
16    let path = PathBuilder::from_circle(200.0, 200.0, 180.0).unwrap();
17
18    let mut pixmap = Pixmap::new(400, 400).unwrap();
19    pixmap.fill_path(
20        &path,
21        &paint,
22        FillRule::Winding,
23        Transform::identity(),
24        None,
25    );
26    pixmap.save_png("image.png").unwrap();
27}
Source

pub fn as_mut(&mut self) -> PixmapMut<'_>

Returns a container that references Pixmap’s data.

Source

pub fn width(&self) -> u32

Returns pixmap’s width.

Source

pub fn height(&self) -> u32

Returns pixmap’s height.

Source

pub fn fill(&mut self, color: Color)

Fills the entire pixmap with a specified color.

Source

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

Returns the internal data.

Byteorder: RGBA

Source

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

Returns the mutable internal data.

Byteorder: RGBA

Source

pub fn pixel(&self, x: u32, y: u32) -> Option<PremultipliedColorU8>

Returns a pixel color.

Returns None when position is out of bounds.

Source

pub fn pixels_mut(&mut self) -> &mut [PremultipliedColorU8]

Returns a mutable slice of pixels.

Source

pub fn pixels(&self) -> &[PremultipliedColorU8]

Returns a slice of pixels.

Source

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

Consumes the internal data.

Byteorder: RGBA

Source

pub fn clone_rect(&self, rect: IntRect) -> Option<Pixmap>

Returns a copy of the pixmap that intersects the rect.

Returns None when Pixmap’s rect doesn’t contain rect.

Source§

impl Pixmap

Source

pub fn fill_rect( &mut self, rect: Rect, paint: &Paint<'_>, transform: Transform, mask: Option<&Mask>, )

Draws a filled rectangle onto the pixmap.

See PixmapMut::fill_rect for details.

Examples found in repository?
examples/mask.rs (lines 23-28)
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 42)
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 fill_path( &mut self, path: &Path, paint: &Paint<'_>, fill_rule: FillRule, transform: Transform, mask: Option<&Mask>, )

Draws a filled path onto the pixmap.

See PixmapMut::fill_path for details.

Examples found in repository?
examples/pattern.rs (lines 19-25)
3fn main() {
4    let triangle = crate_triangle();
5
6    let mut paint = Paint::default();
7    paint.anti_alias = true;
8    paint.shader = Pattern::new(
9        triangle.as_ref(),
10        SpreadMode::Repeat,
11        FilterQuality::Bicubic,
12        1.0,
13        Transform::from_row(1.5, -0.4, 0.0, -0.8, 5.0, 1.0),
14    );
15
16    let path = PathBuilder::from_circle(200.0, 200.0, 180.0).unwrap();
17
18    let mut pixmap = Pixmap::new(400, 400).unwrap();
19    pixmap.fill_path(
20        &path,
21        &paint,
22        FillRule::Winding,
23        Transform::identity(),
24        None,
25    );
26    pixmap.save_png("image.png").unwrap();
27}
28
29fn crate_triangle() -> Pixmap {
30    let mut paint = Paint::default();
31    paint.set_color_rgba8(50, 127, 150, 200);
32    paint.anti_alias = true;
33
34    let mut pb = PathBuilder::new();
35    pb.move_to(0.0, 20.0);
36    pb.line_to(20.0, 20.0);
37    pb.line_to(10.0, 0.0);
38    pb.close();
39    let path = pb.finish().unwrap();
40
41    let mut pixmap = Pixmap::new(20, 20).unwrap();
42    pixmap.fill_path(
43        &path,
44        &paint,
45        FillRule::Winding,
46        Transform::identity(),
47        None,
48    );
49    pixmap
50}
More examples
Hide additional examples
examples/image_on_image.rs (lines 44-50)
30fn create_triangle() -> Pixmap {
31    let mut paint = Paint::default();
32    paint.set_color_rgba8(50, 127, 150, 200);
33    paint.anti_alias = true;
34
35    let mut pb = PathBuilder::new();
36    pb.move_to(0.0, 200.0);
37    pb.line_to(200.0, 200.0);
38    pb.line_to(100.0, 0.0);
39    pb.close();
40    let path = pb.finish().unwrap();
41
42    let mut pixmap = Pixmap::new(200, 200).unwrap();
43
44    pixmap.fill_path(
45        &path,
46        &paint,
47        FillRule::Winding,
48        Transform::identity(),
49        None,
50    );
51
52    let path = PathBuilder::from_rect(Rect::from_ltrb(0.0, 0.0, 200.0, 200.0).unwrap());
53    let stroke = Stroke::default();
54    paint.set_color_rgba8(200, 0, 0, 220);
55
56    pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None); // TODO: stroke_rect
57
58    pixmap
59}
examples/linear_gradient.rs (lines 27-33)
3fn main() {
4    let mut paint = Paint::default();
5    paint.anti_alias = false;
6    paint.shader = LinearGradient::new(
7        Point::from_xy(100.0, 100.0),
8        Point::from_xy(900.0, 900.0),
9        vec![
10            GradientStop::new(0.0, Color::from_rgba8(50, 127, 150, 200)),
11            GradientStop::new(1.0, Color::from_rgba8(220, 140, 75, 180)),
12        ],
13        SpreadMode::Pad,
14        Transform::identity(),
15    )
16    .unwrap();
17
18    let mut pb = PathBuilder::new();
19    pb.move_to(60.0, 60.0);
20    pb.line_to(160.0, 940.0);
21    pb.cubic_to(380.0, 840.0, 660.0, 800.0, 940.0, 800.0);
22    pb.cubic_to(740.0, 460.0, 440.0, 160.0, 60.0, 60.0);
23    pb.close();
24    let path = pb.finish().unwrap();
25
26    let mut pixmap = Pixmap::new(1000, 1000).unwrap();
27    pixmap.fill_path(
28        &path,
29        &paint,
30        FillRule::Winding,
31        Transform::identity(),
32        None,
33    );
34    pixmap.save_png("image.png").unwrap();
35}
examples/fill.rs (lines 33-39)
3fn main() {
4    let mut paint1 = Paint::default();
5    paint1.set_color_rgba8(50, 127, 150, 200);
6    paint1.anti_alias = true;
7
8    let mut paint2 = Paint::default();
9    paint2.set_color_rgba8(220, 140, 75, 180);
10    paint2.anti_alias = false;
11
12    let path1 = {
13        let mut pb = PathBuilder::new();
14        pb.move_to(60.0, 60.0);
15        pb.line_to(160.0, 940.0);
16        pb.cubic_to(380.0, 840.0, 660.0, 800.0, 940.0, 800.0);
17        pb.cubic_to(740.0, 460.0, 440.0, 160.0, 60.0, 60.0);
18        pb.close();
19        pb.finish().unwrap()
20    };
21
22    let path2 = {
23        let mut pb = PathBuilder::new();
24        pb.move_to(940.0, 60.0);
25        pb.line_to(840.0, 940.0);
26        pb.cubic_to(620.0, 840.0, 340.0, 800.0, 60.0, 800.0);
27        pb.cubic_to(260.0, 460.0, 560.0, 160.0, 940.0, 60.0);
28        pb.close();
29        pb.finish().unwrap()
30    };
31
32    let mut pixmap = Pixmap::new(1000, 1000).unwrap();
33    pixmap.fill_path(
34        &path1,
35        &paint1,
36        FillRule::Winding,
37        Transform::identity(),
38        None,
39    );
40    pixmap.fill_path(
41        &path2,
42        &paint2,
43        FillRule::Winding,
44        Transform::identity(),
45        None,
46    );
47    pixmap.save_png("image.png").unwrap();
48}
examples/large_image.rs (lines 46-52)
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 stroke_path( &mut self, path: &Path, paint: &Paint<'_>, stroke: &Stroke, transform: Transform, mask: Option<&Mask>, )

Strokes a path.

See PixmapMut::stroke_path for details.

Examples found in repository?
examples/hairline.rs (line 20)
5fn main() {
6    let mut pb = PathBuilder::new();
7    pb.move_to(50.0, 100.0);
8    pb.cubic_to(130.0, 20.0, 390.0, 120.0, 450.0, 30.0);
9    let path = pb.finish().unwrap();
10
11    let mut paint = Paint::default();
12    paint.set_color_rgba8(50, 127, 150, 200);
13    paint.anti_alias = true;
14
15    let mut pixmap = Pixmap::new(500, 500).unwrap();
16    let mut transform = Transform::identity();
17    for i in 0..20 {
18        let mut stroke = Stroke::default();
19        stroke.width = 2.0 - (i as f32 / 10.0);
20        pixmap.stroke_path(&path, &paint, &stroke, transform, None);
21        transform = transform.pre_translate(0.0, 20.0);
22    }
23
24    pixmap.save_png("image.png").unwrap();
25}
More examples
Hide additional examples
examples/image_on_image.rs (line 56)
30fn create_triangle() -> Pixmap {
31    let mut paint = Paint::default();
32    paint.set_color_rgba8(50, 127, 150, 200);
33    paint.anti_alias = true;
34
35    let mut pb = PathBuilder::new();
36    pb.move_to(0.0, 200.0);
37    pb.line_to(200.0, 200.0);
38    pb.line_to(100.0, 0.0);
39    pb.close();
40    let path = pb.finish().unwrap();
41
42    let mut pixmap = Pixmap::new(200, 200).unwrap();
43
44    pixmap.fill_path(
45        &path,
46        &paint,
47        FillRule::Winding,
48        Transform::identity(),
49        None,
50    );
51
52    let path = PathBuilder::from_rect(Rect::from_ltrb(0.0, 0.0, 200.0, 200.0).unwrap());
53    let stroke = Stroke::default();
54    paint.set_color_rgba8(200, 0, 0, 220);
55
56    pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None); // TODO: stroke_rect
57
58    pixmap
59}
examples/stroke.rs (line 28)
5fn main() {
6    let mut paint = Paint::default();
7    paint.set_color_rgba8(0, 127, 0, 200);
8    paint.anti_alias = true;
9
10    let path = {
11        let mut pb = PathBuilder::new();
12        const RADIUS: f32 = 250.0;
13        const CENTER: f32 = 250.0;
14        pb.move_to(CENTER + RADIUS, CENTER);
15        for i in 1..8 {
16            let a = 2.6927937 * i as f32;
17            pb.line_to(CENTER + RADIUS * a.cos(), CENTER + RADIUS * a.sin());
18        }
19        pb.finish().unwrap()
20    };
21
22    let mut stroke = Stroke::default();
23    stroke.width = 6.0;
24    stroke.line_cap = LineCap::Round;
25    stroke.dash = StrokeDash::new(vec![20.0, 40.0], 0.0);
26
27    let mut pixmap = Pixmap::new(500, 500).unwrap();
28    pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None);
29    pixmap.save_png("image.png").unwrap();
30}
examples/large_image.rs (line 68)
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 draw_pixmap( &mut self, x: i32, y: i32, pixmap: PixmapRef<'_>, paint: &PixmapPaint, transform: Transform, mask: Option<&Mask>, )

Draws a Pixmap on top of the current Pixmap.

See PixmapMut::draw_pixmap for details.

Examples found in repository?
examples/image_on_image.rs (lines 13-20)
3fn main() {
4    let triangle = create_triangle();
5
6    let mut pixmap = Pixmap::new(400, 400).unwrap();
7
8    let now = std::time::Instant::now();
9
10    let mut paint = PixmapPaint::default();
11    paint.quality = FilterQuality::Bicubic;
12
13    pixmap.draw_pixmap(
14        20,
15        20,
16        triangle.as_ref(),
17        &paint,
18        Transform::from_row(1.2, 0.5, 0.5, 1.2, 0.0, 0.0),
19        None,
20    );
21
22    println!(
23        "Rendered in {:.2}ms",
24        now.elapsed().as_micros() as f64 / 1000.0
25    );
26
27    pixmap.save_png("image.png").unwrap();
28}
Source

pub fn apply_mask(&mut self, mask: &Mask)

Applies a masks.

See PixmapMut::apply_mask for details.

Trait Implementations§

Source§

impl Clone for Pixmap

Source§

fn clone(&self) -> Pixmap

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 Pixmap

Source§

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

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

impl PartialEq for Pixmap

Source§

fn eq(&self, other: &Pixmap) -> 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 Pixmap

Auto Trait Implementations§

§

impl Freeze for Pixmap

§

impl RefUnwindSafe for Pixmap

§

impl Send for Pixmap

§

impl Sync for Pixmap

§

impl Unpin for Pixmap

§

impl UnwindSafe for Pixmap

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.