diff --git a/CHANGES.md b/CHANGES.md index 588a97801f..002d4045e5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -17,6 +17,7 @@ Breaking Changes: Features: - Added `RbgImage::from_raw_bgr` and `RbgaImage::from_raw_bgra` constructors, which convert from `BGR(A)` with an optimized specialization for bytes of `BGRA` + - Added `FilterType::Area` for area-averaging resize, like OpenCV's `INTER_AREA` (#1941) Structural changes: - Increased MSRV to 1.88.0 (from 1.85.0) diff --git a/src/imageops/resize.rs b/src/imageops/resize.rs index 73219b7fa2..ae67c23e18 100644 --- a/src/imageops/resize.rs +++ b/src/imageops/resize.rs @@ -36,6 +36,9 @@ pub(crate) fn resize_impl( use pic_scale_safe::*; use DynamicImage::*; let mut resized = match image { + // pic-scale-safe's area filter gives whole input pixels full weight + // when downscaling by a non-integer factor, so use our own. + _ if alg == ResamplingFunction::Area => resize_area(image, dst_width, dst_height), ImageLuma8(src) => { let resized = resize_plane8(src.subpixels(), src_size, dst_size, alg)?; ImageLuma8(ImageBuffer::from_raw(dst_width, dst_height, resized).unwrap()) @@ -92,6 +95,26 @@ pub(crate) fn resize_impl( Ok(()) } +fn resize_area(image: &DynamicImage, dst_width: u32, dst_height: u32) -> DynamicImage { + use crate::imageops::resize; + use DynamicImage::*; + let (w, h, f) = (dst_width, dst_height, FilterType::Area); + match image { + ImageLuma8(src) => ImageLuma8(resize(src, w, h, f)), + ImageLumaA8(src) => ImageLumaA8(resize(src, w, h, f)), + ImageRgb8(src) => ImageRgb8(resize(src, w, h, f)), + ImageRgba8(src) => ImageRgba8(resize(src, w, h, f)), + ImageLuma16(src) => ImageLuma16(resize(src, w, h, f)), + ImageLumaA16(src) => ImageLumaA16(resize(src, w, h, f)), + ImageRgb16(src) => ImageRgb16(resize(src, w, h, f)), + ImageRgba16(src) => ImageRgba16(resize(src, w, h, f)), + ImageLuma32F(src) => ImageLuma32F(resize(src, w, h, f)), + ImageLumaA32F(src) => ImageLumaA32F(resize(src, w, h, f)), + ImageRgb32F(src) => ImageRgb32F(resize(src, w, h, f)), + ImageRgba32F(src) => ImageRgba32F(resize(src, w, h, f)), + } +} + fn convert_filter_type(filter: FilterType) -> ResamplingFunction { match filter { FilterType::Nearest => ResamplingFunction::Nearest, @@ -99,6 +122,7 @@ fn convert_filter_type(filter: FilterType) -> ResamplingFunction { FilterType::CatmullRom => ResamplingFunction::CatmullRom, FilterType::Gaussian => ResamplingFunction::Gaussian, FilterType::Lanczos3 => ResamplingFunction::Lanczos3, + FilterType::Area => ResamplingFunction::Area, } } @@ -228,4 +252,38 @@ mod tests { assert_eq!(image.height(), 5); assert_eq!(image.width(), 5); } + + #[test] + fn area() { + // (input, output width, expected output) + let cases: [(&[u8], u32, &[u8]); 3] = [ + // Integer factor: each output pixel is the mean of two input pixels. + (&[0, 10, 20, 40], 2, &[5, 30]), + // Each output pixel covers one and a half input pixels. + (&[0, 90, 180], 2, &[30, 150]), + // Upscaling: the middle output pixel covers a third of each input pixel. + (&[0, 90], 3, &[0, 45, 90]), + ]; + + for (input, width, expected) in cases { + let len = input.len() as u32; + let row = crate::GrayImage::from_raw(len, 1, input.to_vec()).unwrap(); + let mut image = DynamicImage::ImageLuma8(row); + resize_impl(&mut image, width, 1, crate::imageops::FilterType::Area).unwrap(); + assert_eq!(image.as_bytes(), expected, "row {input:?}"); + + let col = crate::GrayImage::from_raw(1, len, input.to_vec()).unwrap(); + let mut image = DynamicImage::ImageLuma8(col); + resize_impl(&mut image, 1, width, crate::imageops::FilterType::Area).unwrap(); + assert_eq!(image.as_bytes(), expected, "column {input:?}"); + } + + // Float values above 1.0 are not clamped. + let pixels = [0.0, 3.0, 6.0].map(|v| [v; 3]).concat(); + let row = crate::Rgb32FImage::from_raw(3, 1, pixels).unwrap(); + let mut image = DynamicImage::ImageRgb32F(row); + resize_impl(&mut image, 2, 1, crate::imageops::FilterType::Area).unwrap(); + let out = image.as_rgb32f().unwrap().as_raw(); + assert_eq!(out.as_slice(), [1.0, 1.0, 1.0, 5.0, 5.0, 5.0]); + } } diff --git a/src/imageops/sample.rs b/src/imageops/sample.rs index b5d17709ef..dbadf4e84c 100644 --- a/src/imageops/sample.rs +++ b/src/imageops/sample.rs @@ -112,6 +112,12 @@ pub enum FilterType { /// Lanczos with window 3 Lanczos3, + + /// Area averaging, like OpenCV's `INTER_AREA` + /// + /// When downscaling, each output pixel is the average of the input pixels + /// it covers, weighted by how much of each pixel it covers. + Area, } /// A Representation of a separable filter. @@ -204,11 +210,31 @@ pub(crate) fn box_kernel(_x: f32) -> f32 { 1.0 } +/// Build an area-averaging filter for one axis, where `ratio` is the input size +/// divided by the output size. +/// +/// Each output pixel covers `ratio` input pixels. An input pixel's weight is +/// the length of its overlap with that span, so pixels at the edges of the span +/// count in part. Modelled on OpenCV's `INTER_AREA`. +fn area_filter(ratio: f32) -> Filter<'static> { + // The samplers pass the distance between pixel centres divided by this. + let scale = ratio.max(1.0); + let half = ratio / 2.0; + Filter { + kernel: Box::new(move |x| { + let d = x * scale; + ((d + 0.5).min(half) - (d - 0.5).max(-half)).max(0.0) + }), + support: (half + 0.5) / scale, + } +} + // Sample the rows of the supplied image using the provided filter. // // The height of the image remains unchanged. // ```new_width``` is the desired width of the new image // ```filter``` is the filter to use for sampling. +// ```clamp_float``` clamps float output to [0, 1]. // ```image``` is not necessarily Rgba and the order of channels is passed through. // // Note: if an empty image is passed in, panics unless the image is truly empty. @@ -218,6 +244,7 @@ fn horizontal_sample
( image: &Rgba32FImage, new_width: u32, filter: &mut Filter, + clamp_float: bool, ) -> ImageBuffer
>
where
P: Pixel