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