1pub use crate::core::window::icon::*;
3
4use crate::core::window::icon;
5
6use std::io;
7
8#[cfg(feature = "image")]
9use std::path::Path;
10
11#[cfg(feature = "image")]
15pub fn from_file<P: AsRef<Path>>(icon_path: P) -> Result<Icon, Error> {
16 let icon = image::ImageReader::open(icon_path)?.decode()?.to_rgba8();
17
18 Ok(icon::from_rgba(icon.to_vec(), icon.width(), icon.height())?)
19}
20
21#[cfg(feature = "image")]
26pub fn from_file_data(
27 data: &[u8],
28 explicit_format: Option<image::ImageFormat>,
29) -> Result<Icon, Error> {
30 let mut icon = image::ImageReader::new(std::io::Cursor::new(data));
31
32 let icon_with_format = match explicit_format {
33 Some(format) => {
34 icon.set_format(format);
35 icon
36 }
37 None => icon.with_guessed_format()?,
38 };
39
40 let pixels = icon_with_format.decode()?.to_rgba8();
41
42 Ok(icon::from_rgba(
43 pixels.to_vec(),
44 pixels.width(),
45 pixels.height(),
46 )?)
47}
48
49#[derive(Debug, thiserror::Error)]
51pub enum Error {
52 #[error("The icon is invalid: {0}")]
54 InvalidError(#[from] icon::Error),
55
56 #[error("The underlying OS failed to create the window icon: {0}")]
58 OsError(#[from] io::Error),
59
60 #[cfg(feature = "image")]
62 #[error("Unable to create icon from a file: {0}")]
63 ImageError(#[from] image::error::ImageError),
64}