fast image operations
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//! holds scaling operations.
//!
//! choose from the wide expanse of options (ordered fastest to slowest):
//!
//! - [`Nearest`]: quickest, dumbest, jaggedest, scaling algorithm
//! - [`Box`]: you want slightly less pixels than nearest? here you go! kinda blurry though.
//! - [`Bilinear`]: _smooth_ scaling algorithm. rather fuzzy.
//! - [`Hamming`]: solves the [`Box`] problems. clearer image.
//! - [`CatmullRom`]: about the same as [`Hamming`], just a little slower.
//! - [`Mitchell`]: honestly, cant see the difference from [`CatmullRom`].
//! - [`Lanczos3`]: prettiest scaling algorithm. highly recommend.
//!
//! usage:
//! ```
//! # use fimg::{Image, scale::Lanczos3};
//! let i = Image::<_, 3>::open("tdata/small_cat.png");
//! let scaled = i.scale::<Lanczos3>(2144, 1424);
//! ```
use crate::Image;

mod algorithms;
pub mod traits;
pub use algorithms::*;

macro_rules! transparent {
    ($n: literal, $name: ident) => {
        impl<T: AsMut<[u8]> + AsRef<[u8]>> Image<T, $n> {
            /// Scale a
            #[doc = stringify!($name)]
            /// image with a given scaling algorithm.
            pub fn scale<A: traits::ScalingAlgorithm>(
                &mut self,
                width: u32,
                height: u32,
            ) -> Image<std::boxed::Box<[u8]>, $n> {
                A::scale_transparent(
                    self.as_mut(),
                    width.try_into().unwrap(),
                    height.try_into().unwrap(),
                )
            }
        }
    };
}

macro_rules! opaque {
    ($n: literal, $name: ident) => {
        impl<T: AsRef<[u8]>> Image<T, $n> {
            /// Scale a
            #[doc = stringify!($name)]
            /// image with a given scaling algorithm.
            pub fn scale<A: traits::ScalingAlgorithm>(
                &self,
                width: u32,
                height: u32,
            ) -> Image<std::boxed::Box<[u8]>, $n> {
                A::scale_opaque(
                    self.as_ref(),
                    width.try_into().unwrap(),
                    height.try_into().unwrap(),
                )
            }
        }
    };
}

opaque!(1, Y);
transparent!(2, YA);
opaque!(3, RGB);
transparent!(4, RGBA);

#[test]
fn test_nearest() {
    let i = Image::<_, 3>::open("tdata/cat.png");
    assert_eq!(
        &*i.scale::<Nearest>(268, 178).buffer,
        &*Image::<_, 3>::open("tdata/small_cat.png").buffer
    );
}