feat: opening jxls
This commit is contained in:
parent
30b0662731
commit
9b9c04c470
1 changed files with 69 additions and 37 deletions
114
src/main.rs
114
src/main.rs
|
|
@ -14,7 +14,7 @@ use iced::{
|
|||
use image::{
|
||||
DynamicImage, EncodableLayout, ImageDecoder, ImageReader, RgbImage, RgbaImage, imageops,
|
||||
};
|
||||
use jpegxl_rs::decode::JxlDecoder;
|
||||
use jpegxl_rs::image::ToDynamic;
|
||||
use rfd::FileDialog;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
|
|
@ -123,48 +123,80 @@ impl State {
|
|||
}
|
||||
|
||||
fn load_image(&mut self, path: impl AsRef<Path>) -> Option<String> {
|
||||
let reader = match ImageReader::open(&path) {
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
return Some(format!(
|
||||
"failed to create reader for '{}': {e}",
|
||||
path.as_ref().display()
|
||||
));
|
||||
let path = path.as_ref();
|
||||
|
||||
let decoded_image = match path.extension().map(OsStr::to_string_lossy).as_deref() {
|
||||
Some("jxl") => {
|
||||
let data = match std::fs::read(path) {
|
||||
Ok(d) => d,
|
||||
Err(e) => {
|
||||
return Some(format!(
|
||||
"failed to read data from '{}': {e}",
|
||||
path.display()
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
match jpegxl_rs::decoder_builder()
|
||||
.build()
|
||||
.unwrap()
|
||||
.decode_to_image(&data)
|
||||
{
|
||||
Ok(Some(decoded_image)) => decoded_image,
|
||||
Ok(None) => {
|
||||
return Some(format!(
|
||||
"failed to convert jxl '{}' to DynamicImage",
|
||||
path.display()
|
||||
));
|
||||
}
|
||||
Err(e) => return Some(format!("faled to decode '{}' {e}", path.display())),
|
||||
}
|
||||
}
|
||||
|
||||
_ => {
|
||||
let reader = match ImageReader::open(&path) {
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
return Some(format!(
|
||||
"failed to create reader for '{}': {e}",
|
||||
path.display()
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
let mut decoder = match reader.into_decoder() {
|
||||
Ok(d) => d,
|
||||
Err(e) => {
|
||||
return Some(format!(
|
||||
"failed to create decoder for '{}': {e}",
|
||||
path.display()
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
let oreintation = match decoder.orientation() {
|
||||
Ok(o) => o,
|
||||
Err(e) => {
|
||||
return Some(format!(
|
||||
"failed to get oreintation of '{}': {e}",
|
||||
path.display()
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
let mut decoded_image = match DynamicImage::from_decoder(decoder) {
|
||||
Ok(i) => i,
|
||||
Err(e) => {
|
||||
return Some(format!("failed to decode image '{}': {e}", path.display()));
|
||||
}
|
||||
};
|
||||
|
||||
decoded_image.apply_orientation(oreintation);
|
||||
|
||||
decoded_image
|
||||
}
|
||||
};
|
||||
|
||||
let mut decoder = match reader.into_decoder() {
|
||||
Ok(d) => d,
|
||||
Err(e) => {
|
||||
return Some(format!(
|
||||
"failed to create decoder for '{}': {e}",
|
||||
path.as_ref().display()
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
let oreintation = match decoder.orientation() {
|
||||
Ok(o) => o,
|
||||
Err(e) => {
|
||||
return Some(format!(
|
||||
"failed to get oreintation of '{}': {e}",
|
||||
path.as_ref().display()
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
let mut decoded_image = match DynamicImage::from_decoder(decoder) {
|
||||
Ok(i) => i,
|
||||
Err(e) => {
|
||||
return Some(format!(
|
||||
"failed to decode image '{}': {e}",
|
||||
path.as_ref().display()
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
decoded_image.apply_orientation(oreintation);
|
||||
|
||||
self.image = Some(decoded_image.into_rgba8());
|
||||
|
||||
None
|
||||
|
|
@ -219,7 +251,7 @@ impl State {
|
|||
return Some(format!("failed to write jxl to '{}': {e}", path.display()));
|
||||
};
|
||||
}
|
||||
None | Some(_) => {
|
||||
_ => {
|
||||
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