Skip to content

Commit

Permalink
Bumpt fast resize to 5.1.0 (#192)
Browse files Browse the repository at this point in the history
* bump fast resize to 5.1.0

* bump fast resize to 5.1.0
  • Loading branch information
edgarriba authored Dec 23, 2024
1 parent caa9d99 commit cb9cda8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
2 changes: 1 addition & 1 deletion crates/kornia-imgproc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ rust-version.workspace = true
version.workspace = true

[dependencies]
fast_image_resize = "3.0.4"
fast_image_resize = "5.1.0"
kornia-core = { workspace = true }
kornia-image = { workspace = true }
num-traits = { workspace = true }
Expand Down
46 changes: 20 additions & 26 deletions crates/kornia-imgproc/src/resize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::{
};
use fast_image_resize::{self as fr};
use kornia_image::{Image, ImageError};
use std::num::NonZeroU32;

/// Resize an image to a new size.
///
Expand Down Expand Up @@ -157,43 +156,38 @@ pub fn resize_fast(
}

// prepare the input image for the fast_image_resize crate
let src_width = NonZeroU32::new(src.width() as u32).ok_or(ImageError::CastError)?;
let src_height = NonZeroU32::new(src.height() as u32).ok_or(ImageError::CastError)?;
let (src_cols, src_rows) = (src.cols(), src.rows());
let src_data_len = src.as_slice().len();

let src_data_len = src_width.get() as usize * src_height.get() as usize * 3;

let src_image = fr::ImageView::<fast_image_resize::pixels::U8x3>::from_buffer(
src_width,
src_height,
let src_image = fr::images::ImageRef::new(
src_cols as u32,
src_rows as u32,
src.as_slice(),
fr::PixelType::U8x3,
)
.map_err(|_| ImageError::InvalidChannelShape(src_data_len, src.width() * src.height() * 3))?;
.map_err(|_| ImageError::InvalidChannelShape(src_data_len, src_cols * src_rows * 3))?;

// prepare the output image for the fast_image_resize crate
let dst_width = NonZeroU32::new(dst.width() as u32).ok_or(ImageError::CastError)?;
let dst_height = NonZeroU32::new(dst.height() as u32).ok_or(ImageError::CastError)?;
let (dst_cols, dst_rows) = (dst.cols(), dst.rows());
let dst_data_len = dst.as_slice_mut().len();

let dst_data_len = dst_width.get() as usize * dst_height.get() as usize * 3;

let mut dst_image = fr::Image::from_slice_u8(
dst_width,
dst_height,
let mut dst_image = fr::images::Image::from_slice_u8(
dst_cols as u32,
dst_rows as u32,
dst.as_slice_mut(),
fr::PixelType::U8x3,
)
.map_err(|_| ImageError::InvalidChannelShape(dst_data_len, dst_data_len))?;

let mut resizer = {
match interpolation {
InterpolationMode::Bilinear => {
fr::Resizer::new(fr::ResizeAlg::Convolution(fr::FilterType::Bilinear))
}
InterpolationMode::Nearest => fr::Resizer::new(fr::ResizeAlg::Nearest),
}
.map_err(|_| ImageError::InvalidChannelShape(dst_data_len, dst_cols * dst_rows * 3))?;

let mut options = fr::ResizeOptions::new();
options.algorithm = match interpolation {
InterpolationMode::Bilinear => fr::ResizeAlg::Convolution(fr::FilterType::Bilinear),
InterpolationMode::Nearest => fr::ResizeAlg::Nearest,
};

let mut resizer = fr::Resizer::new();
resizer
.resize(&src_image.into(), &mut dst_image.view_mut())
.resize(&src_image, &mut dst_image, &options)
.map_err(|_| ImageError::IncompatiblePixelTypes)?;

Ok(())
Expand Down

0 comments on commit cb9cda8

Please sign in to comment.