use std::cmp;
use crate::traits::{Lerp, Pixel, Primitive};
use crate::{GenericImage, GenericImageView, SubImage};
pub use self::sample::FilterType;
pub use self::sample::FilterType::{CatmullRom, Gaussian, Lanczos3, Nearest, Triangle};
pub use self::affine::{
flip_horizontal, flip_horizontal_in, flip_horizontal_in_place, flip_vertical, flip_vertical_in,
flip_vertical_in_place, rotate180, rotate180_in, rotate180_in_place, rotate270, rotate270_in,
rotate90, rotate90_in,
};
pub use self::sample::{
blur, filter3x3, interpolate_bilinear, interpolate_nearest, resize, sample_bilinear,
sample_nearest, thumbnail, unsharpen,
};
pub use self::colorops::{
brighten, contrast, dither, grayscale, grayscale_alpha, grayscale_with_type,
grayscale_with_type_alpha, huerotate, index_colors, invert, BiLevel, ColorMap,
};
mod affine;
pub mod colorops;
mod fast_blur;
mod filter_1d;
mod sample;
pub use fast_blur::fast_blur;
pub(crate) use sample::gaussian_blur_dyn_image;
pub use sample::{blur_advanced, GaussianBlurParameters};
pub fn crop<I: GenericImageView>(
image: &mut I,
x: u32,
y: u32,
width: u32,
height: u32,
) -> SubImage<&mut I> {
let (x, y, width, height) = crop_dimms(image, x, y, width, height);
SubImage::new(image, x, y, width, height)
}
pub fn crop_imm<I: GenericImageView>(
image: &I,
x: u32,
y: u32,
width: u32,
height: u32,
) -> SubImage<&I> {
let (x, y, width, height) = crop_dimms(image, x, y, width, height);
SubImage::new(image, x, y, width, height)
}
fn crop_dimms<I: GenericImageView>(
image: &I,
x: u32,
y: u32,
width: u32,
height: u32,
) -> (u32, u32, u32, u32) {
let (iwidth, iheight) = image.dimensions();
let x = cmp::min(x, iwidth);
let y = cmp::min(y, iheight);
let height = cmp::min(height, iheight - y);
let width = cmp::min(width, iwidth - x);
(x, y, width, height)
}
#[must_use]
pub fn overlay_bounds(
(bottom_width, bottom_height): (u32, u32),
(top_width, top_height): (u32, u32),
x: u32,
y: u32,
) -> (u32, u32) {
let x_range = top_width
.saturating_add(x) .min(bottom_width) .saturating_sub(x); let y_range = top_height
.saturating_add(y)
.min(bottom_height)
.saturating_sub(y);
(x_range, y_range)
}
fn overlay_bounds_ext(
(bottom_width, bottom_height): (u32, u32),
(top_width, top_height): (u32, u32),
x: i64,
y: i64,
) -> (u32, u32, u32, u32, u32, u32) {
if x > i64::from(bottom_width)
|| y > i64::from(bottom_height)
|| x.saturating_add(i64::from(top_width)) <= 0
|| y.saturating_add(i64::from(top_height)) <= 0
{
return (0, 0, 0, 0, 0, 0);
}
let max_x = x.saturating_add(i64::from(top_width));
let max_y = y.saturating_add(i64::from(top_height));
let max_inbounds_x = max_x.clamp(0, i64::from(bottom_width)) as u32;
let max_inbounds_y = max_y.clamp(0, i64::from(bottom_height)) as u32;
let origin_bottom_x = x.clamp(0, i64::from(bottom_width)) as u32;
let origin_bottom_y = y.clamp(0, i64::from(bottom_height)) as u32;
let x_range = max_inbounds_x - origin_bottom_x;
let y_range = max_inbounds_y - origin_bottom_y;
let origin_top_x = x.saturating_mul(-1).clamp(0, i64::from(top_width)) as u32;
let origin_top_y = y.saturating_mul(-1).clamp(0, i64::from(top_height)) as u32;
(
origin_bottom_x,
origin_bottom_y,
origin_top_x,
origin_top_y,
x_range,
y_range,
)
}
pub fn overlay<I, J>(bottom: &mut I, top: &J, x: i64, y: i64)
where
I: GenericImage,
J: GenericImageView<Pixel = I::Pixel>,
{
let bottom_dims = bottom.dimensions();
let top_dims = top.dimensions();
let (origin_bottom_x, origin_bottom_y, origin_top_x, origin_top_y, range_width, range_height) =
overlay_bounds_ext(bottom_dims, top_dims, x, y);
for y in 0..range_height {
for x in 0..range_width {
let p = top.get_pixel(origin_top_x + x, origin_top_y + y);
let mut bottom_pixel = bottom.get_pixel(origin_bottom_x + x, origin_bottom_y + y);
bottom_pixel.blend(&p);
bottom.put_pixel(origin_bottom_x + x, origin_bottom_y + y, bottom_pixel);
}
}
}
pub fn tile<I, J>(bottom: &mut I, top: &J)
where
I: GenericImage,
J: GenericImageView<Pixel = I::Pixel>,
{
for x in (0..bottom.width()).step_by(top.width() as usize) {
for y in (0..bottom.height()).step_by(top.height() as usize) {
overlay(bottom, top, i64::from(x), i64::from(y));
}
}
}
pub fn vertical_gradient<S, P, I>(img: &mut I, start: &P, stop: &P)
where
I: GenericImage<Pixel = P>,
P: Pixel<Subpixel = S> + 'static,
S: Primitive + Lerp + 'static,
{
for y in 0..img.height() {
let pixel = start.map2(stop, |a, b| {
let y = <S::Ratio as num_traits::NumCast>::from(y).unwrap();
let height = <S::Ratio as num_traits::NumCast>::from(img.height() - 1).unwrap();
S::lerp(a, b, y / height)
});
for x in 0..img.width() {
img.put_pixel(x, y, pixel);
}
}
}
pub fn horizontal_gradient<S, P, I>(img: &mut I, start: &P, stop: &P)
where
I: GenericImage<Pixel = P>,
P: Pixel<Subpixel = S> + 'static,
S: Primitive + Lerp + 'static,
{
for x in 0..img.width() {
let pixel = start.map2(stop, |a, b| {
let x = <S::Ratio as num_traits::NumCast>::from(x).unwrap();
let width = <S::Ratio as num_traits::NumCast>::from(img.width() - 1).unwrap();
S::lerp(a, b, x / width)
});
for y in 0..img.height() {
img.put_pixel(x, y, pixel);
}
}
}
pub fn replace<I, J>(bottom: &mut I, top: &J, x: i64, y: i64)
where
I: GenericImage,
J: GenericImageView<Pixel = I::Pixel>,
{
let bottom_dims = bottom.dimensions();
let top_dims = top.dimensions();
let (origin_bottom_x, origin_bottom_y, origin_top_x, origin_top_y, range_width, range_height) =
overlay_bounds_ext(bottom_dims, top_dims, x, y);
for y in 0..range_height {
for x in 0..range_width {
let p = top.get_pixel(origin_top_x + x, origin_top_y + y);
bottom.put_pixel(origin_bottom_x + x, origin_bottom_y + y, p);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::color::Rgb;
use crate::GrayAlphaImage;
use crate::GrayImage;
use crate::ImageBuffer;
use crate::RgbImage;
use crate::RgbaImage;
#[test]
fn test_overlay_bounds_ext() {
assert_eq!(
overlay_bounds_ext((10, 10), (10, 10), 0, 0),
(0, 0, 0, 0, 10, 10)
);
assert_eq!(
overlay_bounds_ext((10, 10), (10, 10), 1, 0),
(1, 0, 0, 0, 9, 10)
);
assert_eq!(
overlay_bounds_ext((10, 10), (10, 10), 0, 11),
(0, 0, 0, 0, 0, 0)
);
assert_eq!(
overlay_bounds_ext((10, 10), (10, 10), -1, 0),
(0, 0, 1, 0, 9, 10)
);
assert_eq!(
overlay_bounds_ext((10, 10), (10, 10), -10, 0),
(0, 0, 0, 0, 0, 0)
);
assert_eq!(
overlay_bounds_ext((10, 10), (10, 10), 1i64 << 50, 0),
(0, 0, 0, 0, 0, 0)
);
assert_eq!(
overlay_bounds_ext((10, 10), (10, 10), -(1i64 << 50), 0),
(0, 0, 0, 0, 0, 0)
);
assert_eq!(
overlay_bounds_ext((10, 10), (u32::MAX, 10), 10 - i64::from(u32::MAX), 0),
(0, 0, u32::MAX - 10, 0, 10, 10)
);
}
#[test]
fn test_image_in_image() {
let mut target = ImageBuffer::new(32, 32);
let source = ImageBuffer::from_pixel(16, 16, Rgb([255u8, 0, 0]));
overlay(&mut target, &source, 0, 0);
assert!(*target.get_pixel(0, 0) == Rgb([255u8, 0, 0]));
assert!(*target.get_pixel(15, 0) == Rgb([255u8, 0, 0]));
assert!(*target.get_pixel(16, 0) == Rgb([0u8, 0, 0]));
assert!(*target.get_pixel(0, 15) == Rgb([255u8, 0, 0]));
assert!(*target.get_pixel(0, 16) == Rgb([0u8, 0, 0]));
}
#[test]
fn test_image_in_image_outside_of_bounds() {
let mut target = ImageBuffer::new(32, 32);
let source = ImageBuffer::from_pixel(32, 32, Rgb([255u8, 0, 0]));
overlay(&mut target, &source, 1, 1);
assert!(*target.get_pixel(0, 0) == Rgb([0, 0, 0]));
assert!(*target.get_pixel(1, 1) == Rgb([255u8, 0, 0]));
assert!(*target.get_pixel(31, 31) == Rgb([255u8, 0, 0]));
}
#[test]
fn test_image_outside_image_no_wrap_around() {
let mut target = ImageBuffer::new(32, 32);
let source = ImageBuffer::from_pixel(32, 32, Rgb([255u8, 0, 0]));
overlay(&mut target, &source, 33, 33);
assert!(*target.get_pixel(0, 0) == Rgb([0, 0, 0]));
assert!(*target.get_pixel(1, 1) == Rgb([0, 0, 0]));
assert!(*target.get_pixel(31, 31) == Rgb([0, 0, 0]));
}
#[test]
fn test_image_coordinate_overflow() {
let mut target = ImageBuffer::new(16, 16);
let source = ImageBuffer::from_pixel(32, 32, Rgb([255u8, 0, 0]));
overlay(
&mut target,
&source,
i64::from(u32::MAX - 31),
i64::from(u32::MAX - 31),
);
assert!(*target.get_pixel(0, 0) == Rgb([0, 0, 0]));
assert!(*target.get_pixel(1, 1) == Rgb([0, 0, 0]));
assert!(*target.get_pixel(15, 15) == Rgb([0, 0, 0]));
}
use super::{horizontal_gradient, vertical_gradient};
#[test]
fn test_image_horizontal_gradient_limits() {
let mut img = ImageBuffer::new(100, 1);
let start = Rgb([0u8, 128, 0]);
let end = Rgb([255u8, 255, 255]);
horizontal_gradient(&mut img, &start, &end);
assert_eq!(img.get_pixel(0, 0), &start);
assert_eq!(img.get_pixel(img.width() - 1, 0), &end);
}
#[test]
fn test_image_vertical_gradient_limits() {
let mut img = ImageBuffer::new(1, 100);
let start = Rgb([0u8, 128, 0]);
let end = Rgb([255u8, 255, 255]);
vertical_gradient(&mut img, &start, &end);
assert_eq!(img.get_pixel(0, 0), &start);
assert_eq!(img.get_pixel(0, img.height() - 1), &end);
}
#[test]
fn test_blur_zero() {
let image = RgbaImage::new(50, 50);
let _ = blur(&image, 0.);
}
#[test]
fn test_fast_blur_zero() {
let image = RgbaImage::new(50, 50);
let _ = fast_blur(&image, 0.0);
}
#[test]
fn test_fast_blur_negative() {
let image = RgbaImage::new(50, 50);
let _ = fast_blur(&image, -1.0);
}
#[test]
fn test_fast_large_sigma() {
let image = RgbaImage::new(1, 1);
let _ = fast_blur(&image, 50.0);
}
#[test]
fn test_fast_blur_empty() {
let image = RgbaImage::new(0, 0);
let _ = fast_blur(&image, 1.0);
let image = RgbaImage::new(20, 0);
let _ = fast_blur(&image, 1.0);
let image = RgbaImage::new(0, 20);
let _ = fast_blur(&image, 1.0);
}
#[test]
fn test_fast_blur_3_channels() {
let image = RgbImage::new(50, 50);
let _ = fast_blur(&image, 1.0);
}
#[test]
fn test_fast_blur_2_channels() {
let image = GrayAlphaImage::new(50, 50);
let _ = fast_blur(&image, 1.0);
}
#[test]
fn test_fast_blur_1_channels() {
let image = GrayImage::new(50, 50);
let _ = fast_blur(&image, 1.0);
}
#[test]
#[cfg(feature = "tiff")]
fn fast_blur_approximates_gaussian_blur_well() {
let path = concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/images/tiff/testsuite/rgb-3c-16b.tiff"
);
let image = crate::open(path).unwrap();
let image_blurred_gauss = image
.blur_advanced(GaussianBlurParameters::new_from_sigma(50.0))
.to_rgb8();
let image_blurred_gauss_samples = image_blurred_gauss.as_flat_samples();
let image_blurred_gauss_bytes = image_blurred_gauss_samples.as_slice();
let image_blurred_fast = image.fast_blur(50.0).to_rgb8();
let image_blurred_fast_samples = image_blurred_fast.as_flat_samples();
let image_blurred_fast_bytes = image_blurred_fast_samples.as_slice();
let error = image_blurred_gauss_bytes
.iter()
.zip(image_blurred_fast_bytes.iter())
.map(|(a, b)| (f32::from(*a) - f32::from(*b)) / f32::from(*a))
.sum::<f32>()
/ (image_blurred_gauss_bytes.len() as f32);
assert!(error < 0.05);
}
}