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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use std::fmt;
use std::io;
#[cfg(feature = "image_rs")]
use std::path::Path;
#[derive(Debug, Clone)]
pub struct Icon(iced_winit::winit::window::Icon);
impl Icon {
pub fn from_rgba(
rgba: Vec<u8>,
width: u32,
height: u32,
) -> Result<Self, Error> {
let raw =
iced_winit::winit::window::Icon::from_rgba(rgba, width, height)?;
Ok(Icon(raw))
}
#[cfg(feature = "image_rs")]
pub fn from_file<P: AsRef<Path>>(icon_path: P) -> Result<Self, Error> {
let icon = image_rs::io::Reader::open(icon_path)?.decode()?.to_rgba8();
Self::from_rgba(icon.to_vec(), icon.width(), icon.height())
}
#[cfg(feature = "image_rs")]
pub fn from_file_data(
data: &[u8],
explicit_format: Option<image_rs::ImageFormat>,
) -> Result<Self, Error> {
let mut icon = image_rs::io::Reader::new(std::io::Cursor::new(data));
let icon_with_format = match explicit_format {
Some(format) => {
icon.set_format(format);
icon
}
None => icon.with_guessed_format()?,
};
let pixels = icon_with_format.decode()?.to_rgba8();
Self::from_rgba(pixels.to_vec(), pixels.width(), pixels.height())
}
}
#[derive(Debug)]
pub enum Error {
InvalidData {
byte_count: usize,
},
DimensionsMismatch {
width: u32,
height: u32,
pixel_count: usize,
},
OsError(io::Error),
#[cfg(feature = "image_rs")]
ImageError(image_rs::error::ImageError),
}
impl From<std::io::Error> for Error {
fn from(os_error: std::io::Error) -> Self {
Error::OsError(os_error)
}
}
impl From<iced_winit::winit::window::BadIcon> for Error {
fn from(error: iced_winit::winit::window::BadIcon) -> Self {
use iced_winit::winit::window::BadIcon;
match error {
BadIcon::ByteCountNotDivisibleBy4 { byte_count } => {
Error::InvalidData { byte_count }
}
BadIcon::DimensionsVsPixelCount {
width,
height,
pixel_count,
..
} => Error::DimensionsMismatch {
width,
height,
pixel_count,
},
BadIcon::OsError(os_error) => Error::OsError(os_error),
}
}
}
impl From<Icon> for iced_winit::winit::window::Icon {
fn from(icon: Icon) -> Self {
icon.0
}
}
#[cfg(feature = "image_rs")]
impl From<image_rs::error::ImageError> for Error {
fn from(image_error: image_rs::error::ImageError) -> Self {
Self::ImageError(image_error)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::InvalidData { byte_count } => {
write!(
f,
"The provided RGBA data (with length {:?}) isn't divisble by \
4. Therefore, it cannot be safely interpreted as 32bpp RGBA \
pixels.",
byte_count,
)
}
Error::DimensionsMismatch {
width,
height,
pixel_count,
} => {
write!(
f,
"The number of RGBA pixels ({:?}) does not match the provided \
dimensions ({:?}x{:?}).",
pixel_count, width, height,
)
}
Error::OsError(e) => write!(
f,
"The underlying OS failed to create the window \
icon: {:?}",
e
),
#[cfg(feature = "image_rs")]
Error::ImageError(e) => {
write!(f, "Unable to create icon from a file: {:?}", e)
}
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(self)
}
}