///|
/// # 色彩转换 16f color convert 16f
///
/// 图像处理算法模块
/// 图像处理算法模块 Image processing algorithm module
// 颜色空间转换 16-bit/float 变体
// 支持 Image16 / ImageF 两种类型

// 复用 color_16f.mbt 中的 read_u16_le_c / write_u16_le_c / write_f32_le
// 复用 tonemap.mbt 中的 read_float_le

///|
/// 16-bit 去除 alpha 通道(RGBA → RGB,channels=3)
/// @see to_rgb 8-bit 版本,本函数为 Image16 变体
pub fn to_rgb_16(img : @types.Image16) -> @types.Image16 raise @types.LoadError {
  // 输入验证
  if img.width < 0 || img.height < 0 {
    raise @types.LoadError::DecodeFailed("to_rgb_16: 图像尺寸不能为负")
  }
  if img.channels < 1 || img.channels > 4 {
    raise @types.LoadError::DecodeFailed(
      "to_rgb_16: 通道数必须在1-4之间",
    )
  }
  let expected_size = img.width * img.height * img.channels * 2
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "to_rgb_16: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  if img.channels == 3 {
    return img
  }
  if img.channels < 3 {
    return img
  }
  let n = img.width * img.height
  let src_ch = img.channels
  let src_bpp = src_ch * 2
  let out_bpp = 6
  let out : Array[Byte] = Array::make(n * out_bpp, b'\x00')
  for i in 0.. @types.ImageF raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed("to_rgb_f: 图像尺寸必须大于0")
  }
  if img.channels < 1 || img.channels > 4 {
    raise @types.LoadError::DecodeFailed(
      "to_rgb_f: 通道数必须在1-4之间",
    )
  }
  let expected_size = img.width * img.height * img.channels * 4
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "to_rgb_f: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  if img.channels == 3 {
    return img
  }
  if img.channels < 3 {
    return img
  }
  let n = img.width * img.height
  let src_ch = img.channels
  let src_bpp = src_ch * 4
  let out_bpp = 12
  let out : Array[Byte] = Array::make(n * out_bpp, b'\x00')
  for i in 0.. @types.Image16 raise @types.LoadError {
  // 输入验证
  if img.width < 0 || img.height < 0 {
    raise @types.LoadError::DecodeFailed("to_rgba_16: 图像尺寸不能为负")
  }
  if img.channels < 1 || img.channels > 4 {
    raise @types.LoadError::DecodeFailed(
      "to_rgba_16: 通道数必须在1-4之间",
    )
  }
  let expected_size = img.width * img.height * img.channels * 2
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "to_rgba_16: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  if img.channels == 4 {
    return img
  }
  let n = img.width * img.height
  let src_ch = img.channels
  let src_bpp = src_ch * 2
  let out_bpp = 8
  let out : Array[Byte] = Array::make(n * out_bpp, b'\x00')
  for i in 0..= 0 {
        out[dst_off] = img.data[src_off]
        out[dst_off + 1] = img.data[src_off + 1]
      }
    }
    // alpha = 65535
    let alpha_off = i * out_bpp + 6
    out[alpha_off] = b'\xFF'
    out[alpha_off + 1] = b'\xFF'
  }
  @types.Image16::{
    width: img.width,
    height: img.height,
    channels: 4,
    data: Bytes::from_array(out),
  }
}

