///|
/// # 版面分析 ocr/layout_analysis
///
/// 图像处理库子模块
/// 图像处理库子模块 Image processing library submodule
// 版面分析
// 文本区域检测、页面分割、多栏检测

///|
/// 文本区域结构体
#derive(Eq, @debug.Debug)
pub(all) struct TextRegion {
  x : Int32
  y : Int32
  width : Int32
  height : Int32
  region_type : TextRegionType
}

///|
pub extend TextRegion with Eq::{not_equal, equal}

///|
pub extend TextRegion with @debug.Debug::{to_repr}

///|
/// 文本区域类型
/// `#non_exhaustive` 表示未来可能添加新构造器
#derive(Eq, @debug.Debug)
#non_exhaustive
pub(all) enum TextRegionType {
  Text
  Image
  Table
  Unknown
}

///|
pub extend TextRegionType with @debug.Debug::{to_repr}

///|
pub extend TextRegionType with Eq::{not_equal, equal}

///|
/// 文本行结构体
#derive(Eq, @debug.Debug)
pub(all) struct TextLine {
  x : Int32
  y : Int32
  width : Int32
  height : Int32
}

///|
pub extend TextLine with Eq::{not_equal, equal}

///|
pub extend TextLine with @debug.Debug::{to_repr}

///|
/// 检测文本区域(基于投影分析)
/// 返回文本区域列表
pub fn detect_text_regions(
  img : @types.Image,
) -> Array[TextRegion] raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "detect_text_regions: 图像尺寸必须大于0",
    )
  }
  if img.channels != 1 {
    raise @types.LoadError::DecodeFailed(
      "detect_text_regions: 仅支持单通道灰度图",
    )
  }
  let expected_size = img.width * img.height
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "detect_text_regions: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  let w = img.width
  let h = img.height
  let data = img.data
  // 二值化(简单阈值)
  let binary : Array[Bool] = Array::make(w * h, false)
  for i in 0..<(w * h) {
    binary[i] = data[i].to_int() < 128
  }
  // 水平投影(检测文本行)
  let horizontal_proj : Array[Int32] = Array::make(h, 0)
  for y in 0.. threshold && !in_text {
      in_text = true
      start_y = y
    } else if horizontal_proj[y] <= threshold && in_text {
      in_text = false
      // 计算这一行的水平范围
      let mut min_x = w
      let mut max_x = 0
      for yy in start_y.. max_x {
              max_x = x
            }
          }
        }
      }
      if max_x > min_x {
        regions.push(TextRegion::{
          x: min_x,
          y: start_y,
          width: max_x - min_x + 1,
          height: y - start_y,
          region_type: TextRegionType::Text,
        })
      }
    }
  }
  regions
}

///|
/// 页面分割(文本/图像/表格区域)
/// 返回区域列表
pub fn segment_page(
  img : @types.Image,
) -> Array[TextRegion] raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "segment_page: 图像尺寸必须大于0",
    )
  }
  if img.channels != 1 {
    raise @types.LoadError::DecodeFailed(
      "segment_page: 仅支持单通道灰度图",
    )
  }
  let expected_size = img.width * img.height
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "segment_page: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  let w = img.width
  let h = img.height
  let data = img.data
  // 二值化
  let binary : Array[Bool] = Array::make(w * h, false)
  for i in 0..<(w * h) {
    binary[i] = data[i].to_int() < 128
  }
  // 连通域分析(简化版)
  let visited : Array[Bool] = Array::make(w * h, false)
  let regions : Array[TextRegion] = []
  for y in 0.. max_x {
            max_x = cx
          }
          if cy < min_y {
            min_y = cy
          }
          if cy > max_y {
            max_y = cy
          }
          // 8 邻域
          for dy in -1..<2 {
            for dx in -1..<2 {
              let nx = cx + dx
              let ny = cy + dy
              if nx >= 0 &&
                nx < w &&
                ny >= 0 &&
                ny < h &&
                binary[ny * w + nx] &&
                !visited[ny * w + nx] {
                visited[ny * w + nx] = true
                queue.push((nx, ny))
              }
            }
          }
        }
        // 根据区域大小和宽高比判断类型
        let region_w = max_x - min_x + 1
        let region_h = max_y - min_y + 1
        let region_type = if count < 50 {
          TextRegionType::Unknown // 噪声
        } else if region_w > 50 && region_h > 50 {
          TextRegionType::Image // 大区域可能是图像
        } else {
          TextRegionType::Text // 小区域可能是文字
        }
        regions.push(TextRegion::{
          x: min_x,
          y: min_y,
          width: region_w,
          height: region_h,
          region_type,
        })
      }
    }
  }
  regions
}

