///|
/// # 多频段融合 multi band blend
///
/// 图像处理算法模块
/// 图像处理算法模块 Image processing algorithm module
// 拉普拉斯金字塔融合(纯 MoonBit):multi_band_blend
// 多频带融合:对两幅图像和掩码构建拉普拉斯/高斯金字塔,逐层混合后重建

///|
/// 多频带融合(Laplacian pyramid blend)。
/// img_a 和 img_b 尺寸需相同,mask 为灰度图(非零区域取 img_a,零区域取 img_b)。
/// num_bands 为金字塔层数(默认 5)。
pub fn multi_band_blend(
  img_a : @types.Image,
  img_b : @types.Image,
  mask : @types.Image,
  num_bands~ : Int32 = 5,
) -> @types.Image raise @types.LoadError {
  if img_a.width != img_b.width || img_a.height != img_b.height {
    raise @types.LoadError::DecodeFailed("img_a 和 img_b 尺寸需相同")
  }
  if mask.width != img_a.width || mask.height != img_a.height {
    raise @types.LoadError::DecodeFailed("mask 尺寸需与输入图像相同")
  }
  let w = img_a.width
  let h = img_a.height
  let ch = img_a.channels
  let n = w * h
  // 归一化 mask 到 [0, 1](用 Float 数组)
  let mask_f : Array[Float] = Array::make(n, 0.0F)
  for i in 0.. 0 { num_bands } else { 5 }
  // 高斯金字塔:mask
  let mask_pyr : Array[Array[Float]] = [mask_f]
  for _l in 1.. Int32 {
  if h == 0 {
    return 0
  }
  let ratio = n / h
  if ratio > 0 {
    ratio
  } else {
    w
  }
}

///|
fn blur_downsample(data : Array[Float], w : Int32, h : Int32) -> Array[Float] {
  let nw = w / 2
  let nh = h / 2
  if nw == 0 || nh == 0 {
    return [data[0]]
  }
  let out : Array[Float] = Array::make(nw * nh, 0.0F)
  for y in 0.. Array[Array[Float]] {
  let gaussian : Array[(Array[Float], Int32, Int32)] = [(data, w, h)]
  for _l in 1.. Array[Float] {
  let out : Array[Float] = Array::make(dw * dh, 0.0F)
  for y in 0.. Array[Float] {
  if pyramid.length() == 0 {
    return Array::make(w * h, 0.0F)
  }
  // 从最粗层开始重建
  let mut current = pyramid[pyramid.length() - 1]
  let mut cw = 1
  let mut ch = 1
  // 推算最粗层尺寸
  let mut tw = w
  let mut th = h
  let sizes : Array[(Int32, Int32)] = [(tw, th)]
  for _l in 1.. @types.Image16 raise @types.LoadError {
  if img_a.width != img_b.width || img_a.height != img_b.height {
    raise @types.LoadError::DecodeFailed("img_a 和 img_b 尺寸需相同")
  }
  if mask.width != img_a.width || mask.height != img_a.height {
    raise @types.LoadError::DecodeFailed("mask 尺寸需与输入图像相同")
  }
  let w = img_a.width
  let h = img_a.height
  let ch = img_a.channels
  let n = w * h
  let mask_f : Array[Float] = Array::make(n, 0.0F)
  for i in 0.. 0 { num_bands } else { 5 }
  let mask_pyr : Array[Array[Float]] = [mask_f]
  for _l in 1.. 65535 { 65535 } else { v }
    out_bytes[i * 2] = (clamped & 0xFF).to_byte()
    out_bytes[i * 2 + 1] = ((clamped >> 8) & 0xFF).to_byte()
  }
  @types.Image16::{
    width: w,
    height: h,
    channels: ch,
    data: Bytes::from_array(out_bytes),
  }
}

// ========== multi_band_blend float ==========

///|
/// 多频带融合 float 版本。
/// @see multi_band_blend 8-bit 版本,本函数为 ImageF 变体
pub fn multi_band_blend_f(
  img_a : @types.ImageF,
  img_b : @types.ImageF,
  mask : @types.ImageF,
  num_bands~ : Int32 = 5,
) -> @types.ImageF raise @types.LoadError {
  if img_a.width != img_b.width || img_a.height != img_b.height {
    raise @types.LoadError::DecodeFailed("img_a 和 img_b 尺寸需相同")
  }
  if mask.width != img_a.width || mask.height != img_a.height {
    raise @types.LoadError::DecodeFailed("mask 尺寸需与输入图像相同")
  }
  let w = img_a.width
  let h = img_a.height
  let ch = img_a.channels
  let n = w * h
  let mask_f : Array[Float] = Array::make(n, 0.0F)
  for i in 0.. 0 { num_bands } else { 5 }
  let mask_pyr : Array[Array[Float]] = [mask_f]
  for _l in 1..> 8) & 0xFF).to_byte()
    out_bytes[i * 4 + 2] = ((bits >> 16) & 0xFF).to_byte()
    out_bytes[i * 4 + 3] = ((bits >> 24) & 0xFF).to_byte()
  }
  @types.ImageF::{
    width: w,
    height: h,
    channels: ch,
    data: Bytes::from_array(out_bytes),
  }
}