image.codec\codec\gif_animation_decode.mbt
83.6% · 102 / 122 points · 344 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # GIF 动画解码 GIF Animation Decode - 3
/// - 4
/// GIF 动画帧序列解码 - 5
/// GIF 动画帧序列解码 GIF animation frame sequence decoding - 6
// 纯 MoonBit GIF 多帧动画解码(全目标可用:native/wasm/js) - 7
// 支持 GIF89a 多帧动画:Graphics Control Extension + 多 Image Descriptor - 8
// 仅依赖 @types,不依赖 @core/FFI - 9
- 10
///| - 11
/// 读取 GIF 颜色表 - 12
fn gif_read_color_table( - 13
data : Bytes, - 14
pos : Int32, - 15
size : Int32, - 16
len : Int32, - 17
) -> (Array[(Int32, Int32, Int32)], Int32) raise @types.LoadError { - 18●
if pos + size * 3 > len { - 19●
raise @types.LoadError::DecodeFailed( - 20
"GIF 数据过短:无法读取 Color Table", - 21
) - 22
} - 23
let table : Array[(Int32, Int32, Int32)] = [] - 24
let mut p = pos - 25
for _ in 0..<size { - 26●
let r = data[p].to_int() - 27●
let g = data[p + 1].to_int() - 28●
let b = data[p + 2].to_int() - 29●
table.push((r, g, b)) - 30
p += 3 - 31
} - 32
(table, p) - 33
} - 34
- 35
///| - 36
/// 读取 GIF 子块数据(收集模式) - 37
fn gif_read_sub_blocks( - 38
data : Bytes, - 39
pos : Int32, - 40
len : Int32, - 41
) -> (Array[Byte], Int32) raise @types.LoadError { - 42●
let result : Array[Byte] = [] - 43
let mut p = pos - 44
while p < len { - 45●
let sub_len = data[p].to_int() - 46
p += 1 - 47
if sub_len == 0 { - 48●
break - 49
} - 50
if p + sub_len > len { - 51●
raise @types.LoadError::DecodeFailed("GIF 子块长度越界") - 52
} - 53
for i in 0..<sub_len { - 54●
result.push(data[p + i]) - 55
} - 56
p += sub_len - 57
} - 58
(result, p) - 59
} - 60
- 61
///| - 62
/// 跳过 GIF 子块(不收集数据) - 63
fn gif_skip_sub_blocks( - 64
data : Bytes, - 65
pos : Int32, - 66
len : Int32, - 67
) -> Int32 raise @types.LoadError { - 68●
let mut p = pos - 69
while p < len { - 70●
let sub_len = data[p].to_int() - 71
p += 1 - 72
if sub_len == 0 { - 73●
break - 74
} - 75
if p + sub_len > len { - 76○
raise @types.LoadError::DecodeFailed("GIF 子块长度越界") - 77
} - 78
p += sub_len - 79
} - 80
p - 81
} - 82
- 83
///| - 84
/// 处理 GIF disposal 方法 - 85
fn gif_apply_disposal( - 86
disposal : Int32, - 87
canvas : Array[Byte], - 88
prev_canvas : Array[Byte], - 89
left : Int32, - 90
top : Int32, - 91
img_width : Int32, - 92
img_height : Int32, - 93
screen_width : Int32, - 94
screen_height : Int32, - 95
bg_r : Int32, - 96
bg_g : Int32, - 97
bg_b : Int32, - 98
) -> Unit { - 99●
match disposal { - 100
2 => - 101
// restore to background - 102○
for y in 0..<img_height { - 103○
for x in 0..<img_width { - 104○
let cx = left + x - 105
let cy = top + y - 106
if cx >= screen_width || cy >= screen_height { - 107○
continue - 108
} - 109
let canvas_off = (cy * screen_width + cx) * 3 - 110○
canvas[canvas_off] = bg_r.to_byte() - 111○
canvas[canvas_off + 1] = bg_g.to_byte() - 112○
canvas[canvas_off + 2] = bg_b.to_byte() - 113
} - 114
} - 115
3 => - 116
// restore to previous - 117○
for i in 0..<(screen_width * screen_height * 3) { - 118○
canvas[i] = prev_canvas[i] - 119
} - 120●
_ => () - 121
} - 122
} - 123
- 124
///| - 125
/// 将 GIF 帧像素写入 canvas - 126
fn gif_write_frame_pixels( - 127
indices : Array[Int32], - 128
color_table : Array[(Int32, Int32, Int32)], - 129
left : Int32, - 130
top : Int32, - 131
img_width : Int32, - 132
img_height : Int32, - 133
screen_width : Int32, - 134
screen_height : Int32, - 135
gce_transparent : Int32, - 136
canvas : Array[Byte], - 137
) -> Unit raise @types.LoadError { - 138●
for y in 0..<img_height { - 139●
for x in 0..<img_width { - 140●
let idx = indices[y * img_width + x] - 141
let cx = left + x - 142
let cy = top + y - 143
if cx >= screen_width || cy >= screen_height { - 144○
continue - 145
} - 146
if gce_transparent >= 0 && idx == gce_transparent { - 147○
continue - 148
} - 149●
if idx >= color_table.length() { - 150●
raise @types.LoadError::DecodeFailed("GIF 颜色表索引越界") - 151
} - 152
let (r, g, b) = color_table[idx] - 153
let canvas_off = (cy * screen_width + cx) * 3 - 154●
canvas[canvas_off] = r.to_byte() - 155●
canvas[canvas_off + 1] = g.to_byte() - 156●
canvas[canvas_off + 2] = b.to_byte() - 157
} - 158
} - 159
} - 160
- 161
///| - 162
/// 解码 GIF 动画为 GifAnimation(纯 MoonBit,全目标可用)。 - 163
/// 支持多帧动画、延迟时间、基本 disposal 方法。 - 164
/// 不支持 interlace。 - 165
pub fn decode_gif_animation_pure( - 166
data : Bytes, - 167
) -> @types.GifAnimation raise @types.LoadError { - 168●
let len = data.length() - 169
if len < 13 { - 170●
raise @types.LoadError::DecodeFailed( - 171
"GIF 数据过短:无法读取完整 header", - 172
) - 173
} - 174●
if data[0].to_int() != 0x47 || - 175●
data[1].to_int() != 0x49 || - 176●
data[2].to_int() != 0x46 || - 177●
data[3].to_int() != 0x38 || - 178◐
(data[4].to_int() != 0x39 && data[4].to_int() != 0x37) || - 179●
data[5].to_int() != 0x61 { - 180●
raise @types.LoadError::DecodeFailed( - 181
"GIF signature 错误,非 \"GIF87a\"/\"GIF89a\"", - 182
) - 183
} - 184●
let screen_width = read_u16_le(data, 6) - 185●
let screen_height = read_u16_le(data, 8) - 186●
@types.check_dims(screen_width, screen_height, 3) - 187●
let packed = data[10].to_int() - 188
let has_gct = (packed & 0x80) != 0 - 189
let gct_size = 1 << ((packed & 0x07) + 1) - 190●
let bg_idx = data[11].to_int() - 191
let mut pos = 13 - 192
let gct : Array[(Int32, Int32, Int32)] = if has_gct { - 193●
let (table, new_pos) = gif_read_color_table(data, pos, gct_size, len) - 194
pos = new_pos - 195
table - 196
} else { - 197●
[] - 198
} - 199●
let (bg_r, bg_g, bg_b) = if bg_idx < gct.length() { - 200●
gct[bg_idx] - 201
} else { - 202●
(0, 0, 0) - 203
} - 204
let frames : Array[@types.Image] = [] - 205
let delays : Array[Int32] = [] - 206●
let canvas : Array[Byte] = Array::make( - 207
screen_width * screen_height * 3, - 208●
bg_r.to_byte(), - 209
) - 210
for i in 0..<(screen_width * screen_height) { - 211●
canvas[i * 3] = bg_r.to_byte() - 212●
canvas[i * 3 + 1] = bg_g.to_byte() - 213●
canvas[i * 3 + 2] = bg_b.to_byte() - 214
} - 215●
let prev_canvas : Array[Byte] = Array::make( - 216
screen_width * screen_height * 3, - 217
b'\x00', - 218
) - 219
let mut gce_delay = 0 - 220
let mut gce_transparent = -1 - 221
let mut gce_disposal = 0 - 222
while pos < len { - 223●
let block_type = data[pos].to_int() - 224
pos += 1 - 225●
match block_type { - 226●
0x2C => { - 227
// Image Descriptor - 228
if pos + 9 > len { - 229○
raise @types.LoadError::DecodeFailed( - 230
"GIF 数据过短:无法读取 Image Descriptor", - 231
) - 232
} - 233●
let left = read_u16_le(data, pos) - 234●
let top = read_u16_le(data, pos + 2) - 235●
let img_width = read_u16_le(data, pos + 4) - 236●
let img_height = read_u16_le(data, pos + 6) - 237●
let img_packed = data[pos + 8].to_int() - 238
pos += 9 - 239
let has_lct = (img_packed & 0x80) != 0 - 240
let is_interlaced = (img_packed & 0x40) != 0 - 241
let lct_size_val = 1 << ((img_packed & 0x07) + 1) - 242
if is_interlaced { - 243●
raise @types.LoadError::DecodeFailed("GIF interlace 不支持") - 244
} - 245
let lct : Array[(Int32, Int32, Int32)] = if has_lct { - 246●
let (table, new_pos) = gif_read_color_table( - 247
data, pos, lct_size_val, len, - 248
) - 249
pos = new_pos - 250
table - 251
} else { - 252●
[] - 253
} - 254
if pos >= len { - 255○
raise @types.LoadError::DecodeFailed( - 256
"GIF 数据过短:无法读取 LZW min code size", - 257
) - 258
} - 259●
let lzw_min_code_size = data[pos].to_int() - 260
pos += 1 - 261
if lzw_min_code_size < 2 || lzw_min_code_size > 8 { - 262●
raise @types.LoadError::DecodeFailed( - 263
"GIF LZW minimum code size 无效", - 264
) - 265
} - 266●
let (lzw_data, new_pos) = gif_read_sub_blocks(data, pos, len) - 267
pos = new_pos - 268●
let indices = lzw_decode(lzw_data, lzw_min_code_size) - 269●
let color_table = if lct.length() > 0 { lct } else { gct } - 270●
if color_table.length() == 0 { - 271○
raise @types.LoadError::DecodeFailed("GIF 无颜色表可用") - 272
} - 273
for i in 0..<(screen_width * screen_height * 3) { - 274●
prev_canvas[i] = canvas[i] - 275
} - 276●
gif_write_frame_pixels( - 277
indices, color_table, left, top, img_width, img_height, screen_width, screen_height, - 278
gce_transparent, canvas, - 279
) - 280●
let frame_data : Array[Byte] = Array::make( - 281
screen_width * screen_height * 3, - 282
b'\x00', - 283
) - 284
for i in 0..<(screen_width * screen_height * 3) { - 285●
frame_data[i] = canvas[i] - 286
} - 287●
frames.push(@types.Image::{ - 288
width: screen_width, - 289
height: screen_height, - 290
channels: 3, - 291●
data: Bytes::from_array(frame_data), - 292
}) - 293●
delays.push(gce_delay) - 294●
gif_apply_disposal( - 295
gce_disposal, canvas, prev_canvas, left, top, img_width, img_height, screen_width, - 296
screen_height, bg_r, bg_g, bg_b, - 297
) - 298
gce_delay = 0 - 299
gce_transparent = -1 - 300
gce_disposal = 0 - 301
} - 302●
0x21 => { - 303
// Extension - 304
if pos >= len { - 305○
raise @types.LoadError::DecodeFailed( - 306
"GIF 数据过短:无法读取 Extension label", - 307
) - 308
} - 309●
let label = data[pos].to_int() - 310
pos += 1 - 311
if label == 0xF9 && pos + 6 <= len { - 312
// Graphics Control Extension - 313●
let _block_size = data[pos].to_int() - 314●
let gce_packed = data[pos + 1].to_int() - 315●
gce_delay = read_u16_le(data, pos + 2) * 10 - 316
gce_transparent = if (gce_packed & 0x01) != 0 { - 317○
data[pos + 4].to_int() - 318
} else { - 319●
-1 - 320
} - 321
gce_disposal = (gce_packed >> 2) & 0x07 - 322
pos += 6 - 323●
} else if label == 0x01 { - 324
// Plain Text Extension - 325●
if pos + 8 > len { - 326○
raise @types.LoadError::DecodeFailed( - 327
"GIF 数据过短:无法读取 Plain Text Extension header", - 328
) - 329
} - 330
pos += 8 - 331●
pos = gif_skip_sub_blocks(data, pos, len) - 332
} else { - 333●
pos = gif_skip_sub_blocks(data, pos, len) - 334
} - 335
} - 336●
0x3B => break - 337●
_ => raise @types.LoadError::DecodeFailed("GIF 未知 block type") - 338
} - 339
} - 340●
if frames.length() == 0 { - 341●
raise @types.LoadError::DecodeFailed("GIF 无 Image Separator") - 342
} - 343
@types.GifAnimation::{ frames, delays, } - 344
}