///|
/// # 绘图 draw
///
/// 图像处理算法模块
/// 图像处理算法模块 Image processing algorithm module
// 图像合成操作(纯 MoonBit):draw_over / draw_copy

///|
/// 将 src 图像直接拷贝到 dst 的 (x, y) 位置
/// 返回新的 Image,不修改原图。要求 dst 和 src channels 相同。
pub fn draw_copy(
  dst : @types.Image,
  src : @types.Image,
  x : Int32,
  y : Int32,
) -> @types.Image raise @types.LoadError {
  if dst.channels != src.channels {
    raise @types.LoadError::DecodeFailed(
      "draw_copy 要求 dst 和 src channels 相同",
    )
  }
  if x < 0 || y < 0 || x + src.width > dst.width || y + src.height > dst.height {
    raise @types.LoadError::DecodeFailed("draw_copy 区域越界")
  }
  let ch = dst.channels
  let out : Array[Byte] = []
  for i in 0.. @types.Image raise @types.LoadError {
  if dst.channels != 4 || src.channels != 4 {
    raise @types.LoadError::DecodeFailed(
      "draw_over 要求 dst 和 src 均为 RGBA (channels=4)",
    )
  }
  if x < 0 || y < 0 || x + src.width > dst.width || y + src.height > dst.height {
    raise @types.LoadError::DecodeFailed("draw_over 区域越界")
  }
  let out : Array[Byte] = []
  for i in 0.. Unit {
  if x < 0 || x >= w || y < 0 || y >= h {
    return
  }
  let off = (y * w + x) * ch
  for c in 0.. @types.Image {
  let w = img.width
  let h = img.height
  let ch = img.channels
  let out : Array[Byte] = []
  for i in 0.. -dy {
      err -= dy
      x += sx
    }
    if e2 < dx {
      err += dx
      y += sy
    }
  }
  @types.Image::{
    width: w,
    height: h,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

///|
/// 画矩形。color 为颜色数组。thickness 为线宽(默认 1)。fill 为是否填充(默认 false)。
pub fn draw_rectangle(
  img : @types.Image,
  x : Int32,
  y : Int32,
  rw : Int32,
  rh : Int32,
  color : Array[Byte],
  thickness~ : Int32 = 1,
  fill~ : Bool = false,
) -> @types.Image {
  let w = img.width
  let h = img.height
  let ch = img.channels
  let out : Array[Byte] = []
  for i in 0.. @types.Image {
  let w = img.width
  let h = img.height
  let ch = img.channels
  let out : Array[Byte] = []
  for i in 0..= y {
      for dx in -t..<=t {
        put_pixel(out, w, h, ch, cx + x + dx, cy + y, color)
        put_pixel(out, w, h, ch, cx - x + dx, cy + y, color)
        put_pixel(out, w, h, ch, cx + x + dx, cy - y, color)
        put_pixel(out, w, h, ch, cx - x + dx, cy - y, color)
        put_pixel(out, w, h, ch, cx + y + dx, cy + x, color)
        put_pixel(out, w, h, ch, cx - y + dx, cy + x, color)
        put_pixel(out, w, h, ch, cx + y + dx, cy - x, color)
        put_pixel(out, w, h, ch, cx - y + dx, cy - x, color)
      }
      y += 1
      if d <= 0 {
        d += 2 * y + 1
      } else {
        x -= 1
        d += 2 * (y - x) + 1
      }
    }
  }
  @types.Image::{
    width: w,
    height: h,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

///|
/// 画多边形。points 为顶点数组 [(x1,y1), (x2,y2), ...]。color 为颜色数组。thickness 为线宽(默认 1)。fill 为是否填充(默认 false)。
pub fn draw_polygon(
  img : @types.Image,
  points : Array[(Int32, Int32)],
  color : Array[Byte],
  thickness~ : Int32 = 1,
  fill~ : Bool = false,
) -> @types.Image {
  let w = img.width
  let h = img.height
  let ch = img.channels
  let out : Array[Byte] = []
  for i in 0.. y_max {
        y_max = py
      }
    }
    for y in y_min..<=y_max {
      // 求交点
      let xs : Array[Int32] = []
      for i in 0..= y1 && y >= y2 {
          continue
        }
        let x = x1 + (y - y1) * (x2 - x1) / (y2 - y1)
        xs.push(x)
      }
      // 排序交点
      xs.sort()
      // 填充相邻对
      let mut j = 0
      while j + 1 < xs.length() {
        for x in xs[j]..<=xs[j + 1] {
          put_pixel(fill_out, w, h, ch, x, y, color)
        }
        j += 2
      }
    }
    return @types.Image::{
      width: w,
      height: h,
      channels: ch,
      data: Bytes::from_array(fill_out),
    }
  }
  result
}