feat: saving as jxl

This commit is contained in:
electria 2026-07-03 14:02:39 -07:00
commit 30b0662731
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs

View file

@ -1,4 +1,10 @@
use std::{env, ffi::OsStr, fs, io::Read, path::Path}; use std::{
env,
ffi::OsStr,
fs,
io::{Read, Write},
path::Path,
};
use iced::{ use iced::{
Alignment, Color, Element, Length, Renderer, Subscription, Task, Theme, color, event, Alignment, Color, Element, Length, Renderer, Subscription, Task, Theme, color, event,
@ -6,8 +12,7 @@ use iced::{
theme, widget, window, theme, widget, window,
}; };
use image::{ use image::{
DynamicImage, EncodableLayout, ImageDecoder, ImageReader, RgbImage, RgbaImage, DynamicImage, EncodableLayout, ImageDecoder, ImageReader, RgbImage, RgbaImage, imageops,
codecs::webp::WebPEncoder, imageops,
}; };
use jpegxl_rs::decode::JxlDecoder; use jpegxl_rs::decode::JxlDecoder;
use rfd::FileDialog; use rfd::FileDialog;
@ -197,14 +202,27 @@ impl State {
match path.extension().map(OsStr::to_string_lossy).as_deref() { match path.extension().map(OsStr::to_string_lossy).as_deref() {
Some("jxl") => { Some("jxl") => {
jpegxl_rs::encoder_builder().build().unwrap().encode( let mut encoder = jpegxl_rs::encoder_builder().build().unwrap();
RgbImage::from(DynamicImage::from(*image)).as_bytes(), let rgb_image = DynamicImage::from(image.to_owned()).into_rgb8();
image.width(), let jxl = match encoder.encode::<u8, u8>(
image.height(), &rgb_image,
); rgb_image.width(),
rgb_image.height(),
) {
Ok(j) => j,
Err(e) => {
return Some(format!("failed to encode jxl '{}': {e}", path.display()));
}
};
if let Err(e) = file.write(&jxl.data) {
return Some(format!("failed to write jxl to '{}': {e}", path.display()));
};
} }
None | Some(_) => { None | Some(_) => {
image.save(path); if let Err(e) = image.save(&path) {
return Some(format!("failed to save '{}': {e}", path.display()));
};
} }
}; };