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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//! Image structure for widget icons and favicons.
/// Image pixel format.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ImageFormat {
/// Unknown/unspecified format.
Unknown,
/// Raw RGBA pixel data (8 bits per channel).
Rgba8,
/// Raw RGB pixel data (8 bits per channel).
Rgb8,
/// PNG encoded image.
Png,
/// JPEG encoded image.
Jpeg,
/// BMP encoded image.
Bmp,
}
/// Image structure for widget icons and favicons.
#[derive(Debug, Clone, PartialEq)]
pub struct Image {
pub data: Vec<u8>,
pub format: ImageFormat,
pub width: u32,
pub height: u32,
}
impl Image {
/// Creates an empty image.
pub fn new() -> Self {
Self { data: Vec::new(), format: ImageFormat::Unknown, width: 0, height: 0 }
}
/// Creates an image from raw RGBA data.
///
/// # Panics
/// Panics if `data.len()` does not equal `width * height * 4`.
pub fn from_rgba(data: Vec<u8>, width: u32, height: u32) -> Self {
assert_eq!(
data.len(),
width as usize * height as usize * 4,
"Image::from_rgba: data length {} does not match {width}x{height} RGBA (expected {})",
data.len(),
width as usize * height as usize * 4
);
Self { data, format: ImageFormat::Rgba8, width, height }
}
/// Returns the image width in pixels.
pub fn width(&self) -> u32 {
self.width
}
/// Returns the image height in pixels.
pub fn height(&self) -> u32 {
self.height
}
/// Returns the image format.
pub fn format(&self) -> ImageFormat {
self.format
}
/// Returns whether the image has pixel data.
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
/// Returns the raw pixel data.
pub fn data(&self) -> &[u8] {
&self.data
}
/// Decode image from raw bytes into RGBA8 pixels.
///
/// Supported inputs (real codecs): PNG, JPEG, BMP, PNM (P5/P6), QOI and
/// Farbfeld. GIF, WebP, TIFF, AVIF, ICO and SVG are detected but have no
/// codec, so decoding them returns an `Err` (never placeholder pixels).
///
/// Returns an `Image` with RGBA8 pixel data ready for rendering.
#[cfg(feature = "image")]
pub fn from_bytes(data: &[u8]) -> Result<Self, String> {
let decoded = crate::image::decoder::decode_to_rgba8(data)?;
let pixel_data = match decoded.data {
crate::image::format::ImageData::Rgba8(d) => d,
_ => return Err("Decoded image is not RGBA8".to_string()),
};
Ok(Self {
data: pixel_data,
format: ImageFormat::Rgba8,
width: decoded.width,
height: decoded.height,
})
}
/// Load an image from a file path.
///
/// Supported inputs (real codecs): PNG, JPEG, BMP, PNM (P5/P6), QOI and
/// Farbfeld; other detected formats return `Err` since no codec is
/// implemented. The image is decoded into RGBA8 pixel data for rendering.
#[cfg(feature = "image")]
pub fn from_file(path: &str) -> Result<Self, String> {
use std::io::Read;
let mut file = std::fs::File::open(path)
.map_err(|e| format!("Failed to open image file '{path}': {e}"))?;
let mut data = Vec::new();
file.read_to_end(&mut data)
.map_err(|e| format!("Failed to read image file '{path}': {e}"))?;
Self::from_bytes(&data)
}
}
impl Default for Image {
fn default() -> Self {
Self::new()
}
}