Skip to content
This repository was archived by the owner on Jul 7, 2025. It is now read-only.

Commit 2627c0d

Browse files
committed
use fast_image_resize
1 parent 0e9895f commit 2627c0d

4 files changed

Lines changed: 66 additions & 14 deletions

File tree

src-tauri/Cargo.lock

Lines changed: 32 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ crate-type = ["staticlib", "cdylib", "rlib"]
2222
tauri-build = { version = "2.0.2", features = [] }
2323

2424
[dependencies]
25+
fast_image_resize = { version = "5.1.0", features = ["image"] }
2526
flate2 = "1.0.34"
2627
image = "0.25.5"
2728
log = "0.4.22"

src-tauri/src/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ pub enum Error {
1919
#[error("Image error: {0}")]
2020
Image(#[from] image::ImageError),
2121

22+
#[error("Image resize error: {0}")]
23+
ImageResize(#[from] fast_image_resize::ResizeError),
24+
2225
#[error("Extension not found: {0}")]
2326
ExtensionNotFound(String),
2427

@@ -38,6 +41,7 @@ enum ErrorKind {
3841
FileNotFound(String),
3942
Parse(String),
4043
Image(String),
44+
ImageResize(String),
4145
ExtensionNotFound(String),
4246
ExtensionMethod(String),
4347
Wasm(String),
@@ -55,6 +59,7 @@ impl serde::Serialize for Error {
5559
Self::FileNotFound(_) => ErrorKind::FileNotFound(error_message),
5660
Self::Parse(_) => ErrorKind::Parse(error_message),
5761
Self::Image(_) => ErrorKind::Image(error_message),
62+
Self::ImageResize(_) => ErrorKind::ImageResize(error_message),
5863
Self::ExtensionNotFound(_) => ErrorKind::ExtensionNotFound(error_message),
5964
Self::ExtensionMethod(_) => ErrorKind::ExtensionMethod(error_message),
6065
Self::Wasm(_) => ErrorKind::Wasm(error_message),

src-tauri/src/util/image.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::io::Cursor;
22

3-
use image::imageops::FilterType;
3+
use fast_image_resize::{CropBox, FilterType, ResizeAlg, ResizeOptions, Resizer, SrcCropping};
44
use image::{DynamicImage, ImageFormat, ImageReader};
55

66
use crate::error::Error;
@@ -9,8 +9,8 @@ use crate::Result;
99
pub struct Image {
1010
format: ImageFormat,
1111
image_src: DynamicImage,
12-
width: usize,
13-
height: usize,
12+
width: u32,
13+
height: u32,
1414
}
1515

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

2525
let image_src = reader.decode()?;
26-
let width = image_src.width() as usize;
27-
let height = image_src.height() as usize;
26+
let width = image_src.width();
27+
let height = image_src.height();
2828

2929
Ok(Image {
3030
format,
@@ -51,13 +51,29 @@ impl Image {
5151
self.format
5252
}
5353

54-
pub fn resize(mut self, width: usize, height: usize) -> Result<Self> {
55-
self.image_src =
56-
self.image_src
57-
.resize_to_fill(width as u32, height as u32, FilterType::Triangle);
58-
self.width = width;
59-
self.height = height;
54+
pub fn resize(self, width: u32, height: u32) -> Result<Self> {
55+
let mut image_src = DynamicImage::new(width, height, self.image_src.color());
6056

61-
Ok(self)
57+
let mut resizer = Resizer::new();
58+
let resize_options = ResizeOptions {
59+
algorithm: ResizeAlg::Convolution(FilterType::Lanczos3),
60+
cropping: SrcCropping::Crop(CropBox::fit_src_into_dst_size(
61+
self.width,
62+
self.height,
63+
width,
64+
height,
65+
Some((0.5, 0.5)),
66+
)),
67+
..Default::default()
68+
};
69+
70+
resizer.resize(&self.image_src, &mut image_src, Some(&resize_options))?;
71+
72+
Ok(Image {
73+
format: self.format,
74+
image_src,
75+
width,
76+
height,
77+
})
6278
}
6379
}

0 commit comments

Comments
 (0)