img_core/resample.rs#
Source: lib/img_core/resample.rs
resample -- separable image resampling (resize) of 32-bit surfaces [src/img/resample.s]
resize(src, dst, filter, flags) -> 0 / ERR_* resample src into dst (dst's size
is the target size) [rs_resize]
Resize::prepare(src, dst, filter, flags) -> Result<Resize, i32>
validate and build the weight tables (dst is only measured) [rs_prepare]
ctx.rows(band, y0, y1) -> 0 / ERR_* dst rows y0 .. y1-1 (clamped) into
a Band of dst; several threads may run it at once on disjoint bands [rs_resize_rows]
axis_weights(s, d, filter) -> Result<AxisWeights, i32> the table of one axis
[rs_axis_weights]
to_work(w, src, n, fmt, flags) n 8-bit pixels -> working row [rs_to_work]
from_work(dst, w, n, fmt, flags) n working pixels -> 8-bit row [rs_from_work]
Formats: src and dst may be FMT_ARGB (straight) or FMT_PARGB (premultiplied), in any
combination (ERR_UNSUPPORTED otherwise); sizes 1 .. 65535 (ERR_DIMS). Filters and flags:
RS_* below.
Pipeline (filters other than RS_NEAREST):
1. Working values: every source row becomes 4 x i16 per pixel, premultiplied, a full
channel = RS_PONE = 16320 (= 255 * 64):
FMT_PARGB P = v << 6 (all four channels)
FMT_ARGB P_a = a << 6, P_c = ((c * a + 2) * 16448) >> 16
RS_GAMMA straight s (FMT_PARGB: s = min(255, (v * 255 + a / 2) / a), a = 0 -> 0),
lin = LIN16[s] = round(16320 * srgb_to_linear(s / 255)),
P_c = (lin * a * 257 + 32768) >> 16, P_a = a << 6
2. Horizontal pass: H = (sum_k w_k * P[x0 + k] + 8192) >> 14, saturated to i16
(arithmetic shift = floor); results are NOT clamped between the passes.
3. Vertical pass: V = (sum_j w_j * H[y0 + j] + 8192) >> 14, saturated to i16.
4. Output: A = max(P_a, 0) (may exceed 16320: ringing at alpha edges), colours clamped
to 0 .. A, a = (min(A, 16320) + 32) >> 6; the colour ratio P_c / A is kept:
FMT_ARGB c = rint_even(f32(P_c * 255) / f32(max(A, 1)))
FMT_PARGB c = (P_c + 32) >> 6, or the straight c above when A > 16320 (a = 255)
RS_GAMMA A = 0 -> pixel 0; else idx = (P_c * 8190 + A) / (2 A),
s = SRGB8[idx] = round(255 * linear_to_srgb(idx / 4095)); straight: c = s,
premultiplied: c = ((s * a + 128) * 257) >> 16
RS_NEAREST copies the source pixel whose centre is nearest: sx = ((2x + 1) S) / (2 D), no
premultiplication. When both axes are identities (same size and an interpolating filter:
everything except RS_BICUBIC_SMOOTH) the rows are copied. Format changes on the copy /
nearest paths use gfx::straight premul / unpremul.
Weight tables (per axis, per output pixel i, all f64 with IEEE double semantics):
scale = S / D; fs = max(scale, 1) for widened filters, else 1; R = support * fs
(support: triangle 1, cubics 2, Lanczos 3). center = ((2i + 1) S) / (2 D);
j = floor(center - R) .. ceil(center + R): w_j = K(((j + 0.5) - center) / fs).
RS_FANT: lo = (i S) / D, hi = ((i + 1) S) / D, j = floor(lo) .. ceil(hi) - 1:
w_j = min(hi, j + 1) - max(lo, j).
total = sum w_j (in j order); every w_j is added to pixel clamp(j, 0, S - 1) (clamp to
edge); q = floor(w / total * 16384 + 0.5); 16384 - sum(q) is added to the first largest
q; zero taps at both ends are dropped. Kernels (x = |x|):
triangle max(0, 1 - x)
cubic a x < 1: ((a + 2) x - (a + 3)) x x + 1; x < 2: ((a x - 5a) x + 8a) x - 4a
(Catmull-Rom a = -0.5; RS_ADAPTIVE growing: a = -0.75)
Mitchell x < 1: (7/6 x - 2) x x + 8/9; x < 2: ((-7/18 x + 2) x - 10/3) x + 16/9
Lanczos-3 x = 0: 1; x < 3: sinpi(x) sinpi(x / 3) / (pi x (pi x / 3)) with x / 3 computed
as x * (1/3); sinpi: n = floor(t + 0.5), z = pi (t - n), Taylor polynomial
of degree 13 (Horner in z^2), negated for odd n
RS_ADAPTIVE: per axis Lanczos-3 when S > D, else cubic a = -0.75.
Record of a table (AxisWeights::tab, the bytes of the assembly): +0 i32 first source pixel
x0, +4 i32 taps used, +8 8 zero bytes, +16 n/2 pairs of 16 bytes {w_2k, w_2k+1} x 4 (i16)
-- the pmaddwd operand. n = the largest tap count rounded up to even; x0 is moved left so
that x0 + n <= S when possible.
Threads: no global state. rows() allocates its scratch per call (about (S_w + n) * 24 +
K * (D_w + 4) * 8 bytes; K = taps of the vertical filter, at most RS_RING_BUDGET bytes of
ring rows; more taps are summed in groups through i32 accumulators). All sums are i32
(wrapping), so groups, band splits and register widths never change a result. The
horizontal kernels keep the SSE2 layout of the assembly (pair rows); the vertical pass,
to_work and from_work work on SSE2 or AVX2 registers (simd::Kernel), pixel by pixel equal.
RS_NEAREST: u32 = 0 // point sampling (no premultiplication, pixels copied)
RS_BILINEAR: u32 = 1 // triangle, widened by the scale factor when downscaling
RS_BILINEAR_LQ: u32 = 2 // triangle, never widened (plain 2 x 2)
RS_BICUBIC: u32 = 3 // Catmull-Rom (Keys a = -0.5), widened when downscaling
RS_BICUBIC_SMOOTH: u32 = 4 // Mitchell-Netravali B = C = 1/3, widened
RS_LANCZOS3: u32 = 5 // sinc(x) sinc(x/3), |x| < 3, widened when downscaling
RS_FANT: u32 = 6 // exact area average
RS_ADAPTIVE: u32 = 7 // per axis: Lanczos-3 shrinking, Keys a = -0.75 growing
RS_FILTER_COUNT: u32 = 8
RS_MULTISAMPLE: u32 = 8 // xform only: 4 x 4 supersampled bilinear
RS_GAMMA: u32 = 1 // flag: filter in linear light
RS_WONE: i32 = 16384 // sum of every output's weights
RS_PONE: i32 = 16320 // working value of a full channel (255 * 64)
struct AxisWeightsthe weight table of one axis (S source -> D destination pixels)tab: Vec<u8>-- D records of rb bytes (format: module header)n: u32-- taps per record (even)rb: u32-- bytes per record = n * 8 + 16nmax: u32-- largest real tap countid: bool-- S == D with one tap per output (an identity)
fn axis_weights(s: i32, d: i32, filter: u32) -> Result<AxisWeights, i32>the weight table of one axis: ERR_DIMS (sizes outside 1 .. 65535), ERR_UNSUPPORTED (RS_NEAREST or an unknown filter) [rs_axis_weights]fn to_work(w: &mut [i16], src: &[u8], n: usize, fmt: u8, flags: u32)n pixels of an 8-bit row (FMT_ARGB / FMT_PARGB) to working values (4 x i16 per pixel, premultiplied, full = 16320); RS_GAMMA in flags: linear light. Writes w[..n * 4]. [rs_to_work]fn from_work(dst: &mut [u8], w: &[i16], n: usize, fmt: u8, flags: u32)n working pixels (4 x i16, any values) to an 8-bit row of FMT_ARGB / FMT_PARGB (module header). Reads w[..n * 4], writes dst[..n * 4]. [rs_from_work]struct Resize<'a>a prepared resize of src into a destination of the measured size; read-only while rows() runs (Sync) [rs_prepare context]
impl<'a> Resize<'a>
fn prepare(src: &'a Surface, dst: &Surface, filter: u32, flags: u32) -> Result<Resize<'a>, i32>validate and build the weight tables: ERR_UNSUPPORTED (filter, formats), ERR_DIMS (sizes outside 1 .. 65535) [rs_prepare]fn rows(&self, band: &mut Band, y0: i32, y1: i32) -> i32dst rows y0 .. y1-1 (clamped to 0 .. dst h) into band, which must hold them [rs_resize_rows]fn resize(src: &Surface, dst: &mut Surface, filter: u32, flags: u32) -> i32resample src into dst (its size is the target size): prepare + every row [rs_resize]