skia_safe/core/
sampling_options.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use skia_bindings::{SkCubicResampler, SkSamplingOptions};

pub use skia_bindings::SkFilterMode as FilterMode;
variant_name!(FilterMode::Linear);

#[deprecated(since = "0.38.0", note = "Use FilterMode")]
pub type SamplingMode = FilterMode;

pub use skia_bindings::SkMipmapMode as MipmapMode;
variant_name!(MipmapMode::Nearest);

/// Specify `b` and `c` (each between 0...1) to create a shader that applies the corresponding
/// cubic reconstruction filter to the image.
///
/// Example values:
///     b = 1/3, c = 1/3        "Mitchell" filter
///     b = 0,   c = 1/2        "Catmull-Rom" filter
///
/// See "Reconstruction Filters in Computer Graphics"
///         Don P. Mitchell
///         Arun N. Netravali
///         1988
/// <https://www.cs.utexas.edu/~fussell/courses/cs384g-fall2013/lectures/mitchell/Mitchell.pdf>
/// Desmos worksheet <https://www.desmos.com/calculator/aghdpicrvr>
/// Nice overview <https://entropymine.com/imageworsener/bicubic/>
#[repr(C)]
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct CubicResampler {
    pub b: f32,
    pub c: f32,
}

impl CubicResampler {
    pub fn mitchell() -> Self {
        Self {
            b: 1.0 / 3.0,
            c: 1.0 / 3.0,
        }
    }

    pub fn catmull_rom() -> Self {
        Self {
            b: 0.0,
            c: 1.0 / 2.0,
        }
    }
}

native_transmutable!(SkCubicResampler, CubicResampler, cubic_resampler);

#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
#[deprecated(since = "0.38.0", note = "Use SamplingOptions")]
pub struct FilterOptions {
    pub sampling: FilterMode,
    pub mipmap: MipmapMode,
}

#[repr(C)]
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct SamplingOptions {
    pub max_aniso: i32,
    pub use_cubic: bool,
    pub cubic: CubicResampler,
    pub filter: FilterMode,
    pub mipmap: MipmapMode,
}

native_transmutable!(SkSamplingOptions, SamplingOptions, sampling_options_layout);

impl Default for SamplingOptions {
    fn default() -> Self {
        Self {
            max_aniso: 0,
            use_cubic: false,
            // ignored
            cubic: CubicResampler { b: 0.0, c: 0.0 },
            filter: FilterMode::Nearest,
            mipmap: MipmapMode::None,
        }
    }
}

impl SamplingOptions {
    pub fn new(filter_mode: FilterMode, mm: MipmapMode) -> Self {
        Self {
            filter: filter_mode,
            mipmap: mm,
            ..Default::default()
        }
    }
}

impl From<FilterMode> for SamplingOptions {
    fn from(fm: FilterMode) -> Self {
        Self {
            filter: fm,
            ..Default::default()
        }
    }
}

#[allow(deprecated)]
impl From<FilterOptions> for SamplingOptions {
    fn from(filter: FilterOptions) -> Self {
        Self {
            filter: filter.sampling,
            mipmap: filter.mipmap,
            ..Default::default()
        }
    }
}

impl From<CubicResampler> for SamplingOptions {
    #[allow(deprecated)]
    fn from(cubic: CubicResampler) -> Self {
        Self {
            use_cubic: true,
            cubic,
            ..Default::default()
        }
    }
}

impl SamplingOptions {
    pub fn from_aniso(max_aniso: i32) -> Self {
        Self {
            max_aniso: max_aniso.max(1),
            ..Default::default()
        }
    }

    pub fn is_aniso(&self) -> bool {
        self.max_aniso != 0
    }
}