///|
/// # 流式处理 lib/stream
///
/// 编解码工具模块
/// 编解码工具模块 Codec utility module
// 流式解码接口(纯 MoonBit)
// 提供逐行/分块回调式解码接口(当前为全量解码后逐行分发,未来支持增量解码)

///|
/// 流式解码信息
#derive(Eq, Default, @debug.Debug)
pub(all) struct StreamInfo {
  width : Int32
  height : Int32
  channels : Int32
}

///|
pub extend StreamInfo with Eq::{not_equal, equal}

///|
pub extend StreamInfo with Default::{default}

///|
pub extend StreamInfo with @debug.Debug::{to_repr}

///|
/// 流式解码:逐行回调处理。
/// on_row 回调接收 (行号, 行数据) 参数。
/// 行数据为 w*channels 个字节,按 RGB/RGBA 顺序排列。
pub fn decode_stream(
  data : Bytes,
  on_row : (Int32, Array[Byte]) -> Unit,
) -> StreamInfo raise @types.LoadError {
  let img = load_from_bytes_auto(data)
  let w = img.width
  let h = img.height
  let ch = img.channels
  let row_data : Array[Byte] = Array::make(w * ch, b'\x00')
  for y in 0.. Unit,
) -> StreamInfo raise @types.LoadError {
  let img = load_from_bytes_auto(data)
  let w = img.width
  let h = img.height
  let ch = img.channels
  let chunk_h = if chunk_height < 1 { 1 } else { chunk_height }
  let mut y = 0
  while y < h {
    let end_y = if y + chunk_h > h { h } else { y + chunk_h }
    let block_h = end_y - y
    let block_data : Array[Byte] = Array::make(w * block_h * ch, b'\x00')
    for by in 0.. Unit,
) -> StreamInfo raise @types.LoadError {
  let img = load_from_bytes_auto(data, req_channels=Some(req_channels))
  let w = img.width
  let h = img.height
  let ch = img.channels
  let row_data : Array[Byte] = Array::make(w * ch, b'\x00')
  for y in 0..