///|
/// # 高级几何变换 16-bit/float 变体 Advanced geometry transforms 16-bit/float variants
///
/// 16位透视/Seam Carving perspective_transform_16 / seam_carve_16
/// 16位/浮点透视/Seam Carving 16-bit/float perspective/seam carving
// 高级几何变换 16-bit/float 变体
// warp_affine / warp_perspective / resize

// ========== 辅助函数 ==========

///|
fn read_u16_le_geo(data : Bytes, offset : Int32) -> Int32 {
  data[offset].to_int() + data[offset + 1].to_int() * 256
}

///|
fn write_u16_le_geo(buf : Array[Byte], offset : Int32, val : Int32) -> Unit {
  buf[offset] = (val & 0xFF).to_byte()
  buf[offset + 1] = ((val >> 8) & 0xFF).to_byte()
}

///|
fn read_float_le_geo(data : Bytes, offset : Int32) -> Float {
  let b0 = data[offset].to_int()
  let b1 = data[offset + 1].to_int()
  let b2 = data[offset + 2].to_int()
  let b3 = data[offset + 3].to_int()
  let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)
  Float::reinterpret_from_int(bits)
}

///|
fn write_f32_le_geo(buf : Array[Byte], offset : Int32, val : Float) -> Unit {
  let bits = val.reinterpret_as_int()
  buf[offset] = (bits & 0xFF).to_byte()
  buf[offset + 1] = ((bits >> 8) & 0xFF).to_byte()
  buf[offset + 2] = ((bits >> 16) & 0xFF).to_byte()
  buf[offset + 3] = ((bits >> 24) & 0xFF).to_byte()
}

///|
fn sample_bilinear_16(
  data : Array[Int32],
  w : Int32,
  h : Int32,
  ch : Int32,
  c : Int32,
  x : Float,
  y : Float,
) -> Int32 {
  let x0 = x.to_int()
  let y0 = y.to_int()
  let w_minus_1 = w - 1
  let h_minus_1 = h - 1
  let x0c = @types.clamp_int(x0, 0, w_minus_1)
  let y0c = @types.clamp_int(y0, 0, h_minus_1)
  let x1 = @types.clamp_int(x0 + 1, 0, w_minus_1)
  let y1 = @types.clamp_int(y0 + 1, 0, h_minus_1)
  let fx = x - Float::from_int(x0)
  let fy = y - Float::from_int(y0)
  let p00 = data[(y0c * w + x0c) * ch + c]
  let p10 = data[(y0c * w + x1) * ch + c]
  let p01 = data[(y1 * w + x0c) * ch + c]
  let p11 = data[(y1 * w + x1) * ch + c]
  let top = Float::from_int(p00) * (1.0F - fx) + Float::from_int(p10) * fx
  let bot = Float::from_int(p01) * (1.0F - fx) + Float::from_int(p11) * fx
  (top * (1.0F - fy) + bot * fy + 0.5F).to_int()
}

///|
fn sample_bilinear_f(
  data : Array[Float],
  w : Int32,
  h : Int32,
  ch : Int32,
  c : Int32,
  x : Float,
  y : Float,
) -> Float {
  let x0 = x.to_int()
  let y0 = y.to_int()
  let w_minus_1 = w - 1
  let h_minus_1 = h - 1
  let x0c = @types.clamp_int(x0, 0, w_minus_1)
  let y0c = @types.clamp_int(y0, 0, h_minus_1)
  let x1 = @types.clamp_int(x0 + 1, 0, w_minus_1)
  let y1 = @types.clamp_int(y0 + 1, 0, h_minus_1)
  let fx = x - Float::from_int(x0)
  let fy = y - Float::from_int(y0)
  let p00 = data[(y0c * w + x0c) * ch + c]
  let p10 = data[(y0c * w + x1) * ch + c]
  let p01 = data[(y1 * w + x0c) * ch + c]
  let p11 = data[(y1 * w + x1) * ch + c]
  let top = p00 * (1.0F - fx) + p10 * fx
  let bot = p01 * (1.0F - fx) + p11 * fx
  top * (1.0F - fy) + bot * fy
}

// ========== warp_affine 16-bit ==========

///|
/// 16-bit 仿射变换。
/// @see warp_affine 8-bit 版本,本函数为 Image16 变体
pub fn warp_affine_16(
  img : @types.Image16,
  matrix : (Float, Float, Float, Float, Float, Float),
  new_w : Int32,
  new_h : Int32,
) -> @types.Image16 {
  let (a, b, c, d, e, f) = matrix
  let ch = img.channels
  let w = img.width
  let h = img.height
  let data_int : Array[Int32] = Array::make(w * h * ch, 0)
  for i in 0..<(w * h * ch) {
    data_int[i] = read_u16_le_geo(img.data, i * 2)
  }
  let out : Array[Byte] = Array::make(new_w * new_h * ch * 2, b'\x00')
  for dy in 0.. @types.ImageF {
  let (a, b, c, d, e, f) = matrix
  let ch = img.channels
  let w = img.width
  let h = img.height
  let data_f : Array[Float] = Array::make(w * h * ch, 0.0F)
  for i in 0..<(w * h * ch) {
    data_f[i] = read_float_le_geo(img.data, i * 4)
  }
  let out : Array[Byte] = Array::make(new_w * new_h * ch * 4, b'\x00')
  for dy in 0.. @types.Image16 {
  let ch = img.channels
  let w = img.width
  let h = img.height
  let data_int : Array[Int32] = Array::make(w * h * ch, 0)
  for i in 0..<(w * h * ch) {
    data_int[i] = read_u16_le_geo(img.data, i * 2)
  }
  let out : Array[Byte] = Array::make(new_w * new_h * ch * 2, b'\x00')
  for dy in 0.. @types.ImageF {
  let ch = img.channels
  let w = img.width
  let h = img.height
  let data_f : Array[Float] = Array::make(w * h * ch, 0.0F)
  for i in 0..<(w * h * ch) {
    data_f[i] = read_float_le_geo(img.data, i * 4)
  }
  let out : Array[Byte] = Array::make(new_w * new_h * ch * 4, b'\x00')
  for dy in 0.. @types.Image16 {
  let ch = img.channels
  let w = img.width
  let h = img.height
  let data_int : Array[Int32] = Array::make(w * h * ch, 0)
  for i in 0..<(w * h * ch) {
    data_int[i] = read_u16_le_geo(img.data, i * 2)
  }
  let scale_x = Float::from_int(w) / Float::from_int(new_w)
  let scale_y = Float::from_int(h) / Float::from_int(new_h)
  let out : Array[Byte] = Array::make(new_w * new_h * ch * 2, b'\x00')
  for dy in 0.. @types.ImageF {
  let ch = img.channels
  let w = img.width
  let h = img.height
  let data_f : Array[Float] = Array::make(w * h * ch, 0.0F)
  for i in 0..<(w * h * ch) {
    data_f[i] = read_float_le_geo(img.data, i * 4)
  }
  let scale_x = Float::from_int(w) / Float::from_int(new_w)
  let scale_y = Float::from_int(h) / Float::from_int(new_h)
  let out : Array[Byte] = Array::make(new_w * new_h * ch * 4, b'\x00')
  for dy in 0..