image.codec\lib\stream.mbt
96.3% · 26 / 27 points · 111 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 流式处理 lib/stream - 3
/// - 4
/// 编解码工具模块 - 5
/// 编解码工具模块 Codec utility module - 6
// 流式解码接口(纯 MoonBit) - 7
// 提供逐行/分块回调式解码接口(当前为全量解码后逐行分发,未来支持增量解码) - 8
- 9
///| - 10
/// 流式解码信息 - 11
#derive(Eq, Default, @debug.Debug) - 12
pub(all) struct StreamInfo { - 13
width : Int32 - 14
height : Int32 - 15
channels : Int32 - 16
} - 17
- 18
///| - 19
pub extend StreamInfo with Eq::{not_equal, equal} - 20
- 21
///| - 22
pub extend StreamInfo with Default::{default} - 23
- 24
///| - 25
pub extend StreamInfo with @debug.Debug::{to_repr} - 26
- 27
///| - 28
/// 流式解码:逐行回调处理。 - 29
/// on_row 回调接收 (行号, 行数据) 参数。 - 30
/// 行数据为 w*channels 个字节,按 RGB/RGBA 顺序排列。 - 31
pub fn decode_stream( - 32
data : Bytes, - 33
on_row : (Int32, Array[Byte]) -> Unit, - 34
) -> StreamInfo raise @types.LoadError { - 35●
let img = load_from_bytes_auto(data) - 36
let w = img.width - 37
let h = img.height - 38
let ch = img.channels - 39●
let row_data : Array[Byte] = Array::make(w * ch, b'\x00') - 40
for y in 0..<h { - 41●
for x in 0..<w { - 42●
for c in 0..<ch { - 43●
row_data[x * ch + c] = img.data[(y * w + x) * ch + c] - 44
} - 45
} - 46●
on_row(y, row_data) - 47
} - 48
StreamInfo::{ width: w, height: h, channels: ch, } - 49
} - 50
- 51
///| - 52
/// 分块流式解码:每 chunk_height 行回调一次。 - 53
/// on_chunk 回调接收 (起始行号, 块图像) 参数。 - 54
/// 适用于分块处理大图,减少回调开销。 - 55
pub fn decode_stream_chunked( - 56
data : Bytes, - 57
chunk_height : Int32, - 58
on_chunk : (Int32, @types.Image) -> Unit, - 59
) -> StreamInfo raise @types.LoadError { - 60●
let img = load_from_bytes_auto(data) - 61
let w = img.width - 62
let h = img.height - 63
let ch = img.channels - 64◐
let chunk_h = if chunk_height < 1 { 1 } else { chunk_height } - 65
let mut y = 0 - 66
while y < h { - 67●
let end_y = if y + chunk_h > h { h } else { y + chunk_h } - 68
let block_h = end_y - y - 69●
let block_data : Array[Byte] = Array::make(w * block_h * ch, b'\x00') - 70
for by in 0..<block_h { - 71●
for x in 0..<w { - 72●
for c in 0..<ch { - 73●
block_data[(by * w + x) * ch + c] = img.data[((y + by) * w + x) * ch + - 74
c] - 75
} - 76
} - 77
} - 78
let block_img = @types.Image::{ - 79
width: w, - 80
height: block_h, - 81
channels: ch, - 82●
data: Bytes::from_array(block_data), - 83
} - 84●
on_chunk(y, block_img) - 85
y = end_y - 86
} - 87
StreamInfo::{ width: w, height: h, channels: ch, } - 88
} - 89
- 90
///| - 91
/// 流式解码(带 req_channels):指定输出通道数。 - 92
pub fn decode_stream_channels( - 93
data : Bytes, - 94
req_channels : Int32, - 95
on_row : (Int32, Array[Byte]) -> Unit, - 96
) -> StreamInfo raise @types.LoadError { - 97●
let img = load_from_bytes_auto(data, req_channels=Some(req_channels)) - 98
let w = img.width - 99
let h = img.height - 100
let ch = img.channels - 101●
let row_data : Array[Byte] = Array::make(w * ch, b'\x00') - 102
for y in 0..<h { - 103●
for x in 0..<w { - 104●
for c in 0..<ch { - 105●
row_data[x * ch + c] = img.data[(y * w + x) * ch + c] - 106
} - 107
} - 108●
on_row(y, row_data) - 109
} - 110
StreamInfo::{ width: w, height: h, channels: ch, } - 111
}