1use crate::config::Upscale;
5use rayon::prelude::*;
6
7#[allow(clippy::too_many_arguments)]
10pub fn resample(
11 src: &[u32],
12 sw: usize,
13 sh: usize,
14 dst: &mut [u32],
15 stride: usize,
16 ox: usize,
17 oy: usize,
18 dw: usize,
19 dh: usize,
20 mode: Upscale,
21) {
22 if sw == 0 || sh == 0 || dw == 0 || dh == 0 {
23 return;
24 }
25 if sw == dw && sh == dh {
26 dst.par_chunks_mut(stride)
27 .skip(oy)
28 .take(dh)
29 .enumerate()
30 .for_each(|(y, row)| {
31 row[ox..ox + dw].copy_from_slice(&src[y * sw..y * sw + sw]);
32 });
33 return;
34 }
35 let (fx, fy) = (sw as f64 / dw as f64, sh as f64 / dh as f64);
36 let shrink = fx > 1.0 && fy > 1.0;
37 let xs: Vec<Tap> = (0..dw).map(|x| Tap::new(x, fx, sw, mode)).collect();
39 dst.par_chunks_mut(stride)
40 .skip(oy)
41 .take(dh)
42 .enumerate()
43 .for_each(|(y, row)| {
44 let out = &mut row[ox..ox + dw];
45 if shrink {
46 let y0 = (y as f64 * fy) as usize;
47 let y1 = (((y + 1) as f64 * fy).ceil() as usize).clamp(y0 + 1, sh);
48 for (x, o) in out.iter_mut().enumerate() {
49 let x0 = (x as f64 * fx) as usize;
50 let x1 = (((x + 1) as f64 * fx).ceil() as usize).clamp(x0 + 1, sw);
51 let (mut r, mut g, mut b, mut n) = (0u32, 0u32, 0u32, 0u32);
52 for sy in y0..y1 {
53 for &p in &src[sy * sw + x0..sy * sw + x1] {
54 r += (p >> 16) & 255;
55 g += (p >> 8) & 255;
56 b += p & 255;
57 n += 1;
58 }
59 }
60 *o = ((r / n) << 16) | ((g / n) << 8) | (b / n);
61 }
62 return;
63 }
64 let ty = Tap::new(y, fy, sh, mode);
65 let (r0, r1) = (&src[ty.i0 * sw..ty.i0 * sw + sw], &src[ty.i1 * sw..ty.i1 * sw + sw]);
66 for (o, tx) in out.iter_mut().zip(&xs) {
67 *o = if mode == Upscale::Nearest {
68 r0[tx.i0]
69 } else {
70 let top = mix(r0[tx.i0], r0[tx.i1], tx.w);
71 let bot = mix(r1[tx.i0], r1[tx.i1], tx.w);
72 mix(top, bot, ty.w)
73 };
74 }
75 });
76}
77
78#[allow(clippy::too_many_arguments)]
82pub fn resample_cols(
83 src: &[u32],
84 sw: usize,
85 sh: usize,
86 dst: &mut [u32],
87 stride: usize,
88 ox: usize,
89 oy: usize,
90 dw: usize,
91 dh: usize,
92 mode: Upscale,
93) {
94 if sw == 0 || sh == 0 || dw == 0 || dh == 0 {
95 return;
96 }
97 const BAND: usize = 16;
98 let (fx, fy) = (sw as f64 / dw as f64, sh as f64 / dh as f64);
99 let shrink = fx > 1.0 && fy > 1.0;
100 let foot = |d: usize, f: f64, n: usize| {
101 let a = ((d as f64 * f) as usize).min(n - 1);
102 let b = (((d + 1) as f64 * f).ceil() as usize).clamp(a + 1, n);
103 (a, b)
104 };
105 let mode = if sw == dw && sh == dh { Upscale::Nearest } else { mode };
106 let xs: Vec<Tap> = (0..dw).map(|x| Tap::new(x, fx, sw, mode)).collect();
107 let ys: Vec<Tap> = (0..dh).map(|y| Tap::new(y, fy, sh, mode)).collect();
108 let xf: Vec<(usize, usize)> = (0..dw).map(|x| foot(x, fx, sw)).collect();
109 let yf: Vec<(usize, usize)> = (0..dh).map(|y| foot(y, fy, sh)).collect();
110 let area = &mut dst[oy * stride..(oy + dh) * stride];
111 if sw == dw * 2 && sh == dh * 2 {
114 area.par_chunks_mut(stride * BAND).enumerate().for_each(|(band, rows)| {
115 let r0 = band * BAND;
116 let n = rows.len() / stride;
117 for x in 0..dw {
118 let c0 = &src[2 * x * sh..2 * x * sh + sh];
119 let c1 = &src[(2 * x + 1) * sh..(2 * x + 1) * sh + sh];
120 for k in 0..n {
121 let y = 2 * (r0 + k);
122 let (a, b, c, d) = (c0[y], c0[y + 1], c1[y], c1[y + 1]);
123 let rb = (a & 0xff00ff) + (b & 0xff00ff) + (c & 0xff00ff) + (d & 0xff00ff);
124 let g = (a & 0xff00) + (b & 0xff00) + (c & 0xff00) + (d & 0xff00);
125 rows[k * stride + ox + x] = ((rb >> 2) & 0xff00ff) | ((g >> 2) & 0xff00);
126 }
127 }
128 });
129 return;
130 }
131 area.par_chunks_mut(stride * BAND).enumerate().for_each(|(band, rows)| {
132 let r0 = band * BAND;
133 let n = rows.len() / stride;
134 if shrink {
135 let mut acc = [(0u32, 0u32, 0u32, 0u32); BAND];
136 for (x, &(x0, x1)) in xf.iter().enumerate() {
137 acc[..n].fill((0, 0, 0, 0));
138 for c in x0..x1 {
139 let column = &src[c * sh..c * sh + sh];
140 for (k, a) in acc[..n].iter_mut().enumerate() {
141 let (y0, y1) = yf[r0 + k];
142 for &p in &column[y0..y1] {
143 a.0 += (p >> 16) & 255;
144 a.1 += (p >> 8) & 255;
145 a.2 += p & 255;
146 a.3 += 1;
147 }
148 }
149 }
150 for (k, a) in acc[..n].iter().enumerate() {
151 rows[k * stride + ox + x] = ((a.0 / a.3) << 16) | ((a.1 / a.3) << 8) | (a.2 / a.3);
152 }
153 }
154 return;
155 }
156 for (x, tx) in xs.iter().enumerate() {
157 let c0 = &src[tx.i0 * sh..tx.i0 * sh + sh];
158 let c1 = &src[tx.i1 * sh..tx.i1 * sh + sh];
159 for k in 0..n {
160 let ty = &ys[r0 + k];
161 rows[k * stride + ox + x] = if mode == Upscale::Nearest {
162 c0[ty.i0]
163 } else {
164 let a = mix(c0[ty.i0], c1[ty.i0], tx.w);
165 let b = mix(c0[ty.i1], c1[ty.i1], tx.w);
166 mix(a, b, ty.w)
167 };
168 }
169 }
170 });
171}
172
173struct Tap {
175 i0: usize,
176 i1: usize,
177 w: u32,
178}
179
180impl Tap {
181 fn new(d: usize, f: f64, n: usize, mode: Upscale) -> Tap {
182 if mode == Upscale::Nearest {
183 let i = ((d as f64 + 0.5) * f) as usize;
184 let i = i.min(n - 1);
185 return Tap { i0: i, i1: i, w: 0 };
186 }
187 let u = ((d as f64 + 0.5) * f - 0.5).clamp(0.0, (n - 1) as f64);
188 let i0 = u.floor() as usize;
189 let mut t = u - i0 as f64;
190 if mode == Upscale::Sharp {
191 let k = (1.0 / f).max(1.0);
193 t = ((t - 0.5) * k + 0.5).clamp(0.0, 1.0);
194 }
195 Tap {
196 i0,
197 i1: (i0 + 1).min(n - 1),
198 w: (t * 256.0) as u32,
199 }
200 }
201}
202
203fn mix(a: u32, b: u32, w: u32) -> u32 {
204 let iw = 256 - w;
205 let rb = (((a & 0xff00ff) * iw + (b & 0xff00ff) * w) >> 8) & 0xff00ff;
206 let g = (((a & 0x00ff00) * iw + (b & 0x00ff00) * w) >> 8) & 0x00ff00;
207 rb | g
208}
209
210pub fn fit(ww: usize, wh: usize, aw: usize, ah: usize) -> (usize, usize, usize, usize) {
212 if aw * wh > ah * ww {
213 let h = (ww * ah / aw).max(1);
214 (0, (wh - h) / 2, ww, h)
215 } else {
216 let w = (wh * aw / ah).max(1);
217 ((ww - w) / 2, 0, w, wh)
218 }
219}
220
221#[cfg(test)]
222mod tests {
223 use super::*;
224
225 #[test]
226 fn shrinking_averages_pixels() {
227 let src = [0xffffff, 0, 0, 0xffffff];
229 let mut dst = [0u32; 1];
230 resample(&src, 2, 2, &mut dst, 1, 0, 0, 1, 1, Upscale::Sharp);
231 assert_eq!(dst[0], 0x7f7f7f);
232 }
233
234 #[test]
235 fn sharp_keeps_pixel_centres_flat() {
236 let src = [0x000000, 0xffffff];
237 let mut dst = [0u32; 8];
238 resample(&src, 2, 1, &mut dst, 8, 0, 0, 8, 1, Upscale::Sharp);
239 assert_eq!(dst[0], 0);
240 assert_eq!(dst[1], 0);
241 assert_eq!(dst[7], 0xffffff);
242 let mut smooth = [0u32; 8];
243 resample(&src, 2, 1, &mut smooth, 8, 0, 0, 8, 1, Upscale::Smooth);
244 assert_ne!(smooth[2], 0, "bilinear blends further from the seam");
245 }
246
247 #[test]
248 fn column_major_matches_row_major() {
249 let (sw, sh) = (37, 23);
250 let rows: Vec<u32> = (0..sw * sh)
251 .map(|i| (i as u32).wrapping_mul(2654435761) & 0xffffff)
252 .collect();
253 let mut cols = vec![0u32; sw * sh];
254 for y in 0..sh {
255 for x in 0..sw {
256 cols[x * sh + y] = rows[y * sw + x];
257 }
258 }
259 for (dw, dh) in [(37, 23), (80, 50), (15, 9)] {
260 for m in Upscale::ALL {
262 let mut a = vec![0u32; dw * dh];
263 let mut b = vec![0u32; dw * dh];
264 resample(&rows, sw, sh, &mut a, dw, 0, 0, dw, dh, m);
265 resample_cols(&cols, sw, sh, &mut b, dw, 0, 0, dw, dh, m);
266 assert_eq!(a, b, "{dw}x{dh} {m:?}");
267 }
268 }
269 }
270
271 #[test]
272 fn half_size_fast_path_matches_box_average() {
273 let (sw, sh) = (40, 24);
274 let rows: Vec<u32> = (0..sw * sh)
275 .map(|i| (i as u32).wrapping_mul(2654435761) & 0xffffff)
276 .collect();
277 let mut cols = vec![0u32; sw * sh];
278 for y in 0..sh {
279 for x in 0..sw {
280 cols[x * sh + y] = rows[y * sw + x];
281 }
282 }
283 let (dw, dh) = (20, 12);
284 let mut fast = vec![0u32; dw * dh];
285 resample_cols(&cols, sw, sh, &mut fast, dw, 0, 0, dw, dh, Upscale::Sharp);
286 for y in 0..dh {
287 for x in 0..dw {
288 let px = [
289 rows[2 * y * sw + 2 * x],
290 rows[2 * y * sw + 2 * x + 1],
291 rows[(2 * y + 1) * sw + 2 * x],
292 rows[(2 * y + 1) * sw + 2 * x + 1],
293 ];
294 let avg = |sh: u32| px.iter().map(|p| (p >> sh) & 255).sum::<u32>() / 4;
295 let want = (avg(16) << 16) | (avg(8) << 8) | avg(0);
296 assert_eq!(fast[y * dw + x], want, "at {x},{y}");
297 }
298 }
299 }
300
301 #[test]
302 fn fit_letterboxes() {
303 assert_eq!(fit(1920, 1200, 16, 9), (0, 60, 1920, 1080));
304 assert_eq!(fit(1920, 1080, 4, 3), (240, 0, 1440, 1080));
305 }
306}