///|
/// float 添加 alpha 通道(RGB → RGBA,channels=4,alpha=1.0)
/// @see to_rgba 8-bit 版本,本函数为 ImageF 变体
pub fn to_rgba_f(img : @types.ImageF) -> @types.ImageF raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed("to_rgba_f: 图像尺寸必须大于0")
  }
  if img.channels < 1 || img.channels > 4 {
    raise @types.LoadError::DecodeFailed(
      "to_rgba_f: 通道数必须在1-4之间",
    )
  }
  let expected_size = img.width * img.height * img.channels * 4
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "to_rgba_f: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  if img.channels == 4 {
    return img
  }
  let n = img.width * img.height
  let src_ch = img.channels
  let src_bpp = src_ch * 4
  let out_bpp = 16
  let out : Array[Byte] = Array::make(n * out_bpp, b'\x00')
  for i in 0.. @types.Image16 raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "premultiply_alpha_16: 图像尺寸必须大于0",
    )
  }
  if img.channels != 4 {
    raise @types.LoadError::DecodeFailed(
      "premultiply_alpha_16 需要 channels=4 (RGBA)",
    )
  }
  let expected_size = img.width * img.height * img.channels * 2
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "premultiply_alpha_16: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  let n = img.width * img.height
  let bpp = 8
  let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  for i in 0.. @types.ImageF raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "premultiply_alpha_f: 图像尺寸必须大于0",
    )
  }
  if img.channels != 4 {
    raise @types.LoadError::DecodeFailed(
      "premultiply_alpha_f 需要 channels=4 (RGBA)",
    )
  }
  let expected_size = img.width * img.height * img.channels * 4
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "premultiply_alpha_f: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  let n = img.width * img.height
  let bpp = 16
  let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  for i in 0.. @types.Image16 raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "unpremultiply_alpha_16: 图像尺寸必须大于0",
    )
  }
  if img.channels != 4 {
    raise @types.LoadError::DecodeFailed(
      "unpremultiply_alpha_16 需要 channels=4 (RGBA)",
    )
  }
  let expected_size = img.width * img.height * img.channels * 2
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "unpremultiply_alpha_16: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  let n = img.width * img.height
  let bpp = 8
  let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  for i in 0.. @types.ImageF raise @types.LoadError {
  // 输入验证
  if img.width <= 0 || img.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "unpremultiply_alpha_f: 图像尺寸必须大于0",
    )
  }
  if img.channels != 4 {
    raise @types.LoadError::DecodeFailed(
      "unpremultiply_alpha_f 需要 channels=4 (RGBA)",
    )
  }
  let expected_size = img.width * img.height * img.channels * 4
  if img.data.length() != expected_size {
    raise @types.LoadError::DecodeFailed(
      "unpremultiply_alpha_f: 数据大小不匹配,期望 " +
      expected_size.to_string() +
      ",实际 " +
      img.data.length().to_string(),
    )
  }
  let n = img.width * img.height
  let bpp = 16
  let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  for i in 0.. (Int32, Int32, Int32) raise @types.LoadError {
  // 输入验证
  if r < 0 || r > 65535 || g < 0 || g > 65535 || b < 0 || b > 65535 {
    raise @types.LoadError::DecodeFailed(
      "rgb_to_ycbcr_16: r,g,b必须在[0,65535]范围内",
    )
  }
  let y = (299 * r + 587 * g + 114 * b) / 1000
  let cb = (-169 * r - 331 * g + 500 * b) / 1000 + 32768
  let cr = (500 * r - 419 * g - 81 * b) / 1000 + 32768
  (
    y.clamp(min=0, max=65535),
    cb.clamp(min=0, max=65535),
    cr.clamp(min=0, max=65535),
  )
}

///|
/// float RGB → YCbCr(ITU-R BT.601)。输入范围 0.0-1.0,输出范围 0.0-1.0。
/// @see rgb_to_ycbcr 8-bit 版本,本函数为 ImageF 变体
pub fn rgb_to_ycbcr_f(
  r : Float,
  g : Float,
  b : Float,
) -> (Float, Float, Float) raise @types.LoadError {
  // 输入验证
  if r < 0.0F || r > 1.0F || g < 0.0F || g > 1.0F || b < 0.0F || b > 1.0F {
    raise @types.LoadError::DecodeFailed(
      "rgb_to_ycbcr_f: r,g,b必须在[0,1]范围内",
    )
  }
  let y = 0.299F * r + 0.587F * g + 0.114F * b
  let cb = -0.169F * r - 0.331F * g + 0.5F * b + 0.5F
  let cr = 0.5F * r - 0.419F * g - 0.081F * b + 0.5F
  (
    @types.clamp_float(y, 0.0F, 1.0F),
    @types.clamp_float(cb, 0.0F, 1.0F),
    @types.clamp_float(cr, 0.0F, 1.0F),
  )
}

///|
/// 16-bit YCbCr → RGB(ITU-R BT.601)。输入范围 0-65535。
/// @see ycbcr_to_rgb 8-bit 版本,本函数为 Image16 变体
pub fn ycbcr_to_rgb_16(
  y : Int32,
  cb : Int32,
  cr : Int32,
) -> (Int32, Int32, Int32) raise @types.LoadError {
  // 输入验证
  if y < 0 || y > 65535 || cb < 0 || cb > 65535 || cr < 0 || cr > 65535 {
    raise @types.LoadError::DecodeFailed(
      "ycbcr_to_rgb_16: y,cb,cr必须在[0,65535]范围内",
    )
  }
  let cb0 = cb - 32768
  let cr0 = cr - 32768
  let r = y + 1402 * cr0 / 1000
  let g = y - 344 * cb0 / 1000 - 714 * cr0 / 1000
  let b = y + 1772 * cb0 / 1000
  (
    r.clamp(min=0, max=65535),
    g.clamp(min=0, max=65535),
    b.clamp(min=0, max=65535),
  )
}

///|
/// float YCbCr → RGB(ITU-R BT.601)。输入范围 0.0-1.0。
/// @see ycbcr_to_rgb 8-bit 版本,本函数为 ImageF 变体
pub fn ycbcr_to_rgb_f(
  y : Float,
  cb : Float,
  cr : Float,
) -> (Float, Float, Float) raise @types.LoadError {
  // 输入验证
  if y < 0.0F || y > 1.0F || cb < 0.0F || cb > 1.0F || cr < 0.0F || cr > 1.0F {
    raise @types.LoadError::DecodeFailed(
      "ycbcr_to_rgb_f: y,cb,cr必须在[0,1]范围内",
    )
  }
  let cb0 = cb - 0.5F
  let cr0 = cr - 0.5F
  let r = y + 1.402F * cr0
  let g = y - 0.344F * cb0 - 0.714F * cr0
  let b = y + 1.772F * cb0
  (
    @types.clamp_float(r, 0.0F, 1.0F),
    @types.clamp_float(g, 0.0F, 1.0F),
    @types.clamp_float(b, 0.0F, 1.0F),
  )
}

///|
/// 16-bit RGB → CMYK。输入范围 0-65535,输出范围 0-65535。
/// @see rgb_to_cmyk 8-bit 版本,本函数为 Image16 变体
pub fn rgb_to_cmyk_16(
  r : Int32,
  g : Int32,
  b : Int32,
) -> (Int32, Int32, Int32, Int32) raise @types.LoadError {
  // 输入验证
  if r < 0 || r > 65535 || g < 0 || g > 65535 || b < 0 || b > 65535 {
    raise @types.LoadError::DecodeFailed(
      "rgb_to_cmyk_16: r,g,b必须在[0,65535]范围内",
    )
  }
  let c = 65535 - r
  let m = 65535 - g
  let y = 65535 - b
  let k = if c < m { if c < y { c } else { y } } else if m < y { m } else { y }
  if k == 65535 {
    (0, 0, 0, 65535)
  } else {
    let denom = 65535 - k
    (
      (c - k) * 65535 / denom,
      (m - k) * 65535 / denom,
      (y - k) * 65535 / denom,
      k,
    )
  }
}

///|
/// float RGB → CMYK。输入范围 0.0-1.0,输出范围 0.0-1.0。
/// @see rgb_to_cmyk 8-bit 版本,本函数为 ImageF 变体
pub fn rgb_to_cmyk_f(
  r : Float,
  g : Float,
  b : Float,
) -> (Float, Float, Float, Float) raise @types.LoadError {
  // 输入验证
  if r < 0.0F || r > 1.0F || g < 0.0F || g > 1.0F || b < 0.0F || b > 1.0F {
    raise @types.LoadError::DecodeFailed(
      "rgb_to_cmyk_f: r,g,b必须在[0,1]范围内",
    )
  }
  let c = 1.0F - r
  let m = 1.0F - g
  let y = 1.0F - b
  let k = if c < m { if c < y { c } else { y } } else if m < y { m } else { y }
  if k > 0.999999F {
    (0.0F, 0.0F, 0.0F, 1.0F)
  } else {
    let denom = 1.0F - k
    ((c - k) / denom, (m - k) / denom, (y - k) / denom, k)
  }
}

///|
/// 16-bit CMYK → RGB。输入范围 0-65535。
/// @see cmyk_to_rgb 8-bit 版本,本函数为 Image16 变体
pub fn cmyk_to_rgb_16(
  c : Int32,
  m : Int32,
  y : Int32,
  k : Int32,
) -> (Int32, Int32, Int32) raise @types.LoadError {
  // 输入验证
  if c < 0 ||
    c > 65535 ||
    m < 0 ||
    m > 65535 ||
    y < 0 ||
    y > 65535 ||
    k < 0 ||
    k > 65535 {
    raise @types.LoadError::DecodeFailed(
      "cmyk_to_rgb_16: c,m,y,k必须在[0,65535]范围内",
    )
  }
  let r = 65535 - (c * (65535 - k) / 65535 + k)
  let g = 65535 - (m * (65535 - k) / 65535 + k)
  let b = 65535 - (y * (65535 - k) / 65535 + k)
  (
    r.clamp(min=0, max=65535),
    g.clamp(min=0, max=65535),
    b.clamp(min=0, max=65535),
  )
}

///|
/// float CMYK → RGB。输入范围 0.0-1.0。
/// @see cmyk_to_rgb 8-bit 版本,本函数为 ImageF 变体
pub fn cmyk_to_rgb_f(
  c : Float,
  m : Float,
  y : Float,
  k : Float,
) -> (Float, Float, Float) raise @types.LoadError {
  // 输入验证
  if c < 0.0F ||
    c > 1.0F ||
    m < 0.0F ||
    m > 1.0F ||
    y < 0.0F ||
    y > 1.0F ||
    k < 0.0F ||
    k > 1.0F {
    raise @types.LoadError::DecodeFailed(
      "cmyk_to_rgb_f: c,m,y,k必须在[0,1]范围内",
    )
  }
  let r = 1.0F - (c * (1.0F - k) + k)
  let g = 1.0F - (m * (1.0F - k) + k)
  let b = 1.0F - (y * (1.0F - k) + k)
  (
    @types.clamp_float(r, 0.0F, 1.0F),
    @types.clamp_float(g, 0.0F, 1.0F),
    @types.clamp_float(b, 0.0F, 1.0F),
  )
}