1use std::cmp;
3
4use crate::traits::{Lerp, Pixel, Primitive};
5use crate::{GenericImage, GenericImageView, SubImage};
6
7pub use self::sample::FilterType;
8
9pub use self::sample::FilterType::{CatmullRom, Gaussian, Lanczos3, Nearest, Triangle};
10
11pub use self::affine::{
13 flip_horizontal, flip_horizontal_in, flip_horizontal_in_place, flip_vertical, flip_vertical_in,
14 flip_vertical_in_place, rotate180, rotate180_in, rotate180_in_place, rotate270, rotate270_in,
15 rotate90, rotate90_in,
16};
17
18pub use self::sample::{
19 blur, filter3x3, interpolate_bilinear, interpolate_nearest, resize, sample_bilinear,
20 sample_nearest, thumbnail, unsharpen,
21};
22
23pub use self::colorops::{
25 brighten, contrast, dither, grayscale, grayscale_alpha, grayscale_with_type,
26 grayscale_with_type_alpha, huerotate, index_colors, invert, BiLevel, ColorMap,
27};
28
29mod affine;
30pub mod colorops;
33mod fast_blur;
34mod filter_1d;
35mod sample;
36
37pub use fast_blur::fast_blur;
38pub(crate) use sample::gaussian_blur_dyn_image;
39pub use sample::{blur_advanced, GaussianBlurParameters};
40
41pub fn crop<I: GenericImageView>(
44 image: &mut I,
45 x: u32,
46 y: u32,
47 width: u32,
48 height: u32,
49) -> SubImage<&mut I> {
50 let (x, y, width, height) = crop_dimms(image, x, y, width, height);
51 SubImage::new(image, x, y, width, height)
52}
53
54pub fn crop_imm<I: GenericImageView>(
57 image: &I,
58 x: u32,
59 y: u32,
60 width: u32,
61 height: u32,
62) -> SubImage<&I> {
63 let (x, y, width, height) = crop_dimms(image, x, y, width, height);
64 SubImage::new(image, x, y, width, height)
65}
66
67fn crop_dimms<I: GenericImageView>(
68 image: &I,
69 x: u32,
70 y: u32,
71 width: u32,
72 height: u32,
73) -> (u32, u32, u32, u32) {
74 let (iwidth, iheight) = image.dimensions();
75
76 let x = cmp::min(x, iwidth);
77 let y = cmp::min(y, iheight);
78
79 let height = cmp::min(height, iheight - y);
80 let width = cmp::min(width, iwidth - x);
81
82 (x, y, width, height)
83}
84
85#[must_use]
141pub fn overlay_bounds(
142 (bottom_width, bottom_height): (u32, u32),
143 (top_width, top_height): (u32, u32),
144 x: u32,
145 y: u32,
146) -> (u32, u32) {
147 let x_range = top_width
148 .saturating_add(x) .min(bottom_width) .saturating_sub(x); let y_range = top_height
152 .saturating_add(y)
153 .min(bottom_height)
154 .saturating_sub(y);
155 (x_range, y_range)
156}
157
158fn overlay_bounds_ext(
175 (bottom_width, bottom_height): (u32, u32),
176 (top_width, top_height): (u32, u32),
177 x: i64,
178 y: i64,
179) -> (u32, u32, u32, u32, u32, u32) {
180 if x > i64::from(bottom_width)
182 || y > i64::from(bottom_height)
183 || x.saturating_add(i64::from(top_width)) <= 0
184 || y.saturating_add(i64::from(top_height)) <= 0
185 {
186 return (0, 0, 0, 0, 0, 0);
187 }
188
189 let max_x = x.saturating_add(i64::from(top_width));
191 let max_y = y.saturating_add(i64::from(top_height));
192
193 let max_inbounds_x = max_x.clamp(0, i64::from(bottom_width)) as u32;
197 let max_inbounds_y = max_y.clamp(0, i64::from(bottom_height)) as u32;
198 let origin_bottom_x = x.clamp(0, i64::from(bottom_width)) as u32;
199 let origin_bottom_y = y.clamp(0, i64::from(bottom_height)) as u32;
200
201 let x_range = max_inbounds_x - origin_bottom_x;
206 let y_range = max_inbounds_y - origin_bottom_y;
207
208 let origin_top_x = x.saturating_mul(-1).clamp(0, i64::from(top_width)) as u32;
210 let origin_top_y = y.saturating_mul(-1).clamp(0, i64::from(top_height)) as u32;
211
212 (
213 origin_bottom_x,
214 origin_bottom_y,
215 origin_top_x,
216 origin_top_y,
217 x_range,
218 y_range,
219 )
220}
221
222pub fn overlay<I, J>(bottom: &mut I, top: &J, x: i64, y: i64)
224where
225 I: GenericImage,
226 J: GenericImageView<Pixel = I::Pixel>,
227{
228 let bottom_dims = bottom.dimensions();
229 let top_dims = top.dimensions();
230
231 let (origin_bottom_x, origin_bottom_y, origin_top_x, origin_top_y, range_width, range_height) =
233 overlay_bounds_ext(bottom_dims, top_dims, x, y);
234
235 for y in 0..range_height {
236 for x in 0..range_width {
237 let p = top.get_pixel(origin_top_x + x, origin_top_y + y);
238 let mut bottom_pixel = bottom.get_pixel(origin_bottom_x + x, origin_bottom_y + y);
239 bottom_pixel.blend(&p);
240
241 bottom.put_pixel(origin_bottom_x + x, origin_bottom_y + y, bottom_pixel);
242 }
243 }
244}
245
246pub fn tile<I, J>(bottom: &mut I, top: &J)
259where
260 I: GenericImage,
261 J: GenericImageView<Pixel = I::Pixel>,
262{
263 for x in (0..bottom.width()).step_by(top.width() as usize) {
264 for y in (0..bottom.height()).step_by(top.height() as usize) {
265 overlay(bottom, top, i64::from(x), i64::from(y));
266 }
267 }
268}
269
270pub fn vertical_gradient<S, P, I>(img: &mut I, start: &P, stop: &P)
285where
286 I: GenericImage<Pixel = P>,
287 P: Pixel<Subpixel = S> + 'static,
288 S: Primitive + Lerp + 'static,
289{
290 for y in 0..img.height() {
291 let pixel = start.map2(stop, |a, b| {
292 let y = <S::Ratio as num_traits::NumCast>::from(y).unwrap();
293 let height = <S::Ratio as num_traits::NumCast>::from(img.height() - 1).unwrap();
294 S::lerp(a, b, y / height)
295 });
296
297 for x in 0..img.width() {
298 img.put_pixel(x, y, pixel);
299 }
300 }
301}
302
303pub fn horizontal_gradient<S, P, I>(img: &mut I, start: &P, stop: &P)
318where
319 I: GenericImage<Pixel = P>,
320 P: Pixel<Subpixel = S> + 'static,
321 S: Primitive + Lerp + 'static,
322{
323 for x in 0..img.width() {
324 let pixel = start.map2(stop, |a, b| {
325 let x = <S::Ratio as num_traits::NumCast>::from(x).unwrap();
326 let width = <S::Ratio as num_traits::NumCast>::from(img.width() - 1).unwrap();
327 S::lerp(a, b, x / width)
328 });
329
330 for y in 0..img.height() {
331 img.put_pixel(x, y, pixel);
332 }
333 }
334}
335
336pub fn replace<I, J>(bottom: &mut I, top: &J, x: i64, y: i64)
338where
339 I: GenericImage,
340 J: GenericImageView<Pixel = I::Pixel>,
341{
342 let bottom_dims = bottom.dimensions();
343 let top_dims = top.dimensions();
344
345 let (origin_bottom_x, origin_bottom_y, origin_top_x, origin_top_y, range_width, range_height) =
347 overlay_bounds_ext(bottom_dims, top_dims, x, y);
348
349 for y in 0..range_height {
350 for x in 0..range_width {
351 let p = top.get_pixel(origin_top_x + x, origin_top_y + y);
352 bottom.put_pixel(origin_bottom_x + x, origin_bottom_y + y, p);
353 }
354 }
355}
356
357#[cfg(test)]
358mod tests {
359
360 use super::*;
361 use crate::color::Rgb;
362 use crate::GrayAlphaImage;
363 use crate::GrayImage;
364 use crate::ImageBuffer;
365 use crate::RgbImage;
366 use crate::RgbaImage;
367
368 #[test]
369 fn test_overlay_bounds_ext() {
370 assert_eq!(
371 overlay_bounds_ext((10, 10), (10, 10), 0, 0),
372 (0, 0, 0, 0, 10, 10)
373 );
374 assert_eq!(
375 overlay_bounds_ext((10, 10), (10, 10), 1, 0),
376 (1, 0, 0, 0, 9, 10)
377 );
378 assert_eq!(
379 overlay_bounds_ext((10, 10), (10, 10), 0, 11),
380 (0, 0, 0, 0, 0, 0)
381 );
382 assert_eq!(
383 overlay_bounds_ext((10, 10), (10, 10), -1, 0),
384 (0, 0, 1, 0, 9, 10)
385 );
386 assert_eq!(
387 overlay_bounds_ext((10, 10), (10, 10), -10, 0),
388 (0, 0, 0, 0, 0, 0)
389 );
390 assert_eq!(
391 overlay_bounds_ext((10, 10), (10, 10), 1i64 << 50, 0),
392 (0, 0, 0, 0, 0, 0)
393 );
394 assert_eq!(
395 overlay_bounds_ext((10, 10), (10, 10), -(1i64 << 50), 0),
396 (0, 0, 0, 0, 0, 0)
397 );
398 assert_eq!(
399 overlay_bounds_ext((10, 10), (u32::MAX, 10), 10 - i64::from(u32::MAX), 0),
400 (0, 0, u32::MAX - 10, 0, 10, 10)
401 );
402 }
403
404 #[test]
405 fn test_image_in_image() {
407 let mut target = ImageBuffer::new(32, 32);
408 let source = ImageBuffer::from_pixel(16, 16, Rgb([255u8, 0, 0]));
409 overlay(&mut target, &source, 0, 0);
410 assert!(*target.get_pixel(0, 0) == Rgb([255u8, 0, 0]));
411 assert!(*target.get_pixel(15, 0) == Rgb([255u8, 0, 0]));
412 assert!(*target.get_pixel(16, 0) == Rgb([0u8, 0, 0]));
413 assert!(*target.get_pixel(0, 15) == Rgb([255u8, 0, 0]));
414 assert!(*target.get_pixel(0, 16) == Rgb([0u8, 0, 0]));
415 }
416
417 #[test]
418 fn test_image_in_image_outside_of_bounds() {
420 let mut target = ImageBuffer::new(32, 32);
421 let source = ImageBuffer::from_pixel(32, 32, Rgb([255u8, 0, 0]));
422 overlay(&mut target, &source, 1, 1);
423 assert!(*target.get_pixel(0, 0) == Rgb([0, 0, 0]));
424 assert!(*target.get_pixel(1, 1) == Rgb([255u8, 0, 0]));
425 assert!(*target.get_pixel(31, 31) == Rgb([255u8, 0, 0]));
426 }
427
428 #[test]
429 fn test_image_outside_image_no_wrap_around() {
432 let mut target = ImageBuffer::new(32, 32);
433 let source = ImageBuffer::from_pixel(32, 32, Rgb([255u8, 0, 0]));
434 overlay(&mut target, &source, 33, 33);
435 assert!(*target.get_pixel(0, 0) == Rgb([0, 0, 0]));
436 assert!(*target.get_pixel(1, 1) == Rgb([0, 0, 0]));
437 assert!(*target.get_pixel(31, 31) == Rgb([0, 0, 0]));
438 }
439
440 #[test]
441 fn test_image_coordinate_overflow() {
443 let mut target = ImageBuffer::new(16, 16);
444 let source = ImageBuffer::from_pixel(32, 32, Rgb([255u8, 0, 0]));
445 overlay(
447 &mut target,
448 &source,
449 i64::from(u32::MAX - 31),
450 i64::from(u32::MAX - 31),
451 );
452 assert!(*target.get_pixel(0, 0) == Rgb([0, 0, 0]));
453 assert!(*target.get_pixel(1, 1) == Rgb([0, 0, 0]));
454 assert!(*target.get_pixel(15, 15) == Rgb([0, 0, 0]));
455 }
456
457 use super::{horizontal_gradient, vertical_gradient};
458
459 #[test]
460 fn test_image_horizontal_gradient_limits() {
462 let mut img = ImageBuffer::new(100, 1);
463
464 let start = Rgb([0u8, 128, 0]);
465 let end = Rgb([255u8, 255, 255]);
466
467 horizontal_gradient(&mut img, &start, &end);
468
469 assert_eq!(img.get_pixel(0, 0), &start);
470 assert_eq!(img.get_pixel(img.width() - 1, 0), &end);
471 }
472
473 #[test]
474 fn test_image_vertical_gradient_limits() {
476 let mut img = ImageBuffer::new(1, 100);
477
478 let start = Rgb([0u8, 128, 0]);
479 let end = Rgb([255u8, 255, 255]);
480
481 vertical_gradient(&mut img, &start, &end);
482
483 assert_eq!(img.get_pixel(0, 0), &start);
484 assert_eq!(img.get_pixel(0, img.height() - 1), &end);
485 }
486
487 #[test]
488 fn test_blur_zero() {
490 let image = RgbaImage::new(50, 50);
491 let _ = blur(&image, 0.);
492 }
493
494 #[test]
495 fn test_fast_blur_zero() {
497 let image = RgbaImage::new(50, 50);
498 let _ = fast_blur(&image, 0.0);
499 }
500
501 #[test]
502 fn test_fast_blur_negative() {
504 let image = RgbaImage::new(50, 50);
505 let _ = fast_blur(&image, -1.0);
506 }
507
508 #[test]
509 fn test_fast_large_sigma() {
511 let image = RgbaImage::new(1, 1);
512 let _ = fast_blur(&image, 50.0);
513 }
514
515 #[test]
516 fn test_fast_blur_empty() {
518 let image = RgbaImage::new(0, 0);
519 let _ = fast_blur(&image, 1.0);
520 let image = RgbaImage::new(20, 0);
521 let _ = fast_blur(&image, 1.0);
522 let image = RgbaImage::new(0, 20);
523 let _ = fast_blur(&image, 1.0);
524 }
525
526 #[test]
527 fn test_fast_blur_3_channels() {
529 let image = RgbImage::new(50, 50);
530 let _ = fast_blur(&image, 1.0);
531 }
532
533 #[test]
534 fn test_fast_blur_2_channels() {
536 let image = GrayAlphaImage::new(50, 50);
537 let _ = fast_blur(&image, 1.0);
538 }
539
540 #[test]
541 fn test_fast_blur_1_channels() {
543 let image = GrayImage::new(50, 50);
544 let _ = fast_blur(&image, 1.0);
545 }
546
547 #[test]
548 #[cfg(feature = "tiff")]
549 fn fast_blur_approximates_gaussian_blur_well() {
550 let path = concat!(
551 env!("CARGO_MANIFEST_DIR"),
552 "/tests/images/tiff/testsuite/rgb-3c-16b.tiff"
553 );
554 let image = crate::open(path).unwrap();
555 let image_blurred_gauss = image
556 .blur_advanced(GaussianBlurParameters::new_from_sigma(50.0))
557 .to_rgb8();
558 let image_blurred_gauss_samples = image_blurred_gauss.as_flat_samples();
559 let image_blurred_gauss_bytes = image_blurred_gauss_samples.as_slice();
560 let image_blurred_fast = image.fast_blur(50.0).to_rgb8();
561 let image_blurred_fast_samples = image_blurred_fast.as_flat_samples();
562 let image_blurred_fast_bytes = image_blurred_fast_samples.as_slice();
563
564 let error = image_blurred_gauss_bytes
565 .iter()
566 .zip(image_blurred_fast_bytes.iter())
567 .map(|(a, b)| (f32::from(*a) - f32::from(*b)) / f32::from(*a))
568 .sum::<f32>()
569 / (image_blurred_gauss_bytes.len() as f32);
570 assert!(error < 0.05);
571 }
572}