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
//! Multi-pass scaling for extreme scaling ratios.
//!
//! When scaling by a very large factor (e.g., 8K → 480p, a ratio of 16×) a
//! single-pass resampler may introduce aliasing or ringing artefacts. The
//! standard mitigation is to break the scaling into multiple intermediate
//! steps, each with a ratio of at most some maximum factor.
//!
//! `MultiPassScaler` computes the sequence of intermediate resolutions
//! between the source and destination, each step reducing (or enlarging) by
//! at most `max_ratio`. The actual per-step pixel resampling is performed by
//! nearest-neighbour interpolation so the module is self-contained.
//!
//! # Example
//!
//! ```
//! use oximedia_scaling::multi_pass_scale::MultiPassScaler;
//!
//! let scaler = MultiPassScaler::new(2.0); // max 2× per step
//! // 8 → 1: requires multiple steps (8→4→2→1)
//! let steps = scaler.compute_steps(8, 1);
//! assert!(steps.len() >= 3);
//! // Final step must reach the target.
//! assert_eq!(*steps.last().unwrap(), 1);
//! ```
#![allow(clippy::cast_precision_loss)]
#![allow(clippy::cast_possible_truncation)]
#![allow(clippy::cast_sign_loss)]
const CHANNELS: usize = 4;
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
/// Multi-pass image scaler that limits the per-step scale ratio.
#[derive(Debug, Clone)]
pub struct MultiPassScaler {
/// Maximum scale ratio allowed between two consecutive passes.
/// Must be > 1.0; values < 1.0 are clamped to 1.01.
pub max_ratio: f64,
}
impl MultiPassScaler {
/// Create a new `MultiPassScaler` with the specified maximum per-step ratio.
pub fn new(max_ratio: f64) -> Self {
Self {
max_ratio: max_ratio.max(1.01),
}
}
/// Compute the sequence of intermediate widths from `src` to `dst`.
///
/// The returned `Vec` contains every intermediate width in order,
/// **including** the final `dst` value. The source width is *not*
/// included (it is the caller's starting point).
///
/// # Examples
///
/// ```
/// use oximedia_scaling::multi_pass_scale::MultiPassScaler;
///
/// let s = MultiPassScaler::new(2.0);
/// let steps = s.compute_steps(32, 4);
/// assert_eq!(*steps.last().unwrap(), 4);
/// ```
pub fn compute_steps(&self, src: u32, dst: u32) -> Vec<u32> {
if src == dst || src == 0 || dst == 0 {
return vec![dst];
}
let mut steps = Vec::new();
let downscale = dst < src;
let mut current = src as f64;
let target = dst as f64;
loop {
let ratio = if downscale {
current / target
} else {
target / current
};
if ratio <= self.max_ratio {
// Close enough — jump directly to target.
steps.push(dst);
break;
}
// Take one step by max_ratio.
let next = if downscale {
(current / self.max_ratio).round().max(target).max(1.0)
} else {
(current * self.max_ratio).round().min(target)
};
let next_u = next as u32;
steps.push(next_u);
current = next;
// Guard against infinite loops due to floating-point precision.
if next_u == dst {
break;
}
if steps.len() > 64 {
steps.push(dst);
break;
}
}
steps
}
/// Scale an RGBA image (`4 bytes per pixel`) from `(src_w, src_h)` to
/// `(dst_w, dst_h)` using multiple nearest-neighbour passes.
///
/// Each pass reduces (or enlarges) by at most `max_ratio` in each
/// dimension independently.
///
/// # Returns
///
/// An RGBA pixel buffer of length `dst_w × dst_h × 4`.
pub fn scale(&self, src: &[u8], src_w: u32, src_h: u32, dst_w: u32, dst_h: u32) -> Vec<u8> {
if src_w == 0 || src_h == 0 || dst_w == 0 || dst_h == 0 {
return vec![0u8; (dst_w * dst_h) as usize * CHANNELS];
}
let w_steps = self.compute_steps(src_w, dst_w);
let h_steps = self.compute_steps(src_h, dst_h);
let max_steps = w_steps.len().max(h_steps.len());
let mut current_pixels = src.to_vec();
let mut current_w = src_w;
let mut current_h = src_h;
for i in 0..max_steps {
let next_w = w_steps.get(i).copied().unwrap_or(dst_w);
let next_h = h_steps.get(i).copied().unwrap_or(dst_h);
current_pixels = nn_scale_rgba(¤t_pixels, current_w, current_h, next_w, next_h);
current_w = next_w;
current_h = next_h;
}
current_pixels
}
}
/// Nearest-neighbour scale of an RGBA buffer from `(sw, sh)` to `(dw, dh)`.
fn nn_scale_rgba(src: &[u8], sw: u32, sh: u32, dw: u32, dh: u32) -> Vec<u8> {
let sw = sw as usize;
let sh = sh as usize;
let dw_u = dw as usize;
let dh_u = dh as usize;
let mut out = vec![0u8; dw_u * dh_u * CHANNELS];
for dy in 0..dh_u {
let sy = (dy * sh / dh_u).min(sh - 1);
for dx in 0..dw_u {
let sx = (dx * sw / dw_u).min(sw - 1);
let src_off = (sy * sw + sx) * CHANNELS;
let dst_off = (dy * dw_u + dx) * CHANNELS;
if src_off + CHANNELS <= src.len() {
out[dst_off..dst_off + CHANNELS].copy_from_slice(&src[src_off..src_off + CHANNELS]);
}
}
}
out
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_compute_steps_same_size() {
let s = MultiPassScaler::new(2.0);
let steps = s.compute_steps(100, 100);
assert_eq!(steps, vec![100]);
}
#[test]
fn test_compute_steps_downscale_terminates_at_dst() {
let s = MultiPassScaler::new(2.0);
let steps = s.compute_steps(32, 4);
assert_eq!(*steps.last().unwrap(), 4);
}
#[test]
fn test_compute_steps_upscale_terminates_at_dst() {
let s = MultiPassScaler::new(2.0);
let steps = s.compute_steps(4, 32);
assert_eq!(*steps.last().unwrap(), 32);
}
#[test]
fn test_scale_output_size() {
let scaler = MultiPassScaler::new(2.0);
let src = vec![128u8; 64 * 64 * 4];
let out = scaler.scale(&src, 64, 64, 8, 8);
assert_eq!(out.len(), 8 * 8 * 4);
}
#[test]
fn test_scale_identity() {
let scaler = MultiPassScaler::new(2.0);
let src = vec![200u8; 8 * 8 * 4];
let out = scaler.scale(&src, 8, 8, 8, 8);
assert_eq!(out, src);
}
#[test]
fn test_scale_zero_dst_returns_empty_buffer() {
let scaler = MultiPassScaler::new(2.0);
let src = vec![0u8; 16 * 16 * 4];
let out = scaler.scale(&src, 16, 16, 0, 8);
assert!(out.is_empty());
}
#[test]
fn test_compute_steps_large_downscale_uses_multiple_steps() {
let s = MultiPassScaler::new(2.0);
// 8K to 480p: factor of ~16, so should need >=4 steps
let steps = s.compute_steps(7680, 480);
assert!(
steps.len() >= 3,
"expected multiple steps for 16x downscale, got {}",
steps.len()
);
assert_eq!(*steps.last().unwrap_or(&0), 480);
}
#[test]
fn test_compute_steps_min_ratio_clamped() {
// max_ratio below 1.0 should be clamped to 1.01
let s = MultiPassScaler::new(0.5);
assert!(s.max_ratio > 1.0);
let steps = s.compute_steps(10, 5);
assert_eq!(*steps.last().unwrap_or(&0), 5);
}
#[test]
fn test_scale_upscale_output_size() {
let scaler = MultiPassScaler::new(2.0);
let src = vec![64u8; 4 * 4 * 4];
let out = scaler.scale(&src, 4, 4, 32, 32);
assert_eq!(out.len(), 32 * 32 * 4);
}
#[test]
fn test_scale_pixel_value_preserved_on_uniform_image() {
// A uniform image should produce a uniform output at any scale.
let scaler = MultiPassScaler::new(2.0);
let fill = 123u8;
let src = vec![fill; 8 * 8 * 4];
let out = scaler.scale(&src, 8, 8, 2, 2);
assert_eq!(out.len(), 2 * 2 * 4);
for &byte in &out {
assert_eq!(byte, fill);
}
}
#[test]
fn test_compute_steps_single_step_when_within_ratio() {
// src=10, dst=8 — ratio is 1.25 which is < 2.0 so a single jump.
let s = MultiPassScaler::new(2.0);
let steps = s.compute_steps(10, 8);
assert_eq!(steps, vec![8]);
}
#[test]
fn test_scale_zero_src_returns_sized_buffer() {
// src_w=0 — function should return a zero-filled buffer of dst size.
let scaler = MultiPassScaler::new(2.0);
let out = scaler.scale(&[], 0, 4, 8, 4);
assert_eq!(out.len(), 8 * 4 * 4);
for &b in &out {
assert_eq!(b, 0);
}
}
}