///|
/// # Seam Carving 16f seam carving 16f
///
/// 图像处理算法模块
/// 图像处理算法模块 Image processing algorithm module
// 接缝裁剪(Seam Carving)16-bit/float 变体
///|
/// 计算图像能量图 16-bit 版本
/// @see compute_energy 8-bit 版本,本函数为 Image16 变体
pub fn compute_energy_16(img : @types.Image16) -> 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[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.. @types.Image16 {
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 * 2, b'\x00')
for y in 0.. @types.ImageF {
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 * 4, b'\x00')
for y in 0.. @types.Image16 {
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 * 2, b'\x00')
for x in 0.. @types.ImageF {
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 * 4, b'\x00')
for x in 0.. @types.Image16 {
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 * 2, b'\x00')
for y in 0.. @types.ImageF {
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 * 4, b'\x00')
for y in 0.. @types.Image16 {
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 * 2, b'\x00')
for x in 0.. @types.ImageF {
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 * 4, b'\x00')
for x in 0.. @types.Image16 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_16(result)
let seam = find_vertical_seam(energy, result.width, result.height)
result = remove_vertical_seam_16(result, seam)
}
while result.width < new_w {
let energy = compute_energy_16(result)
let seam = find_vertical_seam(energy, result.width, result.height)
result = insert_vertical_seam_16(result, seam)
}
while result.height > new_h {
let energy = compute_energy_16(result)
let seam = find_horizontal_seam(energy, result.width, result.height)
result = remove_horizontal_seam_16(result, seam)
}
while result.height < new_h {
let energy = compute_energy_16(result)
let seam = find_horizontal_seam(energy, result.width, result.height)
result = insert_horizontal_seam_16(result, seam)
}
result
}
///|
/// 内容感知缩放 float 版本
/// @see seam_carve_resize 8-bit 版本,本函数为 ImageF 变体
pub fn seam_carve_resize_f(
img : @types.ImageF,
new_w : Int32,
new_h : Int32,
) -> @types.ImageF 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_f(result)
let seam = find_vertical_seam(energy, result.width, result.height)
result = remove_vertical_seam_f(result, seam)
}
while result.width < new_w {
let energy = compute_energy_f(result)
let seam = find_vertical_seam(energy, result.width, result.height)
result = insert_vertical_seam_f(result, seam)
}
while result.height > new_h {
let energy = compute_energy_f(result)
let seam = find_horizontal_seam(energy, result.width, result.height)
result = remove_horizontal_seam_f(result, seam)
}
while result.height < new_h {
let energy = compute_energy_f(result)
let seam = find_horizontal_seam(energy, result.width, result.height)
result = insert_horizontal_seam_f(result, seam)
}
result
}