///|
/// # 绘制函数 16-bit/float 变体 Drawing functions 16-bit/float variants
///
/// 16位绘图基元 draw_line_16 / draw_rect_16
/// 16位/浮点绘图基元 16-bit/float drawing primitives
// 绘制函数 16-bit/float 变体
// draw_line / draw_rectangle / draw_circle / draw_polygon

// ========== 辅助函数 ==========

///|
fn write_u16_le_draw(buf : Array[Byte], offset : Int32, val : Int32) -> Unit {
  buf[offset] = (val & 0xFF).to_byte()
  buf[offset + 1] = ((val >> 8) & 0xFF).to_byte()
}

///|
fn write_f32_le_draw(buf : Array[Byte], offset : Int32, val : Float) -> Unit {
  let bits = val.reinterpret_as_int()
  buf[offset] = (bits & 0xFF).to_byte()
  buf[offset + 1] = ((bits >> 8) & 0xFF).to_byte()
  buf[offset + 2] = ((bits >> 16) & 0xFF).to_byte()
  buf[offset + 3] = ((bits >> 24) & 0xFF).to_byte()
}

///|
fn set_pixel_16(
  buf : Array[Byte],
  x : Int32,
  y : Int32,
  w : Int32,
  ch : Int32,
  color : Array[Int32],
) -> Unit {
  if x < 0 || x >= w || y < 0 {
    return
  }
  let idx = (y * w + x) * ch * 2
  for c in 0.. Unit {
  if x < 0 || x >= w || y < 0 {
    return
  }
  let idx = (y * w + x) * ch * 4
  for c in 0.. @types.Image16 {
  let w = img.width
  let h = img.height
  let ch = img.channels
  let buf : Array[Byte] = img.data.to_array()
  let dx = if x2 > x1 { x2 - x1 } else { x1 - x2 }
  let dy = if y2 > y1 { y2 - y1 } else { y1 - y2 }
  let sx = if x1 < x2 { 1 } else { -1 }
  let sy = if y1 < y2 { 1 } else { -1 }
  let mut err = dx - dy
  let mut x = x1
  let mut y = y1
  let t = if thickness < 1 { 1 } else { thickness }
  let mut done = false
  while !done {
    for ty in 0.. -dy {
        err -= dy
        x += sx
      }
      if e2 < dx {
        err += dx
        y += sy
      }
    }
  }
  @types.Image16::{
    width: w,
    height: h,
    channels: ch,
    data: Bytes::from_array(buf),
  }
}

///|
/// 16-bit 绘制矩形。
/// @see draw_rectangle 8-bit 版本,本函数为 Image16 变体
pub fn draw_rectangle_16(
  img : @types.Image16,
  x : Int32,
  y : Int32,
  rw : Int32,
  rh : Int32,
  color : Array[Int32],
  thickness~ : Int32 = 1,
  fill~ : Bool = false,
) -> @types.Image16 {
  let w = img.width
  let h = img.height
  let ch = img.channels
  let buf : Array[Byte] = img.data.to_array()
  if fill {
    for py in y..<(y + rh) {
      for px in x..<(x + rw) {
        set_pixel_16(buf, px, py, w, ch, color)
      }
    }
  } else {
    let t = if thickness < 1 { 1 } else { thickness }
    for px in x..<(x + rw) {
      for ty in 0.. @types.Image16 {
  let w = img.width
  let h = img.height
  let ch = img.channels
  let buf : Array[Byte] = img.data.to_array()
  if fill {
    for y in (cy - r)..<=(cy + r) {
      for x in (cx - r)..<=(cx + r) {
        let dx = x - cx
        let dy = y - cy
        if dx * dx + dy * dy <= r * r {
          set_pixel_16(buf, x, y, w, ch, color)
        }
      }
    }
  } else {
    let _ = thickness
    let mut x = r
    let mut y = 0
    let mut err = 0
    while x >= y {
      set_pixel_16(buf, cx + x, cy + y, w, ch, color)
      set_pixel_16(buf, cx + y, cy + x, w, ch, color)
      set_pixel_16(buf, cx - y, cy + x, w, ch, color)
      set_pixel_16(buf, cx - x, cy + y, w, ch, color)
      set_pixel_16(buf, cx - x, cy - y, w, ch, color)
      set_pixel_16(buf, cx - y, cy - x, w, ch, color)
      set_pixel_16(buf, cx + y, cy - x, w, ch, color)
      set_pixel_16(buf, cx + x, cy - y, w, ch, color)
      y += 1
      if err <= 0 {
        err += 2 * y + 1
      }
      if err > 0 {
        x -= 1
        err -= 2 * x + 1
      }
    }
  }
  @types.Image16::{
    width: w,
    height: h,
    channels: ch,
    data: Bytes::from_array(buf),
  }
}

///|
/// 16-bit 绘制多边形。
/// @see draw_polygon 8-bit 版本,本函数为 Image16 变体
pub fn draw_polygon_16(
  img : @types.Image16,
  points : Array[(Int32, Int32)],
  color : Array[Int32],
  thickness~ : Int32 = 1,
) -> @types.Image16 {
  let mut result = img
  let n = points.length()
  if n < 2 {
    return result
  }
  for i in 0.. @types.ImageF {
  let w = img.width
  let h = img.height
  let ch = img.channels
  let buf : Array[Byte] = img.data.to_array()
  let dx = if x2 > x1 { x2 - x1 } else { x1 - x2 }
  let dy = if y2 > y1 { y2 - y1 } else { y1 - y2 }
  let sx = if x1 < x2 { 1 } else { -1 }
  let sy = if y1 < y2 { 1 } else { -1 }
  let mut err = dx - dy
  let mut x = x1
  let mut y = y1
  let t = if thickness < 1 { 1 } else { thickness }
  let mut done = false
  while !done {
    for ty in 0.. -dy {
        err -= dy
        x += sx
      }
      if e2 < dx {
        err += dx
        y += sy
      }
    }
  }
  @types.ImageF::{
    width: w,
    height: h,
    channels: ch,
    data: Bytes::from_array(buf),
  }
}

///|
/// float 绘制矩形。
/// @see draw_rectangle 8-bit 版本,本函数为 ImageF 变体
pub fn draw_rectangle_f(
  img : @types.ImageF,
  x : Int32,
  y : Int32,
  rw : Int32,
  rh : Int32,
  color : Array[Float],
  thickness~ : Int32 = 1,
  fill~ : Bool = false,
) -> @types.ImageF {
  let w = img.width
  let h = img.height
  let ch = img.channels
  let buf : Array[Byte] = img.data.to_array()
  if fill {
    for py in y..<(y + rh) {
      for px in x..<(x + rw) {
        set_pixel_f(buf, px, py, w, ch, color)
      }
    }
  } else {
    let t = if thickness < 1 { 1 } else { thickness }
    for px in x..<(x + rw) {
      for ty in 0.. @types.ImageF {
  let w = img.width
  let h = img.height
  let ch = img.channels
  let buf : Array[Byte] = img.data.to_array()
  if fill {
    for y in (cy - r)..<=(cy + r) {
      for x in (cx - r)..<=(cx + r) {
        let dx = x - cx
        let dy = y - cy
        if dx * dx + dy * dy <= r * r {
          set_pixel_f(buf, x, y, w, ch, color)
        }
      }
    }
  } else {
    let _ = thickness
    let mut x = r
    let mut y = 0
    let mut err = 0
    while x >= y {
      set_pixel_f(buf, cx + x, cy + y, w, ch, color)
      set_pixel_f(buf, cx + y, cy + x, w, ch, color)
      set_pixel_f(buf, cx - y, cy + x, w, ch, color)
      set_pixel_f(buf, cx - x, cy + y, w, ch, color)
      set_pixel_f(buf, cx - x, cy - y, w, ch, color)
      set_pixel_f(buf, cx - y, cy - x, w, ch, color)
      set_pixel_f(buf, cx + y, cy - x, w, ch, color)
      set_pixel_f(buf, cx + x, cy - y, w, ch, color)
      y += 1
      if err <= 0 {
        err += 2 * y + 1
      }
      if err > 0 {
        x -= 1
        err -= 2 * x + 1
      }
    }
  }
  @types.ImageF::{
    width: w,
    height: h,
    channels: ch,
    data: Bytes::from_array(buf),
  }
}

///|
/// float 绘制多边形。
/// @see draw_polygon 8-bit 版本,本函数为 ImageF 变体
pub fn draw_polygon_f(
  img : @types.ImageF,
  points : Array[(Int32, Int32)],
  color : Array[Float],
  thickness~ : Int32 = 1,
) -> @types.ImageF {
  let mut result = img
  let n = points.length()
  if n < 2 {
    return result
  }
  for i in 0..