img_gif_anim.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /* Animated GIF frames: decode up to frame N, applying disposal methods. */
  2. #include "img_internal.h"
  3. struct ga_rd {
  4. const unsigned char *d;
  5. unsigned int n, pos, blk_left;
  6. };
  7. struct ga_rect {
  8. unsigned int x, y, w, h;
  9. };
  10. struct ga_lzw {
  11. struct ga_rd *r;
  12. unsigned int bitbuf;
  13. int bitcnt;
  14. };
  15. static int ga_u8(struct ga_rd *r, unsigned int *v)
  16. {
  17. if (r->pos >= r->n)
  18. return -1;
  19. *v = r->d[r->pos++];
  20. return 0;
  21. }
  22. static int ga_subblock(struct ga_rd *r)
  23. {
  24. unsigned int len;
  25. if (r->blk_left)
  26. return 0;
  27. if (ga_u8(r, &len))
  28. return -1;
  29. r->blk_left = len;
  30. return len ? 0 : 1;
  31. }
  32. static int ga_skip_subblocks(struct ga_rd *r)
  33. {
  34. int e;
  35. do {
  36. e = ga_subblock(r);
  37. if (e < 0)
  38. return -1;
  39. if (e > 0)
  40. return 0;
  41. while (r->blk_left) {
  42. r->blk_left--;
  43. if (r->pos >= r->n)
  44. return -1;
  45. r->pos++;
  46. }
  47. } while (1);
  48. }
  49. static int ga_palette(const unsigned char *d, unsigned int n,
  50. unsigned int off, unsigned int entries,
  51. unsigned char pal[256][3])
  52. {
  53. unsigned int i;
  54. if (off > n || entries * 3u > n - off)
  55. return -1;
  56. for (i = 0; i < entries && i < 256; i++) {
  57. pal[i][0] = d[off + i * 3];
  58. pal[i][1] = d[off + i * 3 + 1];
  59. pal[i][2] = d[off + i * 3 + 2];
  60. }
  61. return 0;
  62. }
  63. static int ga_header(const unsigned char *d, unsigned int n,
  64. unsigned int *w, unsigned int *h, unsigned int *packed,
  65. unsigned int *bg, unsigned int *goff, unsigned int *gent,
  66. unsigned int *pos)
  67. {
  68. if (n < 13 || d[0] != 'G' || d[1] != 'I' || d[2] != 'F' ||
  69. d[3] != '8' || (d[4] != '7' && d[4] != '9') || d[5] != 'a')
  70. return -1;
  71. *w = img_le16(d + 6);
  72. *h = img_le16(d + 8);
  73. *packed = d[10];
  74. *bg = d[11];
  75. *gent = 0;
  76. *goff = 13;
  77. if (*packed & 0x80) {
  78. *gent = 1u << ((*packed & 7) + 1);
  79. if (*goff + *gent * 3u > n)
  80. return -1;
  81. }
  82. *pos = *goff + *gent * 3u;
  83. return (*w && *h && *w <= 32768 && *h <= 32768) ? 0 : -1;
  84. }
  85. static unsigned int ga_scan_row(unsigned int fh, unsigned int n)
  86. {
  87. static const unsigned int start[4] = {0, 4, 2, 1};
  88. static const unsigned int step[4] = {8, 8, 4, 2};
  89. unsigned int p;
  90. for (p = 0; p < 4; p++) {
  91. unsigned int count = 0;
  92. if (fh > start[p])
  93. count = (fh - start[p] + step[p] - 1) / step[p];
  94. if (n < count)
  95. return start[p] + n * step[p];
  96. n -= count;
  97. }
  98. return fh ? fh - 1 : 0;
  99. }
  100. static int ga_code(struct ga_lzw *z, int bits, int *out)
  101. {
  102. int v;
  103. while (z->bitcnt < bits) {
  104. int e = ga_subblock(z->r);
  105. unsigned int b;
  106. if (e < 0 || e > 0)
  107. return -1;
  108. if (ga_u8(z->r, &b))
  109. return -1;
  110. z->r->blk_left--;
  111. z->bitbuf |= b << z->bitcnt;
  112. z->bitcnt += 8;
  113. }
  114. v = (int)(z->bitbuf & ((1u << bits) - 1u));
  115. z->bitbuf >>= bits;
  116. z->bitcnt -= bits;
  117. *out = v;
  118. return 0;
  119. }
  120. /* Decode one frame's LZW stream into `idx` in display order. */
  121. static int ga_decode_lzw(struct ga_rd *rd, unsigned int min_code,
  122. unsigned int fw, unsigned int fh, int interlaced,
  123. unsigned char *idx, struct img_mem *m)
  124. {
  125. int *prefix;
  126. unsigned char *suffix, *stack;
  127. int clear = 1 << min_code;
  128. int end = clear + 1;
  129. int avail = end + 1;
  130. int code_size = (int)min_code + 1;
  131. int prev = -1;
  132. struct ga_lzw z;
  133. unsigned int done = 0;
  134. int rcode = -1;
  135. prefix = (int *)img_mem_alloc(m, 4096 * sizeof(int), 16);
  136. suffix = (unsigned char *)img_mem_alloc(m, 4096, 16);
  137. stack = (unsigned char *)img_mem_alloc(m, 4096, 16);
  138. if (!prefix || !suffix || !stack)
  139. return -1;
  140. {
  141. unsigned int i;
  142. for (i = 0; i < (unsigned int)clear; i++) {
  143. prefix[i] = -1;
  144. suffix[i] = (unsigned char)i;
  145. }
  146. }
  147. z.r = rd;
  148. z.bitbuf = 0;
  149. z.bitcnt = 0;
  150. for (;;) {
  151. int code, stack_len = 0, cur;
  152. unsigned int first;
  153. if (ga_code(&z, code_size, &code)) {
  154. if (done >= fw * fh) {
  155. rcode = 0;
  156. break;
  157. }
  158. goto out;
  159. }
  160. if (code == clear) {
  161. avail = end + 1;
  162. code_size = (int)min_code + 1;
  163. prev = -1;
  164. continue;
  165. }
  166. if (code == end) {
  167. rcode = 0;
  168. break;
  169. }
  170. if (prev < 0) {
  171. if (code < 0 || code >= clear)
  172. goto out;
  173. first = (unsigned int)code;
  174. if (done < fw * fh) {
  175. unsigned int fr = done / fw;
  176. unsigned int oy = interlaced ? ga_scan_row(fh, fr) : fr;
  177. idx[oy * fw + done % fw] = (unsigned char)code;
  178. done++;
  179. }
  180. prev = code;
  181. continue;
  182. }
  183. cur = code;
  184. if (code >= avail) {
  185. if (code != avail)
  186. goto out;
  187. stack[stack_len++] = (unsigned char)suffix[prev];
  188. cur = prev;
  189. }
  190. while (cur >= 0 && cur < avail && stack_len < 4096) {
  191. stack[stack_len++] = suffix[cur];
  192. cur = prefix[cur];
  193. }
  194. first = stack[stack_len - 1];
  195. if (avail < 4096) {
  196. prefix[avail] = prev;
  197. suffix[avail] = (unsigned char)first;
  198. avail++;
  199. if (avail == (1 << code_size) && code_size < 12)
  200. code_size++;
  201. }
  202. while (stack_len > 0) {
  203. unsigned int v = stack[--stack_len];
  204. unsigned int fr, oy;
  205. if (done >= fw * fh)
  206. break;
  207. fr = done / fw;
  208. oy = interlaced ? ga_scan_row(fh, fr) : fr;
  209. idx[oy * fw + done % fw] = (unsigned char)v;
  210. done++;
  211. }
  212. prev = code;
  213. if (done >= fw * fh)
  214. break;
  215. }
  216. out:
  217. rcode = (done >= fw * fh) ? 0 : -1;
  218. return rcode;
  219. }
  220. static void ga_fill(struct img_image *im, unsigned int x, unsigned int y,
  221. unsigned int w, unsigned int h, unsigned int r,
  222. unsigned int g, unsigned int b, unsigned int a)
  223. {
  224. unsigned int i, j;
  225. if (x >= im->w || y >= im->h)
  226. return;
  227. if (x + w > im->w) w = im->w - x;
  228. if (y + h > im->h) h = im->h - y;
  229. for (j = 0; j < h; j++) {
  230. for (i = 0; i < w; i++) {
  231. unsigned int o = (y + j) * im->w + x + i;
  232. im->px[o] = img_rgb565(r, g, b);
  233. if (im->alpha)
  234. im->alpha[o] = (unsigned char)a;
  235. }
  236. }
  237. }
  238. static int ga_ensure_output(struct img_image *out, unsigned int w,
  239. unsigned int h)
  240. {
  241. if (out->px)
  242. return (out->w == w && out->h == h) ? 0 : -1;
  243. return -1;
  244. }
  245. int img_gif_frames(const void *data, unsigned int len, unsigned int *frames,
  246. unsigned int *first_delay, int *has_alpha)
  247. {
  248. const unsigned char *d = (const unsigned char *)data;
  249. unsigned int w, h, packed, bg, goff, gent, pos;
  250. struct ga_rd rd;
  251. unsigned int count = 0, delay = 0;
  252. int alpha = 0;
  253. if (ga_header(d, len, &w, &h, &packed, &bg, &goff, &gent, &pos))
  254. return -1;
  255. rd.d = d;
  256. rd.n = len;
  257. rd.pos = pos;
  258. rd.blk_left = 0;
  259. (void)w;
  260. (void)h;
  261. (void)packed;
  262. (void)bg;
  263. (void)goff;
  264. (void)gent;
  265. while (rd.pos < len) {
  266. unsigned int b;
  267. if (ga_u8(&rd, &b))
  268. break;
  269. if (b == 0x3B)
  270. break;
  271. if (b == 0x21) {
  272. unsigned int label;
  273. if (ga_u8(&rd, &label))
  274. break;
  275. if (label == 0xF9) {
  276. unsigned int l, i, pk = 0, dl = 0;
  277. if (ga_u8(&rd, &l) || l < 4)
  278. break;
  279. for (i = 0; i < l; i++) {
  280. unsigned int v;
  281. if (ga_u8(&rd, &v))
  282. break;
  283. if (i == 0) pk = v;
  284. if (i == 1) dl = v;
  285. if (i == 2) dl |= v << 8;
  286. }
  287. if (pk & 1) alpha = 1;
  288. if (!count && !delay) delay = dl;
  289. if (ga_u8(&rd, &b) || b != 0)
  290. break;
  291. } else if (ga_skip_subblocks(&rd)) {
  292. break;
  293. }
  294. continue;
  295. }
  296. if (b == 0x2C) {
  297. unsigned int fw, fh, fp, lct = 0;
  298. if (rd.pos + 9 > len)
  299. break;
  300. fw = img_le16(d + rd.pos + 4);
  301. fh = img_le16(d + rd.pos + 6);
  302. fp = d[rd.pos + 8];
  303. (void)fw;
  304. (void)fh;
  305. rd.pos += 9;
  306. if (fp & 0x80) {
  307. lct = 1u << ((fp & 7) + 1);
  308. rd.pos += lct * 3;
  309. }
  310. if (rd.pos >= len)
  311. break;
  312. rd.pos++; /* min code size */
  313. if (ga_skip_subblocks(&rd))
  314. break;
  315. count++;
  316. continue;
  317. }
  318. if (b == 0x00) {
  319. while (rd.pos < len && d[rd.pos] == 0)
  320. rd.pos++;
  321. continue;
  322. }
  323. break;
  324. }
  325. if (frames) *frames = count;
  326. if (first_delay) *first_delay = delay ? delay : 10;
  327. if (has_alpha) *has_alpha = alpha;
  328. return count ? 0 : -1;
  329. }
  330. /* Decode frame `target` (0-based) into `out`. The caller may pass a
  331. * preallocated image of the logical screen size for animation. */
  332. int img_decode_gif_frame(const void *data, unsigned int len,
  333. unsigned int target, struct img_mem *m,
  334. struct img_image *out, unsigned int *delay_ms)
  335. {
  336. const unsigned char *d = (const unsigned char *)data;
  337. unsigned int w, h, packed, bg, goff, gent, pos;
  338. unsigned char gpal[256][3], lpal[256][3];
  339. unsigned int lpal_n = 0;
  340. struct ga_rd rd;
  341. struct img_image *im = out;
  342. unsigned int frame = 0;
  343. unsigned int prev_disp = 0;
  344. struct ga_rect prev_rect = {0, 0, 0, 0};
  345. unsigned int cur_disp = 0, cur_trans = 0, cur_delay = 0;
  346. int cur_has_trans = 0;
  347. unsigned char *snapshot = 0;
  348. unsigned char *idx = 0;
  349. unsigned int idx_cap = 0;
  350. unsigned int entry_mark, scratch_mark;
  351. int rcode = -1;
  352. entry_mark = img_mem_mark(m);
  353. scratch_mark = entry_mark;
  354. if (ga_header(d, len, &w, &h, &packed, &bg, &goff, &gent, &pos))
  355. return IMG_ERR_FORMAT;
  356. for (frame = 0; frame < 256; frame++)
  357. gpal[frame][0] = gpal[frame][1] = gpal[frame][2] = 0;
  358. if (gent && ga_palette(d, len, goff, gent, gpal))
  359. return IMG_ERR_CORRUPT;
  360. if (!im->px) {
  361. int r = img_alloc(m, w, h, IMG_FMT_GIF, 1, im);
  362. if (r)
  363. return r;
  364. } else if (ga_ensure_output(im, w, h)) {
  365. return IMG_ERR_CORRUPT;
  366. }
  367. scratch_mark = img_mem_mark(m);
  368. ga_fill(im, 0, 0, w, h,
  369. gent && bg < gent ? gpal[bg][0] : 0,
  370. gent && bg < gent ? gpal[bg][1] : 0,
  371. gent && bg < gent ? gpal[bg][2] : 0, 255);
  372. rd.d = d;
  373. rd.n = len;
  374. rd.pos = pos;
  375. rd.blk_left = 0;
  376. frame = 0;
  377. while (rd.pos < len) {
  378. unsigned int b;
  379. if (ga_u8(&rd, &b))
  380. break;
  381. if (b == 0x3B)
  382. break;
  383. if (b == 0x21) {
  384. unsigned int label;
  385. if (ga_u8(&rd, &label))
  386. break;
  387. if (label == 0xF9) {
  388. unsigned int l, i, pk = 0, dl = 0, tr = 0;
  389. if (ga_u8(&rd, &l) || l < 4)
  390. break;
  391. for (i = 0; i < l; i++) {
  392. unsigned int v;
  393. if (ga_u8(&rd, &v))
  394. break;
  395. if (i == 0) pk = v;
  396. if (i == 1) dl = v;
  397. if (i == 2) dl |= v << 8;
  398. if (i == 3) tr = v;
  399. }
  400. cur_has_trans = (pk & 1) != 0;
  401. cur_disp = (pk >> 2) & 7;
  402. cur_delay = dl;
  403. cur_trans = tr;
  404. if (ga_u8(&rd, &b) || b != 0)
  405. break;
  406. continue;
  407. }
  408. if (ga_skip_subblocks(&rd))
  409. break;
  410. continue;
  411. }
  412. if (b == 0x2C) {
  413. unsigned int left, top, fw, fh, fp, min_code;
  414. unsigned int i, x, y;
  415. if (rd.pos + 9 > len)
  416. break;
  417. left = img_le16(d + rd.pos);
  418. top = img_le16(d + rd.pos + 2);
  419. fw = img_le16(d + rd.pos + 4);
  420. fh = img_le16(d + rd.pos + 6);
  421. fp = d[rd.pos + 8];
  422. (void)fw;
  423. (void)fh;
  424. rd.pos += 9;
  425. if (fw == 0 || fh == 0)
  426. break;
  427. lpal_n = 0;
  428. if (fp & 0x80) {
  429. lpal_n = 1u << ((fp & 7) + 1);
  430. if (ga_palette(d, len, rd.pos, lpal_n, lpal))
  431. break;
  432. rd.pos += lpal_n * 3u;
  433. }
  434. if (rd.pos >= len)
  435. break;
  436. min_code = d[rd.pos++];
  437. if (min_code < 2 || min_code > 8)
  438. break;
  439. if (frame > 0 && prev_disp == 2) {
  440. ga_fill(im, prev_rect.x, prev_rect.y,
  441. prev_rect.w, prev_rect.h,
  442. gent && bg < gent ? gpal[bg][0] : 0,
  443. gent && bg < gent ? gpal[bg][1] : 0,
  444. gent && bg < gent ? gpal[bg][2] : 0, 255);
  445. } else if (frame > 0 && prev_disp == 3 && snapshot) {
  446. for (i = 0; i < w * h; i++) {
  447. im->px[i] = ((unsigned short *)snapshot)[i];
  448. if (im->alpha)
  449. im->alpha[i] = snapshot[w * h * 2 + i];
  450. }
  451. }
  452. if (cur_disp == 3) {
  453. if (!snapshot) {
  454. snapshot = (unsigned char *)img_mem_alloc(m,
  455. w * h * 3u, 16);
  456. if (!snapshot)
  457. goto out;
  458. }
  459. for (i = 0; i < w * h; i++) {
  460. ((unsigned short *)snapshot)[i] = im->px[i];
  461. if (im->alpha)
  462. snapshot[w * h * 2 + i] = im->alpha[i];
  463. else
  464. snapshot[w * h * 2 + i] = 255;
  465. }
  466. }
  467. if (fw * fh > idx_cap) {
  468. idx = (unsigned char *)img_mem_alloc(m, fw * fh, 16);
  469. if (!idx)
  470. goto out;
  471. idx_cap = fw * fh;
  472. }
  473. if (ga_decode_lzw(&rd, min_code, fw, fh,
  474. (fp & 0x40) != 0, idx, m))
  475. goto out;
  476. if (ga_skip_subblocks(&rd))
  477. goto out;
  478. for (y = 0; y < fh; y++) {
  479. for (x = 0; x < fw; x++) {
  480. unsigned int oi = y * fw + x;
  481. unsigned int v = idx[oi];
  482. unsigned int ox = left + x;
  483. unsigned int oy = top + y;
  484. unsigned int o;
  485. if (cur_has_trans && v == cur_trans)
  486. continue;
  487. if (ox >= im->w || oy >= im->h)
  488. continue;
  489. o = oy * im->w + ox;
  490. if (lpal_n && v >= lpal_n)
  491. v = 0;
  492. if (lpal_n) {
  493. im->px[o] = img_rgb565(lpal[v][0],
  494. lpal[v][1], lpal[v][2]);
  495. } else {
  496. im->px[o] = img_rgb565(gpal[v][0],
  497. gpal[v][1], gpal[v][2]);
  498. }
  499. if (im->alpha)
  500. im->alpha[o] = 255;
  501. }
  502. }
  503. if (frame == target) {
  504. if (delay_ms)
  505. *delay_ms = cur_delay ? cur_delay * 10u : 100u;
  506. rcode = 0;
  507. goto out;
  508. }
  509. prev_disp = cur_disp;
  510. prev_rect.x = left;
  511. prev_rect.y = top;
  512. prev_rect.w = fw;
  513. prev_rect.h = fh;
  514. frame++;
  515. cur_has_trans = 0;
  516. cur_disp = 0;
  517. continue;
  518. }
  519. if (b == 0x00) {
  520. while (rd.pos < len && d[rd.pos] == 0)
  521. rd.pos++;
  522. continue;
  523. }
  524. break;
  525. }
  526. out:
  527. img_mem_rewind(m, rcode == 0 ? scratch_mark : entry_mark);
  528. return rcode;
  529. }