image.codec\codec\gif_animation_decode.mbt

83.6% · 102 / 122 points · 344 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

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