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, @@ -328,7 +355,11 @@ where // Write directly to the output slice, bypassing put_pixel's bounds checks. for (&tc, pc) in t.iter().zip(dst.iter_mut()) { - *pc = S::clamp_nearest_from(tc); + *pc = if clamp_float { + S::clamp_nearest_from(tc) + } else { + S::nearest_from(tc) + }; } } } @@ -1005,7 +1036,7 @@ pub fn resize( return tmp; } - let mut method = match filter { + let make_filter = |ratio: f32| match filter { FilterType::Nearest => Filter { kernel: Box::new(box_kernel), support: 0.0, @@ -1026,11 +1057,20 @@ pub fn resize( kernel: Box::new(lanczos3_kernel), support: 3.0, }, + FilterType::Area => area_filter(ratio), }; + let (width, height) = image.dimensions(); + let mut vertical = make_filter(height as f32 / nheight as f32); + let mut horizontal = make_filter(width as f32 / nwidth as f32); + + // Area weights are never negative, so the output stays within the input's + // range. Skip the clamp so float values above 1.0 are kept. + let clamp_float = filter != FilterType::Area; + // Note: tmp is not necessarily actually Rgba - let tmp: Rgba32FImage = vertical_sample(image, nheight, &mut method); - horizontal_sample(&tmp, nwidth, &mut method) + let tmp: Rgba32FImage = vertical_sample(image, nheight, &mut vertical); + horizontal_sample(&tmp, nwidth, &mut horizontal, clamp_float) } /// Performs a Gaussian blur on the supplied image. @@ -1874,7 +1914,7 @@ mod tests { #[test] #[cfg(feature = "png")] fn resize_transparent_image() { - use super::FilterType::{CatmullRom, Gaussian, Lanczos3, Nearest, Triangle}; + use super::FilterType::{Area, CatmullRom, Gaussian, Lanczos3, Nearest, Triangle}; use crate::imageops::crop; use crate::math::Rect; use crate::RgbaImage; @@ -1898,7 +1938,7 @@ mod tests { ); let img = crate::open(path).unwrap(); let rgba8 = img.as_rgba8().unwrap(); - let filters = &[Nearest, Triangle, CatmullRom, Gaussian, Lanczos3]; + let filters = &[Nearest, Triangle, CatmullRom, Gaussian, Lanczos3, Area]; for filter in filters { assert_resize(rgba8, *filter); } @@ -1946,12 +1986,13 @@ mod tests { #[test] fn resize_preserves_a_constant_image() { - const FILTERS: [FilterType; 5] = [ + const FILTERS: [FilterType; 6] = [ FilterType::Nearest, FilterType::Triangle, FilterType::CatmullRom, FilterType::Gaussian, FilterType::Lanczos3, + FilterType::Area, ]; for filter in FILTERS { @@ -1981,4 +2022,36 @@ mod tests { } } } + + #[test] + fn resize_area() { + use crate::GrayImage; + + // (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 = GrayImage::from_raw(len, 1, input.to_vec()).unwrap(); + let out = resize(&row, width, 1, FilterType::Area); + assert_eq!(out.as_raw().as_slice(), expected, "row {input:?}"); + + let col = GrayImage::from_raw(1, len, input.to_vec()).unwrap(); + let out = resize(&col, 1, width, FilterType::Area); + assert_eq!(out.as_raw().as_slice(), 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 out = resize(&row, 2, 1, FilterType::Area); + assert_eq!(out.as_raw().as_slice(), [1.0, 1.0, 1.0, 5.0, 5.0, 5.0]); + } }