Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/imageops/filter_1d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl ToStorage<u16> for u32 {
impl ToStorage<u16> for f32 {
#[inline(always)]
fn to_(self) -> u16 {
(self.min(u16::MAX as f32) + 0.5) as u16
(self + 0.5) as u16
}
}

Expand Down
74 changes: 58 additions & 16 deletions src/imageops/sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,9 @@ impl GaussianBlurParameters {
};

/// Creates a new parameters set from radius only.
///
/// # Panics
/// Panics any radius is negative, subnormal, NaN or infinity.
pub fn new_from_radius(radius: f32) -> GaussianBlurParameters {
// Previous implementation was allowing passing 0 so we'll allow here also.
assert!(radius >= 0.0);
Expand All @@ -1124,13 +1127,12 @@ impl GaussianBlurParameters {
///
/// Kernel size will be rounded to nearest odd, and used with fraction
/// to compute accurate required sigma.
///
/// # Panics
/// Panics any kernel size is zero, negative, infinite, NaN, subnormal.
pub fn new_from_kernel_size(kernel_size: f32) -> GaussianBlurParameters {
assert!(
kernel_size > 0.,
"Kernel size do not allow infinities, zeros, NaNs or subnormals or negatives"
);
assert!(
kernel_size.is_normal(),
kernel_size > 0. && kernel_size.is_normal(),
"Kernel size do not allow infinities, zeros, NaNs or subnormals or negatives"
);
let i_kernel_size = GaussianBlurParameters::round_to_nearest_odd(kernel_size);
Expand All @@ -1148,24 +1150,19 @@ impl GaussianBlurParameters {
///
/// Kernel size will be rounded to nearest odd, and used with fraction
/// to compute accurate required sigma.
///
/// # Panics
/// Panics any kernel size is zero, negative, infinite, NaN, subnormal.
pub fn new_anisotropic_kernel_size(
x_axis_kernel_size: f32,
y_axis_kernel_size: f32,
) -> GaussianBlurParameters {
assert!(
x_axis_kernel_size > 0.,
"Kernel size do not allow infinities, zeros, NaNs or subnormals or negatives"
);
assert!(
y_axis_kernel_size.is_normal(),
x_axis_kernel_size > 0. && x_axis_kernel_size.is_normal(),
"Kernel size do not allow infinities, zeros, NaNs or subnormals or negatives"
);
assert!(
y_axis_kernel_size > 0.,
"Kernel size do not allow infinities, zeros, NaNs or subnormals or negatives"
);
assert!(
y_axis_kernel_size.is_normal(),
y_axis_kernel_size > 0. && y_axis_kernel_size.is_normal(),
"Kernel size do not allow infinities, zeros, NaNs or subnormals or negatives"
);
let x_kernel_size = GaussianBlurParameters::round_to_nearest_odd(x_axis_kernel_size);
Expand All @@ -1183,9 +1180,12 @@ impl GaussianBlurParameters {
}

/// Creates a new parameters set from sigma only
///
/// # Panics
/// Panics if sigma is zero, negative, infinite, NaN, or subnormal.
pub fn new_from_sigma(sigma: f32) -> GaussianBlurParameters {
assert!(
sigma.is_normal(),
sigma.is_normal() && sigma > 0.,
"Sigma cannot be NaN, Infinities, subnormal or zero"
);
assert!(sigma > 0.0, "Sigma must be positive");
Expand All @@ -1198,6 +1198,48 @@ impl GaussianBlurParameters {
}
}

/// Creates a new [`GaussianBlurParameters`] with independent kernel sizes and sigmas
/// for each axis, allowing non-uniform (anisotropic) blurring.
///
/// # Parameters
/// - `x_axis_kernel_size` — width of the horizontal kernel in pixels, must be > 0
/// - `x_axis_sigma` — standard deviation for the horizontal pass; controls blur strength
/// - `y_axis_kernel_size` — height of the vertical kernel in pixels, must be > 0
/// - `y_axis_sigma` — standard deviation for the vertical pass
///
/// # Panics
/// Panics if any kernel size is 0, not odd, or if any sigma is zero, negative, infinite,
/// NaN, or subnormal.
pub fn new(
x_axis_kernel_size: u32,
x_axis_sigma: f32,
y_axis_kernel_size: u32,
y_axis_sigma: f32,
) -> GaussianBlurParameters {
assert!(
x_axis_kernel_size > 0 || !x_axis_kernel_size.is_multiple_of(2),
"Kernel size must be more than 0 and must be odd"
);
assert!(
y_axis_kernel_size > 0 || !y_axis_kernel_size.is_multiple_of(2),
"Kernel size must be more than 0 and must be odd"
);
assert!(
x_axis_sigma.is_normal() && x_axis_sigma > 0.,
"Kernel sigma do not allow infinities, zeros, NaNs or subnormals or negatives"
);
assert!(
y_axis_sigma.is_normal() && y_axis_sigma > 0.,
"Kernel sigma do not allow infinities, zeros, NaNs or subnormals or negatives"
);
GaussianBlurParameters {
x_axis_kernel_size,
x_axis_sigma,
y_axis_kernel_size,
y_axis_sigma,
}
}

#[inline]
fn round_to_nearest_odd(x: f32) -> u32 {
let n = x.round() as u32;
Expand Down
Loading