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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* // Copyright (c) Radzivon Bartoshyk 5/2026. All rights reserved.
* //
* // Redistribution and use in source and binary forms, with or without modification,
* // are permitted provided that the following conditions are met:
* //
* // 1. Redistributions of source code must retain the above copyright notice, this
* // list of conditions and the following disclaimer.
* //
* // 2. Redistributions in binary form must reproduce the above copyright notice,
* // this list of conditions and the following disclaimer in the documentation
* // and/or other materials provided with the distribution.
* //
* // 3. Neither the name of the copyright holder nor the names of its
* // contributors may be used to endorse or promote products derived from
* // this software without specific prior written permission.
* //
* // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
use crate::dct::fmla;
use crate::image::Image3F;
const K_GABORISH: [f32; 5] = [
-0.09495815671340026,
-0.041031725066768575,
0.013710004822696948,
0.006510206083837737,
-0.0014789063378272242,
];
/// Apply the forward (encoder-side) gaborish filter in place. The kernel
/// sharpens slightly; the decoder will apply its complementary inverse.
///
/// Mirror-reflects pixels at image boundaries.
pub(crate) fn gaborish_inverse(image: &mut Image3F, mul: f32) {
debug_assert!(mul >= 0.0);
// Build weights: c at center, r/R/d/D/L at the symmetric positions.
// We compute the un-normalized weights, then normalize so the kernel sums
// to 1 (this matches what libjxl does — center=1, then divide everything
// by the total).
let w_r = mul * K_GABORISH[0];
let w_d = mul * K_GABORISH[1];
let w_r2 = mul * K_GABORISH[2];
let w_l = mul * K_GABORISH[3];
let w_d2 = mul * K_GABORISH[4];
let mut sum = 1.0
+ mul
* 4.
* (K_GABORISH[0] + K_GABORISH[1] + K_GABORISH[2] + K_GABORISH[4] + 2. * K_GABORISH[3]);
if sum < 1e-5 {
sum = 1e-5;
}
let norm = 1.0 / sum;
let n_c = norm;
let n_r = norm * w_r;
let n_d = norm * w_d;
let n_r2 = norm * w_r2;
let n_l = norm * w_l;
let n_d2 = norm * w_d2;
let mut out = vec![0.0; image.xsize() * image.ysize()];
for c in 0..3 {
symmetric5_plane(image, c, n_c, n_r, n_d, n_r2, n_l, n_d2, &mut out);
}
}
#[inline]
fn reflect(i: isize, len: isize) -> usize {
let mut j = i;
// Wrap into [-len+1, 2*len-2] then reflect.
while j < 0 || j >= len {
if j < 0 {
j = -j - 1;
} else if j >= len {
j = 2 * len - j - 1;
}
}
j as usize
}
/// Symmetric 5×5 convolution on one plane, in place (via temp buffer).
/// Reads neighbors using mirror-reflected indices.
fn symmetric5_plane(
image: &mut Image3F,
c: usize,
n_c: f32,
n_r: f32,
n_d: f32,
n_r2: f32,
n_l: f32,
n_d2: f32,
out: &mut Vec<f32>,
) {
let xsize = image.xsize();
let ysize = image.ysize();
let plane = image.plane(c);
for y in 0..ysize {
let iy = y as isize;
// Precompute row indices for ±1, ±2.
let y_m1 = reflect(iy - 1, ysize as isize);
let y_p1 = reflect(iy + 1, ysize as isize);
let y_m2 = reflect(iy - 2, ysize as isize);
let y_p2 = reflect(iy + 2, ysize as isize);
let row_0 = plane.row(y);
let row_m1 = plane.row(y_m1);
let row_p1 = plane.row(y_p1);
let row_m2 = plane.row(y_m2);
let row_p2 = plane.row(y_p2);
let dst_row = &mut out[y * xsize..(y + 1) * xsize];
assert!(dst_row.len() >= xsize);
let border = 2usize;
let center_start = border.min(xsize);
let center_end = xsize.saturating_sub(border);
for x in 0..center_start {
let ix = x as isize;
let x_m1 = reflect(ix - 1, xsize as isize);
let x_p1 = reflect(ix + 1, xsize as isize);
let x_m2 = reflect(ix - 2, xsize as isize);
let x_p2 = reflect(ix + 2, xsize as isize);
// Cardinal neighbors at distance 1 (4 of them): r
let s_r = row_m1[x] + row_p1[x] + row_0[x_m1] + row_0[x_p1];
// Diagonal neighbors at distance √2 (4 of them): d
let s_d = row_m1[x_m1] + row_m1[x_p1] + row_p1[x_m1] + row_p1[x_p1];
// Cardinal neighbors at distance 2 (4 of them): R
let s_r2 = row_m2[x] + row_p2[x] + row_0[x_m2] + row_0[x_p2];
// Diagonal neighbors at distance 2√2 (4 of them): D
let s_d2 = row_m2[x_m2] + row_m2[x_p2] + row_p2[x_m2] + row_p2[x_p2];
// Knight neighbors at distance √5 (8 of them): L
let s_l = row_m2[x_m1]
+ row_m2[x_p1]
+ row_m1[x_m2]
+ row_m1[x_p2]
+ row_p1[x_m2]
+ row_p1[x_p2]
+ row_p2[x_m1]
+ row_p2[x_p1];
let mut acc = n_c * row_0[x];
acc = fmla(n_r, s_r, acc);
acc = fmla(n_d, s_d, acc);
acc = fmla(n_r2, s_r2, acc);
acc = fmla(n_d2, s_d2, acc);
acc = fmla(n_l, s_l, acc);
dst_row[x] = acc;
}
if center_start < center_end {
assert!(center_start >= 2);
assert!(row_0.len() >= center_end + 2);
assert!(row_m1.len() >= center_end + 2);
assert!(row_p1.len() >= center_end + 2);
assert!(row_m2.len() >= center_end + 2);
assert!(row_p2.len() >= center_end + 2);
// dst_row covers at least [0, center_end)
assert!(dst_row.len() >= center_end);
for x in center_start..center_end {
let ix = x as isize;
let x_m1 = (ix - 1) as usize;
let x_p1 = (ix + 1) as usize;
let x_m2 = (ix - 2) as usize;
let x_p2 = (ix + 2) as usize;
// Cardinal neighbors at distance 1 (4 of them): r
let s_r = row_m1[x] + row_p1[x] + row_0[x_m1] + row_0[x_p1];
// Diagonal neighbors at distance √2 (4 of them): d
let s_d = row_m1[x_m1] + row_m1[x_p1] + row_p1[x_m1] + row_p1[x_p1];
// Cardinal neighbors at distance 2 (4 of them): R
let s_r2 = row_m2[x] + row_p2[x] + row_0[x_m2] + row_0[x_p2];
// Diagonal neighbors at distance 2√2 (4 of them): D
let s_d2 = row_m2[x_m2] + row_m2[x_p2] + row_p2[x_m2] + row_p2[x_p2];
// Knight neighbors at distance √5 (8 of them): L
let s_l = row_m2[x_m1]
+ row_m2[x_p1]
+ row_m1[x_m2]
+ row_m1[x_p2]
+ row_p1[x_m2]
+ row_p1[x_p2]
+ row_p2[x_m1]
+ row_p2[x_p1];
let mut acc = n_c * row_0[x];
acc = fmla(n_r, s_r, acc);
acc = fmla(n_d, s_d, acc);
acc = fmla(n_r2, s_r2, acc);
acc = fmla(n_d2, s_d2, acc);
acc = fmla(n_l, s_l, acc);
dst_row[x] = acc;
}
}
for x in center_end..xsize {
let ix = x as isize;
let x_m1 = reflect(ix - 1, xsize as isize);
let x_p1 = reflect(ix + 1, xsize as isize);
let x_m2 = reflect(ix - 2, xsize as isize);
let x_p2 = reflect(ix + 2, xsize as isize);
// Cardinal neighbors at distance 1 (4 of them): r
let s_r = row_m1[x] + row_p1[x] + row_0[x_m1] + row_0[x_p1];
// Diagonal neighbors at distance √2 (4 of them): d
let s_d = row_m1[x_m1] + row_m1[x_p1] + row_p1[x_m1] + row_p1[x_p1];
// Cardinal neighbors at distance 2 (4 of them): R
let s_r2 = row_m2[x] + row_p2[x] + row_0[x_m2] + row_0[x_p2];
// Diagonal neighbors at distance 2√2 (4 of them): D
let s_d2 = row_m2[x_m2] + row_m2[x_p2] + row_p2[x_m2] + row_p2[x_p2];
// Knight neighbors at distance √5 (8 of them): L
let s_l = row_m2[x_m1]
+ row_m2[x_p1]
+ row_m1[x_m2]
+ row_m1[x_p2]
+ row_p1[x_m2]
+ row_p1[x_p2]
+ row_p2[x_m1]
+ row_p2[x_p1];
let mut acc = n_c * row_0[x];
acc = fmla(n_r, s_r, acc);
acc = fmla(n_d, s_d, acc);
acc = fmla(n_r2, s_r2, acc);
acc = fmla(n_d2, s_d2, acc);
acc = fmla(n_l, s_l, acc);
dst_row[x] = acc;
}
}
// The displaced plane becomes the next channel's output buffer.
image.plane_mut(c).swap_data(out);
}
#[cfg(test)]
mod tests {
use super::*;
use crate::image::Image3F;
#[test]
fn constant_input_unchanged() {
let mut img = Image3F::new(16, 16);
for c in 0..3 {
for y in 0..16 {
let row = img.plane_row_mut(c, y);
for x in 0..16 {
row[x] = 0.5;
}
}
}
gaborish_inverse(&mut img, 0.9908511);
for c in 0..3 {
for y in 0..16 {
let row = img.plane_row(c, y);
for x in 0..16 {
assert!(
(row[x] - 0.5).abs() < 1e-5,
"c={} y={} x={}: got {} want 0.5",
c,
y,
x,
row[x]
);
}
}
}
}
#[test]
fn sharpens_step_edge() {
// the sharpening kernel.
let mut img = Image3F::new(16, 16);
for c in 0..3 {
for y in 0..16 {
let row = img.plane_row_mut(c, y);
for x in 0..16 {
row[x] = if x < 8 { 0.0 } else { 1.0 };
}
}
}
let orig_at_8 = 1.0_f32;
let orig_at_7 = 0.0_f32;
gaborish_inverse(&mut img, 0.9908511);
// Sharpening means: value just *after* the edge (at x=8) is slightly
// larger than 1.0, value just *before* the edge (at x=7) slightly
// negative.
let new_at_8 = img.plane_row(0, 8)[8];
let new_at_7 = img.plane_row(0, 8)[7];
assert!(
new_at_8 > orig_at_8 - 0.001,
"value at edge+ should not collapse: {}",
new_at_8
);
assert!(
new_at_7 < orig_at_7 + 0.001,
"value at edge- should not bleed: {}",
new_at_7
);
}
}