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)]
60#[allow(deprecated)]
61pub struct SamplingOptions {
62 pub max_aniso: i32,
63 pub use_cubic: bool,
64 pub cubic: CubicResampler,
65 pub filter: FilterMode,
66 pub mipmap: MipmapMode,
67}
68
69native_transmutable!(SkSamplingOptions, SamplingOptions, sampling_options_layout);
70
71impl Default for SamplingOptions {
72 fn default() -> Self {
73 Self {
74 max_aniso: 0,
75 use_cubic: false,
76 // ignored
77 cubic: CubicResampler { b: 0.0, c: 0.0 },
78 filter: FilterMode::Nearest,
79 mipmap: MipmapMode::None,
80 }
81 }
82}
83
84impl SamplingOptions {
85 pub fn new(filter_mode: FilterMode, mm: MipmapMode) -> Self {
86 Self {
87 filter: filter_mode,
88 mipmap: mm,
89 ..Default::default()
90 }
91 }
92}
93
94impl From<FilterMode> for SamplingOptions {
95 fn from(fm: FilterMode) -> Self {
96 Self {
97 filter: fm,
98 ..Default::default()
99 }
100 }
101}
102
103#[allow(deprecated)]
104impl From<FilterOptions> for SamplingOptions {
105 fn from(filter: FilterOptions) -> Self {
106 Self {
107 filter: filter.sampling,
108 mipmap: filter.mipmap,
109 ..Default::default()
110 }
111 }
112}
113
114impl From<CubicResampler> for SamplingOptions {
115 #[allow(deprecated)]
116 fn from(cubic: CubicResampler) -> Self {
117 Self {
118 use_cubic: true,
119 cubic,
120 ..Default::default()
121 }
122 }
123}
124
125impl SamplingOptions {
126 pub fn from_aniso(max_aniso: i32) -> Self {
127 Self {
128 max_aniso: max_aniso.max(1),
129 ..Default::default()
130 }
131 }
132
133 pub fn is_aniso(&self) -> bool {
134 self.max_aniso != 0
135 }
136}
137