Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if resizing to the same size #1763

Merged
merged 8 commits into from
Jul 24, 2022
Merged
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions src/imageops/sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::f32;

use num_traits::{NumCast, ToPrimitive, Zero};

use crate::image::GenericImageView;
use crate::image::{GenericImage, GenericImageView};
use crate::traits::{Enlargeable, Pixel, Primitive};
use crate::utils::clamp;
use crate::{ImageBuffer, Rgba32FImage};
Expand Down Expand Up @@ -750,16 +750,24 @@ where
/// Resize the supplied image to the specified dimensions.
/// ```nwidth``` and ```nheight``` are the new dimensions.
/// ```filter``` is the sampling filter to use.
pub fn resize<I: GenericImageView>(
pub fn resize<I, P, S>(
image: &I,
nwidth: u32,
nheight: u32,
filter: FilterType,
) -> ImageBuffer<I::Pixel, Vec<<I::Pixel as Pixel>::Subpixel>>
) -> ImageBuffer<P, Vec<S>>
where
I::Pixel: 'static,
<I::Pixel as Pixel>::Subpixel: 'static,
I: GenericImageView<Pixel = P>,
P: Pixel<Subpixel = S> + 'static,
S: Primitive + 'static,
{
// check if the new dimensions are the same as the old. if they are, make a copy instead of resampling
if (nwidth, nheight) == image.dimensions() {
let mut tmp = ImageBuffer::new(image.width(), image.height());
tmp.copy_from(image, 0, 0).unwrap();
return tmp;
}

let mut method = match filter {
FilterType::Nearest => Filter {
kernel: Box::new(box_kernel),
Expand Down Expand Up @@ -860,7 +868,7 @@ where
#[cfg(test)]
mod tests {
use super::{resize, FilterType};
use crate::{ImageBuffer, RgbImage};
use crate::{GenericImageView, ImageBuffer, RgbImage};
#[cfg(feature = "benchmarks")]
use test;

Expand All @@ -875,6 +883,29 @@ mod tests {
b.bytes = 800 * 800 * 3 + 200 * 200 * 3;
}

#[test]
#[cfg(feature = "png")]
fn test_resize_same_size() {
use std::path::Path;
let img = crate::open(&Path::new("./examples/fractal.png")).unwrap();
let resize = img.resize(img.width(), img.height(), FilterType::Triangle);
assert!(img.pixels().eq(resize.pixels()))
}

#[bench]
#[cfg(all(feature = "benchmarks", feature = "tiff"))]
fn bench_resize_same_size(b: &mut test::Bencher) {
let path = concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/images/tiff/testsuite/mandrill.tiff"
);
let image = crate::open(path).unwrap();
b.iter(|| {
test::black_box(image.resize(image.width(), image.height(), FilterType::CatmullRom));
});
b.bytes = (image.width() * image.height() * 3) as u64;
}

#[test]
fn test_issue_186() {
let img: RgbImage = ImageBuffer::new(100, 100);
Expand Down