1use std::f32;
7
8use num_traits::{NumCast, ToPrimitive, Zero};
9
10use crate::image::{GenericImage, GenericImageView};
11use crate::traits::{Enlargeable, Pixel, Primitive};
12use crate::utils::clamp;
13use crate::{ImageBuffer, Rgba32FImage};
14
15#[derive(Clone, Copy, Debug, PartialEq, Hash)]
82pub enum FilterType {
83 Nearest,
85
86 Triangle,
88
89 CatmullRom,
91
92 Gaussian,
94
95 Lanczos3,
97}
98
99pub(crate) struct Filter<'a> {
101 pub(crate) kernel: Box<dyn Fn(f32) -> f32 + 'a>,
103
104 pub(crate) support: f32,
106}
107
108struct FloatNearest(f32);
109
110impl ToPrimitive for FloatNearest {
113 fn to_i8(&self) -> Option<i8> {
116 self.0.round().to_i8()
117 }
118 fn to_i16(&self) -> Option<i16> {
119 self.0.round().to_i16()
120 }
121 fn to_i64(&self) -> Option<i64> {
122 self.0.round().to_i64()
123 }
124 fn to_u8(&self) -> Option<u8> {
125 self.0.round().to_u8()
126 }
127 fn to_u16(&self) -> Option<u16> {
128 self.0.round().to_u16()
129 }
130 fn to_u64(&self) -> Option<u64> {
131 self.0.round().to_u64()
132 }
133 fn to_f64(&self) -> Option<f64> {
134 self.0.to_f64()
135 }
136}
137
138fn sinc(t: f32) -> f32 {
140 let a = t * f32::consts::PI;
141
142 if t == 0.0 {
143 1.0
144 } else {
145 a.sin() / a
146 }
147}
148
149fn lanczos(x: f32, t: f32) -> f32 {
151 if x.abs() < t {
152 sinc(x) * sinc(x / t)
153 } else {
154 0.0
155 }
156}
157
158fn bc_cubic_spline(x: f32, b: f32, c: f32) -> f32 {
161 let a = x.abs();
162
163 let k = if a < 1.0 {
164 (12.0 - 9.0 * b - 6.0 * c) * a.powi(3)
165 + (-18.0 + 12.0 * b + 6.0 * c) * a.powi(2)
166 + (6.0 - 2.0 * b)
167 } else if a < 2.0 {
168 (-b - 6.0 * c) * a.powi(3)
169 + (6.0 * b + 30.0 * c) * a.powi(2)
170 + (-12.0 * b - 48.0 * c) * a
171 + (8.0 * b + 24.0 * c)
172 } else {
173 0.0
174 };
175
176 k / 6.0
177}
178
179pub(crate) fn gaussian(x: f32, r: f32) -> f32 {
182 ((2.0 * f32::consts::PI).sqrt() * r).recip() * (-x.powi(2) / (2.0 * r.powi(2))).exp()
183}
184
185pub(crate) fn lanczos3_kernel(x: f32) -> f32 {
187 lanczos(x, 3.0)
188}
189
190pub(crate) fn gaussian_kernel(x: f32) -> f32 {
193 gaussian(x, 0.5)
194}
195
196pub(crate) fn catmullrom_kernel(x: f32) -> f32 {
199 bc_cubic_spline(x, 0.0, 0.5)
200}
201
202pub(crate) fn triangle_kernel(x: f32) -> f32 {
205 if x.abs() < 1.0 {
206 1.0 - x.abs()
207 } else {
208 0.0
209 }
210}
211
212pub(crate) fn box_kernel(_x: f32) -> f32 {
216 1.0
217}
218
219fn horizontal_sample<P, S>(
227 image: &Rgba32FImage,
228 new_width: u32,
229 filter: &mut Filter,
230) -> ImageBuffer<P, Vec<S>>
231where
232 P: Pixel<Subpixel = S> + 'static,
233 S: Primitive + 'static,
234{
235 let (width, height) = image.dimensions();
236 assert!(
238 width != 0 || height == 0,
240 "Unexpected prior allocation size. This case should have been handled by the caller"
241 );
242
243 let mut out = ImageBuffer::new(new_width, height);
244 let mut ws = Vec::new();
245
246 let max: f32 = NumCast::from(S::DEFAULT_MAX_VALUE).unwrap();
247 let min: f32 = NumCast::from(S::DEFAULT_MIN_VALUE).unwrap();
248 let ratio = width as f32 / new_width as f32;
249 let sratio = if ratio < 1.0 { 1.0 } else { ratio };
250 let src_support = filter.support * sratio;
251
252 for outx in 0..new_width {
253 let inputx = (outx as f32 + 0.5) * ratio;
256
257 let left = (inputx - src_support).floor() as i64;
264 let left = clamp(left, 0, <i64 as From<_>>::from(width) - 1) as u32;
265
266 let right = (inputx + src_support).ceil() as i64;
267 let right = clamp(
268 right,
269 <i64 as From<_>>::from(left) + 1,
270 <i64 as From<_>>::from(width),
271 ) as u32;
272
273 let inputx = inputx - 0.5;
276
277 ws.clear();
278 let mut sum = 0.0;
279 for i in left..right {
280 let w = (filter.kernel)((i as f32 - inputx) / sratio);
281 ws.push(w);
282 sum += w;
283 }
284 ws.iter_mut().for_each(|w| *w /= sum);
285
286 for y in 0..height {
287 let mut t = (0.0, 0.0, 0.0, 0.0);
288
289 for (i, w) in ws.iter().enumerate() {
290 let p = image.get_pixel(left + i as u32, y);
291
292 #[allow(deprecated)]
293 let vec = p.channels4();
294
295 t.0 += vec.0 * w;
296 t.1 += vec.1 * w;
297 t.2 += vec.2 * w;
298 t.3 += vec.3 * w;
299 }
300
301 #[allow(deprecated)]
302 let t = Pixel::from_channels(
303 NumCast::from(FloatNearest(clamp(t.0, min, max))).unwrap(),
304 NumCast::from(FloatNearest(clamp(t.1, min, max))).unwrap(),
305 NumCast::from(FloatNearest(clamp(t.2, min, max))).unwrap(),
306 NumCast::from(FloatNearest(clamp(t.3, min, max))).unwrap(),
307 );
308
309 out.put_pixel(outx, y, t);
310 }
311 }
312
313 out
314}
315
316pub fn sample_bilinear<P: Pixel>(
318 img: &impl GenericImageView<Pixel = P>,
319 u: f32,
320 v: f32,
321) -> Option<P> {
322 if ![u, v].iter().all(|c| (0.0..=1.0).contains(c)) {
323 return None;
324 }
325
326 let (w, h) = img.dimensions();
327 if w == 0 || h == 0 {
328 return None;
329 }
330
331 let ui = w as f32 * u - 0.5;
332 let vi = h as f32 * v - 0.5;
333 interpolate_bilinear(
334 img,
335 ui.max(0.).min((w - 1) as f32),
336 vi.max(0.).min((h - 1) as f32),
337 )
338}
339
340pub fn sample_nearest<P: Pixel>(
342 img: &impl GenericImageView<Pixel = P>,
343 u: f32,
344 v: f32,
345) -> Option<P> {
346 if ![u, v].iter().all(|c| (0.0..=1.0).contains(c)) {
347 return None;
348 }
349
350 let (w, h) = img.dimensions();
351 let ui = w as f32 * u - 0.5;
352 let ui = ui.max(0.).min((w.saturating_sub(1)) as f32);
353
354 let vi = h as f32 * v - 0.5;
355 let vi = vi.max(0.).min((h.saturating_sub(1)) as f32);
356 interpolate_nearest(img, ui, vi)
357}
358
359pub fn interpolate_nearest<P: Pixel>(
366 img: &impl GenericImageView<Pixel = P>,
367 x: f32,
368 y: f32,
369) -> Option<P> {
370 let (w, h) = img.dimensions();
371 if w == 0 || h == 0 {
372 return None;
373 }
374 if !(0.0..=((w - 1) as f32)).contains(&x) {
375 return None;
376 }
377 if !(0.0..=((h - 1) as f32)).contains(&y) {
378 return None;
379 }
380
381 Some(img.get_pixel(x.round() as u32, y.round() as u32))
382}
383
384pub fn interpolate_bilinear<P: Pixel>(
386 img: &impl GenericImageView<Pixel = P>,
387 x: f32,
388 y: f32,
389) -> Option<P> {
390 assert!(P::CHANNEL_COUNT <= 4);
392
393 let (w, h) = img.dimensions();
394 if w == 0 || h == 0 {
395 return None;
396 }
397 if !(0.0..=((w - 1) as f32)).contains(&x) {
398 return None;
399 }
400 if !(0.0..=((h - 1) as f32)).contains(&y) {
401 return None;
402 }
403
404 let uf = x.floor() as u32;
406 let vf = y.floor() as u32;
407 let uc = (uf + 1).min(w - 1);
408 let vc = (vf + 1).min(h - 1);
409
410 let mut sxx = [[0.; 4]; 4];
412
413 let mut compute = |u: u32, v: u32, i| {
421 let s = img.get_pixel(u, v);
422 for (j, c) in s.channels().iter().enumerate() {
423 sxx[j][i] = c.to_f32().unwrap();
424 }
425 s
426 };
427
428 let mut out: P = compute(uf, vf, 0);
430 compute(uf, vc, 1);
431 compute(uc, vf, 2);
432 compute(uc, vc, 3);
433
434 let ufw = x - uf as f32;
436 let vfw = y - vf as f32;
437 let ucw = (uf + 1) as f32 - x;
438 let vcw = (vf + 1) as f32 - y;
439
440 let wff = ucw * vcw;
443 let wfc = ucw * vfw;
444 let wcf = ufw * vcw;
445 let wcc = ufw * vfw;
446 debug_assert!(f32::abs((wff + wfc + wcf + wcc) - 1.) < 1e-3);
448
449 let is_float = P::Subpixel::DEFAULT_MAX_VALUE.to_f32().unwrap() == 1.0;
451
452 for (i, c) in out.channels_mut().iter_mut().enumerate() {
453 let v = wff * sxx[i][0] + wfc * sxx[i][1] + wcf * sxx[i][2] + wcc * sxx[i][3];
454 *c = <P::Subpixel as NumCast>::from(if is_float { v } else { v.round() }).unwrap_or({
458 if v < 0.0 {
459 P::Subpixel::DEFAULT_MIN_VALUE
460 } else {
461 P::Subpixel::DEFAULT_MAX_VALUE
462 }
463 });
464 }
465
466 Some(out)
467}
468
469fn vertical_sample<I, P, S>(image: &I, new_height: u32, filter: &mut Filter) -> Rgba32FImage
478where
479 I: GenericImageView<Pixel = P>,
480 P: Pixel<Subpixel = S> + 'static,
481 S: Primitive + 'static,
482{
483 let (width, height) = image.dimensions();
484
485 assert!(
488 height != 0 || width == 0,
490 "Unexpected prior allocation size. This case should have been handled by the caller"
491 );
492
493 let mut out = ImageBuffer::new(width, new_height);
494 let mut ws = Vec::new();
495
496 let ratio = height as f32 / new_height as f32;
497 let sratio = if ratio < 1.0 { 1.0 } else { ratio };
498 let src_support = filter.support * sratio;
499
500 for outy in 0..new_height {
501 let inputy = (outy as f32 + 0.5) * ratio;
504
505 let left = (inputy - src_support).floor() as i64;
506 let left = clamp(left, 0, <i64 as From<_>>::from(height) - 1) as u32;
507
508 let right = (inputy + src_support).ceil() as i64;
509 let right = clamp(
510 right,
511 <i64 as From<_>>::from(left) + 1,
512 <i64 as From<_>>::from(height),
513 ) as u32;
514
515 let inputy = inputy - 0.5;
516
517 ws.clear();
518 let mut sum = 0.0;
519 for i in left..right {
520 let w = (filter.kernel)((i as f32 - inputy) / sratio);
521 ws.push(w);
522 sum += w;
523 }
524 ws.iter_mut().for_each(|w| *w /= sum);
525
526 for x in 0..width {
527 let mut t = (0.0, 0.0, 0.0, 0.0);
528
529 for (i, w) in ws.iter().enumerate() {
530 let p = image.get_pixel(x, left + i as u32);
531
532 #[allow(deprecated)]
533 let (k1, k2, k3, k4) = p.channels4();
534 let vec: (f32, f32, f32, f32) = (
535 NumCast::from(k1).unwrap(),
536 NumCast::from(k2).unwrap(),
537 NumCast::from(k3).unwrap(),
538 NumCast::from(k4).unwrap(),
539 );
540
541 t.0 += vec.0 * w;
542 t.1 += vec.1 * w;
543 t.2 += vec.2 * w;
544 t.3 += vec.3 * w;
545 }
546
547 #[allow(deprecated)]
548 let t = Pixel::from_channels(t.0, t.1, t.2, t.3);
550
551 out.put_pixel(x, outy, t);
552 }
553 }
554
555 out
556}
557
558struct ThumbnailSum<S: Primitive + Enlargeable>(S::Larger, S::Larger, S::Larger, S::Larger);
560
561impl<S: Primitive + Enlargeable> ThumbnailSum<S> {
562 fn zeroed() -> Self {
563 ThumbnailSum(
564 S::Larger::zero(),
565 S::Larger::zero(),
566 S::Larger::zero(),
567 S::Larger::zero(),
568 )
569 }
570
571 fn sample_val(val: S) -> S::Larger {
572 <S::Larger as NumCast>::from(val).unwrap()
573 }
574
575 fn add_pixel<P: Pixel<Subpixel = S>>(&mut self, pixel: P) {
576 #[allow(deprecated)]
577 let pixel = pixel.channels4();
578 self.0 += Self::sample_val(pixel.0);
579 self.1 += Self::sample_val(pixel.1);
580 self.2 += Self::sample_val(pixel.2);
581 self.3 += Self::sample_val(pixel.3);
582 }
583}
584
585pub fn thumbnail<I, P, S>(image: &I, new_width: u32, new_height: u32) -> ImageBuffer<P, Vec<S>>
598where
599 I: GenericImageView<Pixel = P>,
600 P: Pixel<Subpixel = S> + 'static,
601 S: Primitive + Enlargeable + 'static,
602{
603 let (width, height) = image.dimensions();
604 let mut out = ImageBuffer::new(new_width, new_height);
605 if height == 0 || width == 0 {
606 return out;
607 }
608
609 let x_ratio = width as f32 / new_width as f32;
610 let y_ratio = height as f32 / new_height as f32;
611
612 for outy in 0..new_height {
613 let bottomf = outy as f32 * y_ratio;
614 let topf = bottomf + y_ratio;
615
616 let bottom = clamp(bottomf.ceil() as u32, 0, height - 1);
617 let top = clamp(topf.ceil() as u32, bottom, height);
618
619 for outx in 0..new_width {
620 let leftf = outx as f32 * x_ratio;
621 let rightf = leftf + x_ratio;
622
623 let left = clamp(leftf.ceil() as u32, 0, width - 1);
624 let right = clamp(rightf.ceil() as u32, left, width);
625
626 let avg = if bottom != top && left != right {
627 thumbnail_sample_block(image, left, right, bottom, top)
628 } else if bottom != top {
629 debug_assert!(
633 left > 0 && right > 0,
634 "First output column must have corresponding pixels"
635 );
636
637 let fraction_horizontal = (leftf.fract() + rightf.fract()) / 2.;
638 thumbnail_sample_fraction_horizontal(
639 image,
640 right - 1,
641 fraction_horizontal,
642 bottom,
643 top,
644 )
645 } else if left != right {
646 debug_assert!(
650 bottom > 0 && top > 0,
651 "First output row must have corresponding pixels"
652 );
653
654 let fraction_vertical = (topf.fract() + bottomf.fract()) / 2.;
655 thumbnail_sample_fraction_vertical(image, left, right, top - 1, fraction_vertical)
656 } else {
657 let fraction_horizontal = (topf.fract() + bottomf.fract()) / 2.;
659 let fraction_vertical = (leftf.fract() + rightf.fract()) / 2.;
660
661 thumbnail_sample_fraction_both(
662 image,
663 right - 1,
664 fraction_horizontal,
665 top - 1,
666 fraction_vertical,
667 )
668 };
669
670 #[allow(deprecated)]
671 let pixel = Pixel::from_channels(avg.0, avg.1, avg.2, avg.3);
672 out.put_pixel(outx, outy, pixel);
673 }
674 }
675
676 out
677}
678
679fn thumbnail_sample_block<I, P, S>(
681 image: &I,
682 left: u32,
683 right: u32,
684 bottom: u32,
685 top: u32,
686) -> (S, S, S, S)
687where
688 I: GenericImageView<Pixel = P>,
689 P: Pixel<Subpixel = S>,
690 S: Primitive + Enlargeable,
691{
692 let mut sum = ThumbnailSum::zeroed();
693
694 for y in bottom..top {
695 for x in left..right {
696 let k = image.get_pixel(x, y);
697 sum.add_pixel(k);
698 }
699 }
700
701 let n = <S::Larger as NumCast>::from((right - left) * (top - bottom)).unwrap();
702 let round = <S::Larger as NumCast>::from(n / NumCast::from(2).unwrap()).unwrap();
703 (
704 S::clamp_from((sum.0 + round) / n),
705 S::clamp_from((sum.1 + round) / n),
706 S::clamp_from((sum.2 + round) / n),
707 S::clamp_from((sum.3 + round) / n),
708 )
709}
710
711fn thumbnail_sample_fraction_horizontal<I, P, S>(
713 image: &I,
714 left: u32,
715 fraction_horizontal: f32,
716 bottom: u32,
717 top: u32,
718) -> (S, S, S, S)
719where
720 I: GenericImageView<Pixel = P>,
721 P: Pixel<Subpixel = S>,
722 S: Primitive + Enlargeable,
723{
724 let fract = fraction_horizontal;
725
726 let mut sum_left = ThumbnailSum::zeroed();
727 let mut sum_right = ThumbnailSum::zeroed();
728 for x in bottom..top {
729 let k_left = image.get_pixel(left, x);
730 sum_left.add_pixel(k_left);
731
732 let k_right = image.get_pixel(left + 1, x);
733 sum_right.add_pixel(k_right);
734 }
735
736 let fact_right = fract / ((top - bottom) as f32);
738 let fact_left = (1. - fract) / ((top - bottom) as f32);
739
740 let mix_left_and_right = |leftv: S::Larger, rightv: S::Larger| {
741 <S as NumCast>::from(
742 fact_left * leftv.to_f32().unwrap() + fact_right * rightv.to_f32().unwrap(),
743 )
744 .expect("Average sample value should fit into sample type")
745 };
746
747 (
748 mix_left_and_right(sum_left.0, sum_right.0),
749 mix_left_and_right(sum_left.1, sum_right.1),
750 mix_left_and_right(sum_left.2, sum_right.2),
751 mix_left_and_right(sum_left.3, sum_right.3),
752 )
753}
754
755fn thumbnail_sample_fraction_vertical<I, P, S>(
757 image: &I,
758 left: u32,
759 right: u32,
760 bottom: u32,
761 fraction_vertical: f32,
762) -> (S, S, S, S)
763where
764 I: GenericImageView<Pixel = P>,
765 P: Pixel<Subpixel = S>,
766 S: Primitive + Enlargeable,
767{
768 let fract = fraction_vertical;
769
770 let mut sum_bot = ThumbnailSum::zeroed();
771 let mut sum_top = ThumbnailSum::zeroed();
772 for x in left..right {
773 let k_bot = image.get_pixel(x, bottom);
774 sum_bot.add_pixel(k_bot);
775
776 let k_top = image.get_pixel(x, bottom + 1);
777 sum_top.add_pixel(k_top);
778 }
779
780 let fact_top = fract / ((right - left) as f32);
782 let fact_bot = (1. - fract) / ((right - left) as f32);
783
784 let mix_bot_and_top = |botv: S::Larger, topv: S::Larger| {
785 <S as NumCast>::from(fact_bot * botv.to_f32().unwrap() + fact_top * topv.to_f32().unwrap())
786 .expect("Average sample value should fit into sample type")
787 };
788
789 (
790 mix_bot_and_top(sum_bot.0, sum_top.0),
791 mix_bot_and_top(sum_bot.1, sum_top.1),
792 mix_bot_and_top(sum_bot.2, sum_top.2),
793 mix_bot_and_top(sum_bot.3, sum_top.3),
794 )
795}
796
797fn thumbnail_sample_fraction_both<I, P, S>(
799 image: &I,
800 left: u32,
801 fraction_vertical: f32,
802 bottom: u32,
803 fraction_horizontal: f32,
804) -> (S, S, S, S)
805where
806 I: GenericImageView<Pixel = P>,
807 P: Pixel<Subpixel = S>,
808 S: Primitive + Enlargeable,
809{
810 #[allow(deprecated)]
811 let k_bl = image.get_pixel(left, bottom).channels4();
812 #[allow(deprecated)]
813 let k_tl = image.get_pixel(left, bottom + 1).channels4();
814 #[allow(deprecated)]
815 let k_br = image.get_pixel(left + 1, bottom).channels4();
816 #[allow(deprecated)]
817 let k_tr = image.get_pixel(left + 1, bottom + 1).channels4();
818
819 let frac_v = fraction_vertical;
820 let frac_h = fraction_horizontal;
821
822 let fact_tr = frac_v * frac_h;
823 let fact_tl = frac_v * (1. - frac_h);
824 let fact_br = (1. - frac_v) * frac_h;
825 let fact_bl = (1. - frac_v) * (1. - frac_h);
826
827 let mix = |br: S, tr: S, bl: S, tl: S| {
828 <S as NumCast>::from(
829 fact_br * br.to_f32().unwrap()
830 + fact_tr * tr.to_f32().unwrap()
831 + fact_bl * bl.to_f32().unwrap()
832 + fact_tl * tl.to_f32().unwrap(),
833 )
834 .expect("Average sample value should fit into sample type")
835 };
836
837 (
838 mix(k_br.0, k_tr.0, k_bl.0, k_tl.0),
839 mix(k_br.1, k_tr.1, k_bl.1, k_tl.1),
840 mix(k_br.2, k_tr.2, k_bl.2, k_tl.2),
841 mix(k_br.3, k_tr.3, k_bl.3, k_tl.3),
842 )
843}
844
845pub fn filter3x3<I, P, S>(image: &I, kernel: &[f32]) -> ImageBuffer<P, Vec<S>>
848where
849 I: GenericImageView<Pixel = P>,
850 P: Pixel<Subpixel = S> + 'static,
851 S: Primitive + 'static,
852{
853 let taps: &[(isize, isize)] = &[
855 (-1, -1),
856 (0, -1),
857 (1, -1),
858 (-1, 0),
859 (0, 0),
860 (1, 0),
861 (-1, 1),
862 (0, 1),
863 (1, 1),
864 ];
865
866 let (width, height) = image.dimensions();
867
868 let mut out = ImageBuffer::new(width, height);
869
870 let max = S::DEFAULT_MAX_VALUE;
871 let max: f32 = NumCast::from(max).unwrap();
872
873 let sum = match kernel.iter().fold(0.0, |s, &item| s + item) {
874 x if x == 0.0 => 1.0,
875 sum => sum,
876 };
877 let sum = (sum, sum, sum, sum);
878
879 for y in 1..height - 1 {
880 for x in 1..width - 1 {
881 let mut t = (0.0, 0.0, 0.0, 0.0);
882
883 for (&k, &(a, b)) in kernel.iter().zip(taps.iter()) {
887 let k = (k, k, k, k);
888 let x0 = x as isize + a;
889 let y0 = y as isize + b;
890
891 let p = image.get_pixel(x0 as u32, y0 as u32);
892
893 #[allow(deprecated)]
894 let (k1, k2, k3, k4) = p.channels4();
895
896 let vec: (f32, f32, f32, f32) = (
897 NumCast::from(k1).unwrap(),
898 NumCast::from(k2).unwrap(),
899 NumCast::from(k3).unwrap(),
900 NumCast::from(k4).unwrap(),
901 );
902
903 t.0 += vec.0 * k.0;
904 t.1 += vec.1 * k.1;
905 t.2 += vec.2 * k.2;
906 t.3 += vec.3 * k.3;
907 }
908
909 let (t1, t2, t3, t4) = (t.0 / sum.0, t.1 / sum.1, t.2 / sum.2, t.3 / sum.3);
910
911 #[allow(deprecated)]
912 let t = Pixel::from_channels(
913 NumCast::from(clamp(t1, 0.0, max)).unwrap(),
914 NumCast::from(clamp(t2, 0.0, max)).unwrap(),
915 NumCast::from(clamp(t3, 0.0, max)).unwrap(),
916 NumCast::from(clamp(t4, 0.0, max)).unwrap(),
917 );
918
919 out.put_pixel(x, y, t);
920 }
921 }
922
923 out
924}
925
926pub fn resize<I: GenericImageView>(
930 image: &I,
931 nwidth: u32,
932 nheight: u32,
933 filter: FilterType,
934) -> ImageBuffer<I::Pixel, Vec<<I::Pixel as Pixel>::Subpixel>>
935where
936 I::Pixel: 'static,
937 <I::Pixel as Pixel>::Subpixel: 'static,
938{
939 let is_empty = {
941 let (width, height) = image.dimensions();
942 width == 0 || height == 0
943 };
944
945 if is_empty {
946 return ImageBuffer::new(nwidth, nheight);
947 }
948
949 if (nwidth, nheight) == image.dimensions() {
951 let mut tmp = ImageBuffer::new(image.width(), image.height());
952 tmp.copy_from(image, 0, 0).unwrap();
953 return tmp;
954 }
955
956 let mut method = match filter {
957 FilterType::Nearest => Filter {
958 kernel: Box::new(box_kernel),
959 support: 0.0,
960 },
961 FilterType::Triangle => Filter {
962 kernel: Box::new(triangle_kernel),
963 support: 1.0,
964 },
965 FilterType::CatmullRom => Filter {
966 kernel: Box::new(catmullrom_kernel),
967 support: 2.0,
968 },
969 FilterType::Gaussian => Filter {
970 kernel: Box::new(gaussian_kernel),
971 support: 3.0,
972 },
973 FilterType::Lanczos3 => Filter {
974 kernel: Box::new(lanczos3_kernel),
975 support: 3.0,
976 },
977 };
978
979 let tmp: Rgba32FImage = vertical_sample(image, nheight, &mut method);
981 horizontal_sample(&tmp, nwidth, &mut method)
982}
983
984pub fn blur<I: GenericImageView>(
989 image: &I,
990 sigma: f32,
991) -> ImageBuffer<I::Pixel, Vec<<I::Pixel as Pixel>::Subpixel>>
992where
993 I::Pixel: 'static,
994{
995 let sigma = if sigma <= 0.0 { 1.0 } else { sigma };
996
997 let mut method = Filter {
998 kernel: Box::new(|x| gaussian(x, sigma)),
999 support: 2.0 * sigma,
1000 };
1001
1002 let (width, height) = image.dimensions();
1003 let is_empty = width == 0 || height == 0;
1004
1005 if is_empty {
1006 return ImageBuffer::new(width, height);
1007 }
1008
1009 let tmp: Rgba32FImage = vertical_sample(image, height, &mut method);
1013 horizontal_sample(&tmp, width, &mut method)
1014}
1015
1016pub fn unsharpen<I, P, S>(image: &I, sigma: f32, threshold: i32) -> ImageBuffer<P, Vec<S>>
1022where
1023 I: GenericImageView<Pixel = P>,
1024 P: Pixel<Subpixel = S> + 'static,
1025 S: Primitive + 'static,
1026{
1027 let mut tmp = blur(image, sigma);
1028
1029 let max = S::DEFAULT_MAX_VALUE;
1030 let max: i32 = NumCast::from(max).unwrap();
1031 let (width, height) = image.dimensions();
1032
1033 for y in 0..height {
1034 for x in 0..width {
1035 let a = image.get_pixel(x, y);
1036 let b = tmp.get_pixel_mut(x, y);
1037
1038 let p = a.map2(b, |c, d| {
1039 let ic: i32 = NumCast::from(c).unwrap();
1040 let id: i32 = NumCast::from(d).unwrap();
1041
1042 let diff = ic - id;
1043
1044 if diff.abs() > threshold {
1045 let e = clamp(ic + diff, 0, max); NumCast::from(e).unwrap()
1048 } else {
1049 c
1050 }
1051 });
1052
1053 *b = p;
1054 }
1055 }
1056
1057 tmp
1058}
1059
1060#[cfg(test)]
1061mod tests {
1062 use super::{resize, sample_bilinear, sample_nearest, FilterType};
1063 use crate::{GenericImageView, ImageBuffer, RgbImage};
1064 #[cfg(feature = "benchmarks")]
1065 use test;
1066
1067 #[bench]
1068 #[cfg(all(feature = "benchmarks", feature = "png"))]
1069 fn bench_resize(b: &mut test::Bencher) {
1070 use std::path::Path;
1071 let img = crate::open(Path::new("./examples/fractal.png")).unwrap();
1072 b.iter(|| {
1073 test::black_box(resize(&img, 200, 200, FilterType::Nearest));
1074 });
1075 b.bytes = 800 * 800 * 3 + 200 * 200 * 3;
1076 }
1077
1078 #[test]
1079 #[cfg(feature = "png")]
1080 fn test_resize_same_size() {
1081 use std::path::Path;
1082 let img = crate::open(Path::new("./examples/fractal.png")).unwrap();
1083 let resize = img.resize(img.width(), img.height(), FilterType::Triangle);
1084 assert!(img.pixels().eq(resize.pixels()))
1085 }
1086
1087 #[test]
1088 #[cfg(feature = "png")]
1089 fn test_sample_bilinear() {
1090 use std::path::Path;
1091 let img = crate::open(Path::new("./examples/fractal.png")).unwrap();
1092 assert!(sample_bilinear(&img, 0., 0.).is_some());
1093 assert!(sample_bilinear(&img, 1., 0.).is_some());
1094 assert!(sample_bilinear(&img, 0., 1.).is_some());
1095 assert!(sample_bilinear(&img, 1., 1.).is_some());
1096 assert!(sample_bilinear(&img, 0.5, 0.5).is_some());
1097
1098 assert!(sample_bilinear(&img, 1.2, 0.5).is_none());
1099 assert!(sample_bilinear(&img, 0.5, 1.2).is_none());
1100 assert!(sample_bilinear(&img, 1.2, 1.2).is_none());
1101
1102 assert!(sample_bilinear(&img, -0.1, 0.2).is_none());
1103 assert!(sample_bilinear(&img, 0.2, -0.1).is_none());
1104 assert!(sample_bilinear(&img, -0.1, -0.1).is_none());
1105 }
1106 #[test]
1107 #[cfg(feature = "png")]
1108 fn test_sample_nearest() {
1109 use std::path::Path;
1110 let img = crate::open(Path::new("./examples/fractal.png")).unwrap();
1111 assert!(sample_nearest(&img, 0., 0.).is_some());
1112 assert!(sample_nearest(&img, 1., 0.).is_some());
1113 assert!(sample_nearest(&img, 0., 1.).is_some());
1114 assert!(sample_nearest(&img, 1., 1.).is_some());
1115 assert!(sample_nearest(&img, 0.5, 0.5).is_some());
1116
1117 assert!(sample_nearest(&img, 1.2, 0.5).is_none());
1118 assert!(sample_nearest(&img, 0.5, 1.2).is_none());
1119 assert!(sample_nearest(&img, 1.2, 1.2).is_none());
1120
1121 assert!(sample_nearest(&img, -0.1, 0.2).is_none());
1122 assert!(sample_nearest(&img, 0.2, -0.1).is_none());
1123 assert!(sample_nearest(&img, -0.1, -0.1).is_none());
1124 }
1125 #[test]
1126 fn test_sample_bilinear_correctness() {
1127 use crate::Rgba;
1128 let img = ImageBuffer::from_fn(2, 2, |x, y| match (x, y) {
1129 (0, 0) => Rgba([255, 0, 0, 0]),
1130 (0, 1) => Rgba([0, 255, 0, 0]),
1131 (1, 0) => Rgba([0, 0, 255, 0]),
1132 (1, 1) => Rgba([0, 0, 0, 255]),
1133 _ => panic!(),
1134 });
1135 assert_eq!(sample_bilinear(&img, 0.5, 0.5), Some(Rgba([64; 4])));
1136 assert_eq!(sample_bilinear(&img, 0.0, 0.0), Some(Rgba([255, 0, 0, 0])));
1137 assert_eq!(sample_bilinear(&img, 0.0, 1.0), Some(Rgba([0, 255, 0, 0])));
1138 assert_eq!(sample_bilinear(&img, 1.0, 0.0), Some(Rgba([0, 0, 255, 0])));
1139 assert_eq!(sample_bilinear(&img, 1.0, 1.0), Some(Rgba([0, 0, 0, 255])));
1140
1141 assert_eq!(
1142 sample_bilinear(&img, 0.5, 0.0),
1143 Some(Rgba([128, 0, 128, 0]))
1144 );
1145 assert_eq!(
1146 sample_bilinear(&img, 0.0, 0.5),
1147 Some(Rgba([128, 128, 0, 0]))
1148 );
1149 assert_eq!(
1150 sample_bilinear(&img, 0.5, 1.0),
1151 Some(Rgba([0, 128, 0, 128]))
1152 );
1153 assert_eq!(
1154 sample_bilinear(&img, 1.0, 0.5),
1155 Some(Rgba([0, 0, 128, 128]))
1156 );
1157 }
1158 #[bench]
1159 #[cfg(feature = "benchmarks")]
1160 fn bench_sample_bilinear(b: &mut test::Bencher) {
1161 use crate::Rgba;
1162 let img = ImageBuffer::from_fn(2, 2, |x, y| match (x, y) {
1163 (0, 0) => Rgba([255, 0, 0, 0]),
1164 (0, 1) => Rgba([0, 255, 0, 0]),
1165 (1, 0) => Rgba([0, 0, 255, 0]),
1166 (1, 1) => Rgba([0, 0, 0, 255]),
1167 _ => panic!(),
1168 });
1169 b.iter(|| {
1170 sample_bilinear(&img, test::black_box(0.5), test::black_box(0.5));
1171 });
1172 }
1173 #[test]
1174 fn test_sample_nearest_correctness() {
1175 use crate::Rgba;
1176 let img = ImageBuffer::from_fn(2, 2, |x, y| match (x, y) {
1177 (0, 0) => Rgba([255, 0, 0, 0]),
1178 (0, 1) => Rgba([0, 255, 0, 0]),
1179 (1, 0) => Rgba([0, 0, 255, 0]),
1180 (1, 1) => Rgba([0, 0, 0, 255]),
1181 _ => panic!(),
1182 });
1183
1184 assert_eq!(sample_nearest(&img, 0.0, 0.0), Some(Rgba([255, 0, 0, 0])));
1185 assert_eq!(sample_nearest(&img, 0.0, 1.0), Some(Rgba([0, 255, 0, 0])));
1186 assert_eq!(sample_nearest(&img, 1.0, 0.0), Some(Rgba([0, 0, 255, 0])));
1187 assert_eq!(sample_nearest(&img, 1.0, 1.0), Some(Rgba([0, 0, 0, 255])));
1188
1189 assert_eq!(sample_nearest(&img, 0.5, 0.5), Some(Rgba([0, 0, 0, 255])));
1190 assert_eq!(sample_nearest(&img, 0.5, 0.0), Some(Rgba([0, 0, 255, 0])));
1191 assert_eq!(sample_nearest(&img, 0.0, 0.5), Some(Rgba([0, 255, 0, 0])));
1192 assert_eq!(sample_nearest(&img, 0.5, 1.0), Some(Rgba([0, 0, 0, 255])));
1193 assert_eq!(sample_nearest(&img, 1.0, 0.5), Some(Rgba([0, 0, 0, 255])));
1194 }
1195
1196 #[bench]
1197 #[cfg(all(feature = "benchmarks", feature = "tiff"))]
1198 fn bench_resize_same_size(b: &mut test::Bencher) {
1199 let path = concat!(
1200 env!("CARGO_MANIFEST_DIR"),
1201 "/tests/images/tiff/testsuite/mandrill.tiff"
1202 );
1203 let image = crate::open(path).unwrap();
1204 b.iter(|| {
1205 test::black_box(image.resize(image.width(), image.height(), FilterType::CatmullRom));
1206 });
1207 b.bytes = (image.width() * image.height() * 3) as u64;
1208 }
1209
1210 #[test]
1211 fn test_issue_186() {
1212 let img: RgbImage = ImageBuffer::new(100, 100);
1213 let _ = resize(&img, 50, 50, FilterType::Lanczos3);
1214 }
1215
1216 #[bench]
1217 #[cfg(all(feature = "benchmarks", feature = "tiff"))]
1218 fn bench_thumbnail(b: &mut test::Bencher) {
1219 let path = concat!(
1220 env!("CARGO_MANIFEST_DIR"),
1221 "/tests/images/tiff/testsuite/mandrill.tiff"
1222 );
1223 let image = crate::open(path).unwrap();
1224 b.iter(|| {
1225 test::black_box(image.thumbnail(256, 256));
1226 });
1227 b.bytes = 512 * 512 * 4 + 256 * 256 * 4;
1228 }
1229
1230 #[bench]
1231 #[cfg(all(feature = "benchmarks", feature = "tiff"))]
1232 fn bench_thumbnail_upsize(b: &mut test::Bencher) {
1233 let path = concat!(
1234 env!("CARGO_MANIFEST_DIR"),
1235 "/tests/images/tiff/testsuite/mandrill.tiff"
1236 );
1237 let image = crate::open(path).unwrap().thumbnail(256, 256);
1238 b.iter(|| {
1239 test::black_box(image.thumbnail(512, 512));
1240 });
1241 b.bytes = 512 * 512 * 4 + 256 * 256 * 4;
1242 }
1243
1244 #[bench]
1245 #[cfg(all(feature = "benchmarks", feature = "tiff"))]
1246 fn bench_thumbnail_upsize_irregular(b: &mut test::Bencher) {
1247 let path = concat!(
1248 env!("CARGO_MANIFEST_DIR"),
1249 "/tests/images/tiff/testsuite/mandrill.tiff"
1250 );
1251 let image = crate::open(path).unwrap().thumbnail(193, 193);
1252 b.iter(|| {
1253 test::black_box(image.thumbnail(256, 256));
1254 });
1255 b.bytes = 193 * 193 * 4 + 256 * 256 * 4;
1256 }
1257
1258 #[test]
1259 #[cfg(feature = "png")]
1260 fn resize_transparent_image() {
1261 use super::FilterType::{CatmullRom, Gaussian, Lanczos3, Nearest, Triangle};
1262 use crate::imageops::crop_imm;
1263 use crate::RgbaImage;
1264
1265 fn assert_resize(image: &RgbaImage, filter: FilterType) {
1266 let resized = resize(image, 16, 16, filter);
1267 let cropped = crop_imm(&resized, 5, 5, 6, 6).to_image();
1268 for pixel in cropped.pixels() {
1269 let alpha = pixel.0[3];
1270 assert!(
1271 alpha != 254 && alpha != 253,
1272 "alpha value: {}, {:?}",
1273 alpha,
1274 filter
1275 );
1276 }
1277 }
1278
1279 let path = concat!(
1280 env!("CARGO_MANIFEST_DIR"),
1281 "/tests/images/png/transparency/tp1n3p08.png"
1282 );
1283 let img = crate::open(path).unwrap();
1284 let rgba8 = img.as_rgba8().unwrap();
1285 let filters = &[Nearest, Triangle, CatmullRom, Gaussian, Lanczos3];
1286 for filter in filters {
1287 assert_resize(rgba8, *filter);
1288 }
1289 }
1290
1291 #[test]
1292 fn bug_1600() {
1293 let image = crate::RgbaImage::from_raw(629, 627, vec![255; 629 * 627 * 4]).unwrap();
1294 let result = resize(&image, 22, 22, FilterType::Lanczos3);
1295 assert!(result.into_raw().into_iter().any(|c| c != 0));
1296 }
1297
1298 #[test]
1299 fn issue_2340() {
1300 let empty = crate::GrayImage::from_raw(1 << 31, 0, vec![]).unwrap();
1301 let result = resize(&empty, 1, 1, FilterType::Lanczos3);
1303 assert!(result.into_raw().into_iter().all(|c| c == 0));
1304 let result = resize(&empty, 256, 256, FilterType::Lanczos3);
1307 assert!(result.into_raw().into_iter().all(|c| c == 0));
1308 }
1309
1310 #[test]
1311 fn issue_2340_refl() {
1312 let empty = crate::GrayImage::from_raw(0, 1 << 31, vec![]).unwrap();
1314 let result = resize(&empty, 1, 1, FilterType::Lanczos3);
1315 assert!(result.into_raw().into_iter().all(|c| c == 0));
1316 let result = resize(&empty, 256, 256, FilterType::Lanczos3);
1317 assert!(result.into_raw().into_iter().all(|c| c == 0));
1318 }
1319}