///|
/// # Seam Carving 接缝裁剪 seam carving
///
/// 图像处理算法模块
/// 图像处理算法模块 Image processing algorithm module
// 接缝裁剪(Seam Carving)— 内容感知缩放
// 纯 MoonBit 实现,基于 Avidan & Shamir 2007

///|
/// 计算图像能量图(Sobel 梯度幅值 |Gx| + |Gy|)
pub fn compute_energy(img : @types.Image) -> Array[Double] {
  let w = img.width
  let h = img.height
  let ch = img.channels
  let energy = Array::make(w * h, 0.0)
  for y in 0.. Array[Int32] {
  let m = Array::make(w * h, 0.0)
  for j in 0.. 0 {
        let v = m[(i - 1) * w + j - 1]
        if v < min_prev {
          min_prev = v
        }
      }
      if j < w - 1 {
        let v = m[(i - 1) * w + j + 1]
        if v < min_prev {
          min_prev = v
        }
      }
      m[i * w + j] = energy[i * w + j] + min_prev
    }
  }
  let seam = Array::make(h, 0)
  let mut min_val = m[(h - 1) * w]
  let mut min_j = 0
  for j in 1.. 0 {
      let v = m[(row - 1) * w + prev_j - 1]
      if v < best_val {
        best_val = v
        best_j = prev_j - 1
      }
    }
    if prev_j < w - 1 {
      let v = m[(row - 1) * w + prev_j + 1]
      if v < best_val {
        best_val = v
        best_j = prev_j + 1
      }
    }
    seam[row - 1] = best_j
  }
  seam
}

///|
/// 移除垂直 seam,宽度减 1
pub fn remove_vertical_seam(
  img : @types.Image,
  seam : Array[Int32],
) -> @types.Image {
  let w = img.width
  let h = img.height
  let ch = img.channels
  let new_w = w - 1
  let out : Array[Byte] = Array::make(new_w * h * ch, b'\x00')
  for y in 0.. Array[Int32] {
  let m = Array::make(w * h, 0.0)
  for i in 0.. 0 {
        let v = m[(i - 1) * w + j - 1]
        if v < min_prev {
          min_prev = v
        }
      }
      if i < h - 1 {
        let v = m[(i + 1) * w + j - 1]
        if v < min_prev {
          min_prev = v
        }
      }
      m[i * w + j] = energy[i * w + j] + min_prev
    }
  }
  let seam = Array::make(w, 0)
  let mut min_val = m[w - 1]
  let mut min_i = 0
  for i in 1.. 0 {
      let v = m[(prev_i - 1) * w + col - 1]
      if v < best_val {
        best_val = v
        best_i = prev_i - 1
      }
    }
    if prev_i < h - 1 {
      let v = m[(prev_i + 1) * w + col - 1]
      if v < best_val {
        best_val = v
        best_i = prev_i + 1
      }
    }
    seam[col - 1] = best_i
  }
  seam
}

///|
/// 移除水平 seam,高度减 1
pub fn remove_horizontal_seam(
  img : @types.Image,
  seam : Array[Int32],
) -> @types.Image {
  let w = img.width
  let h = img.height
  let ch = img.channels
  let new_h = h - 1
  let out : Array[Byte] = Array::make(w * new_h * ch, b'\x00')
  for x in 0.. @types.Image {
  let w = img.width
  let h = img.height
  let ch = img.channels
  let new_w = w + 1
  let out : Array[Byte] = Array::make(new_w * h * ch, b'\x00')
  for y in 0.. @types.Image {
  let w = img.width
  let h = img.height
  let ch = img.channels
  let new_h = h + 1
  let out : Array[Byte] = Array::make(w * new_h * ch, b'\x00')
  for x in 0.. @types.Image raise @types.LoadError {
  if new_w <= 0 || new_h <= 0 {
    raise @types.LoadError::DecodeFailed("目标尺寸必须为正数")
  }
  if new_w > img.width * 2 || new_h > img.height * 2 {
    raise @types.LoadError::DecodeFailed("放大不能超过原尺寸的 2 倍")
  }
  let mut result = img
  while result.width > new_w {
    let energy = compute_energy(result)
    let seam = find_vertical_seam(energy, result.width, result.height)
    result = remove_vertical_seam(result, seam)
  }
  while result.width < new_w {
    let energy = compute_energy(result)
    let seam = find_vertical_seam(energy, result.width, result.height)
    result = insert_vertical_seam(result, seam)
  }
  while result.height > new_h {
    let energy = compute_energy(result)
    let seam = find_horizontal_seam(energy, result.width, result.height)
    result = remove_horizontal_seam(result, seam)
  }
  while result.height < new_h {
    let energy = compute_energy(result)
    let seam = find_horizontal_seam(energy, result.width, result.height)
    result = insert_horizontal_seam(result, seam)
  }
  result
}