jxl_modular/
sample.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use jxl_bitstream::unpack_signed;
use jxl_grid::{AlignedGrid, MutableSubgrid};

pub trait Sealed: Copy + Default + Send + Sync {
    fn try_as_mutable_subgrid_i32<'a, 'g>(
        grid: &'a mut MutableSubgrid<'g, Self>,
    ) -> Option<&'a mut MutableSubgrid<'g, i32>>;
    fn try_as_mutable_subgrid_i16<'a, 'g>(
        grid: &'a mut MutableSubgrid<'g, Self>,
    ) -> Option<&'a mut MutableSubgrid<'g, i16>>;
    fn try_into_grid_i32(grid: AlignedGrid<Self>) -> Result<AlignedGrid<i32>, AlignedGrid<Self>>;
    fn try_into_grid_i16(grid: AlignedGrid<Self>) -> Result<AlignedGrid<i16>, AlignedGrid<Self>>;

    fn unpack_signed_u32(value: u32) -> Self;

    /// Performs wrapping addition.
    fn add(self, rhs: Self) -> Self;

    /// Performs wrapping multiplication followed by wrapping addition.
    fn wrapping_muladd_i32(self, mul: i32, add: i32) -> Self;

    /// Computes clamped gradient, which is `(n + w - nw).clamp(w.min(n), w.max(n))`.
    fn grad_clamped(n: Self, w: Self, nw: Self) -> Self;
}

/// Type of Modular image samples.
///
/// Currently `i32` and `i16` implements `Sample`.
pub trait Sample: Copy + Default + Send + Sync + Sealed + 'static {
    fn from_i32(value: i32) -> Self;
    fn from_u32(value: u32) -> Self;

    fn to_i32(self) -> i32;
    fn to_i64(self) -> i64;
    fn to_f32(self) -> f32;
}

impl Sample for i32 {
    #[inline]
    fn from_i32(value: i32) -> Self {
        value
    }

    #[inline]
    fn from_u32(value: u32) -> Self {
        value as i32
    }

    #[inline]
    fn to_i32(self) -> i32 {
        self
    }

    #[inline]
    fn to_i64(self) -> i64 {
        self as i64
    }

    #[inline]
    fn to_f32(self) -> f32 {
        self as f32
    }
}

impl Sample for i16 {
    #[inline]
    fn from_i32(value: i32) -> Self {
        value as i16
    }

    #[inline]
    fn from_u32(value: u32) -> Self {
        value as i16
    }

    #[inline]
    fn to_i32(self) -> i32 {
        self as i32
    }

    #[inline]
    fn to_i64(self) -> i64 {
        self as i64
    }

    #[inline]
    fn to_f32(self) -> f32 {
        self as f32
    }
}

impl Sealed for i32 {
    fn try_as_mutable_subgrid_i32<'a, 'g>(
        grid: &'a mut MutableSubgrid<'g, i32>,
    ) -> Option<&'a mut MutableSubgrid<'g, i32>> {
        Some(grid)
    }

    fn try_as_mutable_subgrid_i16<'a, 'g>(
        _: &'a mut MutableSubgrid<'g, i32>,
    ) -> Option<&'a mut MutableSubgrid<'g, i16>> {
        None
    }

    fn try_into_grid_i32(grid: AlignedGrid<Self>) -> Result<AlignedGrid<i32>, AlignedGrid<Self>> {
        Ok(grid)
    }

    fn try_into_grid_i16(grid: AlignedGrid<Self>) -> Result<AlignedGrid<i16>, AlignedGrid<Self>> {
        Err(grid)
    }

    #[inline]
    fn unpack_signed_u32(value: u32) -> Self {
        unpack_signed(value)
    }

    #[inline]
    fn add(self, rhs: i32) -> i32 {
        self.wrapping_add(rhs)
    }

    #[inline]
    fn wrapping_muladd_i32(self, mul: i32, add: i32) -> i32 {
        self.wrapping_mul(mul).wrapping_add(add)
    }

    #[inline]
    fn grad_clamped(n: i32, w: i32, nw: i32) -> i32 {
        let (n, w) = if w > n {
            (w as i64, n as i64)
        } else {
            (n as i64, w as i64)
        };
        (w + n - nw as i64).clamp(w, n) as i32
    }
}

impl Sealed for i16 {
    fn try_as_mutable_subgrid_i32<'a, 'g>(
        _: &'a mut MutableSubgrid<'g, i16>,
    ) -> Option<&'a mut MutableSubgrid<'g, i32>> {
        None
    }

    fn try_as_mutable_subgrid_i16<'a, 'g>(
        grid: &'a mut MutableSubgrid<'g, i16>,
    ) -> Option<&'a mut MutableSubgrid<'g, i16>> {
        Some(grid)
    }

    fn try_into_grid_i32(grid: AlignedGrid<Self>) -> Result<AlignedGrid<i32>, AlignedGrid<Self>> {
        Err(grid)
    }

    fn try_into_grid_i16(grid: AlignedGrid<Self>) -> Result<AlignedGrid<i16>, AlignedGrid<Self>> {
        Ok(grid)
    }

    #[inline]
    fn unpack_signed_u32(value: u32) -> Self {
        let bit = (value & 1) as u16;
        let base = (value >> 1) as u16;
        let flip = 0u16.wrapping_sub(bit);
        (base ^ flip) as i16
    }

    #[inline]
    fn add(self, rhs: i16) -> i16 {
        self.wrapping_add(rhs)
    }

    #[inline]
    fn wrapping_muladd_i32(self, mul: i32, add: i32) -> i16 {
        self.wrapping_mul(mul as i16).wrapping_add(add as i16)
    }

    #[inline]
    fn grad_clamped(n: i16, w: i16, nw: i16) -> i16 {
        let (n, w) = if w > n {
            (w as i32, n as i32)
        } else {
            (n as i32, w as i32)
        };
        (w + n - nw as i32).clamp(w, n) as i16
    }
}