1use num_traits::{NumCast, ToPrimitive, Zero};
7use std::f32;
8use std::ops::Mul;
9
10use crate::imageops::filter_1d::{
11 filter_2d_sep_la, filter_2d_sep_la_f32, filter_2d_sep_la_u16, filter_2d_sep_plane,
12 filter_2d_sep_plane_f32, filter_2d_sep_plane_u16, filter_2d_sep_rgb, filter_2d_sep_rgb_f32,
13 filter_2d_sep_rgb_u16, filter_2d_sep_rgba, filter_2d_sep_rgba_f32, filter_2d_sep_rgba_u16,
14 FilterImageSize,
15};
16use crate::images::buffer::{Gray16Image, GrayAlpha16Image, Rgb16Image, Rgba16Image};
17use crate::traits::{Enlargeable, Pixel, Primitive};
18use crate::utils::clamp;
19use crate::{
20 DynamicImage, GenericImage, GenericImageView, GrayAlphaImage, GrayImage, ImageBuffer,
21 Rgb32FImage, RgbImage, Rgba32FImage, RgbaImage,
22};
23
24#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
91#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
92pub enum FilterType {
93 Nearest,
95
96 Triangle,
98
99 CatmullRom,
101
102 Gaussian,
104
105 Lanczos3,
107}
108
109pub(crate) struct Filter<'a> {
111 pub(crate) kernel: Box<dyn Fn(f32) -> f32 + 'a>,
113
114 pub(crate) support: f32,
116}
117
118struct FloatNearest(f32);
119
120impl ToPrimitive for FloatNearest {
123 fn to_i8(&self) -> Option<i8> {
126 self.0.round().to_i8()
127 }
128 fn to_i16(&self) -> Option<i16> {
129 self.0.round().to_i16()
130 }
131 fn to_i64(&self) -> Option<i64> {
132 self.0.round().to_i64()
133 }
134 fn to_u8(&self) -> Option<u8> {
135 self.0.round().to_u8()
136 }
137 fn to_u16(&self) -> Option<u16> {
138 self.0.round().to_u16()
139 }
140 fn to_u64(&self) -> Option<u64> {
141 self.0.round().to_u64()
142 }
143 fn to_f64(&self) -> Option<f64> {
144 self.0.to_f64()
145 }
146}
147
148fn sinc(t: f32) -> f32 {
150 let a = t * f32::consts::PI;
151
152 if t == 0.0 {
153 1.0
154 } else {
155 a.sin() / a
156 }
157}
158
159fn lanczos(x: f32, t: f32) -> f32 {
161 if x.abs() < t {
162 sinc(x) * sinc(x / t)
163 } else {
164 0.0
165 }
166}
167
168fn bc_cubic_spline(x: f32, b: f32, c: f32) -> f32 {
171 let a = x.abs();
172
173 let k = if a < 1.0 {
174 (12.0 - 9.0 * b - 6.0 * c) * a.powi(3)
175 + (-18.0 + 12.0 * b + 6.0 * c) * a.powi(2)
176 + (6.0 - 2.0 * b)
177 } else if a < 2.0 {
178 (-b - 6.0 * c) * a.powi(3)
179 + (6.0 * b + 30.0 * c) * a.powi(2)
180 + (-12.0 * b - 48.0 * c) * a
181 + (8.0 * b + 24.0 * c)
182 } else {
183 0.0
184 };
185
186 k / 6.0
187}
188
189pub(crate) fn gaussian(x: f32, r: f32) -> f32 {
192 ((2.0 * f32::consts::PI).sqrt() * r).recip() * (-x.powi(2) / (2.0 * r.powi(2))).exp()
193}
194
195pub(crate) fn lanczos3_kernel(x: f32) -> f32 {
197 lanczos(x, 3.0)
198}
199
200pub(crate) fn gaussian_kernel(x: f32) -> f32 {
203 gaussian(x, 0.5)
204}
205
206pub(crate) fn catmullrom_kernel(x: f32) -> f32 {
209 bc_cubic_spline(x, 0.0, 0.5)
210}
211
212pub(crate) fn triangle_kernel(x: f32) -> f32 {
215 if x.abs() < 1.0 {
216 1.0 - x.abs()
217 } else {
218 0.0
219 }
220}
221
222pub(crate) fn box_kernel(_x: f32) -> f32 {
226 1.0
227}
228
229fn horizontal_sample<P, S>(
237 image: &Rgba32FImage,
238 new_width: u32,
239 filter: &mut Filter,
240) -> ImageBuffer<P, Vec<S>>
241where
242 P: Pixel<Subpixel = S> + 'static,
243 S: Primitive + 'static,
244{
245 let (width, height) = image.dimensions();
246 assert!(
248 width != 0 || height == 0,
250 "Unexpected prior allocation size. This case should have been handled by the caller"
251 );
252
253 let mut out = ImageBuffer::new(new_width, height);
254 out.copy_color_space_from(image);
255 let mut ws = Vec::new();
256
257 let max: f32 = NumCast::from(S::DEFAULT_MAX_VALUE).unwrap();
258 let min: f32 = NumCast::from(S::DEFAULT_MIN_VALUE).unwrap();
259 let ratio = width as f32 / new_width as f32;
260 let sratio = if ratio < 1.0 { 1.0 } else { ratio };
261 let src_support = filter.support * sratio;
262
263 for outx in 0..new_width {
264 let inputx = (outx as f32 + 0.5) * ratio;
267
268 let left = (inputx - src_support).floor() as i64;
275 let left = clamp(left, 0, <i64 as From<_>>::from(width) - 1) as u32;
276
277 let right = (inputx + src_support).ceil() as i64;
278 let right = clamp(
279 right,
280 <i64 as From<_>>::from(left) + 1,
281 <i64 as From<_>>::from(width),
282 ) as u32;
283
284 let inputx = inputx - 0.5;
287
288 ws.clear();
289 let mut sum = 0.0;
290 for i in left..right {
291 let w = (filter.kernel)((i as f32 - inputx) / sratio);
292 ws.push(w);
293 sum += w;
294 }
295 for w in ws.iter_mut() {
296 *w /= sum;
297 }
298
299 for y in 0..height {
300 let mut t = (0.0, 0.0, 0.0, 0.0);
301
302 for (i, w) in ws.iter().enumerate() {
303 let p = image.get_pixel(left + i as u32, y);
304
305 #[allow(deprecated)]
306 let vec = p.channels4();
307
308 t.0 += vec.0 * w;
309 t.1 += vec.1 * w;
310 t.2 += vec.2 * w;
311 t.3 += vec.3 * w;
312 }
313
314 #[allow(deprecated)]
315 let t = Pixel::from_channels(
316 NumCast::from(FloatNearest(clamp(t.0, min, max))).unwrap(),
317 NumCast::from(FloatNearest(clamp(t.1, min, max))).unwrap(),
318 NumCast::from(FloatNearest(clamp(t.2, min, max))).unwrap(),
319 NumCast::from(FloatNearest(clamp(t.3, min, max))).unwrap(),
320 );
321
322 out.put_pixel(outx, y, t);
323 }
324 }
325
326 out
327}
328
329pub fn sample_bilinear<P: Pixel>(
331 img: &impl GenericImageView<Pixel = P>,
332 u: f32,
333 v: f32,
334) -> Option<P> {
335 if ![u, v].iter().all(|c| (0.0..=1.0).contains(c)) {
336 return None;
337 }
338
339 let (w, h) = img.dimensions();
340 if w == 0 || h == 0 {
341 return None;
342 }
343
344 let ui = w as f32 * u - 0.5;
345 let vi = h as f32 * v - 0.5;
346 interpolate_bilinear(
347 img,
348 ui.max(0.).min((w - 1) as f32),
349 vi.max(0.).min((h - 1) as f32),
350 )
351}
352
353pub fn sample_nearest<P: Pixel>(
355 img: &impl GenericImageView<Pixel = P>,
356 u: f32,
357 v: f32,
358) -> Option<P> {
359 if ![u, v].iter().all(|c| (0.0..=1.0).contains(c)) {
360 return None;
361 }
362
363 let (w, h) = img.dimensions();
364 let ui = w as f32 * u - 0.5;
365 let ui = ui.max(0.).min((w.saturating_sub(1)) as f32);
366
367 let vi = h as f32 * v - 0.5;
368 let vi = vi.max(0.).min((h.saturating_sub(1)) as f32);
369 interpolate_nearest(img, ui, vi)
370}
371
372pub fn interpolate_nearest<P: Pixel>(
379 img: &impl GenericImageView<Pixel = P>,
380 x: f32,
381 y: f32,
382) -> Option<P> {
383 let (w, h) = img.dimensions();
384 if w == 0 || h == 0 {
385 return None;
386 }
387 if !(0.0..=((w - 1) as f32)).contains(&x) {
388 return None;
389 }
390 if !(0.0..=((h - 1) as f32)).contains(&y) {
391 return None;
392 }
393
394 Some(img.get_pixel(x.round() as u32, y.round() as u32))
395}
396
397pub fn interpolate_bilinear<P: Pixel>(
399 img: &impl GenericImageView<Pixel = P>,
400 x: f32,
401 y: f32,
402) -> Option<P> {
403 assert!(P::CHANNEL_COUNT <= 4);
405
406 let (w, h) = img.dimensions();
407 if w == 0 || h == 0 {
408 return None;
409 }
410 if !(0.0..=((w - 1) as f32)).contains(&x) {
411 return None;
412 }
413 if !(0.0..=((h - 1) as f32)).contains(&y) {
414 return None;
415 }
416
417 let uf = x.floor() as u32;
419 let vf = y.floor() as u32;
420 let uc = (uf + 1).min(w - 1);
421 let vc = (vf + 1).min(h - 1);
422
423 let mut sxx = [[0.; 4]; 4];
425
426 let mut compute = |u: u32, v: u32, i| {
434 let s = img.get_pixel(u, v);
435 for (j, c) in s.channels().iter().enumerate() {
436 sxx[j][i] = c.to_f32().unwrap();
437 }
438 s
439 };
440
441 let mut out: P = compute(uf, vf, 0);
443 compute(uf, vc, 1);
444 compute(uc, vf, 2);
445 compute(uc, vc, 3);
446
447 let ufw = x - uf as f32;
449 let vfw = y - vf as f32;
450 let ucw = (uf + 1) as f32 - x;
451 let vcw = (vf + 1) as f32 - y;
452
453 let wff = ucw * vcw;
456 let wfc = ucw * vfw;
457 let wcf = ufw * vcw;
458 let wcc = ufw * vfw;
459 debug_assert!(f32::abs((wff + wfc + wcf + wcc) - 1.) < 1e-3);
461
462 let is_float = P::Subpixel::DEFAULT_MAX_VALUE.to_f32().unwrap() == 1.0;
464
465 for (i, c) in out.channels_mut().iter_mut().enumerate() {
466 let v = wff * sxx[i][0] + wfc * sxx[i][1] + wcf * sxx[i][2] + wcc * sxx[i][3];
467 *c = <P::Subpixel as NumCast>::from(if is_float { v } else { v.round() }).unwrap_or({
471 if v < 0.0 {
472 P::Subpixel::DEFAULT_MIN_VALUE
473 } else {
474 P::Subpixel::DEFAULT_MAX_VALUE
475 }
476 });
477 }
478
479 Some(out)
480}
481
482fn vertical_sample<I, P, S>(image: &I, new_height: u32, filter: &mut Filter) -> Rgba32FImage
491where
492 I: GenericImageView<Pixel = P>,
493 P: Pixel<Subpixel = S> + 'static,
494 S: Primitive + 'static,
495{
496 let (width, height) = image.dimensions();
497
498 assert!(
501 height != 0 || width == 0,
503 "Unexpected prior allocation size. This case should have been handled by the caller"
504 );
505
506 let mut out = ImageBuffer::new(width, new_height);
507 out.copy_color_space_from(&image.buffer_with_dimensions(0, 0));
508 let mut ws = Vec::new();
509
510 let ratio = height as f32 / new_height as f32;
511 let sratio = if ratio < 1.0 { 1.0 } else { ratio };
512 let src_support = filter.support * sratio;
513
514 for outy in 0..new_height {
515 let inputy = (outy as f32 + 0.5) * ratio;
518
519 let left = (inputy - src_support).floor() as i64;
520 let left = clamp(left, 0, <i64 as From<_>>::from(height) - 1) as u32;
521
522 let right = (inputy + src_support).ceil() as i64;
523 let right = clamp(
524 right,
525 <i64 as From<_>>::from(left) + 1,
526 <i64 as From<_>>::from(height),
527 ) as u32;
528
529 let inputy = inputy - 0.5;
530
531 ws.clear();
532 let mut sum = 0.0;
533 for i in left..right {
534 let w = (filter.kernel)((i as f32 - inputy) / sratio);
535 ws.push(w);
536 sum += w;
537 }
538 for w in ws.iter_mut() {
539 *w /= sum;
540 }
541
542 for x in 0..width {
543 let mut t = (0.0, 0.0, 0.0, 0.0);
544
545 for (i, w) in ws.iter().enumerate() {
546 let p = image.get_pixel(x, left + i as u32);
547
548 #[allow(deprecated)]
549 let (k1, k2, k3, k4) = p.channels4();
550 let vec: (f32, f32, f32, f32) = (
551 NumCast::from(k1).unwrap(),
552 NumCast::from(k2).unwrap(),
553 NumCast::from(k3).unwrap(),
554 NumCast::from(k4).unwrap(),
555 );
556
557 t.0 += vec.0 * w;
558 t.1 += vec.1 * w;
559 t.2 += vec.2 * w;
560 t.3 += vec.3 * w;
561 }
562
563 #[allow(deprecated)]
564 let t = Pixel::from_channels(t.0, t.1, t.2, t.3);
566
567 out.put_pixel(x, outy, t);
568 }
569 }
570
571 out
572}
573
574struct ThumbnailSum<S: Primitive + Enlargeable>(S::Larger, S::Larger, S::Larger, S::Larger);
576
577impl<S: Primitive + Enlargeable> ThumbnailSum<S> {
578 fn zeroed() -> Self {
579 ThumbnailSum(
580 S::Larger::zero(),
581 S::Larger::zero(),
582 S::Larger::zero(),
583 S::Larger::zero(),
584 )
585 }
586
587 fn sample_val(val: S) -> S::Larger {
588 <S::Larger as NumCast>::from(val).unwrap()
589 }
590
591 fn add_pixel<P: Pixel<Subpixel = S>>(&mut self, pixel: P) {
592 #[allow(deprecated)]
593 let pixel = pixel.channels4();
594 self.0 += Self::sample_val(pixel.0);
595 self.1 += Self::sample_val(pixel.1);
596 self.2 += Self::sample_val(pixel.2);
597 self.3 += Self::sample_val(pixel.3);
598 }
599}
600
601pub fn thumbnail<I, P, S>(image: &I, new_width: u32, new_height: u32) -> ImageBuffer<P, Vec<S>>
614where
615 I: GenericImageView<Pixel = P>,
616 P: Pixel<Subpixel = S> + 'static,
617 S: Primitive + Enlargeable + 'static,
618{
619 let (width, height) = image.dimensions();
620 let mut out = image.buffer_with_dimensions(new_width, new_height);
621
622 if height == 0 || width == 0 {
623 return out;
624 }
625
626 let x_ratio = width as f32 / new_width as f32;
627 let y_ratio = height as f32 / new_height as f32;
628
629 for outy in 0..new_height {
630 let bottomf = outy as f32 * y_ratio;
631 let topf = bottomf + y_ratio;
632
633 let bottom = clamp(bottomf.ceil() as u32, 0, height - 1);
634 let top = clamp(topf.ceil() as u32, bottom, height);
635
636 for outx in 0..new_width {
637 let leftf = outx as f32 * x_ratio;
638 let rightf = leftf + x_ratio;
639
640 let left = clamp(leftf.ceil() as u32, 0, width - 1);
641 let right = clamp(rightf.ceil() as u32, left, width);
642
643 let avg = if bottom != top && left != right {
644 thumbnail_sample_block(image, left, right, bottom, top)
645 } else if bottom != top {
646 debug_assert!(
650 left > 0 && right > 0,
651 "First output column must have corresponding pixels"
652 );
653
654 let fraction_horizontal = (leftf.fract() + rightf.fract()) / 2.;
655 thumbnail_sample_fraction_horizontal(
656 image,
657 right - 1,
658 fraction_horizontal,
659 bottom,
660 top,
661 )
662 } else if left != right {
663 debug_assert!(
667 bottom > 0 && top > 0,
668 "First output row must have corresponding pixels"
669 );
670
671 let fraction_vertical = (topf.fract() + bottomf.fract()) / 2.;
672 thumbnail_sample_fraction_vertical(image, left, right, top - 1, fraction_vertical)
673 } else {
674 let fraction_horizontal = (topf.fract() + bottomf.fract()) / 2.;
676 let fraction_vertical = (leftf.fract() + rightf.fract()) / 2.;
677
678 thumbnail_sample_fraction_both(
679 image,
680 right - 1,
681 fraction_horizontal,
682 top - 1,
683 fraction_vertical,
684 )
685 };
686
687 #[allow(deprecated)]
688 let pixel = Pixel::from_channels(avg.0, avg.1, avg.2, avg.3);
689 out.put_pixel(outx, outy, pixel);
690 }
691 }
692
693 out
694}
695
696fn thumbnail_sample_block<I, P, S>(
698 image: &I,
699 left: u32,
700 right: u32,
701 bottom: u32,
702 top: u32,
703) -> (S, S, S, S)
704where
705 I: GenericImageView<Pixel = P>,
706 P: Pixel<Subpixel = S>,
707 S: Primitive + Enlargeable,
708{
709 let mut sum = ThumbnailSum::zeroed();
710
711 for y in bottom..top {
712 for x in left..right {
713 let k = image.get_pixel(x, y);
714 sum.add_pixel(k);
715 }
716 }
717
718 let n = <S::Larger as NumCast>::from((right - left) * (top - bottom)).unwrap();
719 let round = <S::Larger as NumCast>::from(n / NumCast::from(2).unwrap()).unwrap();
720 (
721 S::clamp_from((sum.0 + round) / n),
722 S::clamp_from((sum.1 + round) / n),
723 S::clamp_from((sum.2 + round) / n),
724 S::clamp_from((sum.3 + round) / n),
725 )
726}
727
728fn thumbnail_sample_fraction_horizontal<I, P, S>(
730 image: &I,
731 left: u32,
732 fraction_horizontal: f32,
733 bottom: u32,
734 top: u32,
735) -> (S, S, S, S)
736where
737 I: GenericImageView<Pixel = P>,
738 P: Pixel<Subpixel = S>,
739 S: Primitive + Enlargeable,
740{
741 let fract = fraction_horizontal;
742
743 let mut sum_left = ThumbnailSum::zeroed();
744 let mut sum_right = ThumbnailSum::zeroed();
745 for x in bottom..top {
746 let k_left = image.get_pixel(left, x);
747 sum_left.add_pixel(k_left);
748
749 let k_right = image.get_pixel(left + 1, x);
750 sum_right.add_pixel(k_right);
751 }
752
753 let fact_right = fract / ((top - bottom) as f32);
755 let fact_left = (1. - fract) / ((top - bottom) as f32);
756
757 let mix_left_and_right = |leftv: S::Larger, rightv: S::Larger| {
758 <S as NumCast>::from(
759 fact_left * leftv.to_f32().unwrap() + fact_right * rightv.to_f32().unwrap(),
760 )
761 .expect("Average sample value should fit into sample type")
762 };
763
764 (
765 mix_left_and_right(sum_left.0, sum_right.0),
766 mix_left_and_right(sum_left.1, sum_right.1),
767 mix_left_and_right(sum_left.2, sum_right.2),
768 mix_left_and_right(sum_left.3, sum_right.3),
769 )
770}
771
772fn thumbnail_sample_fraction_vertical<I, P, S>(
774 image: &I,
775 left: u32,
776 right: u32,
777 bottom: u32,
778 fraction_vertical: f32,
779) -> (S, S, S, S)
780where
781 I: GenericImageView<Pixel = P>,
782 P: Pixel<Subpixel = S>,
783 S: Primitive + Enlargeable,
784{
785 let fract = fraction_vertical;
786
787 let mut sum_bot = ThumbnailSum::zeroed();
788 let mut sum_top = ThumbnailSum::zeroed();
789 for x in left..right {
790 let k_bot = image.get_pixel(x, bottom);
791 sum_bot.add_pixel(k_bot);
792
793 let k_top = image.get_pixel(x, bottom + 1);
794 sum_top.add_pixel(k_top);
795 }
796
797 let fact_top = fract / ((right - left) as f32);
799 let fact_bot = (1. - fract) / ((right - left) as f32);
800
801 let mix_bot_and_top = |botv: S::Larger, topv: S::Larger| {
802 <S as NumCast>::from(fact_bot * botv.to_f32().unwrap() + fact_top * topv.to_f32().unwrap())
803 .expect("Average sample value should fit into sample type")
804 };
805
806 (
807 mix_bot_and_top(sum_bot.0, sum_top.0),
808 mix_bot_and_top(sum_bot.1, sum_top.1),
809 mix_bot_and_top(sum_bot.2, sum_top.2),
810 mix_bot_and_top(sum_bot.3, sum_top.3),
811 )
812}
813
814fn thumbnail_sample_fraction_both<I, P, S>(
816 image: &I,
817 left: u32,
818 fraction_vertical: f32,
819 bottom: u32,
820 fraction_horizontal: f32,
821) -> (S, S, S, S)
822where
823 I: GenericImageView<Pixel = P>,
824 P: Pixel<Subpixel = S>,
825 S: Primitive + Enlargeable,
826{
827 #[allow(deprecated)]
828 let k_bl = image.get_pixel(left, bottom).channels4();
829 #[allow(deprecated)]
830 let k_tl = image.get_pixel(left, bottom + 1).channels4();
831 #[allow(deprecated)]
832 let k_br = image.get_pixel(left + 1, bottom).channels4();
833 #[allow(deprecated)]
834 let k_tr = image.get_pixel(left + 1, bottom + 1).channels4();
835
836 let frac_v = fraction_vertical;
837 let frac_h = fraction_horizontal;
838
839 let fact_tr = frac_v * frac_h;
840 let fact_tl = frac_v * (1. - frac_h);
841 let fact_br = (1. - frac_v) * frac_h;
842 let fact_bl = (1. - frac_v) * (1. - frac_h);
843
844 let mix = |br: S, tr: S, bl: S, tl: S| {
845 <S as NumCast>::from(
846 fact_br * br.to_f32().unwrap()
847 + fact_tr * tr.to_f32().unwrap()
848 + fact_bl * bl.to_f32().unwrap()
849 + fact_tl * tl.to_f32().unwrap(),
850 )
851 .expect("Average sample value should fit into sample type")
852 };
853
854 (
855 mix(k_br.0, k_tr.0, k_bl.0, k_tl.0),
856 mix(k_br.1, k_tr.1, k_bl.1, k_tl.1),
857 mix(k_br.2, k_tr.2, k_bl.2, k_tl.2),
858 mix(k_br.3, k_tr.3, k_bl.3, k_tl.3),
859 )
860}
861
862pub fn filter3x3<I, P, S>(image: &I, kernel: &[f32]) -> ImageBuffer<P, Vec<S>>
872where
873 I: GenericImageView<Pixel = P>,
874 P: Pixel<Subpixel = S> + 'static,
875 S: Primitive + 'static,
876{
877 let taps: &[(isize, isize)] = &[
879 (-1, -1),
880 (0, -1),
881 (1, -1),
882 (-1, 0),
883 (0, 0),
884 (1, 0),
885 (-1, 1),
886 (0, 1),
887 (1, 1),
888 ];
889
890 let (width, height) = image.dimensions();
891 let mut out = image.buffer_like();
892
893 let max = S::DEFAULT_MAX_VALUE;
894 let max: f32 = NumCast::from(max).unwrap();
895
896 #[allow(clippy::redundant_guards)]
897 let sum = match kernel.iter().fold(0.0, |s, &item| s + item) {
898 x if x == 0.0 => 1.0,
899 sum => sum,
900 };
901 let sum = (sum, sum, sum, sum);
902
903 for y in 1..height - 1 {
904 for x in 1..width - 1 {
905 let mut t = (0.0, 0.0, 0.0, 0.0);
906
907 for (&k, &(a, b)) in kernel.iter().zip(taps.iter()) {
911 let k = (k, k, k, k);
912 let x0 = x as isize + a;
913 let y0 = y as isize + b;
914
915 let p = image.get_pixel(x0 as u32, y0 as u32);
916
917 #[allow(deprecated)]
918 let (k1, k2, k3, k4) = p.channels4();
919
920 let vec: (f32, f32, f32, f32) = (
921 NumCast::from(k1).unwrap(),
922 NumCast::from(k2).unwrap(),
923 NumCast::from(k3).unwrap(),
924 NumCast::from(k4).unwrap(),
925 );
926
927 t.0 += vec.0 * k.0;
928 t.1 += vec.1 * k.1;
929 t.2 += vec.2 * k.2;
930 t.3 += vec.3 * k.3;
931 }
932
933 let (t1, t2, t3, t4) = (t.0 / sum.0, t.1 / sum.1, t.2 / sum.2, t.3 / sum.3);
934
935 #[allow(deprecated)]
936 let t = Pixel::from_channels(
937 NumCast::from(clamp(t1, 0.0, max)).unwrap(),
938 NumCast::from(clamp(t2, 0.0, max)).unwrap(),
939 NumCast::from(clamp(t3, 0.0, max)).unwrap(),
940 NumCast::from(clamp(t4, 0.0, max)).unwrap(),
941 );
942
943 out.put_pixel(x, y, t);
944 }
945 }
946
947 out
948}
949
950pub fn resize<I: GenericImageView>(
963 image: &I,
964 nwidth: u32,
965 nheight: u32,
966 filter: FilterType,
967) -> ImageBuffer<I::Pixel, Vec<<I::Pixel as Pixel>::Subpixel>>
968where
969 I::Pixel: 'static,
970 <I::Pixel as Pixel>::Subpixel: 'static,
971{
972 let is_empty = {
974 let (width, height) = image.dimensions();
975 width == 0 || height == 0
976 };
977
978 if is_empty {
979 return image.buffer_with_dimensions(nwidth, nheight);
980 }
981
982 if (nwidth, nheight) == image.dimensions() {
984 let mut tmp = image.buffer_like();
985 tmp.copy_from(image, 0, 0).unwrap();
986 return tmp;
987 }
988
989 let mut method = match filter {
990 FilterType::Nearest => Filter {
991 kernel: Box::new(box_kernel),
992 support: 0.0,
993 },
994 FilterType::Triangle => Filter {
995 kernel: Box::new(triangle_kernel),
996 support: 1.0,
997 },
998 FilterType::CatmullRom => Filter {
999 kernel: Box::new(catmullrom_kernel),
1000 support: 2.0,
1001 },
1002 FilterType::Gaussian => Filter {
1003 kernel: Box::new(gaussian_kernel),
1004 support: 3.0,
1005 },
1006 FilterType::Lanczos3 => Filter {
1007 kernel: Box::new(lanczos3_kernel),
1008 support: 3.0,
1009 },
1010 };
1011
1012 let tmp: Rgba32FImage = vertical_sample(image, nheight, &mut method);
1014 horizontal_sample(&tmp, nwidth, &mut method)
1015}
1016
1017pub fn blur<I: GenericImageView>(
1029 image: &I,
1030 sigma: f32,
1031) -> ImageBuffer<I::Pixel, Vec<<I::Pixel as Pixel>::Subpixel>>
1032where
1033 I::Pixel: 'static,
1034{
1035 gaussian_blur_indirect(
1036 image,
1037 GaussianBlurParameters::new_from_sigma(if sigma == 0.0 { 0.8 } else { sigma }),
1038 )
1039}
1040
1041pub fn blur_advanced<I: GenericImageView>(
1051 image: &I,
1052 parameters: GaussianBlurParameters,
1053) -> ImageBuffer<I::Pixel, Vec<<I::Pixel as Pixel>::Subpixel>>
1054where
1055 I::Pixel: 'static,
1056{
1057 gaussian_blur_indirect(image, parameters)
1058}
1059
1060fn get_gaussian_kernel_1d(width: usize, sigma: f32) -> Vec<f32> {
1061 let mut sum_norm: f32 = 0f32;
1062 let mut kernel = vec![0f32; width];
1063 let scale = 1f32 / (f32::sqrt(2f32 * f32::consts::PI) * sigma);
1064 let mean = (width / 2) as f32;
1065
1066 for (x, weight) in kernel.iter_mut().enumerate() {
1067 let new_weight = f32::exp(-0.5f32 * f32::powf((x as f32 - mean) / sigma, 2.0f32)) * scale;
1068 *weight = new_weight;
1069 sum_norm += new_weight;
1070 }
1071
1072 if sum_norm != 0f32 {
1073 let sum_scale = 1f32 / sum_norm;
1074 for weight in &mut kernel {
1075 *weight = weight.mul(sum_scale);
1076 }
1077 }
1078
1079 kernel
1080}
1081
1082#[derive(Copy, Clone, PartialOrd, PartialEq)]
1084pub struct GaussianBlurParameters {
1085 x_axis_kernel_size: u32,
1087 x_axis_sigma: f32,
1089 y_axis_kernel_size: u32,
1091 y_axis_sigma: f32,
1093}
1094
1095impl GaussianBlurParameters {
1096 pub const SMOOTHING_3: GaussianBlurParameters = GaussianBlurParameters {
1098 x_axis_kernel_size: 3,
1099 x_axis_sigma: 0.8,
1100 y_axis_kernel_size: 3,
1101 y_axis_sigma: 0.8,
1102 };
1103
1104 pub const SMOOTHING_5: GaussianBlurParameters = GaussianBlurParameters {
1106 x_axis_kernel_size: 5,
1107 x_axis_sigma: 1.1,
1108 y_axis_kernel_size: 5,
1109 y_axis_sigma: 1.1,
1110 };
1111
1112 pub const SMOOTHING_7: GaussianBlurParameters = GaussianBlurParameters {
1114 x_axis_kernel_size: 7,
1115 x_axis_sigma: 1.4,
1116 y_axis_kernel_size: 7,
1117 y_axis_sigma: 1.4,
1118 };
1119
1120 pub fn new_from_radius(radius: f32) -> GaussianBlurParameters {
1122 assert!(radius >= 0.0);
1124 if radius != 0. {
1125 assert!(
1126 radius.is_normal(),
1127 "Radius do not allow infinities, NaNs or subnormals"
1128 );
1129 }
1130 GaussianBlurParameters::new_from_kernel_size(radius * 2. + 1.)
1131 }
1132
1133 pub fn new_from_kernel_size(kernel_size: f32) -> GaussianBlurParameters {
1138 assert!(
1139 kernel_size > 0.,
1140 "Kernel size do not allow infinities, zeros, NaNs or subnormals or negatives"
1141 );
1142 assert!(
1143 kernel_size.is_normal(),
1144 "Kernel size do not allow infinities, zeros, NaNs or subnormals or negatives"
1145 );
1146 let i_kernel_size = GaussianBlurParameters::round_to_nearest_odd(kernel_size);
1147 assert_ne!(i_kernel_size % 2, 0, "Kernel size must be odd");
1148 let v_sigma = GaussianBlurParameters::sigma_size(kernel_size);
1149 GaussianBlurParameters {
1150 x_axis_kernel_size: i_kernel_size,
1151 x_axis_sigma: v_sigma,
1152 y_axis_kernel_size: i_kernel_size,
1153 y_axis_sigma: v_sigma,
1154 }
1155 }
1156
1157 pub fn new_anisotropic_kernel_size(
1162 x_axis_kernel_size: f32,
1163 y_axis_kernel_size: f32,
1164 ) -> GaussianBlurParameters {
1165 assert!(
1166 x_axis_kernel_size > 0.,
1167 "Kernel size do not allow infinities, zeros, NaNs or subnormals or negatives"
1168 );
1169 assert!(
1170 y_axis_kernel_size.is_normal(),
1171 "Kernel size do not allow infinities, zeros, NaNs or subnormals or negatives"
1172 );
1173 assert!(
1174 y_axis_kernel_size > 0.,
1175 "Kernel size do not allow infinities, zeros, NaNs or subnormals or negatives"
1176 );
1177 assert!(
1178 y_axis_kernel_size.is_normal(),
1179 "Kernel size do not allow infinities, zeros, NaNs or subnormals or negatives"
1180 );
1181 let x_kernel_size = GaussianBlurParameters::round_to_nearest_odd(x_axis_kernel_size);
1182 assert_ne!(x_kernel_size % 2, 0, "Kernel size must be odd");
1183 let y_kernel_size = GaussianBlurParameters::round_to_nearest_odd(y_axis_kernel_size);
1184 assert_ne!(y_kernel_size % 2, 0, "Kernel size must be odd");
1185 let x_sigma = GaussianBlurParameters::sigma_size(x_axis_kernel_size);
1186 let y_sigma = GaussianBlurParameters::sigma_size(y_axis_kernel_size);
1187 GaussianBlurParameters {
1188 x_axis_kernel_size: x_kernel_size,
1189 x_axis_sigma: x_sigma,
1190 y_axis_kernel_size: y_kernel_size,
1191 y_axis_sigma: y_sigma,
1192 }
1193 }
1194
1195 pub fn new_from_sigma(sigma: f32) -> GaussianBlurParameters {
1197 assert!(
1198 sigma.is_normal(),
1199 "Sigma cannot be NaN, Infinities, subnormal or zero"
1200 );
1201 assert!(sigma > 0.0, "Sigma must be positive");
1202 let kernel_size = GaussianBlurParameters::kernel_size_from_sigma(sigma);
1203 GaussianBlurParameters {
1204 x_axis_kernel_size: kernel_size,
1205 x_axis_sigma: sigma,
1206 y_axis_kernel_size: kernel_size,
1207 y_axis_sigma: sigma,
1208 }
1209 }
1210
1211 #[inline]
1212 fn round_to_nearest_odd(x: f32) -> u32 {
1213 let n = x.round() as u32;
1214 if n % 2 != 0 {
1215 n
1216 } else {
1217 let lower = n - 1;
1218 let upper = n + 1;
1219
1220 let dist_lower = (x - lower as f32).abs();
1221 let dist_upper = (x - upper as f32).abs();
1222
1223 if dist_lower <= dist_upper {
1224 lower
1225 } else {
1226 upper
1227 }
1228 }
1229 }
1230
1231 fn sigma_size(kernel_size: f32) -> f32 {
1232 let safe_kernel_size = if kernel_size <= 1. { 0.8 } else { kernel_size };
1233 0.3 * ((safe_kernel_size - 1.) * 0.5 - 1.) + 0.8
1234 }
1235
1236 fn kernel_size_from_sigma(sigma: f32) -> u32 {
1237 let possible_size = (((((sigma - 0.8) / 0.3) + 1.) * 2.) + 1.).max(3.) as u32;
1238 if possible_size % 2 == 0 {
1239 return possible_size + 1;
1240 }
1241 possible_size
1242 }
1243}
1244
1245pub(crate) fn gaussian_blur_dyn_image(
1246 image: &DynamicImage,
1247 parameters: GaussianBlurParameters,
1248) -> DynamicImage {
1249 let x_axis_kernel = get_gaussian_kernel_1d(
1250 parameters.x_axis_kernel_size as usize,
1251 parameters.x_axis_sigma,
1252 );
1253
1254 let y_axis_kernel = get_gaussian_kernel_1d(
1255 parameters.y_axis_kernel_size as usize,
1256 parameters.y_axis_sigma,
1257 );
1258
1259 let filter_image_size = FilterImageSize {
1260 width: image.width() as usize,
1261 height: image.height() as usize,
1262 };
1263
1264 let mut target = match image {
1265 DynamicImage::ImageLuma8(img) => {
1266 let mut dest_image = vec![0u8; img.len()];
1267 filter_2d_sep_plane(
1268 img.as_raw(),
1269 &mut dest_image,
1270 filter_image_size,
1271 &x_axis_kernel,
1272 &y_axis_kernel,
1273 )
1274 .unwrap();
1275 DynamicImage::ImageLuma8(
1276 GrayImage::from_raw(img.width(), img.height(), dest_image).unwrap(),
1277 )
1278 }
1279 DynamicImage::ImageLumaA8(img) => {
1280 let mut dest_image = vec![0u8; img.len()];
1281 filter_2d_sep_la(
1282 img.as_raw(),
1283 &mut dest_image,
1284 filter_image_size,
1285 &x_axis_kernel,
1286 &y_axis_kernel,
1287 )
1288 .unwrap();
1289 DynamicImage::ImageLumaA8(
1290 GrayAlphaImage::from_raw(img.width(), img.height(), dest_image).unwrap(),
1291 )
1292 }
1293 DynamicImage::ImageRgb8(img) => {
1294 let mut dest_image = vec![0u8; img.len()];
1295 filter_2d_sep_rgb(
1296 img.as_raw(),
1297 &mut dest_image,
1298 filter_image_size,
1299 &x_axis_kernel,
1300 &y_axis_kernel,
1301 )
1302 .unwrap();
1303 DynamicImage::ImageRgb8(
1304 RgbImage::from_raw(img.width(), img.height(), dest_image).unwrap(),
1305 )
1306 }
1307 DynamicImage::ImageRgba8(img) => {
1308 let mut dest_image = vec![0u8; img.len()];
1309 filter_2d_sep_rgba(
1310 img.as_raw(),
1311 &mut dest_image,
1312 filter_image_size,
1313 &x_axis_kernel,
1314 &y_axis_kernel,
1315 )
1316 .unwrap();
1317 DynamicImage::ImageRgba8(
1318 RgbaImage::from_raw(img.width(), img.height(), dest_image).unwrap(),
1319 )
1320 }
1321 DynamicImage::ImageLuma16(img) => {
1322 let mut dest_image = vec![0u16; img.len()];
1323 filter_2d_sep_plane_u16(
1324 img.as_raw(),
1325 &mut dest_image,
1326 filter_image_size,
1327 &x_axis_kernel,
1328 &y_axis_kernel,
1329 )
1330 .unwrap();
1331 DynamicImage::ImageLuma16(
1332 Gray16Image::from_raw(img.width(), img.height(), dest_image).unwrap(),
1333 )
1334 }
1335 DynamicImage::ImageLumaA16(img) => {
1336 let mut dest_image = vec![0u16; img.len()];
1337 filter_2d_sep_la_u16(
1338 img.as_raw(),
1339 &mut dest_image,
1340 filter_image_size,
1341 &x_axis_kernel,
1342 &y_axis_kernel,
1343 )
1344 .unwrap();
1345 DynamicImage::ImageLumaA16(
1346 GrayAlpha16Image::from_raw(img.width(), img.height(), dest_image).unwrap(),
1347 )
1348 }
1349 DynamicImage::ImageRgb16(img) => {
1350 let mut dest_image = vec![0u16; img.len()];
1351 filter_2d_sep_rgb_u16(
1352 img.as_raw(),
1353 &mut dest_image,
1354 filter_image_size,
1355 &x_axis_kernel,
1356 &y_axis_kernel,
1357 )
1358 .unwrap();
1359 DynamicImage::ImageRgb16(
1360 Rgb16Image::from_raw(img.width(), img.height(), dest_image).unwrap(),
1361 )
1362 }
1363 DynamicImage::ImageRgba16(img) => {
1364 let mut dest_image = vec![0u16; img.len()];
1365 filter_2d_sep_rgba_u16(
1366 img.as_raw(),
1367 &mut dest_image,
1368 filter_image_size,
1369 &x_axis_kernel,
1370 &y_axis_kernel,
1371 )
1372 .unwrap();
1373 DynamicImage::ImageRgba16(
1374 Rgba16Image::from_raw(img.width(), img.height(), dest_image).unwrap(),
1375 )
1376 }
1377 DynamicImage::ImageRgb32F(img) => {
1378 let mut dest_image = vec![0f32; img.len()];
1379 filter_2d_sep_rgb_f32(
1380 img.as_raw(),
1381 &mut dest_image,
1382 filter_image_size,
1383 &x_axis_kernel,
1384 &y_axis_kernel,
1385 )
1386 .unwrap();
1387 DynamicImage::ImageRgb32F(
1388 Rgb32FImage::from_raw(img.width(), img.height(), dest_image).unwrap(),
1389 )
1390 }
1391 DynamicImage::ImageRgba32F(img) => {
1392 let mut dest_image = vec![0f32; img.len()];
1393 filter_2d_sep_rgba_f32(
1394 img.as_raw(),
1395 &mut dest_image,
1396 filter_image_size,
1397 &x_axis_kernel,
1398 &y_axis_kernel,
1399 )
1400 .unwrap();
1401 DynamicImage::ImageRgba32F(
1402 Rgba32FImage::from_raw(img.width(), img.height(), dest_image).unwrap(),
1403 )
1404 }
1405 };
1406
1407 let _ = target.set_color_space(image.color_space());
1409 target
1410}
1411
1412fn gaussian_blur_indirect<I: GenericImageView>(
1413 image: &I,
1414 parameters: GaussianBlurParameters,
1415) -> ImageBuffer<I::Pixel, Vec<<I::Pixel as Pixel>::Subpixel>>
1416where
1417 I::Pixel: 'static,
1418{
1419 match I::Pixel::CHANNEL_COUNT {
1420 1 => gaussian_blur_indirect_impl::<I, 1>(image, parameters),
1421 2 => gaussian_blur_indirect_impl::<I, 2>(image, parameters),
1422 3 => gaussian_blur_indirect_impl::<I, 3>(image, parameters),
1423 4 => gaussian_blur_indirect_impl::<I, 4>(image, parameters),
1424 _ => unimplemented!(),
1425 }
1426}
1427
1428fn gaussian_blur_indirect_impl<I: GenericImageView, const CN: usize>(
1429 image: &I,
1430 parameters: GaussianBlurParameters,
1431) -> ImageBuffer<I::Pixel, Vec<<I::Pixel as Pixel>::Subpixel>>
1432where
1433 I::Pixel: 'static,
1434{
1435 let mut transient = vec![0f32; image.width() as usize * image.height() as usize * CN];
1436 for (pixel, dst) in image.pixels().zip(transient.chunks_exact_mut(CN)) {
1437 let px = pixel.2.channels();
1438 match CN {
1439 1 => {
1440 dst[0] = NumCast::from(px[0]).unwrap();
1441 }
1442 2 => {
1443 dst[0] = NumCast::from(px[0]).unwrap();
1444 dst[1] = NumCast::from(px[1]).unwrap();
1445 }
1446 3 => {
1447 dst[0] = NumCast::from(px[0]).unwrap();
1448 dst[1] = NumCast::from(px[1]).unwrap();
1449 dst[2] = NumCast::from(px[2]).unwrap();
1450 }
1451 4 => {
1452 dst[0] = NumCast::from(px[0]).unwrap();
1453 dst[1] = NumCast::from(px[1]).unwrap();
1454 dst[2] = NumCast::from(px[2]).unwrap();
1455 dst[3] = NumCast::from(px[3]).unwrap();
1456 }
1457 _ => unreachable!(),
1458 }
1459 }
1460
1461 let mut transient_dst = vec![0.; image.width() as usize * image.height() as usize * CN];
1462
1463 let x_axis_kernel = get_gaussian_kernel_1d(
1464 parameters.x_axis_kernel_size as usize,
1465 parameters.x_axis_sigma,
1466 );
1467 let y_axis_kernel = get_gaussian_kernel_1d(
1468 parameters.y_axis_kernel_size as usize,
1469 parameters.y_axis_sigma,
1470 );
1471
1472 let filter_image_size = FilterImageSize {
1473 width: image.width() as usize,
1474 height: image.height() as usize,
1475 };
1476
1477 match CN {
1478 1 => {
1479 filter_2d_sep_plane_f32(
1480 &transient,
1481 &mut transient_dst,
1482 filter_image_size,
1483 &x_axis_kernel,
1484 &y_axis_kernel,
1485 )
1486 .unwrap();
1487 }
1488 2 => {
1489 filter_2d_sep_la_f32(
1490 &transient,
1491 &mut transient_dst,
1492 filter_image_size,
1493 &x_axis_kernel,
1494 &y_axis_kernel,
1495 )
1496 .unwrap();
1497 }
1498 3 => {
1499 filter_2d_sep_rgb_f32(
1500 &transient,
1501 &mut transient_dst,
1502 filter_image_size,
1503 &x_axis_kernel,
1504 &y_axis_kernel,
1505 )
1506 .unwrap();
1507 }
1508 4 => {
1509 filter_2d_sep_rgba_f32(
1510 &transient,
1511 &mut transient_dst,
1512 filter_image_size,
1513 &x_axis_kernel,
1514 &y_axis_kernel,
1515 )
1516 .unwrap();
1517 }
1518 _ => unreachable!(),
1519 }
1520
1521 let mut out = image.buffer_like();
1522 for (dst, src) in out.pixels_mut().zip(transient_dst.chunks_exact_mut(CN)) {
1523 match CN {
1524 1 => {
1525 let v0 = NumCast::from(FloatNearest(src[0])).unwrap();
1526 #[allow(deprecated)]
1527 let t = Pixel::from_channels(v0, v0, v0, v0);
1528 *dst = t;
1529 }
1530 2 => {
1531 let v0 = NumCast::from(FloatNearest(src[0])).unwrap();
1532 let v1 = NumCast::from(FloatNearest(src[1])).unwrap();
1533 #[allow(deprecated)]
1534 let t = Pixel::from_channels(v0, v1, v0, v0);
1535 *dst = t;
1536 }
1537 3 => {
1538 let v0 = NumCast::from(FloatNearest(src[0])).unwrap();
1539 let v1 = NumCast::from(FloatNearest(src[1])).unwrap();
1540 let v2 = NumCast::from(FloatNearest(src[2])).unwrap();
1541 #[allow(deprecated)]
1542 let t = Pixel::from_channels(v0, v1, v2, v0);
1543 *dst = t;
1544 }
1545 4 => {
1546 let v0 = NumCast::from(FloatNearest(src[0])).unwrap();
1547 let v1 = NumCast::from(FloatNearest(src[1])).unwrap();
1548 let v2 = NumCast::from(FloatNearest(src[2])).unwrap();
1549 let v3 = NumCast::from(FloatNearest(src[3])).unwrap();
1550 #[allow(deprecated)]
1551 let t = Pixel::from_channels(v0, v1, v2, v3);
1552 *dst = t;
1553 }
1554 _ => unreachable!(),
1555 }
1556 }
1557
1558 out
1559}
1560
1561pub fn unsharpen<I, P, S>(image: &I, sigma: f32, threshold: i32) -> ImageBuffer<P, Vec<S>>
1573where
1574 I: GenericImageView<Pixel = P>,
1575 P: Pixel<Subpixel = S> + 'static,
1576 S: Primitive + 'static,
1577{
1578 let mut tmp = blur_advanced(image, GaussianBlurParameters::new_from_sigma(sigma));
1579
1580 let max = S::DEFAULT_MAX_VALUE;
1581 let max: i32 = NumCast::from(max).unwrap();
1582 let (width, height) = image.dimensions();
1583
1584 for y in 0..height {
1585 for x in 0..width {
1586 let a = image.get_pixel(x, y);
1587 let b = tmp.get_pixel_mut(x, y);
1588
1589 let p = a.map2(b, |c, d| {
1590 let ic: i32 = NumCast::from(c).unwrap();
1591 let id: i32 = NumCast::from(d).unwrap();
1592
1593 let diff = ic - id;
1594
1595 if diff.abs() > threshold {
1596 let e = clamp(ic + diff, 0, max); NumCast::from(e).unwrap()
1599 } else {
1600 c
1601 }
1602 });
1603
1604 *b = p;
1605 }
1606 }
1607
1608 tmp
1609}
1610
1611#[cfg(test)]
1612mod tests {
1613 use super::{resize, sample_bilinear, sample_nearest, FilterType};
1614 use crate::{GenericImageView, ImageBuffer, RgbImage};
1615 #[cfg(feature = "benchmarks")]
1616 use test;
1617
1618 #[bench]
1619 #[cfg(all(feature = "benchmarks", feature = "png"))]
1620 fn bench_resize(b: &mut test::Bencher) {
1621 use std::path::Path;
1622 let img = crate::open(Path::new("./examples/fractal.png")).unwrap();
1623 b.iter(|| {
1624 test::black_box(resize(&img, 200, 200, FilterType::Nearest));
1625 });
1626 b.bytes = 800 * 800 * 3 + 200 * 200 * 3;
1627 }
1628
1629 #[test]
1630 #[cfg(feature = "png")]
1631 fn test_resize_same_size() {
1632 use std::path::Path;
1633 let img = crate::open(Path::new("./examples/fractal.png")).unwrap();
1634 let resize = img.resize(img.width(), img.height(), FilterType::Triangle);
1635 assert!(img.pixels().eq(resize.pixels()));
1636 }
1637
1638 #[test]
1639 #[cfg(feature = "png")]
1640 fn test_sample_bilinear() {
1641 use std::path::Path;
1642 let img = crate::open(Path::new("./examples/fractal.png")).unwrap();
1643 assert!(sample_bilinear(&img, 0., 0.).is_some());
1644 assert!(sample_bilinear(&img, 1., 0.).is_some());
1645 assert!(sample_bilinear(&img, 0., 1.).is_some());
1646 assert!(sample_bilinear(&img, 1., 1.).is_some());
1647 assert!(sample_bilinear(&img, 0.5, 0.5).is_some());
1648
1649 assert!(sample_bilinear(&img, 1.2, 0.5).is_none());
1650 assert!(sample_bilinear(&img, 0.5, 1.2).is_none());
1651 assert!(sample_bilinear(&img, 1.2, 1.2).is_none());
1652
1653 assert!(sample_bilinear(&img, -0.1, 0.2).is_none());
1654 assert!(sample_bilinear(&img, 0.2, -0.1).is_none());
1655 assert!(sample_bilinear(&img, -0.1, -0.1).is_none());
1656 }
1657 #[test]
1658 #[cfg(feature = "png")]
1659 fn test_sample_nearest() {
1660 use std::path::Path;
1661 let img = crate::open(Path::new("./examples/fractal.png")).unwrap();
1662 assert!(sample_nearest(&img, 0., 0.).is_some());
1663 assert!(sample_nearest(&img, 1., 0.).is_some());
1664 assert!(sample_nearest(&img, 0., 1.).is_some());
1665 assert!(sample_nearest(&img, 1., 1.).is_some());
1666 assert!(sample_nearest(&img, 0.5, 0.5).is_some());
1667
1668 assert!(sample_nearest(&img, 1.2, 0.5).is_none());
1669 assert!(sample_nearest(&img, 0.5, 1.2).is_none());
1670 assert!(sample_nearest(&img, 1.2, 1.2).is_none());
1671
1672 assert!(sample_nearest(&img, -0.1, 0.2).is_none());
1673 assert!(sample_nearest(&img, 0.2, -0.1).is_none());
1674 assert!(sample_nearest(&img, -0.1, -0.1).is_none());
1675 }
1676 #[test]
1677 fn test_sample_bilinear_correctness() {
1678 use crate::Rgba;
1679 let img = ImageBuffer::from_fn(2, 2, |x, y| match (x, y) {
1680 (0, 0) => Rgba([255, 0, 0, 0]),
1681 (0, 1) => Rgba([0, 255, 0, 0]),
1682 (1, 0) => Rgba([0, 0, 255, 0]),
1683 (1, 1) => Rgba([0, 0, 0, 255]),
1684 _ => panic!(),
1685 });
1686 assert_eq!(sample_bilinear(&img, 0.5, 0.5), Some(Rgba([64; 4])));
1687 assert_eq!(sample_bilinear(&img, 0.0, 0.0), Some(Rgba([255, 0, 0, 0])));
1688 assert_eq!(sample_bilinear(&img, 0.0, 1.0), Some(Rgba([0, 255, 0, 0])));
1689 assert_eq!(sample_bilinear(&img, 1.0, 0.0), Some(Rgba([0, 0, 255, 0])));
1690 assert_eq!(sample_bilinear(&img, 1.0, 1.0), Some(Rgba([0, 0, 0, 255])));
1691
1692 assert_eq!(
1693 sample_bilinear(&img, 0.5, 0.0),
1694 Some(Rgba([128, 0, 128, 0]))
1695 );
1696 assert_eq!(
1697 sample_bilinear(&img, 0.0, 0.5),
1698 Some(Rgba([128, 128, 0, 0]))
1699 );
1700 assert_eq!(
1701 sample_bilinear(&img, 0.5, 1.0),
1702 Some(Rgba([0, 128, 0, 128]))
1703 );
1704 assert_eq!(
1705 sample_bilinear(&img, 1.0, 0.5),
1706 Some(Rgba([0, 0, 128, 128]))
1707 );
1708 }
1709 #[bench]
1710 #[cfg(feature = "benchmarks")]
1711 fn bench_sample_bilinear(b: &mut test::Bencher) {
1712 use crate::Rgba;
1713 let img = ImageBuffer::from_fn(2, 2, |x, y| match (x, y) {
1714 (0, 0) => Rgba([255, 0, 0, 0]),
1715 (0, 1) => Rgba([0, 255, 0, 0]),
1716 (1, 0) => Rgba([0, 0, 255, 0]),
1717 (1, 1) => Rgba([0, 0, 0, 255]),
1718 _ => panic!(),
1719 });
1720 b.iter(|| {
1721 sample_bilinear(&img, test::black_box(0.5), test::black_box(0.5));
1722 });
1723 }
1724 #[test]
1725 fn test_sample_nearest_correctness() {
1726 use crate::Rgba;
1727 let img = ImageBuffer::from_fn(2, 2, |x, y| match (x, y) {
1728 (0, 0) => Rgba([255, 0, 0, 0]),
1729 (0, 1) => Rgba([0, 255, 0, 0]),
1730 (1, 0) => Rgba([0, 0, 255, 0]),
1731 (1, 1) => Rgba([0, 0, 0, 255]),
1732 _ => panic!(),
1733 });
1734
1735 assert_eq!(sample_nearest(&img, 0.0, 0.0), Some(Rgba([255, 0, 0, 0])));
1736 assert_eq!(sample_nearest(&img, 0.0, 1.0), Some(Rgba([0, 255, 0, 0])));
1737 assert_eq!(sample_nearest(&img, 1.0, 0.0), Some(Rgba([0, 0, 255, 0])));
1738 assert_eq!(sample_nearest(&img, 1.0, 1.0), Some(Rgba([0, 0, 0, 255])));
1739
1740 assert_eq!(sample_nearest(&img, 0.5, 0.5), Some(Rgba([0, 0, 0, 255])));
1741 assert_eq!(sample_nearest(&img, 0.5, 0.0), Some(Rgba([0, 0, 255, 0])));
1742 assert_eq!(sample_nearest(&img, 0.0, 0.5), Some(Rgba([0, 255, 0, 0])));
1743 assert_eq!(sample_nearest(&img, 0.5, 1.0), Some(Rgba([0, 0, 0, 255])));
1744 assert_eq!(sample_nearest(&img, 1.0, 0.5), Some(Rgba([0, 0, 0, 255])));
1745 }
1746
1747 #[bench]
1748 #[cfg(all(feature = "benchmarks", feature = "tiff"))]
1749 fn bench_resize_same_size(b: &mut test::Bencher) {
1750 let path = concat!(
1751 env!("CARGO_MANIFEST_DIR"),
1752 "/tests/images/tiff/testsuite/mandrill.tiff"
1753 );
1754 let image = crate::open(path).unwrap();
1755 b.iter(|| {
1756 test::black_box(image.resize(image.width(), image.height(), FilterType::CatmullRom));
1757 });
1758 b.bytes = u64::from(image.width() * image.height() * 3);
1759 }
1760
1761 #[test]
1762 fn test_issue_186() {
1763 let img: RgbImage = ImageBuffer::new(100, 100);
1764 let _ = resize(&img, 50, 50, FilterType::Lanczos3);
1765 }
1766
1767 #[bench]
1768 #[cfg(all(feature = "benchmarks", feature = "tiff"))]
1769 fn bench_thumbnail(b: &mut test::Bencher) {
1770 let path = concat!(
1771 env!("CARGO_MANIFEST_DIR"),
1772 "/tests/images/tiff/testsuite/mandrill.tiff"
1773 );
1774 let image = crate::open(path).unwrap();
1775 b.iter(|| {
1776 test::black_box(image.thumbnail(256, 256));
1777 });
1778 b.bytes = 512 * 512 * 4 + 256 * 256 * 4;
1779 }
1780
1781 #[bench]
1782 #[cfg(all(feature = "benchmarks", feature = "tiff"))]
1783 fn bench_thumbnail_upsize(b: &mut test::Bencher) {
1784 let path = concat!(
1785 env!("CARGO_MANIFEST_DIR"),
1786 "/tests/images/tiff/testsuite/mandrill.tiff"
1787 );
1788 let image = crate::open(path).unwrap().thumbnail(256, 256);
1789 b.iter(|| {
1790 test::black_box(image.thumbnail(512, 512));
1791 });
1792 b.bytes = 512 * 512 * 4 + 256 * 256 * 4;
1793 }
1794
1795 #[bench]
1796 #[cfg(all(feature = "benchmarks", feature = "tiff"))]
1797 fn bench_thumbnail_upsize_irregular(b: &mut test::Bencher) {
1798 let path = concat!(
1799 env!("CARGO_MANIFEST_DIR"),
1800 "/tests/images/tiff/testsuite/mandrill.tiff"
1801 );
1802 let image = crate::open(path).unwrap().thumbnail(193, 193);
1803 b.iter(|| {
1804 test::black_box(image.thumbnail(256, 256));
1805 });
1806 b.bytes = 193 * 193 * 4 + 256 * 256 * 4;
1807 }
1808
1809 #[test]
1810 #[cfg(feature = "png")]
1811 fn resize_transparent_image() {
1812 use super::FilterType::{CatmullRom, Gaussian, Lanczos3, Nearest, Triangle};
1813 use crate::imageops::crop_imm;
1814 use crate::RgbaImage;
1815
1816 fn assert_resize(image: &RgbaImage, filter: FilterType) {
1817 let resized = resize(image, 16, 16, filter);
1818 let cropped = crop_imm(&resized, 5, 5, 6, 6).to_image();
1819 for pixel in cropped.pixels() {
1820 let alpha = pixel.0[3];
1821 assert!(
1822 alpha != 254 && alpha != 253,
1823 "alpha value: {alpha}, {filter:?}"
1824 );
1825 }
1826 }
1827
1828 let path = concat!(
1829 env!("CARGO_MANIFEST_DIR"),
1830 "/tests/images/png/transparency/tp1n3p08.png"
1831 );
1832 let img = crate::open(path).unwrap();
1833 let rgba8 = img.as_rgba8().unwrap();
1834 let filters = &[Nearest, Triangle, CatmullRom, Gaussian, Lanczos3];
1835 for filter in filters {
1836 assert_resize(rgba8, *filter);
1837 }
1838 }
1839
1840 #[test]
1841 fn bug_1600() {
1842 let image = crate::RgbaImage::from_raw(629, 627, vec![255; 629 * 627 * 4]).unwrap();
1843 let result = resize(&image, 22, 22, FilterType::Lanczos3);
1844 assert!(result.into_raw().into_iter().any(|c| c != 0));
1845 }
1846
1847 #[test]
1848 fn issue_2340() {
1849 let empty = crate::GrayImage::from_raw(1 << 31, 0, vec![]).unwrap();
1850 let result = resize(&empty, 1, 1, FilterType::Lanczos3);
1852 assert!(result.into_raw().into_iter().all(|c| c == 0));
1853 let result = resize(&empty, 256, 256, FilterType::Lanczos3);
1856 assert!(result.into_raw().into_iter().all(|c| c == 0));
1857 }
1858
1859 #[test]
1860 fn issue_2340_refl() {
1861 let empty = crate::GrayImage::from_raw(0, 1 << 31, vec![]).unwrap();
1863 let result = resize(&empty, 1, 1, FilterType::Lanczos3);
1864 assert!(result.into_raw().into_iter().all(|c| c == 0));
1865 let result = resize(&empty, 256, 256, FilterType::Lanczos3);
1866 assert!(result.into_raw().into_iter().all(|c| c == 0));
1867 }
1868}