Skip to content

Commit

Permalink
Fix nearest scaling, remove invalid functions
Browse files Browse the repository at this point in the history
  • Loading branch information
awxkee committed Jun 5, 2024
1 parent c46ae0f commit 0daaf2c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 72 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
43 changes: 40 additions & 3 deletions app/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fmt::format;
use std::time::Instant;

use fast_image_resize::images::Image;
Expand All @@ -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::<u8, 4>::from_slice(&mut bytes, dimensions.0 as usize, dimensions.1 as usize);
Expand All @@ -44,7 +45,7 @@ fn main() {
resized.height as u32,
image::ExtendedColorType::Rgba8,
)
.unwrap();
.unwrap();
} else {
image::save_buffer(
"converted.jpg",
Expand All @@ -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::<u8, 4>::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() {
Expand Down
87 changes: 21 additions & 66 deletions src/sampler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 };
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -440,32 +405,27 @@ impl From<u32> 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,
}
}
Expand Down Expand Up @@ -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),
Expand All @@ -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),
};
}
}
Expand Down
1 change: 0 additions & 1 deletion src/scaler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ impl Scaling for Scaler {
new_size.width,
new_size.height,
);
let new_image = ImageStore::<u8, 4>::alloc(new_size.width, new_size.height);
if is_alpha_premultiplied {
let mut premultiplied_store = ImageStore::<u8, 4>::alloc(new_image.width, new_image.height);
new_image.premultiply_alpha(&mut premultiplied_store);
Expand Down

0 comments on commit 0daaf2c

Please sign in to comment.