// ============================================================================
// Image Support
// ============================================================================

///|
/// Image data (RGBA pixels)
pub struct Image {
  priv width : Int
  priv height : Int
  priv pixels : Array[Color] // Row-major order
}

///|
/// Create a new image with specified dimensions
pub fn Image::new(width : Int, height : Int) -> Image {
  let width = if width < 0 { 0 } else { width }
  let height = if height < 0 { 0 } else { height }
  let total = width * height
  let pixels = Array::make(total, Color::transparent())
  { width, height, pixels }
}

///|
/// Create an image filled with a color
pub fn Image::filled(width : Int, height : Int, color : Color) -> Image {
  let width = if width < 0 { 0 } else { width }
  let height = if height < 0 { 0 } else { height }
  let total = width * height
  let pixels = Array::make(total, color)
  { width, height, pixels }
}

///|
/// Create an image from row-major RGBA pixels when dimensions match exactly.
pub fn Image::from_pixels(
  width : Int,
  height : Int,
  source : Array[Color],
) -> Image? {
  if width < 0 || height < 0 || source.length() != width * height {
    return None
  }
  let pixels : Array[Color] = []
  for color in source {
    pixels.push(color)
  }
  Some({ width, height, pixels })
}

///|
pub fn Image::width(self : Image) -> Int {
  self.width
}

///|
pub fn Image::height(self : Image) -> Int {
  self.height
}

///|
/// Get pixel at coordinates
pub fn Image::get_pixel(self : Image, x : Int, y : Int) -> Color {
  if x < 0 || x >= self.width || y < 0 || y >= self.height {
    return Color::transparent()
  }
  self.pixels[y * self.width + x]
}

///|
/// Set pixel at coordinates
pub fn Image::set_pixel(self : Image, x : Int, y : Int, color : Color) -> Unit {
  if x >= 0 && x < self.width && y >= 0 && y < self.height {
    self.pixels[y * self.width + x] = color
  }
}

///|
/// Clone the image
fn Image::clone(self : Image) -> Image {
  let pixels : Array[Color] = []
  for i in 0.. Unit {
  if radius <= 0 {
    return
  }
  // Convert to 2D array for processing
  let rows : Array[Array[Color]] = []
  for y in 0.. Unit {
  for i in 0.. Unit {
  for i in 0.. Unit {
  for i in 0.. Unit {
  for i in 0.. Unit {
  for i in 0.. Unit {
  for i in 0.. Unit {
  for i in 0.. Unit {
  for i in 0..