Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
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)]
|
||||
struct State {
|
||||
image: Option<RgbaImage>,
|
||||
image_display: Option<widget::image::Allocation>,
|
||||
image_filter: widget::image::FilterMethod,
|
||||
|
||||
|
|
@ -168,8 +167,7 @@ impl State {
|
|||
if image.dimensions() < (100, 100) {
|
||||
self.image_filter = widget::image::FilterMethod::Nearest;
|
||||
}
|
||||
self.image = Some(image);
|
||||
self.allocate_image()
|
||||
self.allocate_image(image)
|
||||
})
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
fn allocate_image(&self) -> Task<Message> {
|
||||
let Some(image) = self.image.as_ref() else {
|
||||
eprintln!("no image to allocate");
|
||||
return Task::none();
|
||||
};
|
||||
|
||||
fn allocate_image(&self, image: RgbaImage) -> Task<Message> {
|
||||
widget::image::allocate(widget::image::Handle::from_rgba(
|
||||
image.width(),
|
||||
image.height(),
|
||||
unsafe { std::mem::transmute::<&[u8], &'static [u8]>(image.as_bytes()) },
|
||||
image.into_vec(),
|
||||
))
|
||||
.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 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> {
|
||||
_load_image(path).map_err(|e| e.to_string())
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue