///|
/// # 自适应阈值 adaptive threshold
///
/// 图像处理算法模块
/// 图像处理算法模块 Image processing algorithm module
// 自适应阈值(纯 MoonBit)

///|
/// 自适应阈值(均值法)。以每个像素周围 block_size×block_size 区域的均值减去 C 作为局部阈值。
/// 仅处理灰度图(channels=1)或 RGB 图(channels=3/4),alpha 通道不变。
pub fn adaptive_threshold_mean(
  img : @types.Image,
  block_size : Int32,
  c : Int32,
) -> @types.Image raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "adaptive_threshold_mean: 图像尺寸必须大于0",
    )
  }
  if img.channels != 1 && img.channels != 3 && img.channels != 4 {
    raise @types.LoadError::DecodeFailed(
      "adaptive_threshold_mean 仅支持 channels=1/3/4",
    )
  }
  let expected_size = img.width * img.height * img.channels
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "adaptive_threshold_mean: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  if block_size < 3 || block_size % 2 == 0 {
    raise @types.LoadError::DecodeFailed("block_size 必须为 >=3 的奇数")
  }
  let ch = img.channels
  let color_ch = if ch >= 3 { 3 } else { 1 }
  let w = img.width
  let h = img.height
  let n = w * h
  let out : Array[Byte] = Array::make(n * ch, b'\x00')
  let half = block_size / 2
  // 预计算像素 Int 数组
  let src : Array[Int32] = Array::make(n * ch, 0)
  for i in 0..<(n * ch) {
    src[i] = img.data[i].to_int()
  }
  // 内部区域 count 恒为 block_size²
  let inner_count = block_size * block_size
  for c_idx in 0.. threshold {
          b'\xFF'
        } else {
          b'\x00'
        }
      }
    }
    // 边界区域
    for y in 0..= half && y < h - half
      for x in 0..= half && x < w - half {
          continue
        }
        let mut sum = 0
        let mut count = 0
        for dy in -half..<=half {
          for dx in -half..<=half {
            let ny = y + dy
            let nx = x + dx
            if ny >= 0 && ny < h && nx >= 0 && nx < w {
              sum += src[(ny * w + nx) * ch + c_idx]
              count += 1
            }
          }
        }
        let mean = if count > 0 { sum / count } else { 0 }
        let threshold = mean - c
        let v = src[(y * w + x) * ch + c_idx]
        out[(y * w + x) * ch + c_idx] = if v > threshold {
          b'\xFF'
        } else {
          b'\x00'
        }
      }
    }
  }
  if ch == 4 {
    for i in 0.. @types.Image raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "adaptive_threshold_gaussian: 图像尺寸必须大于0",
    )
  }
  if img.channels != 1 && img.channels != 3 && img.channels != 4 {
    raise @types.LoadError::DecodeFailed(
      "adaptive_threshold_gaussian 仅支持 channels=1/3/4",
    )
  }
  let expected_size = img.width * img.height * img.channels
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "adaptive_threshold_gaussian: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  if block_size < 3 || block_size % 2 == 0 {
    raise @types.LoadError::DecodeFailed("block_size 必须为 >=3 的奇数")
  }
  let ch = img.channels
  let color_ch = if ch >= 3 { 3 } else { 1 }
  let w = img.width
  let h = img.height
  let n = w * h
  let out : Array[Byte] = Array::make(n * ch, b'\x00')
  let half = block_size / 2
  let sigma = Float::from_int(half) / 2.0F
  let two_sigma_sq = 2.0F * sigma * sigma
  // 预计算高斯权重(1D 扁平化)
  let weights : Array[Float] = Array::make(block_size * block_size, 0.0F)
  let mut total_weight = 0.0F
  for dy in 0.. threshold {
          b'\xFF'
        } else {
          b'\x00'
        }
      }
    }
    // 边界区域
    for y in 0..= half && y < h - half
      for x in 0..= half && x < w - half {
          continue
        }
        let mut sum = 0.0F
        let mut weight_sum = 0.0F
        for dy in -half..<=half {
          for dx in -half..<=half {
            let ny = y + dy
            let nx = x + dx
            if ny >= 0 && ny < h && nx >= 0 && nx < w {
              let weight = weights[(dy + half) * block_size + (dx + half)]
              sum += src[(ny * w + nx) * ch + c_idx] * weight
              weight_sum += weight
            }
          }
        }
        let mean = if weight_sum > 0.0F { sum / weight_sum } else { 0.0F }
        let threshold = mean - Float::from_int(c)
        let v = src[(y * w + x) * ch + c_idx]
        out[(y * w + x) * ch + c_idx] = if v > threshold {
          b'\xFF'
        } else {
          b'\x00'
        }
      }
    }
  }
  if ch == 4 {
    for i in 0.. @types.Image raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "threshold_otsu: 图像尺寸必须大于0",
    )
  }
  if img.channels != 1 && img.channels != 3 && img.channels != 4 {
    raise @types.LoadError::DecodeFailed(
      "threshold_otsu 仅支持 channels=1/3/4",
    )
  }
  let expected_size = img.width * img.height * img.channels
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "threshold_otsu: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  let ch = img.channels
  let color_ch = if ch >= 3 { 3 } else { 1 }
  let w = img.width
  let h = img.height
  let n = w * h
  let out : Array[Byte] = Array::make(n * ch, b'\x00')
  for c_idx in 0.. best_var {
        best_var = var_between
        best_t = t
      }
    }
    for i in 0.. best_t { b'\xFF' } else { b'\x00' }
    }
  }
  if ch == 4 {
    for i in 0..