Struct tiny_skia::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)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
fn main() {
    let triangle = create_triangle();

    let mut pixmap = Pixmap::new(400, 400).unwrap();

    let now = std::time::Instant::now();

    let mut paint = PixmapPaint::default();
    paint.quality = FilterQuality::Bicubic;

    pixmap.draw_pixmap(
        20,
        20,
        triangle.as_ref(),
        &paint,
        Transform::from_row(1.2, 0.5, 0.5, 1.2, 0.0, 0.0),
        None,
    );

    println!(
        "Rendered in {:.2}ms",
        now.elapsed().as_micros() as f64 / 1000.0
    );

    pixmap.save_png("image.png").unwrap();
}

fn create_triangle() -> Pixmap {
    let mut paint = Paint::default();
    paint.set_color_rgba8(50, 127, 150, 200);
    paint.anti_alias = true;

    let mut pb = PathBuilder::new();
    pb.move_to(0.0, 200.0);
    pb.line_to(200.0, 200.0);
    pb.line_to(100.0, 0.0);
    pb.close();
    let path = pb.finish().unwrap();

    let mut pixmap = Pixmap::new(200, 200).unwrap();

    pixmap.fill_path(
        &path,
        &paint,
        FillRule::Winding,
        Transform::identity(),
        None,
    );

    let path = PathBuilder::from_rect(Rect::from_ltrb(0.0, 0.0, 200.0, 200.0).unwrap());
    let stroke = Stroke::default();
    paint.set_color_rgba8(200, 0, 0, 220);

    pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None); // TODO: stroke_rect

    pixmap
}
More examples
Hide additional examples
examples/pattern.rs (line 18)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
fn main() {
    let triangle = crate_triangle();

    let mut paint = Paint::default();
    paint.anti_alias = true;
    paint.shader = Pattern::new(
        triangle.as_ref(),
        SpreadMode::Repeat,
        FilterQuality::Bicubic,
        1.0,
        Transform::from_row(1.5, -0.4, 0.0, -0.8, 5.0, 1.0),
    );

    let path = PathBuilder::from_circle(200.0, 200.0, 180.0).unwrap();

    let mut pixmap = Pixmap::new(400, 400).unwrap();
    pixmap.fill_path(
        &path,
        &paint,
        FillRule::Winding,
        Transform::identity(),
        None,
    );
    pixmap.save_png("image.png").unwrap();
}

fn crate_triangle() -> Pixmap {
    let mut paint = Paint::default();
    paint.set_color_rgba8(50, 127, 150, 200);
    paint.anti_alias = true;

    let mut pb = PathBuilder::new();
    pb.move_to(0.0, 20.0);
    pb.line_to(20.0, 20.0);
    pb.line_to(10.0, 0.0);
    pb.close();
    let path = pb.finish().unwrap();

    let mut pixmap = Pixmap::new(20, 20).unwrap();
    pixmap.fill_path(
        &path,
        &paint,
        FillRule::Winding,
        Transform::identity(),
        None,
    );
    pixmap
}
examples/hairline.rs (line 15)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
fn main() {
    let mut pb = PathBuilder::new();
    pb.move_to(50.0, 100.0);
    pb.cubic_to(130.0, 20.0, 390.0, 120.0, 450.0, 30.0);
    let path = pb.finish().unwrap();

    let mut paint = Paint::default();
    paint.set_color_rgba8(50, 127, 150, 200);
    paint.anti_alias = true;

    let mut pixmap = Pixmap::new(500, 500).unwrap();
    let mut transform = Transform::identity();
    for i in 0..20 {
        let mut stroke = Stroke::default();
        stroke.width = 2.0 - (i as f32 / 10.0);
        pixmap.stroke_path(&path, &paint, &stroke, transform, None);
        transform = transform.pre_translate(0.0, 20.0);
    }

    pixmap.save_png("image.png").unwrap();
}
examples/clip_path.rs (line 21)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
fn main() {
    let clip_path = {
        let mut pb = PathBuilder::new();
        pb.push_circle(250.0, 250.0, 200.0);
        pb.push_circle(250.0, 250.0, 100.0);
        pb.finish().unwrap()
    };

    let clip_path = clip_path
        .transform(Transform::from_row(1.0, -0.3, 0.0, 1.0, 0.0, 75.0))
        .unwrap();

    let mut clip_mask = ClipMask::new();
    clip_mask.set_path(500, 500, &clip_path, FillRule::EvenOdd, true);

    let mut paint = Paint::default();
    paint.set_color_rgba8(50, 127, 150, 200);

    let mut pixmap = Pixmap::new(500, 500).unwrap();
    pixmap.fill_rect(
        Rect::from_xywh(0.0, 0.0, 500.0, 500.0).unwrap(),
        &paint,
        Transform::identity(),
        Some(&clip_mask),
    );
    pixmap.save_png("image.png").unwrap();
}
examples/stroke.rs (line 27)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
fn main() {
    let mut paint = Paint::default();
    paint.set_color_rgba8(0, 127, 0, 200);
    paint.anti_alias = true;

    let path = {
        let mut pb = PathBuilder::new();
        const RADIUS: f32 = 250.0;
        const CENTER: f32 = 250.0;
        pb.move_to(CENTER + RADIUS, CENTER);
        for i in 1..8 {
            let a = 2.6927937 * i as f32;
            pb.line_to(CENTER + RADIUS * a.cos(), CENTER + RADIUS * a.sin());
        }
        pb.finish().unwrap()
    };

    let mut stroke = Stroke::default();
    stroke.width = 6.0;
    stroke.line_cap = LineCap::Round;
    stroke.dash = StrokeDash::new(vec![20.0, 40.0], 0.0);

    let mut pixmap = Pixmap::new(500, 500).unwrap();
    pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None);
    pixmap.save_png("image.png").unwrap();
}
examples/linear_gradient.rs (line 25)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
fn main() {
    let mut paint = Paint::default();
    paint.shader = LinearGradient::new(
        Point::from_xy(100.0, 100.0),
        Point::from_xy(900.0, 900.0),
        vec![
            GradientStop::new(0.0, Color::from_rgba8(50, 127, 150, 200)),
            GradientStop::new(1.0, Color::from_rgba8(220, 140, 75, 180)),
        ],
        SpreadMode::Pad,
        Transform::identity(),
    )
    .unwrap();

    let mut pb = PathBuilder::new();
    pb.move_to(60.0, 60.0);
    pb.line_to(160.0, 940.0);
    pb.cubic_to(380.0, 840.0, 660.0, 800.0, 940.0, 800.0);
    pb.cubic_to(740.0, 460.0, 440.0, 160.0, 60.0, 60.0);
    pb.close();
    let path = pb.finish().unwrap();

    let mut pixmap = Pixmap::new(1000, 1000).unwrap();
    pixmap.fill_path(
        &path,
        &paint,
        FillRule::Winding,
        Transform::identity(),
        None,
    );
    pixmap.save_png("image.png").unwrap();
}
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)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
fn main() {
    let triangle = create_triangle();

    let mut pixmap = Pixmap::new(400, 400).unwrap();

    let now = std::time::Instant::now();

    let mut paint = PixmapPaint::default();
    paint.quality = FilterQuality::Bicubic;

    pixmap.draw_pixmap(
        20,
        20,
        triangle.as_ref(),
        &paint,
        Transform::from_row(1.2, 0.5, 0.5, 1.2, 0.0, 0.0),
        None,
    );

    println!(
        "Rendered in {:.2}ms",
        now.elapsed().as_micros() as f64 / 1000.0
    );

    pixmap.save_png("image.png").unwrap();
}
More examples
Hide additional examples
examples/pattern.rs (line 26)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
fn main() {
    let triangle = crate_triangle();

    let mut paint = Paint::default();
    paint.anti_alias = true;
    paint.shader = Pattern::new(
        triangle.as_ref(),
        SpreadMode::Repeat,
        FilterQuality::Bicubic,
        1.0,
        Transform::from_row(1.5, -0.4, 0.0, -0.8, 5.0, 1.0),
    );

    let path = PathBuilder::from_circle(200.0, 200.0, 180.0).unwrap();

    let mut pixmap = Pixmap::new(400, 400).unwrap();
    pixmap.fill_path(
        &path,
        &paint,
        FillRule::Winding,
        Transform::identity(),
        None,
    );
    pixmap.save_png("image.png").unwrap();
}
examples/hairline.rs (line 24)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
fn main() {
    let mut pb = PathBuilder::new();
    pb.move_to(50.0, 100.0);
    pb.cubic_to(130.0, 20.0, 390.0, 120.0, 450.0, 30.0);
    let path = pb.finish().unwrap();

    let mut paint = Paint::default();
    paint.set_color_rgba8(50, 127, 150, 200);
    paint.anti_alias = true;

    let mut pixmap = Pixmap::new(500, 500).unwrap();
    let mut transform = Transform::identity();
    for i in 0..20 {
        let mut stroke = Stroke::default();
        stroke.width = 2.0 - (i as f32 / 10.0);
        pixmap.stroke_path(&path, &paint, &stroke, transform, None);
        transform = transform.pre_translate(0.0, 20.0);
    }

    pixmap.save_png("image.png").unwrap();
}
examples/clip_path.rs (line 28)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
fn main() {
    let clip_path = {
        let mut pb = PathBuilder::new();
        pb.push_circle(250.0, 250.0, 200.0);
        pb.push_circle(250.0, 250.0, 100.0);
        pb.finish().unwrap()
    };

    let clip_path = clip_path
        .transform(Transform::from_row(1.0, -0.3, 0.0, 1.0, 0.0, 75.0))
        .unwrap();

    let mut clip_mask = ClipMask::new();
    clip_mask.set_path(500, 500, &clip_path, FillRule::EvenOdd, true);

    let mut paint = Paint::default();
    paint.set_color_rgba8(50, 127, 150, 200);

    let mut pixmap = Pixmap::new(500, 500).unwrap();
    pixmap.fill_rect(
        Rect::from_xywh(0.0, 0.0, 500.0, 500.0).unwrap(),
        &paint,
        Transform::identity(),
        Some(&clip_mask),
    );
    pixmap.save_png("image.png").unwrap();
}
examples/stroke.rs (line 29)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
fn main() {
    let mut paint = Paint::default();
    paint.set_color_rgba8(0, 127, 0, 200);
    paint.anti_alias = true;

    let path = {
        let mut pb = PathBuilder::new();
        const RADIUS: f32 = 250.0;
        const CENTER: f32 = 250.0;
        pb.move_to(CENTER + RADIUS, CENTER);
        for i in 1..8 {
            let a = 2.6927937 * i as f32;
            pb.line_to(CENTER + RADIUS * a.cos(), CENTER + RADIUS * a.sin());
        }
        pb.finish().unwrap()
    };

    let mut stroke = Stroke::default();
    stroke.width = 6.0;
    stroke.line_cap = LineCap::Round;
    stroke.dash = StrokeDash::new(vec![20.0, 40.0], 0.0);

    let mut pixmap = Pixmap::new(500, 500).unwrap();
    pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None);
    pixmap.save_png("image.png").unwrap();
}
examples/linear_gradient.rs (line 33)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
fn main() {
    let mut paint = Paint::default();
    paint.shader = LinearGradient::new(
        Point::from_xy(100.0, 100.0),
        Point::from_xy(900.0, 900.0),
        vec![
            GradientStop::new(0.0, Color::from_rgba8(50, 127, 150, 200)),
            GradientStop::new(1.0, Color::from_rgba8(220, 140, 75, 180)),
        ],
        SpreadMode::Pad,
        Transform::identity(),
    )
    .unwrap();

    let mut pb = PathBuilder::new();
    pb.move_to(60.0, 60.0);
    pb.line_to(160.0, 940.0);
    pb.cubic_to(380.0, 840.0, 660.0, 800.0, 940.0, 800.0);
    pb.cubic_to(740.0, 460.0, 440.0, 160.0, 60.0, 60.0);
    pb.close();
    let path = pb.finish().unwrap();

    let mut pixmap = Pixmap::new(1000, 1000).unwrap();
    pixmap.fill_path(
        &path,
        &paint,
        FillRule::Winding,
        Transform::identity(),
        None,
    );
    pixmap.save_png("image.png").unwrap();
}
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)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
fn main() {
    let triangle = create_triangle();

    let mut pixmap = Pixmap::new(400, 400).unwrap();

    let now = std::time::Instant::now();

    let mut paint = PixmapPaint::default();
    paint.quality = FilterQuality::Bicubic;

    pixmap.draw_pixmap(
        20,
        20,
        triangle.as_ref(),
        &paint,
        Transform::from_row(1.2, 0.5, 0.5, 1.2, 0.0, 0.0),
        None,
    );

    println!(
        "Rendered in {:.2}ms",
        now.elapsed().as_micros() as f64 / 1000.0
    );

    pixmap.save_png("image.png").unwrap();
}
More examples
Hide additional examples
examples/pattern.rs (line 9)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
fn main() {
    let triangle = crate_triangle();

    let mut paint = Paint::default();
    paint.anti_alias = true;
    paint.shader = Pattern::new(
        triangle.as_ref(),
        SpreadMode::Repeat,
        FilterQuality::Bicubic,
        1.0,
        Transform::from_row(1.5, -0.4, 0.0, -0.8, 5.0, 1.0),
    );

    let path = PathBuilder::from_circle(200.0, 200.0, 180.0).unwrap();

    let mut pixmap = Pixmap::new(400, 400).unwrap();
    pixmap.fill_path(
        &path,
        &paint,
        FillRule::Winding,
        Transform::identity(),
        None,
    );
    pixmap.save_png("image.png").unwrap();
}
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, clip_mask: Option<&ClipMask> ) -> Option<()>

