Skip to content

Commit

Permalink
use fast_image_resize
Browse files Browse the repository at this point in the history
  • Loading branch information
sehnryr committed Dec 13, 2024
1 parent 0e9895f commit 2627c0d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 14 deletions.
34 changes: 32 additions & 2 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ crate-type = ["staticlib", "cdylib", "rlib"]
tauri-build = { version = "2.0.2", features = [] }

[dependencies]
fast_image_resize = { version = "5.1.0", features = ["image"] }
flate2 = "1.0.34"
image = "0.25.5"
log = "0.4.22"
Expand Down
5 changes: 5 additions & 0 deletions src-tauri/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pub enum Error {
#[error("Image error: {0}")]
Image(#[from] image::ImageError),

#[error("Image resize error: {0}")]
ImageResize(#[from] fast_image_resize::ResizeError),

#[error("Extension not found: {0}")]
ExtensionNotFound(String),

Expand All @@ -38,6 +41,7 @@ enum ErrorKind {
FileNotFound(String),
Parse(String),
Image(String),
ImageResize(String),
ExtensionNotFound(String),
ExtensionMethod(String),
Wasm(String),
Expand All @@ -55,6 +59,7 @@ impl serde::Serialize for Error {
Self::FileNotFound(_) => ErrorKind::FileNotFound(error_message),
Self::Parse(_) => ErrorKind::Parse(error_message),
Self::Image(_) => ErrorKind::Image(error_message),
Self::ImageResize(_) => ErrorKind::ImageResize(error_message),
Self::ExtensionNotFound(_) => ErrorKind::ExtensionNotFound(error_message),
Self::ExtensionMethod(_) => ErrorKind::ExtensionMethod(error_message),
Self::Wasm(_) => ErrorKind::Wasm(error_message),
Expand Down
40 changes: 28 additions & 12 deletions src-tauri/src/util/image.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::io::Cursor;

use image::imageops::FilterType;
use fast_image_resize::{CropBox, FilterType, ResizeAlg, ResizeOptions, Resizer, SrcCropping};
use image::{DynamicImage, ImageFormat, ImageReader};

use crate::error::Error;
Expand All @@ -9,8 +9,8 @@ use crate::Result;
pub struct Image {
format: ImageFormat,
image_src: DynamicImage,
width: usize,
height: usize,
width: u32,
height: u32,
}

impl TryFrom<Vec<u8>> for Image {
Expand All @@ -23,8 +23,8 @@ impl TryFrom<Vec<u8>> for Image {
.ok_or(Error::Parse("image format could not be guessed.".into()))?;

let image_src = reader.decode()?;
let width = image_src.width() as usize;
let height = image_src.height() as usize;
let width = image_src.width();
let height = image_src.height();

Ok(Image {
format,
Expand All @@ -51,13 +51,29 @@ impl Image {
self.format
}

pub fn resize(mut self, width: usize, height: usize) -> Result<Self> {
self.image_src =
self.image_src
.resize_to_fill(width as u32, height as u32, FilterType::Triangle);
self.width = width;
self.height = height;
pub fn resize(self, width: u32, height: u32) -> Result<Self> {
let mut image_src = DynamicImage::new(width, height, self.image_src.color());

Ok(self)
let mut resizer = Resizer::new();
let resize_options = ResizeOptions {
algorithm: ResizeAlg::Convolution(FilterType::Lanczos3),
cropping: SrcCropping::Crop(CropBox::fit_src_into_dst_size(
self.width,
self.height,
width,
height,
Some((0.5, 0.5)),
)),
..Default::default()
};

resizer.resize(&self.image_src, &mut image_src, Some(&resize_options))?;

Ok(Image {
format: self.format,
image_src,
width,
height,
})
}
}

0 comments on commit 2627c0d

Please sign in to comment.