forked from Cykooz/fast_image_resize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
structs.rs
89 lines (81 loc) · 2.96 KB
/
structs.rs
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
80
81
82
83
84
85
86
87
88
89
use crate::get_non_zero_u32;
use fast_image_resize as fr;
use std::num::{NonZeroU16, NonZeroU32, ParseIntError};
use std::str::FromStr;
#[derive(Copy, Clone, Debug)]
pub enum Size {
Pixels(NonZeroU32),
Percent(NonZeroU16),
}
impl Size {
pub fn calculate_size(&self, src_size: NonZeroU32) -> NonZeroU32 {
match *self {
Self::Pixels(size) => size,
Self::Percent(percent) => get_non_zero_u32(
(src_size.get() as f32 * percent.get() as f32 / 100.).round() as u32,
),
}
}
}
impl FromStr for Size {
type Err = ParseIntError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if let Some(percent_str) = s.strip_suffix('%') {
NonZeroU16::from_str(percent_str).map(Self::Percent)
} else {
NonZeroU32::from_str(s).map(Self::Pixels)
}
}
}
#[derive(Copy, Clone, Debug, clap::ValueEnum)]
pub enum Algorithm {
Nearest,
Convolution,
SuperSampling,
}
#[derive(Copy, Clone, Debug, clap::ValueEnum)]
pub enum FilterType {
/// Each pixel of source image contributes to one pixel of the
/// destination image with identical weights. For upscaling is equivalent
/// of `Nearest` resize algorithm.
Box,
/// Bilinear filter calculate the output pixel value using linear
/// interpolation on all pixels that may contribute to the output value.
Bilinear,
/// Hamming filter has the same performance as `Bilinear` filter while
/// providing the image downscaling quality comparable to bicubic
/// (`CatmulRom` or `Mitchell`). Produces a sharper image than `Bilinear`,
/// doesn't have dislocations on local level like with `Box`.
/// The filter don’t show good quality for the image upscaling.
Hamming,
/// Catmull-Rom bicubic filter calculate the output pixel value using
/// cubic interpolation on all pixels that may contribute to the output
/// value.
CatmullRom,
/// Mitchell–Netravali bicubic filter calculate the output pixel value
/// using cubic interpolation on all pixels that may contribute to the
/// output value.
Mitchell,
/// Lanczos3 filter calculate the output pixel value using a high-quality
/// Lanczos filter (a truncated sinc) on all pixels that may contribute
/// to the output value.
Lanczos3,
}
impl From<FilterType> for fr::FilterType {
fn from(filter_type: FilterType) -> Self {
match filter_type {
FilterType::Box => fr::FilterType::Box,
FilterType::Bilinear => fr::FilterType::Bilinear,
FilterType::Hamming => fr::FilterType::Hamming,
FilterType::CatmullRom => fr::FilterType::CatmullRom,
FilterType::Mitchell => fr::FilterType::Mitchell,
FilterType::Lanczos3 => fr::FilterType::Lanczos3,
}
}
}
#[derive(Copy, Clone, Debug, clap::ValueEnum)]
pub enum ColorSpace {
Linear,
/// sRGB for color images or gamma 2.2 for grayscale images
NonLinear,
}