Draws a filled rectangle onto the pixmap.

See PixmapMut::fill_rect for details.

Examples found in repository?
examples/clip_path.rs (lines 22-27)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
fn main() {
    let clip_path = {
        let mut pb = PathBuilder::new();
        pb.push_circle(250.0, 250.0, 200.0);
        pb.push_circle(250.0, 250.0, 100.0);
        pb.finish().unwrap()
    };

    let clip_path = clip_path
        .transform(Transform::from_row(1.0, -0.3, 0.0, 1.0, 0.0, 75.0))
        .unwrap();

    let mut clip_mask = ClipMask::new();
    clip_mask.set_path(500, 500, &clip_path, FillRule::EvenOdd, true);

    let mut paint = Paint::default();
    paint.set_color_rgba8(50, 127, 150, 200);

    let mut pixmap = Pixmap::new(500, 500).unwrap();
    pixmap.fill_rect(
        Rect::from_xywh(0.0, 0.0, 500.0, 500.0).unwrap(),
        &paint,
        Transform::identity(),
        Some(&clip_mask),
    );
    pixmap.save_png("image.png").unwrap();
}
More examples
Hide additional examples
examples/large_image.rs (line 43)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
fn main() {
    let path1 = {
        let mut pb = PathBuilder::new();
        pb.move_to(1200.0, 1200.0);
        pb.line_to(3200.0, 18800.0);
        pb.cubic_to(7600.0, 16800.0, 13200.0, 16000.0, 18800.0, 16000.0);
        pb.cubic_to(14800.0, 9200.0, 8800.0, 3200.0, 1200.0, 1200.0);
        pb.close();
        pb.finish().unwrap()
    };

    let path2 = {
        let mut pb = PathBuilder::new();
        pb.move_to(18800.0, 1200.0);
        pb.line_to(16800.0, 18800.0);
        pb.cubic_to(12400.0, 16800.0, 6800.0, 16000.0, 1200.0, 16000.0);
        pb.cubic_to(5200.0, 9200.0, 11200.0, 3200.0, 18800.0, 1200.0);
        pb.close();
        pb.finish().unwrap()
    };

    let mut pixmap = Pixmap::new(20000, 20000).unwrap();

    let clip_path = {
        let mut pb = PathBuilder::new();
        pb.push_circle(10000.0, 10000.0, 7000.0);
        pb.finish().unwrap()
    };

    let mut clip = ClipMask::new();
    clip.set_path(20000, 20000, &clip_path, FillRule::Winding, true);

    let mut paint = Paint::default();
    paint.set_color_rgba8(90, 175, 100, 150);
    paint.anti_alias = true;
    let large_rect = Rect::from_xywh(500.0, 500.0, 19000.0, 19000.0).unwrap();
    pixmap
        .fill_rect(large_rect, &paint, Transform::identity(), None)
        .unwrap();

    paint.set_color_rgba8(50, 127, 150, 200);
    paint.anti_alias = true;
    pixmap.fill_path(
        &path1,
        &paint,
        FillRule::Winding,
        Transform::default(),
        Some(&clip),
    );

    paint.set_color_rgba8(220, 140, 75, 180);
    paint.anti_alias = false;
    pixmap.fill_path(
        &path2,
        &paint,
        FillRule::Winding,
        Transform::default(),
        None,
    );

    paint.set_color_rgba8(255, 10, 15, 180);
    paint.anti_alias = true;
    let mut stroke = Stroke::default();
    stroke.width = 0.8; // hairline
    pixmap.stroke_path(&path2, &paint, &stroke, Transform::default(), None);

    pixmap.save_png("image.png").unwrap();
}
source

