1
2
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
//! Image representation for rendered plots
/// In-memory image representation
#[derive(Debug, Clone)]
pub struct Image {
/// Width in pixels
pub width: u32,
/// Height in pixels
pub height: u32,
/// Pixel data in RGBA byte order.
///
/// The alpha representation depends on the producer. Renderer-native images
/// are typically premultiplied alpha for efficient composition, while PNG
/// encoding expects straight-alpha RGBA bytes.
pub pixels: Vec<u8>,
}
impl Image {
/// Create a new image
pub fn new(width: u32, height: u32, pixels: Vec<u8>) -> Self {
Self {
width,
height,
pixels,
}
}
/// Get image width
pub fn width(&self) -> u32 {
self.width
}
/// Get image height
pub fn height(&self) -> u32 {
self.height
}
/// Encode the image as PNG bytes.
pub fn encode_png(&self) -> crate::core::Result<Vec<u8>> {
crate::export::encode_rgba_png(self)
}
}