try to give ownership of the image to the Handle
this doesn't work without copying, because RgbaImage is always an owned type as such, it is likely not worth it
This commit is contained in:
parent
5a84b56025
commit
e8761f8fcf
2 changed files with 15 additions and 10 deletions
13
src/main.rs
13
src/main.rs
|
|
@ -19,7 +19,6 @@ enum Message {
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default)]
|
#[derive(Clone, Debug, Default)]
|
||||||
struct State {
|
struct State {
|
||||||
image: Option<RgbaImage>,
|
|
||||||
image_display: Option<widget::image::Allocation>,
|
image_display: Option<widget::image::Allocation>,
|
||||||
image_filter: widget::image::FilterMethod,
|
image_filter: widget::image::FilterMethod,
|
||||||
|
|
||||||
|
|
@ -168,8 +167,7 @@ impl State {
|
||||||
if image.dimensions() < (100, 100) {
|
if image.dimensions() < (100, 100) {
|
||||||
self.image_filter = widget::image::FilterMethod::Nearest;
|
self.image_filter = widget::image::FilterMethod::Nearest;
|
||||||
}
|
}
|
||||||
self.image = Some(image);
|
self.allocate_image(image)
|
||||||
self.allocate_image()
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
fn pick_and_load_image(&mut self) -> Result<Task<Message>, String> {
|
fn pick_and_load_image(&mut self) -> Result<Task<Message>, String> {
|
||||||
|
|
@ -180,16 +178,11 @@ impl State {
|
||||||
self.error = utils::save_image(self.image.as_ref()).err();
|
self.error = utils::save_image(self.image.as_ref()).err();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn allocate_image(&self) -> Task<Message> {
|
fn allocate_image(&self, image: RgbaImage) -> Task<Message> {
|
||||||
let Some(image) = self.image.as_ref() else {
|
|
||||||
eprintln!("no image to allocate");
|
|
||||||
return Task::none();
|
|
||||||
};
|
|
||||||
|
|
||||||
widget::image::allocate(widget::image::Handle::from_rgba(
|
widget::image::allocate(widget::image::Handle::from_rgba(
|
||||||
image.width(),
|
image.width(),
|
||||||
image.height(),
|
image.height(),
|
||||||
unsafe { std::mem::transmute::<&[u8], &'static [u8]>(image.as_bytes()) },
|
image.into_vec(),
|
||||||
))
|
))
|
||||||
.map(Message::ImageDisplayReady)
|
.map(Message::ImageDisplayReady)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
12
src/utils.rs
12
src/utils.rs
|
|
@ -4,6 +4,18 @@ use iced::widget;
|
||||||
use image::{DynamicImage, ImageDecoder, ImageReader, ImageResult, RgbaImage};
|
use image::{DynamicImage, ImageDecoder, ImageReader, ImageResult, RgbaImage};
|
||||||
use rfd::FileDialog;
|
use rfd::FileDialog;
|
||||||
|
|
||||||
|
pub fn image_from_handle(handle: widget::image::Handle) -> RgbaImage {
|
||||||
|
match handle {
|
||||||
|
widget::image::Handle::Rgba {
|
||||||
|
id,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
pixels,
|
||||||
|
} => RgbaImage::from_raw(width, height, pixels.as_ref()).unwrap(),
|
||||||
|
_ => panic!("handle should always hold rgba data"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn load_image(path: impl AsRef<Path>) -> Result<RgbaImage, String> {
|
pub fn load_image(path: impl AsRef<Path>) -> Result<RgbaImage, String> {
|
||||||
_load_image(path).map_err(|e| e.to_string())
|
_load_image(path).map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue