feat: saving as jxl
This commit is contained in:
parent
770bdc76d3
commit
30b0662731
1 changed files with 27 additions and 9 deletions
36
src/main.rs
36
src/main.rs
|
|
@ -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()));
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue