fix: pull oreintation workaround from imagey
it also made sense to create the thumnail in the function, since it is easier to rotate a smaller image. (this is fine because max_width == max_height)
This commit is contained in:
parent
7d45896a6f
commit
1e46fed95a
2 changed files with 27 additions and 12 deletions
|
|
@ -69,14 +69,11 @@ impl State {
|
||||||
let entry = self.images.entry(path.clone()).or_default();
|
let entry = self.images.entry(path.clone()).or_default();
|
||||||
|
|
||||||
if matches!(entry, MaybeImage::Unloaded) {
|
if matches!(entry, MaybeImage::Unloaded) {
|
||||||
let columns = self.columns;
|
let size = 1000 / self.columns as u32;
|
||||||
let (task, handle) = Task::perform(
|
let (task, handle) = Task::perform(
|
||||||
async move {
|
async move {
|
||||||
utils::load_image(&path)
|
utils::load_thumbnail(&path, size)
|
||||||
.map(|img| {
|
.map(|img| (path, handle_from_image(img)))
|
||||||
let size = 1000 / columns as u32;
|
|
||||||
(path, handle_from_image(img.thumbnail(size, size)))
|
|
||||||
})
|
|
||||||
.map_err(Arc::new)
|
.map_err(Arc::new)
|
||||||
},
|
},
|
||||||
Message::ImageLoaded,
|
Message::ImageLoaded,
|
||||||
|
|
|
||||||
30
src/utils.rs
30
src/utils.rs
|
|
@ -1,9 +1,27 @@
|
||||||
use std::path::Path;
|
use std::{ffi::OsStr, path::Path};
|
||||||
|
|
||||||
use image::{DynamicImage, ImageReader, ImageResult};
|
use image::{DynamicImage, ImageDecoder, ImageReader, ImageResult};
|
||||||
|
|
||||||
pub fn load_image(path: impl AsRef<Path>) -> ImageResult<DynamicImage> {
|
pub fn load_thumbnail(path: impl AsRef<Path>, max_size: u32) -> ImageResult<DynamicImage> {
|
||||||
Ok(DynamicImage::from_decoder(
|
let mut decoder = ImageReader::open(&path)?
|
||||||
ImageReader::open(path)?.into_decoder()?,
|
.with_guessed_format()?
|
||||||
)?)
|
.into_decoder()?;
|
||||||
|
|
||||||
|
let oreintation = decoder.orientation()?;
|
||||||
|
|
||||||
|
let mut decoded_image = DynamicImage::from_decoder(decoder)?.thumbnail(max_size, max_size);
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(decoded_image)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue