///|
/// # 倾斜校正 ocr/deskew
///
/// 图像处理库子模块
/// 图像处理库子模块 Image processing library submodule
// 倾斜校正
// 基于霍夫变换的倾斜检测与校正

///|
/// 检测图像倾斜角度(基于水平投影的方差最大化)
/// 返回角度(度),正值表示顺时针倾斜
pub fn detect_skew_angle(img : @types.Image) -> Double raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "detect_skew_angle: 图像尺寸必须大于0",
    )
  }
  if img.channels != 1 {
    raise @types.LoadError::DecodeFailed(
      "detect_skew_angle: 仅支持单通道灰度图",
    )
  }
  let expected_size = img.width * img.height
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "detect_skew_angle: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }

  let w = img.width
  let h = img.height
  let data = img.data
  // 在 -15 到 +15 度范围内搜索最佳倾斜角度
  let mut best_angle = 0.0
  let mut best_variance = 0.0
  let step = 0.5 // 0.5 度步长
  let mut angle = -15.0
  while angle <= 15.0 {
    let variance = projection_variance_at_angle(data, w, h, angle)
    if variance > best_variance {
      best_variance = variance
      best_angle = angle
    }
    angle += step
  }
  best_angle
}

///|
/// 计算指定角度下的水平投影方差
fn projection_variance_at_angle(
  data : Bytes,
  width : Int32,
  height : Int32,
  angle_deg : Double,
) -> Double {
  let angle_rad = angle_deg * 3.141592653589793 / 180.0
  let cos_a = @math.cos(angle_rad)
  let sin_a = @math.sin(angle_rad)
  // 计算旋转后的图像尺寸
  let new_h = (height.to_double() * cos_a.abs() +
    width.to_double() * sin_a.abs()).to_int() +
    1
  // 计算水平投影
  let projection : Array[Int32] = Array::make(new_h, 0)
  for y in 0..= 0 && new_y < new_h {
          projection[new_y] += 1
        }
      }
    }
  }
  // 计算投影的方差
  let mut sum = 0
  let mut sum_sq = 0
  let mut count = 0
  for i in 0.. @types.Image raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed("deskew: 图像尺寸必须大于0")
  }
  if img.channels != 1 {
    raise @types.LoadError::DecodeFailed("deskew: 仅支持单通道灰度图")
  }
  let expected_size = img.width * img.height
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "deskew: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }

  let angle = detect_skew_angle(img)
  rotate_document(img, -angle)
}

///|
/// 文档旋转(带白色背景填充)
/// angle: 旋转角度(度),正值表示逆时针
pub fn rotate_document(
  img : @types.Image,
  angle : Double,
) -> @types.Image raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "rotate_document: 图像尺寸必须大于0",
    )
  }
  if img.channels < 1 || img.channels > 4 {
    raise @types.LoadError::DecodeFailed(
      "rotate_document: 通道数必须在1-4之间",
    )
  }
  let expected_size = img.width * img.height * img.channels
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "rotate_document: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }

  let w = img.width
  let h = img.height
  let ch = img.channels
  let angle_rad = angle * 3.141592653589793 / 180.0
  let cos_a = @math.cos(angle_rad)
  let sin_a = @math.sin(angle_rad)
  // 计算新图像尺寸
  let new_w = (w.to_double() * cos_a.abs() + h.to_double() * sin_a.abs()).to_int() +
    1
  let new_h = (w.to_double() * sin_a.abs() + h.to_double() * cos_a.abs()).to_int() +
    1
  // 创建白色背景的新图像
  let out : Array[Byte] = Array::make(new_w * new_h * ch, b'\xFF')
  let data = img.data
  // 反向映射:对新图像的每个像素,找到原图像对应的像素
  for y in 0..= 0 && x1 < w && y0 >= 0 && y1 < h {
        for c in 0..