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