///|
/// # OCR 性能 ocr/ocr_performance
///
/// 图像处理库子模块
/// 图像处理库子模块 Image processing library submodule
// v13.5.0 OCR 性能优化(纯 MoonBit 算法层面)
// 查找表预计算 + 循环展开 + 缓存友好访问 + 内联展开

///|
/// 预计算灰度查找表(0-255 映射)
pub fn build_grayscale_lut() -> Array[Int32] {
  let lut : Array[Int32] = Array::make(256, 0)
  for i in 0..<256 {
    lut[i] = i
  }
  lut
}

///|
/// 预计算二值化查找表
pub fn build_threshold_lut(threshold : Int32) -> Array[Int32] {
  let lut : Array[Int32] = Array::make(256, 0)
  for i in 0..<256 {
    lut[i] = if i >= threshold { 255 } else { 0 }
  }
  lut
}

///|
/// 使用查找表快速二值化
pub fn fast_threshold_lut(
  img : @types.Image,
  lut : Array[Int32],
) -> @types.Image raise @types.LoadError {
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "fast_threshold_lut: 图像尺寸必须大于0",
    )
  }
  if img.channels < 1 || img.channels > 4 {
    raise @types.LoadError::DecodeFailed("fast_threshold_lut: 通道数非法")
  }
  let expected_size = img.width * img.height * img.channels
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "fast_threshold_lut: 数据大小不匹配",
    )
  }
  let result_data : Array[Byte] = Array::make(expected_size, b'\x00')
  for i in 0.. 0 { b'\xFF' } else { b'\x00' }
  }
  @types.Image::{
    width: img.width,
    height: img.height,
    channels: img.channels,
    data: Bytes::from_array(result_data),
  }
}

///|
/// 缓存友好的水平投影计算(按行连续访问)
pub fn cache_friendly_horizontal_projection(
  img : @types.Image,
) -> Array[Int32] raise @types.LoadError {
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "cache_friendly_horizontal_projection: 图像尺寸必须大于0",
    )
  }
  if img.channels < 1 || img.channels > 4 {
    raise @types.LoadError::DecodeFailed(
      "cache_friendly_horizontal_projection: 通道数非法",
    )
  }
  let expected_size = img.width * img.height * img.channels
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "cache_friendly_horizontal_projection: 数据大小不匹配",
    )
  }
  let projections : Array[Int32] = Array::make(img.height, 0)
  for y in 0.. Array[Int32] raise @types.LoadError {
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "cache_friendly_vertical_projection: 图像尺寸必须大于0",
    )
  }
  if img.channels < 1 || img.channels > 4 {
    raise @types.LoadError::DecodeFailed(
      "cache_friendly_vertical_projection: 通道数非法",
    )
  }
  let expected_size = img.width * img.height * img.channels
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "cache_friendly_vertical_projection: 数据大小不匹配",
    )
  }
  let projections : Array[Int32] = Array::make(img.width, 0)
  for x in 0.. Int32 raise @types.LoadError {
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "unrolled_pixel_sum: 图像尺寸必须大于0",
    )
  }
  if img.channels < 1 || img.channels > 4 {
    raise @types.LoadError::DecodeFailed("unrolled_pixel_sum: 通道数非法")
  }
  let expected_size = img.width * img.height * img.channels
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "unrolled_pixel_sum: 数据大小不匹配",
    )
  }
  let mut total = 0
  let n = expected_size
  let unrolled_end = n - n % 4
  let mut i = 0
  while i < unrolled_end {
    total += img.data[i].to_int()
    total += img.data[i + 1].to_int()
    total += img.data[i + 2].to_int()
    total += img.data[i + 3].to_int()
    i += 4
  }
  while i < n {
    total += img.data[i].to_int()
    i += 1
  }
  total
}

///|
/// 快速连通域标记(两遍扫描法优化版)
pub fn fast_connected_components(
  binary_img : @types.Image,
) -> Array[Int32] raise @types.LoadError {
  if binary_img.width <= 0 || binary_img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "fast_connected_components: 图像尺寸必须大于0",
    )
  }
  if binary_img.channels < 1 || binary_img.channels > 4 {
    raise @types.LoadError::DecodeFailed(
      "fast_connected_components: 通道数非法",
    )
  }
  let w = binary_img.width
  let h = binary_img.height
  let expected_size = w * h * binary_img.channels
  if binary_img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "fast_connected_components: 数据大小不匹配",
    )
  }
  let labels : Array[Int32] = Array::make(w * h, 0)
  let mut current_label = 0
  for y in 0.. 128 && labels[idx] == 0 {
        current_label += 1
        labels[idx] = current_label
      }
    }
  }
  labels
}

///|
/// 预计算高斯核查找表
pub fn build_gaussian_kernel_lut(
  sigma : Double,
  radius : Int32,
) -> Array[Double] raise @types.LoadError {
  if sigma <= 0.0 {
    raise @types.LoadError::DecodeFailed(
      "build_gaussian_kernel_lut: sigma必须大于0",
    )
  }
  if radius <= 0 {
    raise @types.LoadError::DecodeFailed(
      "build_gaussian_kernel_lut: 半径必须大于0",
    )
  }
  let size = 2 * radius + 1
  let kernel : Array[Double] = Array::make(size, 0.0)
  let two_sigma_sq = 2.0 * sigma * sigma
  let mut sum = 0.0
  for i in 0.. @types.Image raise @types.LoadError {
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "fast_gaussian_blur_1d: 图像尺寸必须大于0",
    )
  }
  if img.channels < 1 || img.channels > 4 {
    raise @types.LoadError::DecodeFailed(
      "fast_gaussian_blur_1d: 通道数非法",
    )
  }
  let w = img.width
  let h = img.height
  let c = img.channels
  let expected_size = w * h * c
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "fast_gaussian_blur_1d: 数据大小不匹配",
    )
  }
  let radius = (kernel.length() - 1) / 2
  let result_data : Array[Byte] = Array::make(expected_size, b'\x00')
  for y in 0..= w { w - 1 } else { sx }
          let idx = (y * w + clamped_x) * c + ch
          sum += img.data[idx].to_int().to_double() * kernel[k]
        }
        let val = if sum > 255.0 {
          255
        } else if sum < 0.0 {
          0
        } else {
          sum.to_int()
        }
        result_data[(y * w + x) * c + ch] = val.to_byte()
      }
    }
  }
  @types.Image::{
    width: w,
    height: h,
    channels: c,
    data: Bytes::from_array(result_data),
  }
}

///|
/// 批量字符识别优化(减少重复计算)
pub fn batch_recognize_chars(
  images : Array[@types.Image],
  config : DLCnnModelConfig,
  labels : Array[String],
) -> Array[DLInferenceResult] raise @types.LoadError {
  if images.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "batch_recognize_chars: 图像列表不能为空",
    )
  }
  if labels.length() != config.num_classes {
    raise @types.LoadError::DecodeFailed(
      "batch_recognize_chars: 标签数量不匹配",
    )
  }
  let results : Array[DLInferenceResult] = []
  for i in 0.. (Int32, Int32, Double) raise @types.LoadError {
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "ocr_pipeline_stats: 图像尺寸必须大于0",
    )
  }
  if img.channels < 1 || img.channels > 4 {
    raise @types.LoadError::DecodeFailed("ocr_pipeline_stats: 通道数非法")
  }
  let expected_size = img.width * img.height * img.channels
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "ocr_pipeline_stats: 数据大小不匹配",
    )
  }
  let pixel_count = img.width * img.height
  let byte_count = expected_size
  let avg_pixel = unrolled_pixel_sum(img).to_double() / pixel_count.to_double()
  (pixel_count, byte_count, avg_pixel)
}

///|
/// 内存优化的图像降采样(2x2 均值采样)
pub fn fast_downsample_2x(
  img : @types.Image,
) -> @types.Image raise @types.LoadError {
  if img.width <= 1 || img.height <= 1 {
    raise @types.LoadError::DecodeFailed(
      "fast_downsample_2x: 图像尺寸必须大于1",
    )
  }
  if img.channels < 1 || img.channels > 4 {
    raise @types.LoadError::DecodeFailed("fast_downsample_2x: 通道数非法")
  }
  let w = img.width
  let h = img.height
  let c = img.channels
  let expected_size = w * h * c
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "fast_downsample_2x: 数据大小不匹配",
    )
  }
  let new_w = w / 2
  let new_h = h / 2
  let result_data : Array[Byte] = Array::make(new_w * new_h * c, b'\x00')
  for y in 0..