|
7 | 7 | pub(crate) mod plugin; |
8 | 8 |
|
9 | 9 | use std::borrow::Cow; |
10 | | -use std::io::{Error, ErrorKind}; |
11 | 10 | use std::sync::Arc; |
12 | 11 |
|
13 | 12 | use crate::{Manager, Resource, ResourceId, Runtime}; |
@@ -45,88 +44,32 @@ impl<'a> Image<'a> { |
45 | 44 | } |
46 | 45 | } |
47 | 46 |
|
48 | | - /// Creates a new image using the provided png bytes. |
49 | | - #[cfg(feature = "image-png")] |
50 | | - #[cfg_attr(docsrs, doc(cfg(feature = "image-png")))] |
51 | | - pub fn from_png_bytes(bytes: &[u8]) -> std::io::Result<Self> { |
52 | | - let decoder = png::Decoder::new(std::io::Cursor::new(bytes)); |
53 | | - let mut reader = decoder.read_info()?; |
54 | | - let mut buffer = Vec::new(); |
55 | | - while let Ok(Some(row)) = reader.next_row() { |
56 | | - buffer.extend(row.data()); |
57 | | - } |
58 | | - Ok(Self { |
59 | | - rgba: Cow::Owned(buffer), |
60 | | - width: reader.info().width, |
61 | | - height: reader.info().height, |
62 | | - }) |
63 | | - } |
64 | | - |
65 | | - /// Creates a new image using the provided ico bytes. |
66 | | - #[cfg(feature = "image-ico")] |
67 | | - #[cfg_attr(docsrs, doc(cfg(feature = "image-ico")))] |
68 | | - pub fn from_ico_bytes(bytes: &[u8]) -> std::io::Result<Self> { |
69 | | - let icon_dir = ico::IconDir::read(std::io::Cursor::new(&bytes))?; |
70 | | - let first = icon_dir.entries().first().ok_or_else(|| { |
71 | | - Error::new( |
72 | | - ErrorKind::NotFound, |
73 | | - "Couldn't find any icons inside provided ico bytes", |
74 | | - ) |
75 | | - })?; |
76 | | - |
77 | | - let rgba = first.decode()?.rgba_data().to_vec(); |
78 | | - |
79 | | - Ok(Self { |
80 | | - rgba: Cow::Owned(rgba), |
81 | | - width: first.width(), |
82 | | - height: first.height(), |
83 | | - }) |
84 | | - } |
85 | | - |
86 | 47 | /// Creates a new image using the provided bytes. |
87 | 48 | /// |
88 | 49 | /// Only `ico` and `png` are supported (based on activated feature flag). |
89 | 50 | #[cfg(any(feature = "image-ico", feature = "image-png"))] |
90 | 51 | #[cfg_attr(docsrs, doc(cfg(any(feature = "image-ico", feature = "image-png"))))] |
91 | | - pub fn from_bytes(bytes: &[u8]) -> std::io::Result<Self> { |
92 | | - let extension = infer::get(bytes) |
93 | | - .expect("could not determine icon extension") |
94 | | - .extension(); |
95 | | - |
96 | | - match extension { |
97 | | - #[cfg(feature = "image-ico")] |
98 | | - "ico" => Self::from_ico_bytes(bytes), |
99 | | - #[cfg(feature = "image-png")] |
100 | | - "png" => Self::from_png_bytes(bytes), |
101 | | - _ => { |
102 | | - let supported = [ |
103 | | - #[cfg(feature = "image-png")] |
104 | | - "'png'", |
105 | | - #[cfg(feature = "image-ico")] |
106 | | - "'ico'", |
107 | | - ]; |
108 | | - |
109 | | - Err(Error::new( |
110 | | - ErrorKind::InvalidInput, |
111 | | - format!( |
112 | | - "Unexpected image format, expected {}, found '{extension}'. Please check the `image-*` Cargo features on the tauri crate to see if Tauri has optional support for this format.", |
113 | | - if supported.is_empty() { |
114 | | - "''".to_string() |
115 | | - } else { |
116 | | - supported.join(" or ") |
117 | | - } |
118 | | - ), |
119 | | - )) |
120 | | - } |
121 | | - } |
| 52 | + pub fn from_bytes(bytes: &[u8]) -> crate::Result<Self> { |
| 53 | + use image::GenericImageView; |
| 54 | + |
| 55 | + let img = image::load_from_memory(bytes)?; |
| 56 | + let pixels = img |
| 57 | + .pixels() |
| 58 | + .flat_map(|(_, _, pixel)| pixel.0) |
| 59 | + .collect::<Vec<_>>(); |
| 60 | + Ok(Self { |
| 61 | + rgba: Cow::Owned(pixels), |
| 62 | + width: img.width(), |
| 63 | + height: img.height(), |
| 64 | + }) |
122 | 65 | } |
123 | 66 |
|
124 | 67 | /// Creates a new image using the provided path. |
125 | 68 | /// |
126 | 69 | /// Only `ico` and `png` are supported (based on activated feature flag). |
127 | 70 | #[cfg(any(feature = "image-ico", feature = "image-png"))] |
128 | 71 | #[cfg_attr(docsrs, doc(cfg(any(feature = "image-ico", feature = "image-png"))))] |
129 | | - pub fn from_path<P: AsRef<std::path::Path>>(path: P) -> std::io::Result<Self> { |
| 72 | + pub fn from_path<P: AsRef<std::path::Path>>(path: P) -> crate::Result<Self> { |
130 | 73 | let bytes = std::fs::read(path)?; |
131 | 74 | Self::from_bytes(&bytes) |
132 | 75 | } |
@@ -242,8 +185,8 @@ impl JsImage { |
242 | 185 |
|
243 | 186 | #[cfg(not(any(feature = "image-ico", feature = "image-png")))] |
244 | 187 | _ => Err( |
245 | | - Error::new( |
246 | | - ErrorKind::InvalidInput, |
| 188 | + std::io::Error::new( |
| 189 | + std::io::ErrorKind::InvalidInput, |
247 | 190 | format!( |
248 | 191 | "expected RGBA image data, found {}", |
249 | 192 | match self { |
|
0 commit comments