Skip to main content

skia_safe/core/
sampling_options.rs

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