///|
fn clamp_int(v : Int32, lo : Int32, hi : Int32) -> Int32 {
if v < lo {
lo
} else if v > hi {
hi
} else {
v
}
}
///|
fn cubic_weight(t : Double) -> Double {
let a = -0.5
let abs_t = if t < 0.0 { -t } else { t }
if abs_t <= 1.0 {
(a + 2.0) * abs_t * abs_t * abs_t - (a + 3.0) * abs_t * abs_t + 1.0
} else if abs_t <= 2.0 {
a * abs_t * abs_t * abs_t -
5.0 * a * abs_t * abs_t +
8.0 * a * abs_t -
4.0 * a
} else {
0.0
}
}
///|
fn resize_nearest(
img : ImageData,
new_width : Int32,
new_height : Int32,
) -> ImageData {
let src = img.data.to_fixedarray()
let src_w = img.width
let src_h = img.height
let buf = FixedArray::make(new_width * new_height * 4, b'\x00')
for dy in 0.. ResampleTaps {
if src_len == dst_len {
// Identity: a single tap of weight 1.0.
return {
n: 1,
offsets: FixedArray::makei(dst_len, fn(d) { d * stride }),
weights: FixedArray::make(dst_len, RESAMPLE_ONE),
}
}
let n = match kind {
Bicubic => 4
_ => 2
}
let offsets = FixedArray::make(dst_len * n, 0)
let weights = FixedArray::make(dst_len * n, 0)
let wf = FixedArray::make(n, 0.0)
let scale = src_len.to_double() / dst_len.to_double()
for d in 0.. {
wf[0] = cubic_weight(f + 1.0)
wf[1] = cubic_weight(f)
wf[2] = cubic_weight(1.0 - f)
wf[3] = cubic_weight(2.0 - f)
i0 - 1
}
_ => {
wf[0] = 1.0 - f
wf[1] = f
i0
}
}
let mut sum = 0
let mut best = 0
for k in 0.. bw.abs() {
best = k
}
offsets[d * n + k] = clamp_int(first + k, 0, src_len - 1) * stride
}
// Normalize the rounded weights so that they sum to exactly 1.0.
weights[d * n + best] += RESAMPLE_ONE - sum
}
{ n, offsets, weights, }
}
///|
/// Separable two-pass resampling with 14-bit fixed-point weights.
///
/// The horizontal pass writes RGBA samples as `Int` with
/// 6 fractional bits (unclamped); the vertical pass
/// rounds and clamps to 8 bits. An axis whose size does not change uses an
/// identity tap, so the passes stay uniform.
fn resize_separable(
img : ImageData,
new_width : Int32,
new_height : Int32,
kind : ResizeMethod,
) -> ImageData {
let src_w = img.width
let src_h = img.height
// Horizontal pass: src_w x src_h -> new_width x src_h
let h_taps = ResampleTaps::new(src_w, new_width, kind, 4)
let row_len = new_width * 4
let inter = FixedArray::make(row_len * src_h, 0)
for y in 0.. new_width x new_height
let v_taps = ResampleTaps::new(src_h, new_height, kind, row_len)
let out = FixedArray::make(row_len * new_height, b'\x00')
for y in 0.. ImageData raise EncodeError {
if new_width <= 0 || new_height <= 0 {
raise InvalidDimensions(
"width and height must be positive, got " +
new_width.to_string() +
"x" +
new_height.to_string(),
)
}
if new_width == img.width && new_height == img.height {
let arr = img.data.to_fixedarray()
return {
width: img.width,
height: img.height,
data: Bytes::from_array(arr),
}
}
match resize_method {
Nearest => resize_nearest(img, new_width, new_height)
Bilinear | Bicubic =>
resize_separable(img, new_width, new_height, resize_method)
}
}