///|
/// 检测多栏布局
/// 返回栏数和每栏的 x 范围
pub fn detect_columns(
  img : @types.Image,
) -> (Int32, Array[(Int32, Int32)]) raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "detect_columns: 图像尺寸必须大于0",
    )
  }
  if img.channels != 1 {
    raise @types.LoadError::DecodeFailed(
      "detect_columns: 仅支持单通道灰度图",
    )
  }
  let expected_size = img.width * img.height
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "detect_columns: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  let w = img.width
  let h = img.height
  let data = img.data
  // 垂直投影
  let vertical_proj : Array[Int32] = Array::make(w, 0)
  for y in 0..= threshold && in_gap {
      in_gap = false
      let gap_width = x - gap_start
      if gap_width > w / 20 { // 足够宽的间隙才算栏间距
        gaps.push((gap_start, x))
      }
    }
  }
  // 根据间隙计算栏数和范围
  let num_columns = gaps.length() + 1
  let columns : Array[(Int32, Int32)] = []
  let mut prev_end = 0
  for i in 0.. Array[@types.Image] raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "extract_text_lines: 图像尺寸必须大于0",
    )
  }
  if img.channels != 1 {
    raise @types.LoadError::DecodeFailed(
      "extract_text_lines: 仅支持单通道灰度图",
    )
  }
  let expected_size = img.width * img.height
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "extract_text_lines: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  let regions = detect_text_regions(img)
  let lines : Array[@types.Image] = []
  for region in regions {
    if region.region_type == TextRegionType::Text {
      let line = crop_region(
        img,
        region.x,
        region.y,
        region.width,
        region.height,
      )
      match line {
        Some(l) => lines.push(l)
        None => ignore(())
      }
    }
  }
  lines
}

///|
/// 裁剪图像区域
fn crop_region(
  img : @types.Image,
  x : Int32,
  y : Int32,
  width : Int32,
  height : Int32,
) -> @types.Image? {
  let w = img.width
  let h = img.height
  let ch = img.channels
  if x < 0 || y < 0 || x + width > w || y + height > h {
    return None
  }
  let out : Array[Byte] = Array::make(width * height * ch, b'\x00')
  let data = img.data
  for yy in 0.. 去噪 -> 倾斜校正 -> 二值化 -> 输出
pub fn ocr_preprocess(
  img : @types.Image,
  binarization_method~ : String = "sauvola",
) -> @types.Image raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "ocr_preprocess: 图像尺寸必须大于0",
    )
  }
  if img.channels < 1 || img.channels > 4 {
    raise @types.LoadError::DecodeFailed(
      "ocr_preprocess: 通道数必须在1-4之间",
    )
  }
  let expected_size = img.width * img.height * img.channels
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "ocr_preprocess: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  let mut result = img
  // 1. 转灰度
  if result.channels != 1 {
    result = to_grayscale_ocr(result)
  }
  // 2. 文档增强(去噪+对比度+锐化)
  result = document_enhance(result)
  // 3. 倾斜校正
  result = deskew(result)
  // 4. 二值化
  result = match binarization_method {
    "sauvola" => sauvola_threshold(result)
    "niblack" => niblack_threshold(result)
    "otsu" => otsu_threshold(result)
    "adaptive" => adaptive_threshold_ocr(result)
    _ => sauvola_threshold(result)
  }
  result
}

///|
/// 转灰度(OCR 专用)
fn to_grayscale_ocr(img : @types.Image) -> @types.Image {
  if img.channels == 1 {
    return img
  }
  let n = img.width * img.height
  let ch = img.channels
  let out : Array[Byte] = Array::make(n, b'\x00')
  let data = img.data
  for i in 0..