skia_safe/core/
sampling_options.rs

1use skia_bindings::{SkCubicResampler, SkSamplingOptions};
2
3pub use skia_bindings::SkFilterMode as FilterMode;
4variant_name!(FilterMode::Linear);
5
6#[deprecated(since = "0.38.0", note = "Use FilterMode")]
7pub type SamplingMode = FilterMode;
8
9pub use skia_bindings::SkMipmapMode as MipmapMode;
10variant_name!(MipmapMode::Nearest);
11
12/// Specify `b` and `c` (each between 0...1) to create a shader that applies the corresponding
13/// cubic reconstruction filter to the image.
14///
15/// Example values:
16///     b = 1/3, c = 1/3        "Mitchell" filter
17///     b = 0,   c = 1/2        "Catmull-Rom" filter
18///
19/// See "Reconstruction Filters in Computer Graphics"
20///         Don P. Mitchell
21///         Arun N. Netravali
22///         1988
23/// <https://www.cs.utexas.edu/~fussell/courses/cs384g-fall2013/lectures/mitchell/Mitchell.pdf>
24/// Desmos worksheet <https://www.desmos.com/calculator/aghdpicrvr>
25/// Nice overview <https://entropymine.com/imageworsener/bicubic/>
26#[repr(C)]
27#[derive(Copy, Clone, PartialEq, Debug)]
28pub struct CubicResampler {
29    pub b: f32,
30    pub c: f32,
31}
32
33impl CubicResampler {
34    pub fn mitchell() -> Self {
35        Self {
36            b: 1.0 / 3.0,
37            c: 1.0 / 3.0,
38        }
39    }
40
41    pub fn catmull_rom() -> Self {
42        Self {
43            b: 0.0,
44            c: 1.0 / 2.0,
45        }
46    }
47}
48
49native_transmutable!(SkCubicResampler, CubicResampler, cubic_resampler);
50
51#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
52#[deprecated(since = "0.38.0", note = "Use SamplingOptions")]
53pub struct FilterOptions {
54    pub sampling: FilterMode,
55    pub mipmap: MipmapMode,
56}
57
58#[repr(C)]
59#[derive(Copy, Clone, PartialEq, Debug)]
60pub struct SamplingOptions {
61    pub max_aniso: i32,
62    pub use_cubic: bool,
63    pub cubic: CubicResampler,
64    pub filter: FilterMode,
65    pub mipmap: MipmapMode,
66}
67
68native_transmutable!(SkSamplingOptions, SamplingOptions, sampling_options_layout);
69
70impl Default for SamplingOptions {
71    fn default() -> Self {
72        Self {
73            max_aniso: 0,
74            use_cubic: false,
75            // ignored
76            cubic: CubicResampler { b: 0.0, c: 0.0 },
77            filter: FilterMode::Nearest,
78            mipmap: MipmapMode::None,
79        }
80    }
81}
82
83impl SamplingOptions {
84    pub fn new(filter_mode: FilterMode, mm: MipmapMode) -> Self {
85        Self {
86            filter: filter_mode,
87            mipmap: mm,
88            ..Default::default()
89        }
90    }
91}
92
93impl From<FilterMode> for SamplingOptions {
94    fn from(fm: FilterMode) -> Self {
95        Self {
96            filter: fm,
97            ..Default::default()
98        }
99    }
100}
101
102#[allow(deprecated)]
103impl From<FilterOptions> for SamplingOptions {
104    fn from(filter: FilterOptions) -> Self {
105        Self {
106            filter: filter.sampling,
107            mipmap: filter.mipmap,
108            ..Default::default()
109        }
110    }
111}
112
113impl From<CubicResampler> for SamplingOptions {
114    #[allow(deprecated)]
115    fn from(cubic: CubicResampler) -> Self {
116        Self {
117            use_cubic: true,
118            cubic,
119            ..Default::default()
120        }
121    }
122}
123
124impl SamplingOptions {
125    pub fn from_aniso(max_aniso: i32) -> Self {
126        Self {
127            max_aniso: max_aniso.max(1),
128            ..Default::default()
129        }
130    }
131
132    pub fn is_aniso(&self) -> bool {
133        self.max_aniso != 0
134    }
135}