///|
/// Floor for `Double` that is also correct for negative inputs (`to_int`
/// truncates toward zero, which would round the wrong way below 0).
fn ifloor(v : Double) -> Int {
  let t = v.to_int()
  if t.to_double() > v {
    t - 1
  } else {
    t
  }
}

///|
/// Nearest-neighbor resize: picks the closest source pixel. Fast and exact,
/// ideal for pixel-art style scaling. Output is `new_width` x `new_height`
/// (both clamped to at least 1). An empty source produces transparent pixels.
pub fn Image::resize_nearest(
  self : Image,
  new_width : Int,
  new_height : Int,
) -> Image {
  let w = if new_width < 1 { 1 } else { new_width }
  let h = if new_height < 1 { 1 } else { new_height }
  let out = Image::new(w, h)
  if self.width == 0 || self.height == 0 {
    return out
  }
  for y in 0.. Image {
  let w = if new_width < 1 { 1 } else { new_width }
  let h = if new_height < 1 { 1 } else { new_height }
  let out = Image::new(w, h)
  if self.width == 0 || self.height == 0 {
    return out
  }
  for y in 0..