diff --git a/src/imageops/filter_1d.rs b/src/imageops/filter_1d.rs index db023080e3..289bbdc666 100644 --- a/src/imageops/filter_1d.rs +++ b/src/imageops/filter_1d.rs @@ -194,7 +194,7 @@ impl ToStorage for u32 { impl ToStorage for f32 { #[inline(always)] fn to_(self) -> u16 { - (self.min(u16::MAX as f32) + 0.5) as u16 + (self + 0.5) as u16 } } diff --git a/src/imageops/sample.rs b/src/imageops/sample.rs index 8d13605cf4..b07dd0955c 100644 --- a/src/imageops/sample.rs +++ b/src/imageops/sample.rs @@ -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); @@ -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); @@ -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); @@ -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"); @@ -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;