///|
/// # 图像金字塔 16-bit/float 变体 Image pyramid 16-bit/float variants
///
/// 16位高斯/拉普拉斯金字塔 gaussian_pyramid_16 / laplacian_pyramid_16
/// 16位/浮点高斯/拉普拉斯金字塔 16-bit/float Gaussian/Laplacian pyramid
// 图像金字塔 16-bit/float 变体
// pyr_down / pyr_up

///|
fn read_u16_p(data : Bytes, offset : Int32) -> Int32 {
  data[offset].to_int() | (data[offset + 1].to_int() << 8)
}

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

///|
fn read_f32_p(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_p(data : Array[Byte], offset : Int32, v : Float) -> Unit {
  let bits = v.reinterpret_as_int()
  data[offset] = (bits & 0xFF).to_byte()
  data[offset + 1] = ((bits >> 8) & 0xFF).to_byte()
  data[offset + 2] = ((bits >> 16) & 0xFF).to_byte()
  data[offset + 3] = ((bits >> 24) & 0xFF).to_byte()
}

// ========== pyr_down 16-bit ==========

///|
/// 高斯金字塔下采样 16-bit 版本。
/// @see pyr_down 8-bit 版本,本函数为 Image16 变体
pub fn pyr_down_16(
  img : @types.Image16,
) -> @types.Image16 raise @types.LoadError {
  if img.channels != 1 && img.channels != 3 && img.channels != 4 {
    raise @types.LoadError::DecodeFailed("pyr_down_16 仅支持 channels=1/3/4")
  }
  let ch = img.channels
  let w = img.width
  let h = img.height
  let new_w = w / 2
  let new_h = h / 2
  if new_w == 0 || new_h == 0 {
    raise @types.LoadError::DecodeFailed("图像太小,无法下采样")
  }
  let data_int : Array[Int32] = Array::make(w * h * ch, 0)
  for i in 0..<(w * h * ch) {
    data_int[i] = read_u16_p(img.data, i * 2)
  }
  let out : Array[Byte] = Array::make(new_w * new_h * ch * 2, b'\x00')
  for y in 0.. @types.ImageF raise @types.LoadError {
  if img.channels != 1 && img.channels != 3 && img.channels != 4 {
    raise @types.LoadError::DecodeFailed("pyr_down_f 仅支持 channels=1/3/4")
  }
  let ch = img.channels
  let w = img.width
  let h = img.height
  let new_w = w / 2
  let new_h = h / 2
  if new_w == 0 || new_h == 0 {
    raise @types.LoadError::DecodeFailed("图像太小,无法下采样")
  }
  let data_f : Array[Float] = Array::make(w * h * ch, 0.0F)
  for i in 0..<(w * h * ch) {
    data_f[i] = read_f32_p(img.data, i * 4)
  }
  let out : Array[Byte] = Array::make(new_w * new_h * ch * 4, b'\x00')
  for y in 0.. @types.Image16 raise @types.LoadError {
  if img.channels != 1 && img.channels != 3 && img.channels != 4 {
    raise @types.LoadError::DecodeFailed("pyr_up_16 仅支持 channels=1/3/4")
  }
  let ch = img.channels
  let w = img.width
  let h = img.height
  let new_w = w * 2
  let new_h = h * 2
  let data_int : Array[Int32] = Array::make(w * h * ch, 0)
  for i in 0..<(w * h * ch) {
    data_int[i] = read_u16_p(img.data, i * 2)
  }
  let x0_arr : Array[Int32] = Array::make(new_w, 0)
  let x1_arr : Array[Int32] = Array::make(new_w, 0)
  let wx_arr : Array[Float] = Array::make(new_w, 0.0F)
  for x in 0.. 65535.0F {
          65535
        } else {
          (v + 0.5F).to_int()
        }
        write_u16_p(out, ((y * new_w + x) * ch + c) * 2, clamped)
      }
    }
  }
  @types.Image16::{
    width: new_w,
    height: new_h,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

// ========== pyr_up float ==========

///|
/// 拉普拉斯金字塔上采样 float 版本。
/// @see pyr_up 8-bit 版本,本函数为 ImageF 变体
pub fn pyr_up_f(img : @types.ImageF) -> @types.ImageF raise @types.LoadError {
  if img.channels != 1 && img.channels != 3 && img.channels != 4 {
    raise @types.LoadError::DecodeFailed("pyr_up_f 仅支持 channels=1/3/4")
  }
  let ch = img.channels
  let w = img.width
  let h = img.height
  let new_w = w * 2
  let new_h = h * 2
  let data_f : Array[Float] = Array::make(w * h * ch, 0.0F)
  for i in 0..<(w * h * ch) {
    data_f[i] = read_f32_p(img.data, i * 4)
  }
  let x0_arr : Array[Int32] = Array::make(new_w, 0)
  let x1_arr : Array[Int32] = Array::make(new_w, 0)
  let wx_arr : Array[Float] = Array::make(new_w, 0.0F)
  for x in 0.. @types.Image16 {
  let ch = a.channels
  let w = a.width
  let h = a.height
  let a_int : Array[Int32] = Array::make(w * h * ch, 0)
  for i in 0..<(w * h * ch) {
    a_int[i] = read_u16_p(a.data, i * 2)
  }
  let bw = b.width
  let bh = b.height
  let b_int : Array[Int32] = Array::make(bw * bh * ch, 0)
  for i in 0..<(bw * bh * ch) {
    b_int[i] = read_u16_p(b.data, i * 2)
  }
  let out : Array[Byte] = Array::make(w * h * ch * 2, b'\x00')
  for y in 0.. 65535 {
          65535
        } else {
          diff
        }
        write_u16_p(out, ((y * w + x) * ch + c) * 2, clamped)
      }
    }
  }
  @types.Image16::{
    width: w,
    height: h,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

// ========== laplacian_diff float ==========

///|
fn laplacian_diff_pf(a : @types.ImageF, b : @types.ImageF) -> @types.ImageF {
  let ch = a.channels
  let w = a.width
  let h = a.height
  let a_f : Array[Float] = Array::make(w * h * ch, 0.0F)
  for i in 0..<(w * h * ch) {
    a_f[i] = read_f32_p(a.data, i * 4)
  }
  let bw = b.width
  let bh = b.height
  let b_f : Array[Float] = Array::make(bw * bh * ch, 0.0F)
  for i in 0..<(bw * bh * ch) {
    b_f[i] = read_f32_p(b.data, i * 4)
  }
  let out : Array[Byte] = Array::make(w * h * ch * 4, b'\x00')
  for y in 0.. Array[@types.Image16] raise @types.LoadError {
  let result : Array[@types.Image16] = [img]
  let mut current = img
  let max_levels = if levels == 0 { 100 } else { levels }
  for _ in 1.. Array[@types.ImageF] raise @types.LoadError {
  let result : Array[@types.ImageF] = [img]
  let mut current = img
  let max_levels = if levels == 0 { 100 } else { levels }
  for _ in 1.. Array[@types.Image16] raise @types.LoadError {
  let gaussian = build_gaussian_pyramid_16(img, levels)
  let result : Array[@types.Image16] = []
  for i in 0..<(gaussian.length() - 1) {
    let upsampled = pyr_up_16(gaussian[i + 1])
    let lap = laplacian_diff_p16(gaussian[i], upsampled)
    result.push(lap)
  }
  result.push(gaussian[gaussian.length() - 1])
  result
}

// ========== build_laplacian_pyramid float ==========

///|
/// 构建拉普拉斯金字塔 float 版本。
pub fn build_laplacian_pyramid_float(
  img : @types.ImageF,
  levels : Int32,
) -> Array[@types.ImageF] raise @types.LoadError {
  let gaussian = build_gaussian_pyramid_f(img, levels)
  let result : Array[@types.ImageF] = []
  for i in 0..<(gaussian.length() - 1) {
    let upsampled = pyr_up_f(gaussian[i + 1])
    let lap = laplacian_diff_pf(gaussian[i], upsampled)
    result.push(lap)
  }
  result.push(gaussian[gaussian.length() - 1])
  result
}