From 0daaf2cdebfcae2d8ea46d881237c72d9bd1a6c5 Mon Sep 17 00:00:00 2001 From: awxkee Date: Wed, 5 Jun 2024 17:42:45 +0100 Subject: [PATCH] Fix nearest scaling, remove invalid functions --- Cargo.lock | 2 +- Cargo.toml | 2 +- app/src/main.rs | 43 ++++++++++++++++++++++-- src/sampler.rs | 87 ++++++++++++------------------------------------- src/scaler.rs | 1 - 5 files changed, 63 insertions(+), 72 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a0661a6..25c58ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -587,7 +587,7 @@ checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" [[package]] name = "pic-scale" -version = "0.1.1" +version = "0.1.3" dependencies = [ "colorutils-rs", "num-traits", diff --git a/Cargo.toml b/Cargo.toml index 774d80f..0b44895 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ workspace = { members = ["app"] } [package] name = "pic-scale" -version = "0.1.2" +version = "0.1.3" edition = "2021" description = "High performance image scaling" readme = "README.md" diff --git a/app/src/main.rs b/app/src/main.rs index 5e42e3a..80fc214 100644 --- a/app/src/main.rs +++ b/app/src/main.rs @@ -1,3 +1,4 @@ +use std::fmt::format; use std::time::Instant; use fast_image_resize::images::Image; @@ -22,7 +23,7 @@ fn main() { let start_time = Instant::now(); - let mut scaler = Scaler::new(ResamplingFunction::Cubic); + let mut scaler = Scaler::new(ResamplingFunction::Hanning); scaler.set_threading_policy(ThreadingPolicy::Adaptive); let store = ImageStore::::from_slice(&mut bytes, dimensions.0 as usize, dimensions.1 as usize); @@ -44,7 +45,7 @@ fn main() { resized.height as u32, image::ExtendedColorType::Rgba8, ) - .unwrap(); + .unwrap(); } else { image::save_buffer( "converted.jpg", @@ -53,8 +54,44 @@ fn main() { resized.height as u32, image::ExtendedColorType::Rgb8, ) - .unwrap(); + .unwrap(); } + + // for i in 0..37 { + // let mut scaler = Scaler::new(i.into()); + // scaler.set_threading_policy(ThreadingPolicy::Adaptive); + // let store = + // ImageStore::::from_slice(&mut bytes, dimensions.0 as usize, dimensions.1 as usize); + // let resized = scaler.resize_rgba( + // ImageSize::new(dimensions.0 as usize / 3, dimensions.1 as usize / 3), + // store, + // true, + // ); + // + // let elapsed_time = start_time.elapsed(); + // // Print the elapsed time in milliseconds + // println!("Scaler: {:.2?}", elapsed_time); + // + // if resized.channels == 4 { + // image::save_buffer( + // format!("converted_{}.png", i), + // resized.as_bytes(), + // resized.width as u32, + // resized.height as u32, + // image::ExtendedColorType::Rgba8, + // ) + // .unwrap(); + // } else { + // image::save_buffer( + // format!("converted_{}.jpg", i), + // resized.as_bytes(), + // resized.width as u32, + // resized.height as u32, + // image::ExtendedColorType::Rgb8, + // ) + // .unwrap(); + // } + // } } fn test_fast_image() { diff --git a/src/sampler.rs b/src/sampler.rs index 4e0f05d..a7effd6 100644 --- a/src/sampler.rs +++ b/src/sampler.rs @@ -89,11 +89,6 @@ pub fn robidoux(x: f32) -> f32 { ); } -#[inline(always)] -pub fn ewa_robidoux(x: f32) -> f32 { - return jinc(x as f64) as f32 * robidoux(x); -} - #[inline(always)] pub fn robidoux_sharp(x: f32) -> f32 { return bc_spline( @@ -103,11 +98,6 @@ pub fn robidoux_sharp(x: f32) -> f32 { ); } -#[inline(always)] -pub fn ewa_robidoux_sharp(x: f32) -> f32 { - jinc(x as f64) as f32 * robidoux_sharp(x) -} - #[allow(dead_code)] pub fn sinc(x: f32) -> f32 { return if x == 0.0 { 1f32 } else { x.sin() / x }; @@ -246,11 +236,6 @@ fn hanning(x: f32) -> f32 { } } -#[inline(always)] -fn ewa_hanning(x: f32) -> f32 { - jinc(x as f64) as f32 * hanning(x) -} - #[inline(always)] pub(crate) fn blackman_window(x: f32) -> f32 { let pi = std::f32::consts::PI; @@ -267,16 +252,6 @@ pub(crate) fn blackman(x: f32) -> f32 { } } -#[inline(always)] -pub(crate) fn ewa_blackman(x: f32) -> f32 { - let x = x.abs(); - if x < 2.0f32 { - jinc(x as f64) as f32 * blackman_window(x / 2f32) - } else { - 0f32 - } -} - #[inline(always)] pub(crate) fn gaussian(x: f32) -> f32 { let sigma: f32 = 0.35f32; @@ -297,11 +272,6 @@ pub(crate) fn quadric(x: f32) -> f32 { return 0f32; } -#[inline(always)] -pub(crate) fn ewa_quadric(x: f32) -> f32 { - jinc(x as f64) as f32 * quadric(x) -} - #[inline(always)] pub(crate) fn spline16(x: f32) -> f32 { return if x < 1.0 { @@ -398,19 +368,14 @@ pub enum ResamplingFunction { Bicubic, Hamming, Hanning, - EwaHanning, Blackman, - EwaBlackman, Welch, Quadric, - EwaQuadric, Gaussian, Sphinx, Bartlett, Robidoux, - EwaRobidoux, RobidouxSharp, - EwaRobidouxSharp, Spline16, Spline36, Spline64, @@ -440,32 +405,27 @@ impl From for ResamplingFunction { 8 => ResamplingFunction::Bicubic, 9 => ResamplingFunction::Hamming, 10 => ResamplingFunction::Hanning, - 11 => ResamplingFunction::EwaHanning, - 12 => ResamplingFunction::Blackman, - 13 => ResamplingFunction::EwaBlackman, - 14 => ResamplingFunction::Welch, - 15 => ResamplingFunction::Quadric, - 16 => ResamplingFunction::EwaQuadric, - 17 => ResamplingFunction::Gaussian, - 18 => ResamplingFunction::Sphinx, - 19 => ResamplingFunction::Bartlett, - 20 => ResamplingFunction::Robidoux, - 21 => ResamplingFunction::EwaRobidoux, - 22 => ResamplingFunction::RobidouxSharp, - 23 => ResamplingFunction::EwaRobidouxSharp, - 24 => ResamplingFunction::Spline16, - 25 => ResamplingFunction::Spline36, - 26 => ResamplingFunction::Spline64, - 27 => ResamplingFunction::Kaiser, - 28 => ResamplingFunction::BartlettHann, - 29 => ResamplingFunction::Box, - 30 => ResamplingFunction::Bohman, - 31 => ResamplingFunction::Lanczos2, - 32 => ResamplingFunction::Lanczos3, - 33 => ResamplingFunction::Lanczos4, - 34 => ResamplingFunction::Lanczos2Jinc, - 35 => ResamplingFunction::Lanczos3Jinc, - 36 => ResamplingFunction::Lanczos4Jinc, + 11 => ResamplingFunction::Blackman, + 12 => ResamplingFunction::Welch, + 13 => ResamplingFunction::Quadric, + 14 => ResamplingFunction::Gaussian, + 15 => ResamplingFunction::Sphinx, + 16 => ResamplingFunction::Bartlett, + 17 => ResamplingFunction::Robidoux, + 18 => ResamplingFunction::RobidouxSharp, + 19 => ResamplingFunction::Spline16, + 20 => ResamplingFunction::Spline36, + 21 => ResamplingFunction::Spline64, + 22 => ResamplingFunction::Kaiser, + 23 => ResamplingFunction::BartlettHann, + 24 => ResamplingFunction::Box, + 25 => ResamplingFunction::Bohman, + 26 => ResamplingFunction::Lanczos2, + 27 => ResamplingFunction::Lanczos3, + 28 => ResamplingFunction::Lanczos4, + 29 => ResamplingFunction::Lanczos2Jinc, + 30 => ResamplingFunction::Lanczos3Jinc, + 31 => ResamplingFunction::Lanczos4Jinc, _ => ResamplingFunction::Bilinear, } } @@ -506,17 +466,13 @@ impl ResamplingFunction { ResamplingFunction::Lanczos2 => ResamplingFilter::new(lanczos2, 2), ResamplingFunction::Hamming => ResamplingFilter::new(hamming, 1), ResamplingFunction::Hanning => ResamplingFilter::new(hanning, 1), - ResamplingFunction::EwaHanning => ResamplingFilter::new(ewa_hanning, 1), ResamplingFunction::Welch => ResamplingFilter::new(welch, 1), ResamplingFunction::Quadric => ResamplingFilter::new(quadric, 1), - ResamplingFunction::EwaQuadric => ResamplingFilter::new(ewa_quadric, 1), ResamplingFunction::Gaussian => ResamplingFilter::new(gaussian, 2), ResamplingFunction::Sphinx => ResamplingFilter::new(sphinx, 2), ResamplingFunction::Bartlett => ResamplingFilter::new(bartlett, 1), ResamplingFunction::Robidoux => ResamplingFilter::new(robidoux, 2), - ResamplingFunction::EwaRobidoux => ResamplingFilter::new(ewa_robidoux, 2), ResamplingFunction::RobidouxSharp => ResamplingFilter::new(robidoux_sharp, 2), - ResamplingFunction::EwaRobidouxSharp => ResamplingFilter::new(ewa_robidoux_sharp, 2), ResamplingFunction::Spline16 => ResamplingFilter::new(spline16, 2), ResamplingFunction::Spline36 => ResamplingFilter::new(spline36, 2), ResamplingFunction::Spline64 => ResamplingFilter::new(spline64, 2), @@ -528,7 +484,6 @@ impl ResamplingFunction { ResamplingFunction::Lanczos3Jinc => ResamplingFilter::new(lanczos3_jinc, 3), ResamplingFunction::Lanczos4Jinc => ResamplingFilter::new(lanczos4_jinc, 4), ResamplingFunction::Blackman => ResamplingFilter::new(blackman, 2), - ResamplingFunction::EwaBlackman => ResamplingFilter::new(ewa_blackman, 2), }; } } diff --git a/src/scaler.rs b/src/scaler.rs index ba14502..25c3e6f 100644 --- a/src/scaler.rs +++ b/src/scaler.rs @@ -250,7 +250,6 @@ impl Scaling for Scaler { new_size.width, new_size.height, ); - let new_image = ImageStore::::alloc(new_size.width, new_size.height); if is_alpha_premultiplied { let mut premultiplied_store = ImageStore::::alloc(new_image.width, new_image.height); new_image.premultiply_alpha(&mut premultiplied_store);