2026-07-31 19:36:58 -07:00
|
|
|
use std::{
|
|
|
|
|
borrow::Cow,
|
2026-08-01 10:22:41 -07:00
|
|
|
ffi::OsStr,
|
2026-07-31 19:36:58 -07:00
|
|
|
path::{Path, PathBuf},
|
|
|
|
|
};
|
2026-07-09 20:48:34 -07:00
|
|
|
|
2026-08-28 12:28:45 -07:00
|
|
|
use iced::{clipboard, widget};
|
|
|
|
|
use image::{DynamicImage, EncodableLayout, ImageDecoder, ImageReader, ImageResult, RgbaImage};
|
2026-07-31 15:11:41 -07:00
|
|
|
use rfd::AsyncFileDialog;
|
2026-07-09 20:48:34 -07:00
|
|
|
|
|
|
|
|
pub fn load_image(path: impl AsRef<Path>) -> Result<RgbaImage, String> {
|
2026-07-31 14:36:36 -07:00
|
|
|
load_image_impl(path).map_err(|e| e.to_string())
|
2026-07-31 09:37:16 -07:00
|
|
|
}
|
2026-07-31 14:36:36 -07:00
|
|
|
fn load_image_impl(path: impl AsRef<Path>) -> ImageResult<RgbaImage> {
|
2026-08-01 10:22:41 -07:00
|
|
|
let mut decoder = ImageReader::open(&path)?
|
2026-07-31 09:37:16 -07:00
|
|
|
.with_guessed_format()?
|
|
|
|
|
.into_decoder()?;
|
2026-07-09 20:48:34 -07:00
|
|
|
|
2026-07-31 09:37:16 -07:00
|
|
|
let oreintation = decoder.orientation()?;
|
2026-07-29 15:58:08 -07:00
|
|
|
|
2026-07-31 09:37:16 -07:00
|
|
|
let mut decoded_image = DynamicImage::from_decoder(decoder)?;
|
2026-07-30 23:40:14 -07:00
|
|
|
|
2026-08-01 10:22:41 -07:00
|
|
|
// the condition is a workaround to not rotate JXL images twice;
|
|
|
|
|
// since they are already rotated by the decoder
|
|
|
|
|
// (while jpegs for instance aren't)
|
|
|
|
|
if !path
|
|
|
|
|
.as_ref()
|
|
|
|
|
.extension()
|
|
|
|
|
.map(OsStr::to_string_lossy)
|
|
|
|
|
.is_some_and(|s| s == "jxl")
|
|
|
|
|
{
|
|
|
|
|
decoded_image.apply_orientation(oreintation);
|
|
|
|
|
}
|
2026-07-30 23:40:14 -07:00
|
|
|
|
2026-07-09 20:48:34 -07:00
|
|
|
Ok(decoded_image.into_rgba8())
|
|
|
|
|
}
|
2026-08-25 09:37:00 -07:00
|
|
|
pub async fn pick_image() -> Result<PathBuf, Cow<'static, str>> {
|
2026-07-31 15:11:41 -07:00
|
|
|
let Some(filehandle) = AsyncFileDialog::new()
|
2026-07-09 20:48:34 -07:00
|
|
|
.add_filter(
|
|
|
|
|
"image",
|
|
|
|
|
&[
|
|
|
|
|
"png", "PNG", "jpg", "JPG", "jpeg", "JPEG", "avif", "jxl", "bmp", "exr", "ff",
|
2026-08-14 10:25:46 -07:00
|
|
|
"gif", "hdr", "ico", "pnm", "qoi", "tga", "tiff", "webp", "kra", "svg", "svgz",
|
2026-07-09 20:48:34 -07:00
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
.pick_file()
|
2026-07-31 15:11:41 -07:00
|
|
|
.await
|
2026-07-09 20:48:34 -07:00
|
|
|
else {
|
|
|
|
|
return Err("no path to open provided".into());
|
|
|
|
|
};
|
|
|
|
|
|
2026-07-31 15:11:41 -07:00
|
|
|
Ok(filehandle.path().to_owned())
|
2026-07-09 20:48:34 -07:00
|
|
|
}
|
|
|
|
|
|
2026-08-25 09:37:00 -07:00
|
|
|
pub async fn pick_save_path() -> Result<PathBuf, Cow<'static, str>> {
|
2026-07-31 15:11:41 -07:00
|
|
|
let Some(filehandle) = AsyncFileDialog::new().save_file().await else {
|
2026-07-09 20:58:44 -07:00
|
|
|
return Err("no path to save provided".into());
|
2026-07-09 20:48:34 -07:00
|
|
|
};
|
|
|
|
|
|
2026-07-31 15:11:41 -07:00
|
|
|
Ok(filehandle.path().to_owned())
|
2026-07-09 20:48:34 -07:00
|
|
|
}
|
2026-07-30 20:17:21 -07:00
|
|
|
|
2026-07-31 14:36:36 -07:00
|
|
|
pub const fn string_from_filter_type(f: widget::image::FilterMethod) -> &'static str {
|
2026-07-30 20:17:21 -07:00
|
|
|
match f {
|
|
|
|
|
widget::image::FilterMethod::Linear => "bilinear",
|
|
|
|
|
widget::image::FilterMethod::Nearest => "nearest neighbor",
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-31 19:36:58 -07:00
|
|
|
|
2026-08-28 12:28:45 -07:00
|
|
|
pub fn clipboard_image_from_rgbaimage(image: &RgbaImage) -> clipboard::Image {
|
|
|
|
|
clipboard::Image {
|
|
|
|
|
size: (image.width(), image.height()).into(),
|
|
|
|
|
// SAFETY: this is also probably a race condition,
|
|
|
|
|
// where if the user switches the image before it's done,
|
|
|
|
|
// then something will die.
|
|
|
|
|
//
|
|
|
|
|
// however, especially with larger images,
|
|
|
|
|
// copying them is just too expensive.
|
|
|
|
|
rgba: unsafe { std::mem::transmute::<&[u8], &'static [u8]>(image.as_bytes()) }.into(),
|
2026-07-31 19:36:58 -07:00
|
|
|
}
|
|
|
|
|
}
|
2026-08-28 12:28:45 -07:00
|
|
|
pub fn rgbaimage_from_clipboard_image(image: clipboard::Image) -> RgbaImage {
|
|
|
|
|
RgbaImage::from_raw(image.size.width, image.size.height, image.rgba.into()).unwrap()
|
2026-07-31 19:36:58 -07:00
|
|
|
}
|