pub fn fill_path( &mut self, path: &Path, paint: &Paint<'_>, fill_rule: FillRule, transform: Transform, clip_mask: Option<&ClipMask> ) -> Option<()>

Draws a filled path onto the pixmap.

See PixmapMut::fill_path for details.

Examples found in repository?
examples/pattern.rs (lines 19-25)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
fn main() {
    let triangle = crate_triangle();

    let mut paint = Paint::default();
    paint.anti_alias = true;
    paint.shader = Pattern::new(
        triangle.as_ref(),
        SpreadMode::Repeat,
        FilterQuality::Bicubic,
        1.0,
        Transform::from_row(1.5, -0.4, 0.0, -0.8, 5.0, 1.0),
    );

    let path = PathBuilder::from_circle(200.0, 200.0, 180.0).unwrap();

    let mut pixmap = Pixmap::new(400, 400).unwrap();
    pixmap.fill_path(
        &path,
        &paint,
        FillRule::Winding,
        Transform::identity(),
        None,
    );
    pixmap.save_png("image.png").unwrap();
}

fn crate_triangle() -> Pixmap {
    let mut paint = Paint::default();
    paint.set_color_rgba8(50, 127, 150, 200);
    paint.anti_alias = true;

    let mut pb = PathBuilder::new();
    pb.move_to(0.0, 20.0);
    pb.line_to(20.0, 20.0);
    pb.line_to(10.0, 0.0);
    pb.close();
    let path = pb.finish().unwrap();

    let mut pixmap = Pixmap::new(20, 20).unwrap();
    pixmap.fill_path(
        &path,
        &paint,
        FillRule::Winding,
        Transform::identity(),
        None,
    );
    pixmap
}
More examples
Hide additional examples
examples/image_on_image.rs (lines 44-50)
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
fn create_triangle() -> Pixmap {
    let mut paint = Paint::default();
    paint.set_color_rgba8(50, 127, 150, 200);
    paint.anti_alias = true;

    let mut pb = PathBuilder::new();
    pb.move_to(0.0, 200.0);
    pb.line_to(200.0, 200.0);
    pb.line_to(100.0, 0.0);
    pb.close();
    let path = pb.finish().unwrap();

    let mut pixmap = Pixmap::new(200, 200).unwrap();

    pixmap.fill_path(
        &path,
        &paint,
        FillRule::Winding,
        Transform::identity(),
        None,
    );

    let path = PathBuilder::from_rect(Rect::from_ltrb(0.0, 0.0, 200.0, 200.0).unwrap());
    let stroke = Stroke::default();
    paint.set_color_rgba8(200, 0, 0, 220);

    pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None); // TODO: stroke_rect

    pixmap
}
examples/linear_gradient.rs (lines 26-32)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
fn main() {
    let mut paint = Paint::default();
    paint.shader = LinearGradient::new(
        Point::from_xy(100.0, 100.0),
        Point::from_xy(900.0, 900.0),
        vec![
            GradientStop::new(0.0, Color::from_rgba8(50, 127, 150, 200)),
            GradientStop::new(1.0, Color::from_rgba8(220, 140, 75, 180)),
        ],
        SpreadMode::Pad,
        Transform::identity(),
    )
    .unwrap();

    let mut pb = PathBuilder::new();
    pb.move_to(60.0, 60.0);
    pb.line_to(160.0, 940.0);
    pb.cubic_to(380.0, 840.0, 660.0, 800.0, 940.0, 800.0);
    pb.cubic_to(740.0, 460.0, 440.0, 160.0, 60.0, 60.0);
    pb.close();
    let path = pb.finish().unwrap();

    let mut pixmap = Pixmap::new(1000, 1000).unwrap();
    pixmap.fill_path(
        &path,
        &paint,
        FillRule::Winding,
        Transform::identity(),
        None,
    );
    pixmap.save_png("image.png").unwrap();
}
examples/fill.rs (lines 32-38)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
fn main() {
    let mut paint1 = Paint::default();
    paint1.set_color_rgba8(50, 127, 150, 200);
    paint1.anti_alias = true;

    let mut paint2 = Paint::default();
    paint2.set_color_rgba8(220, 140, 75, 180);

    let path1 = {
        let mut pb = PathBuilder::new();
        pb.move_to(60.0, 60.0);
        pb.line_to(160.0, 940.0);
        pb.cubic_to(380.0, 840.0, 660.0, 800.0, 940.0, 800.0);
        pb.cubic_to(740.0, 460.0, 440.0, 160.0, 60.0, 60.0);
        pb.close();
        pb.finish().unwrap()
    };

    let path2 = {
        let mut pb = PathBuilder::new();
        pb.move_to(940.0, 60.0);
        pb.line_to(840.0, 940.0);
        pb.cubic_to(620.0, 840.0, 340.0, 800.0, 60.0, 800.0);
        pb.cubic_to(260.0, 460.0, 560.0, 160.0, 940.0, 60.0);
        pb.close();
        pb.finish().unwrap()
    };

    let mut pixmap = Pixmap::new(1000, 1000).unwrap();
    pixmap.fill_path(
        &path1,
        &paint1,
        FillRule::Winding,
        Transform::identity(),
        None,
    );
    pixmap.fill_path(
        &path2,
        &paint2,
        FillRule::Winding,
        Transform::identity(),
        None,
    );
    pixmap.save_png("image.png").unwrap();
}
examples/large_image.rs (lines 48-54)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
fn main() {
    let path1 = {
        let mut pb = PathBuilder::new();
        pb.move_to(1200.0, 1200.0);
        pb.line_to(3200.0, 18800.0);
        pb.cubic_to(7600.0, 16800.0, 13200.0, 16000.0, 18800.0, 16000.0);
        pb.cubic_to(14800.0, 9200.0, 8800.0, 3200.0, 1200.0, 1200.0);
        pb.close();
        pb.finish().unwrap()
    };

    let path2 = {
        let mut pb = PathBuilder::new();
        pb.move_to(18800.0, 1200.0);
        pb.line_to(16800.0, 18800.0);
        pb.cubic_to(12400.0, 16800.0, 6800.0, 16000.0, 1200.0, 16000.0);
        pb.cubic_to(5200.0, 9200.0, 11200.0, 3200.0, 18800.0, 1200.0);
        pb.close();
        pb.finish().unwrap()
    };

    let mut pixmap = Pixmap::new(20000, 20000).unwrap();

    let clip_path = {
        let mut pb = PathBuilder::new();
        pb.push_circle(10000.0, 10000.0, 7000.0);
        pb.finish().unwrap()
    };

    let mut clip = ClipMask::new();
    clip.set_path(20000, 20000, &clip_path, FillRule::Winding, true);

    let mut paint = Paint::default();
    paint.set_color_rgba8(90, 175, 100, 150);
    paint.anti_alias = true;
    let large_rect = Rect::from_xywh(500.0, 500.0, 19000.0, 19000.0).unwrap();
    pixmap
        .fill_rect(large_rect, &paint, Transform::identity(), None)
        .unwrap();

    paint.set_color_rgba8(50, 127, 150, 200);
    paint.anti_alias = true;
    pixmap.fill_path(
        &path1,
        &paint,
        FillRule::Winding,
        Transform::default(),
        Some(&clip),
    );

    paint.set_color_rgba8(220, 140, 75, 180);
    paint.anti_alias = false;
    pixmap.fill_path(
        &path2,
        &paint,
        FillRule::Winding,
        Transform::default(),
        None,
    );

    paint.set_color_rgba8(255, 10, 15, 180);
    paint.anti_alias = true;
    let mut stroke = Stroke::default();
    stroke.width = 0.8; // hairline
    pixmap.stroke_path(&path2, &paint, &stroke, Transform::default(), None);

    pixmap.save_png("image.png").unwrap();
}
source

