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 sample;
35
36pub fn crop<I: GenericImageView>(
39 image: &mut I,
40 x: u32,
41 y: u32,
42 width: u32,
43 height: u32,
44) -> SubImage<&mut I> {
45 let (x, y, width, height) = crop_dimms(image, x, y, width, height);
46 SubImage::new(image, x, y, width, height)
47}
48
49pub fn crop_imm<I: GenericImageView>(
52 image: &I,
53 x: u32,
54 y: u32,
55 width: u32,
56 height: u32,
57) -> SubImage<&I> {
58 let (x, y, width, height) = crop_dimms(image, x, y, width, height);
59 SubImage::new(image, x, y, width, height)
60}
61
62fn crop_dimms<I: GenericImageView>(
63 image: &I,
64 x: u32,
65 y: u32,
66 width: u32,
67 height: u32,
68) -> (u32, u32, u32, u32) {
69 let (iwidth, iheight) = image.dimensions();
70
71 let x = cmp::min(x, iwidth);
72 let y = cmp::min(y, iheight);
73
74 let height = cmp::min(height, iheight - y);
75 let width = cmp::min(width, iwidth - x);
76
77 (x, y, width, height)
78}
79
80pub fn overlay_bounds(
136 (bottom_width, bottom_height): (u32, u32),
137 (top_width, top_height): (u32, u32),
138 x: u32,
139 y: u32,
140) -> (u32, u32) {
141 let x_range = top_width
142 .saturating_add(x) .min(bottom_width) .saturating_sub(x); let y_range = top_height
146 .saturating_add(y)
147 .min(bottom_height)
148 .saturating_sub(y);
149 (x_range, y_range)
150}
151
152fn overlay_bounds_ext(
170 (bottom_width, bottom_height): (u32, u32),
171 (top_width, top_height): (u32, u32),
172 x: i64,
173 y: i64,
174) -> (u32, u32, u32, u32, u32, u32) {
175 if x > i64::from(bottom_width)
177 || y > i64::from(bottom_height)
178 || x.saturating_add(i64::from(top_width)) <= 0
179 || y.saturating_add(i64::from(top_height)) <= 0
180 {
181 return (0, 0, 0, 0, 0, 0);
182 }
183
184 let max_x = x.saturating_add(i64::from(top_width));
186 let max_y = y.saturating_add(i64::from(top_height));
187
188 let max_inbounds_x = max_x.clamp(0, i64::from(bottom_width)) as u32;
192 let max_inbounds_y = max_y.clamp(0, i64::from(bottom_height)) as u32;
193 let origin_bottom_x = x.clamp(0, i64::from(bottom_width)) as u32;
194 let origin_bottom_y = y.clamp(0, i64::from(bottom_height)) as u32;
195
196 let x_range = max_inbounds_x - origin_bottom_x;
201 let y_range = max_inbounds_y - origin_bottom_y;
202
203 let origin_top_x = x.saturating_mul(-1).clamp(0, i64::from(top_width)) as u32;
205 let origin_top_y = y.saturating_mul(-1).clamp(0, i64::from(top_height)) as u32;
206
207 (
208 origin_bottom_x,
209 origin_bottom_y,
210 origin_top_x,
211 origin_top_y,
212 x_range,
213 y_range,
214 )
215}
216
217pub fn overlay<I, J>(bottom: &mut I, top: &J, x: i64, y: i64)
219where
220 I: GenericImage,
221 J: GenericImageView<Pixel = I::Pixel>,
222{
223 let bottom_dims = bottom.dimensions();
224 let top_dims = top.dimensions();
225
226 let (origin_bottom_x, origin_bottom_y, origin_top_x, origin_top_y, range_width, range_height) =
228 overlay_bounds_ext(bottom_dims, top_dims, x, y);
229
230 for y in 0..range_height {
231 for x in 0..range_width {
232 let p = top.get_pixel(origin_top_x + x, origin_top_y + y);
233 let mut bottom_pixel = bottom.get_pixel(origin_bottom_x + x, origin_bottom_y + y);
234 bottom_pixel.blend(&p);
235
236 bottom.put_pixel(origin_bottom_x + x, origin_bottom_y + y, bottom_pixel);
237 }
238 }
239}
240
241pub fn tile<I, J>(bottom: &mut I, top: &J)
254where
255 I: GenericImage,
256 J: GenericImageView<Pixel = I::Pixel>,
257{
258 for x in (0..bottom.width()).step_by(top.width() as usize) {
259 for y in (0..bottom.height()).step_by(top.height() as usize) {
260 overlay(bottom, top, i64::from(x), i64::from(y));
261 }
262 }
263}
264
265pub fn vertical_gradient<S, P, I>(img: &mut I, start: &P, stop: &P)
280where
281 I: GenericImage<Pixel = P>,
282 P: Pixel<Subpixel = S> + 'static,
283 S: Primitive + Lerp + 'static,
284{
285 for y in 0..img.height() {
286 let pixel = start.map2(stop, |a, b| {
287 let y = <S::Ratio as num_traits::NumCast>::from(y).unwrap();
288 let height = <S::Ratio as num_traits::NumCast>::from(img.height() - 1).unwrap();
289 S::lerp(a, b, y / height)
290 });
291
292 for x in 0..img.width() {
293 img.put_pixel(x, y, pixel);
294 }
295 }
296}
297
298pub fn horizontal_gradient<S, P, I>(img: &mut I, start: &P, stop: &P)
313where
314 I: GenericImage<Pixel = P>,
315 P: Pixel<Subpixel = S> + 'static,
316 S: Primitive + Lerp + 'static,
317{
318 for x in 0..img.width() {
319 let pixel = start.map2(stop, |a, b| {
320 let x = <S::Ratio as num_traits::NumCast>::from(x).unwrap();
321 let width = <S::Ratio as num_traits::NumCast>::from(img.width() - 1).unwrap();
322 S::lerp(a, b, x / width)
323 });
324
325 for y in 0..img.height() {
326 img.put_pixel(x, y, pixel);
327 }
328 }
329}
330
331pub fn replace<I, J>(bottom: &mut I, top: &J, x: i64, y: i64)
333where
334 I: GenericImage,
335 J: GenericImageView<Pixel = I::Pixel>,
336{
337 let bottom_dims = bottom.dimensions();
338 let top_dims = top.dimensions();
339
340 let (origin_bottom_x, origin_bottom_y, origin_top_x, origin_top_y, range_width, range_height) =
342 overlay_bounds_ext(bottom_dims, top_dims, x, y);
343
344 for y in 0..range_height {
345 for x in 0..range_width {
346 let p = top.get_pixel(origin_top_x + x, origin_top_y + y);
347 bottom.put_pixel(origin_bottom_x + x, origin_bottom_y + y, p);
348 }
349 }
350}
351
352#[cfg(test)]
353mod tests {
354
355 use super::{overlay, overlay_bounds_ext};
356 use crate::color::Rgb;
357 use crate::ImageBuffer;
358 use crate::RgbaImage;
359
360 #[test]
361 fn test_overlay_bounds_ext() {
362 assert_eq!(
363 overlay_bounds_ext((10, 10), (10, 10), 0, 0),
364 (0, 0, 0, 0, 10, 10)
365 );
366 assert_eq!(
367 overlay_bounds_ext((10, 10), (10, 10), 1, 0),
368 (1, 0, 0, 0, 9, 10)
369 );
370 assert_eq!(
371 overlay_bounds_ext((10, 10), (10, 10), 0, 11),
372 (0, 0, 0, 0, 0, 0)
373 );
374 assert_eq!(
375 overlay_bounds_ext((10, 10), (10, 10), -1, 0),
376 (0, 0, 1, 0, 9, 10)
377 );
378 assert_eq!(
379 overlay_bounds_ext((10, 10), (10, 10), -10, 0),
380 (0, 0, 0, 0, 0, 0)
381 );
382 assert_eq!(
383 overlay_bounds_ext((10, 10), (10, 10), 1i64 << 50, 0),
384 (0, 0, 0, 0, 0, 0)
385 );
386 assert_eq!(
387 overlay_bounds_ext((10, 10), (10, 10), -(1i64 << 50), 0),
388 (0, 0, 0, 0, 0, 0)
389 );
390 assert_eq!(
391 overlay_bounds_ext((10, 10), (u32::MAX, 10), 10 - i64::from(u32::MAX), 0),
392 (0, 0, u32::MAX - 10, 0, 10, 10)
393 );
394 }
395
396 #[test]
397 fn test_image_in_image() {
399 let mut target = ImageBuffer::new(32, 32);
400 let source = ImageBuffer::from_pixel(16, 16, Rgb([255u8, 0, 0]));
401 overlay(&mut target, &source, 0, 0);
402 assert!(*target.get_pixel(0, 0) == Rgb([255u8, 0, 0]));
403 assert!(*target.get_pixel(15, 0) == Rgb([255u8, 0, 0]));
404 assert!(*target.get_pixel(16, 0) == Rgb([0u8, 0, 0]));
405 assert!(*target.get_pixel(0, 15) == Rgb([255u8, 0, 0]));
406 assert!(*target.get_pixel(0, 16) == Rgb([0u8, 0, 0]));
407 }
408
409 #[test]
410 fn test_image_in_image_outside_of_bounds() {
412 let mut target = ImageBuffer::new(32, 32);
413 let source = ImageBuffer::from_pixel(32, 32, Rgb([255u8, 0, 0]));
414 overlay(&mut target, &source, 1, 1);
415 assert!(*target.get_pixel(0, 0) == Rgb([0, 0, 0]));
416 assert!(*target.get_pixel(1, 1) == Rgb([255u8, 0, 0]));
417 assert!(*target.get_pixel(31, 31) == Rgb([255u8, 0, 0]));
418 }
419
420 #[test]
421 fn test_image_outside_image_no_wrap_around() {
424 let mut target = ImageBuffer::new(32, 32);
425 let source = ImageBuffer::from_pixel(32, 32, Rgb([255u8, 0, 0]));
426 overlay(&mut target, &source, 33, 33);
427 assert!(*target.get_pixel(0, 0) == Rgb([0, 0, 0]));
428 assert!(*target.get_pixel(1, 1) == Rgb([0, 0, 0]));
429 assert!(*target.get_pixel(31, 31) == Rgb([0, 0, 0]));
430 }
431
432 #[test]
433 fn test_image_coordinate_overflow() {
435 let mut target = ImageBuffer::new(16, 16);
436 let source = ImageBuffer::from_pixel(32, 32, Rgb([255u8, 0, 0]));
437 overlay(
439 &mut target,
440 &source,
441 i64::from(u32::max_value() - 31),
442 i64::from(u32::max_value() - 31),
443 );
444 assert!(*target.get_pixel(0, 0) == Rgb([0, 0, 0]));
445 assert!(*target.get_pixel(1, 1) == Rgb([0, 0, 0]));
446 assert!(*target.get_pixel(15, 15) == Rgb([0, 0, 0]));
447 }
448
449 use super::{horizontal_gradient, vertical_gradient};
450
451 #[test]
452 fn test_image_horizontal_gradient_limits() {
454 let mut img = ImageBuffer::new(100, 1);
455
456 let start = Rgb([0u8, 128, 0]);
457 let end = Rgb([255u8, 255, 255]);
458
459 horizontal_gradient(&mut img, &start, &end);
460
461 assert_eq!(img.get_pixel(0, 0), &start);
462 assert_eq!(img.get_pixel(img.width() - 1, 0), &end);
463 }
464
465 #[test]
466 fn test_image_vertical_gradient_limits() {
468 let mut img = ImageBuffer::new(1, 100);
469
470 let start = Rgb([0u8, 128, 0]);
471 let end = Rgb([255u8, 255, 255]);
472
473 vertical_gradient(&mut img, &start, &end);
474
475 assert_eq!(img.get_pixel(0, 0), &start);
476 assert_eq!(img.get_pixel(0, img.height() - 1), &end);
477 }
478
479 #[test]
480 fn test_blur_zero() {
482 let image = RgbaImage::new(50, 50);
483 let _ = super::blur(&image, 0.0);
484 }
485}