///|
/// # GIF 解码 GIF Decode
///
/// GIF 图像格式解码 GIF image format decoding
// 纯 MoonBit GIF 解码器
// 支持 GIF89a/GIF87a 单帧解码(RGB,channels=3)
// 规范: GIF89a Specification + src/format/gif_encode.mbt
// 单帧解码委托 decode_gif_animation_pure 取第一帧

///|
/// LZW 解码(LSB 优先变长码)
/// 返回颜色索引序列
fn lzw_decode(
  data : Array[Byte],
  min_code_size : Int32,
) -> Array[Int32] raise @types.LoadError {
  let clear_code = 1 << min_code_size
  let end_code = clear_code + 1
  let mut code_width = min_code_size + 1
  let mut dict : Array[Array[Int32]] = []
  for i in 0..= data_len {
        done = true
        break
      }
      bit_buffer = bit_buffer | (data[data_pos].to_int() << bit_count)
      bit_count += 8
      data_pos += 1
    }
    if done {
      break
    }
    let code = bit_buffer & ((1 << code_width) - 1)
    bit_buffer = bit_buffer >> code_width
    bit_count -= code_width
    if code == clear_code {
      dict = []
      for i in 0..= 0 {
        let prev_entry = dict[prev]
        let new_entry : Array[Int32] = []
        for v in prev_entry {
          new_entry.push(v)
        }
        new_entry.push(prev_entry[0])
        new_entry
      } else {
        raise @types.LoadError::DecodeFailed(
          "GIF LZW 解码错误:code 超出字典范围",
        )
      }
      for v in entry {
        output.push(v)
      }
      if prev >= 0 {
        let prev_entry = dict[prev]
        let new_entry : Array[Int32] = []
        for v in prev_entry {
          new_entry.push(v)
        }
        new_entry.push(entry[0])
        dict.push(new_entry)
        if dict.length() == 1 << code_width && code_width < 12 {
          code_width += 1
        }
      }
      prev = code
    }
  }
  output
}

///|
/// 解码 GIF 字节流为 Image
/// 支持 GIF89a/GIF87a 单帧解码(RGB,channels=3)
/// 解码 GIF 字节流为 Image(GIF89a/GIF87a 单帧解码,RGB,channels=3)不支持 interlace(packed bit6=1 时 raise LoadError),委托 decode_gif_animation_pure 取第一帧 Decode GIF byte stream to Image (GIF89a/GIF87a single-frame decoding, RGB, channels=3) Does not support interlace (raises LoadError when packed bit6=1) Delegates to decode_gif_animation_pure, takes first frame

///|
/// 参数 Parameters:
/// - `data` - GIF 字节流 GIF byte stream
///
/// 返回 Return:
/// - 解码后的 8-bit Image Decoded 8-bit Image
///
/// 错误 Errors:
/// - `LoadError::DecodeFailed` - 数据过短、GIF 签名无效、interlace 不支持、LZW 解码失败 Data too short, invalid GIF signature, interlace not supported, LZW decode failure
pub fn decode_gif_pure(data : Bytes) -> @types.Image raise @types.LoadError {
  let anim = decode_gif_animation_pure(data)
  if anim.frames.length() == 0 {
    raise @types.LoadError::DecodeFailed("GIF 无有效帧")
  }
  anim.frames[0]
}