pub fn stroke_path( &mut self, path: &Path, paint: &Paint<'_>, stroke: &Stroke, transform: Transform, clip_mask: Option<&ClipMask> ) -> Option<()>

Strokes a path.

See PixmapMut::stroke_path for details.

Examples found in repository?
examples/hairline.rs (line 20)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
fn main() {
    let mut pb = PathBuilder::new();
    pb.move_to(50.0, 100.0);
    pb.cubic_to(130.0, 20.0, 390.0, 120.0, 450.0, 30.0);
    let path = pb.finish().unwrap();

    let mut paint = Paint::default();
    paint.set_color_rgba8(50, 127, 150, 200);
    paint.anti_alias = true;

    let mut pixmap = Pixmap::new(500, 500).unwrap();
    let mut transform = Transform::identity();
    for i in 0..20 {
        let mut stroke = Stroke::default();
        stroke.width = 2.0 - (i as f32 / 10.0);
        pixmap.stroke_path(&path, &paint, &stroke, transform, None);
        transform = transform.pre_translate(0.0, 20.0);
    }

    pixmap.save_png("image.png").unwrap();
}
More examples
Hide additional examples
examples/image_on_image.rs (line 56)
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
fn create_triangle() -> Pixmap {
    let mut paint = Paint::default();
    paint.set_color_rgba8(50, 127, 150, 200);
    paint.anti_alias = true;

    let mut pb = PathBuilder::new();
    pb.move_to(0.0, 200.0);
    pb.line_to(200.0, 200.0);
    pb.line_to(100.0, 0.0);
    pb.close();
    let path = pb.finish().unwrap();

    let mut pixmap = Pixmap::new(200, 200).unwrap();

    pixmap.fill_path(
        &path,
        &paint,
        FillRule::Winding,
        Transform::identity(),
        None,
    );

    let path = PathBuilder::from_rect(Rect::from_ltrb(0.0, 0.0, 200.0, 200.0).unwrap());
    let stroke = Stroke::default();
    paint.set_color_rgba8(200, 0, 0, 220);

    pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None); // TODO: stroke_rect

    pixmap
}
examples/stroke.rs (line 28)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
fn main() {
    let mut paint = Paint::default();
    paint.set_color_rgba8(0, 127, 0, 200);
    paint.anti_alias = true;

    let path = {
        let mut pb = PathBuilder::new();
        const RADIUS: f32 = 250.0;
        const CENTER: f32 = 250.0;
        pb.move_to(CENTER + RADIUS, CENTER);
        for i in 1..8 {
            let a = 2.6927937 * i as f32;
            pb.line_to(CENTER + RADIUS * a.cos(), CENTER + RADIUS * a.sin());
        }
        pb.finish().unwrap()
    };

    let mut stroke = Stroke::default();
    stroke.width = 6.0;
    stroke.line_cap = LineCap::Round;
    stroke.dash = StrokeDash::new(vec![20.0, 40.0], 0.0);

    let mut pixmap = Pixmap::new(500, 500).unwrap();
    pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None);
    pixmap.save_png("image.png").unwrap();
}
examples/large_image.rs (line 70)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
fn main() {
    let path1 = {
        let mut pb = PathBuilder::new();
        pb.move_to(1200.0, 1200.0);
        pb.line_to(3200.0, 18800.0);
        pb.cubic_to(7600.0, 16800.0, 13200.0, 16000.0, 18800.0, 16000.0);
        pb.cubic_to(14800.0, 9200.0, 8800.0, 3200.0, 1200.0, 1200.0);
        pb.close();
        pb.finish().unwrap()
    };

    let path2 = {
        let mut pb = PathBuilder::new();
        pb.move_to(18800.0, 1200.0);
        pb.line_to(16800.0, 18800.0);
        pb.cubic_to(12400.0, 16800.0, 6800.0, 16000.0, 1200.0, 16000.0);
        pb.cubic_to(5200.0, 9200.0, 11200.0, 3200.0, 18800.0, 1200.0);
        pb.close();
        pb.finish().unwrap()
    };

    let mut pixmap = Pixmap::new(20000, 20000).unwrap();

    let clip_path = {
        let mut pb = PathBuilder::new();
        pb.push_circle(10000.0, 10000.0, 7000.0);
        pb.finish().unwrap()
    };

    let mut clip = ClipMask::new();
    clip.set_path(20000, 20000, &clip_path, FillRule::Winding, true);

    let mut paint = Paint::default();
    paint.set_color_rgba8(90, 175, 100, 150);
    paint.anti_alias = true;
    let large_rect = Rect::from_xywh(500.0, 500.0, 19000.0, 19000.0).unwrap();
    pixmap
        .fill_rect(large_rect, &paint, Transform::identity(), None)
        .unwrap();

    paint.set_color_rgba8(50, 127, 150, 200);
    paint.anti_alias = true;
    pixmap.fill_path(
        &path1,
        &paint,
        FillRule::Winding,
        Transform::default(),
        Some(&clip),
    );

    paint.set_color_rgba8(220, 140, 75, 180);
    paint.anti_alias = false;
    pixmap.fill_path(
        &path2,
        &paint,
        FillRule::Winding,
        Transform::default(),
        None,
    );

    paint.set_color_rgba8(255, 10, 15, 180);
    paint.anti_alias = true;
    let mut stroke = Stroke::default();
    stroke.width = 0.8; // hairline
    pixmap.stroke_path(&path2, &paint, &stroke, Transform::default(), None);

    pixmap.save_png("image.png").unwrap();
}
source

pub fn draw_pixmap( &mut self, x: i32, y: i32, pixmap: PixmapRef<'_>, paint: &PixmapPaint, transform: Transform, clip_mask: Option<&ClipMask> ) -> Option<()>

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)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
fn main() {
    let triangle = create_triangle();

    let mut pixmap = Pixmap::new(400, 400).unwrap();

    let now = std::time::Instant::now();

    let mut paint = PixmapPaint::default();
    paint.quality = FilterQuality::Bicubic;

    pixmap.draw_pixmap(
        20,
        20,
        triangle.as_ref(),
        &paint,
        Transform::from_row(1.2, 0.5, 0.5, 1.2, 0.0, 0.0),
        None,
    );

    println!(
        "Rendered in {:.2}ms",
        now.elapsed().as_micros() as f64 / 1000.0
    );

    pixmap.save_png("image.png").unwrap();
}

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<Pixmap> for Pixmap

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.