img_png.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /* PNG: all standard colour types and bit depths, palette + tRNS, Adam7.
  2. *
  3. * The zlib stream is fed straight from consecutive IDAT chunks through
  4. * inflate.c's segmented source, so the compressed data is never copied. */
  5. #include "img_internal.h"
  6. #include "inflate.h"
  7. #ifdef IMG_HOST_DEBUG
  8. #include <stdio.h>
  9. #define DBG(...) fprintf(stderr, __VA_ARGS__)
  10. #else
  11. #define DBG(...) do { } while (0)
  12. #endif
  13. struct png_ctx {
  14. const unsigned char *d;
  15. unsigned int n;
  16. unsigned int w, h;
  17. unsigned int bit_depth, color_type, interlace;
  18. unsigned int channels;
  19. unsigned int has_trns;
  20. unsigned int trns_gray;
  21. unsigned int trns_r, trns_g, trns_b;
  22. unsigned char palette[256][3];
  23. unsigned char trns[256];
  24. unsigned int palette_entries;
  25. struct img_image *out;
  26. struct img_mem *mem;
  27. struct inflate *z;
  28. unsigned char *prev, *cur;
  29. unsigned int max_rowbytes;
  30. unsigned int filter_bpp;
  31. unsigned int pass;
  32. unsigned int pass_w, pass_h, pass_x0, pass_y0, pass_dx, pass_dy;
  33. unsigned int pass_row;
  34. unsigned int rowbytes;
  35. unsigned int rowpos;
  36. unsigned char *rowbuf;
  37. unsigned int finished;
  38. int err;
  39. };
  40. static const unsigned char pass_x0[7] = {0, 4, 0, 2, 0, 1, 0};
  41. static const unsigned char pass_y0[7] = {0, 0, 4, 0, 2, 0, 1};
  42. static const unsigned char pass_dx[7] = {8, 8, 4, 4, 2, 2, 1};
  43. static const unsigned char pass_dy[7] = {8, 8, 8, 4, 4, 2, 2};
  44. static int png_chunk_crc(const unsigned char *p, unsigned int len)
  45. {
  46. unsigned int want = img_be32(p + 8 + len);
  47. unsigned int got = crc32_bytes(p + 4, len + 4);
  48. return want == got;
  49. }
  50. static void png_setup_pass(struct png_ctx *c)
  51. {
  52. unsigned int pw, ph;
  53. if (c->interlace == 0) {
  54. unsigned int i;
  55. if (c->pass != 0) {
  56. c->finished = 1;
  57. return;
  58. }
  59. c->pass_w = c->w;
  60. c->pass_h = c->h;
  61. c->pass_x0 = 0;
  62. c->pass_y0 = 0;
  63. c->pass_dx = 1;
  64. c->pass_dy = 1;
  65. c->rowbytes = (c->w * c->channels * c->bit_depth + 7) / 8;
  66. c->pass_row = 0;
  67. c->rowpos = 0;
  68. for (i = 0; i < c->max_rowbytes; i++)
  69. c->prev[i] = 0;
  70. return;
  71. }
  72. while (c->pass < 7) {
  73. unsigned int x0 = pass_x0[c->pass], y0 = pass_y0[c->pass];
  74. unsigned int dx = pass_dx[c->pass], dy = pass_dy[c->pass];
  75. pw = c->w > x0 ? (c->w - x0 + dx - 1) / dx : 0;
  76. ph = c->h > y0 ? (c->h - y0 + dy - 1) / dy : 0;
  77. if (pw && ph) {
  78. c->pass_w = pw;
  79. c->pass_h = ph;
  80. c->pass_x0 = x0;
  81. c->pass_y0 = y0;
  82. c->pass_dx = dx;
  83. c->pass_dy = dy;
  84. c->rowbytes = (pw * c->channels * c->bit_depth + 7) / 8;
  85. c->pass_row = 0;
  86. c->rowpos = 0;
  87. {
  88. unsigned int i;
  89. for (i = 0; i < c->max_rowbytes; i++)
  90. c->prev[i] = 0;
  91. }
  92. return;
  93. }
  94. c->pass++;
  95. }
  96. c->finished = 1;
  97. }
  98. static unsigned int png_chan(const unsigned char *row, unsigned int px,
  99. unsigned int ch_index, unsigned int nch,
  100. unsigned int bd)
  101. {
  102. unsigned int bits = nch * bd;
  103. unsigned int off = px * bits + ch_index * bd;
  104. if (bd == 8)
  105. return row[off >> 3];
  106. if (bd == 16)
  107. return ((unsigned int)row[off >> 3] << 8) | row[(off >> 3) + 1];
  108. {
  109. unsigned int byte = off >> 3;
  110. unsigned int shift = 8 - bd - (off & 7);
  111. return (row[byte] >> shift) & ((1u << bd) - 1u);
  112. }
  113. }
  114. static unsigned int png_scale(unsigned int v, unsigned int bd)
  115. {
  116. if (bd == 8)
  117. return v;
  118. if (bd == 16)
  119. return v >> 8;
  120. return (v * 255u) / ((1u << bd) - 1u);
  121. }
  122. static int png_paeth(int a, int b, int c)
  123. {
  124. int p = a + b - c;
  125. int pa = p > a ? p - a : a - p;
  126. int pb = p > b ? p - b : b - p;
  127. int pc = p > c ? p - c : c - p;
  128. if (pa <= pb && pa <= pc)
  129. return a;
  130. if (pb <= pc)
  131. return b;
  132. return c;
  133. }
  134. static int png_row_done(struct png_ctx *c)
  135. {
  136. unsigned int filter = c->rowbuf[0];
  137. unsigned int i;
  138. const unsigned char *src = c->rowbuf + 1;
  139. unsigned char *dst = c->cur;
  140. if (filter > 4) {
  141. DBG("png: bad filter %u\n", filter);
  142. return -1;
  143. }
  144. for (i = 0; i < c->rowbytes; i++) {
  145. int a = i >= c->filter_bpp ? dst[i - c->filter_bpp] : 0;
  146. int b = c->prev[i];
  147. int cc = i >= c->filter_bpp ? c->prev[i - c->filter_bpp] : 0;
  148. int x = src[i];
  149. switch (filter) {
  150. case 0: break;
  151. case 1: x += a; break;
  152. case 2: x += b; break;
  153. case 3: x += (a + b) >> 1; break;
  154. case 4: x += png_paeth(a, b, cc); break;
  155. }
  156. dst[i] = (unsigned char)x;
  157. }
  158. /* Map this pass row into the output image. */
  159. {
  160. unsigned int px;
  161. unsigned int oy = c->pass_y0 + c->pass_row * c->pass_dy;
  162. for (px = 0; px < c->pass_w; px++) {
  163. unsigned int ox = c->pass_x0 + px * c->pass_dx;
  164. unsigned int oi = oy * c->w + ox;
  165. unsigned int r = 0, g = 0, b = 0, a = 255;
  166. unsigned int bd = c->bit_depth;
  167. if (c->color_type == 0) {
  168. unsigned int raw = png_chan(dst, px, 0, 1, bd);
  169. unsigned int gray = png_scale(raw, bd);
  170. r = g = b = gray;
  171. if (c->has_trns && raw == c->trns_gray)
  172. a = 0;
  173. } else if (c->color_type == 2) {
  174. unsigned int rr = png_chan(dst, px, 0, 3, bd);
  175. unsigned int gg = png_chan(dst, px, 1, 3, bd);
  176. unsigned int bb = png_chan(dst, px, 2, 3, bd);
  177. r = png_scale(rr, bd);
  178. g = png_scale(gg, bd);
  179. b = png_scale(bb, bd);
  180. if (c->has_trns && rr == c->trns_r &&
  181. gg == c->trns_g && bb == c->trns_b)
  182. a = 0;
  183. } else if (c->color_type == 3) {
  184. unsigned int idx = png_chan(dst, px, 0, 1, bd);
  185. if (idx >= c->palette_entries) {
  186. DBG("png: palette idx %u >= %u\n", idx,
  187. c->palette_entries);
  188. return -1;
  189. }
  190. r = c->palette[idx][0];
  191. g = c->palette[idx][1];
  192. b = c->palette[idx][2];
  193. if (c->has_trns)
  194. a = c->trns[idx];
  195. } else if (c->color_type == 4) {
  196. unsigned int raw = png_chan(dst, px, 0, 2, bd);
  197. unsigned int ar = png_chan(dst, px, 1, 2, bd);
  198. r = g = b = png_scale(raw, bd);
  199. a = png_scale(ar, bd);
  200. } else if (c->color_type == 6) {
  201. unsigned int rr = png_chan(dst, px, 0, 4, bd);
  202. unsigned int gg = png_chan(dst, px, 1, 4, bd);
  203. unsigned int bb = png_chan(dst, px, 2, 4, bd);
  204. unsigned int aa = png_chan(dst, px, 3, 4, bd);
  205. r = png_scale(rr, bd);
  206. g = png_scale(gg, bd);
  207. b = png_scale(bb, bd);
  208. a = png_scale(aa, bd);
  209. }
  210. c->out->px[oi] = img_rgb565(r, g, b);
  211. if (c->out->alpha)
  212. c->out->alpha[oi] = (unsigned char)a;
  213. }
  214. }
  215. {
  216. unsigned char *tmp = c->prev;
  217. c->prev = c->cur;
  218. c->cur = tmp;
  219. }
  220. c->pass_row++;
  221. if (c->pass_row >= c->pass_h) {
  222. c->pass++;
  223. png_setup_pass(c);
  224. }
  225. return 0;
  226. }
  227. static int png_sink_write(void *ctx, const unsigned char *data, unsigned int len)
  228. {
  229. struct png_ctx *c = (struct png_ctx *)ctx;
  230. while (len--) {
  231. if (c->finished)
  232. return -1;
  233. c->rowbuf[c->rowpos++] = *data++;
  234. if (c->rowpos == c->rowbytes + 1) {
  235. c->rowpos = 0;
  236. if (png_row_done(c))
  237. return -1;
  238. }
  239. }
  240. return 0;
  241. }
  242. /* IDAT walker: serves consecutive IDAT payloads and verifies their CRCs. */
  243. struct png_idat {
  244. const unsigned char *p;
  245. const unsigned char *end;
  246. const unsigned char *cur;
  247. unsigned int cur_len;
  248. int err;
  249. };
  250. static int png_idat_more(void *ctx, const unsigned char **out, unsigned int *len)
  251. {
  252. struct png_idat *c = (struct png_idat *)ctx;
  253. while (c->cur_len == 0) {
  254. unsigned int l;
  255. if (c->p + 12 > c->end) {
  256. DBG("png: idat: no more header\n");
  257. return 0;
  258. }
  259. l = img_be32(c->p);
  260. if (l > (unsigned int)(c->end - (c->p + 12))) {
  261. DBG("png: idat: bad len %u\n", l);
  262. return 0;
  263. }
  264. if (c->p[4] != 'I' || c->p[5] != 'D' || c->p[6] != 'A' ||
  265. c->p[7] != 'T') {
  266. DBG("png: idat: next type %c%c%c%c len %u\n",
  267. c->p[4], c->p[5], c->p[6], c->p[7], l);
  268. return 0;
  269. }
  270. if (!png_chunk_crc(c->p, l)) {
  271. DBG("png: idat: bad crc len %u\n", l);
  272. c->err = 1;
  273. return 0;
  274. }
  275. DBG("png: idat chunk len %u at %u\n", l,
  276. (unsigned)(c->p - c->end));
  277. c->cur = c->p + 8;
  278. c->cur_len = l;
  279. c->p += 12 + l;
  280. }
  281. *out = c->cur;
  282. *len = c->cur_len;
  283. c->cur_len = 0;
  284. return 1;
  285. }
  286. int img_sniff_png(const unsigned char *d, unsigned int n, struct img_info *out)
  287. {
  288. unsigned int pos = 8;
  289. int trns = 0;
  290. if (n < 33)
  291. return IMG_FMT_UNKNOWN;
  292. if (img_be32(d + 8) != 13 || d[12] != 'I' || d[13] != 'H' ||
  293. d[14] != 'D' || d[15] != 'R')
  294. return IMG_FMT_UNKNOWN;
  295. out->format = IMG_FMT_PNG;
  296. out->width = img_be32(d + 16);
  297. out->height = img_be32(d + 20);
  298. {
  299. unsigned int ct = d[25];
  300. if (ct == 4 || ct == 6)
  301. trns = 1;
  302. }
  303. /* Find tRNS before the image data (small header scan). */
  304. while (pos + 12 <= n) {
  305. unsigned int l = img_be32(d + pos);
  306. if (l > n - pos - 12)
  307. break;
  308. if (d[pos + 4] == 't' && d[pos + 5] == 'R' &&
  309. d[pos + 6] == 'N' && d[pos + 7] == 'S')
  310. trns = 1;
  311. if (d[pos + 4] == 'I' && d[pos + 5] == 'D' &&
  312. d[pos + 6] == 'A' && d[pos + 7] == 'T')
  313. break;
  314. if (d[pos + 4] == 'I' && d[pos + 5] == 'E' &&
  315. d[pos + 6] == 'N' && d[pos + 7] == 'D')
  316. break;
  317. pos += 12 + l;
  318. }
  319. out->has_alpha = trns;
  320. return IMG_FMT_PNG;
  321. }
  322. int img_decode_png(const unsigned char *d, unsigned int n,
  323. const struct img_options *opt, struct img_mem *m,
  324. struct img_image *out)
  325. {
  326. struct png_ctx c;
  327. struct png_idat idat;
  328. unsigned int pos, mark, image_end, zsize;
  329. unsigned int i;
  330. int have_plte = 0, have_idat = 0;
  331. int rcode;
  332. mark = img_mem_mark(m);
  333. for (i = 0; i < sizeof(c); i++)
  334. ((unsigned char *)&c)[i] = 0;
  335. if (n < 33 || img_be32(d + 8) != 13 || d[12] != 'I' || d[13] != 'H' ||
  336. d[14] != 'D' || d[15] != 'R' || !png_chunk_crc(d + 8, 13))
  337. return IMG_ERR_FORMAT;
  338. c.d = d;
  339. c.n = n;
  340. c.w = img_be32(d + 16);
  341. c.h = img_be32(d + 20);
  342. c.bit_depth = d[24];
  343. c.color_type = d[25];
  344. c.interlace = d[28];
  345. if (!img_size_ok(c.w, c.h, opt))
  346. return IMG_ERR_TOOBIG;
  347. if (d[26] != 0 || d[27] != 0 || c.interlace > 1)
  348. return IMG_ERR_UNSUPPORTED;
  349. if (c.color_type == 0) {
  350. if (c.bit_depth != 1 && c.bit_depth != 2 && c.bit_depth != 4 &&
  351. c.bit_depth != 8 && c.bit_depth != 16)
  352. return IMG_ERR_UNSUPPORTED;
  353. c.channels = 1;
  354. } else if (c.color_type == 2) {
  355. if (c.bit_depth != 8 && c.bit_depth != 16)
  356. return IMG_ERR_UNSUPPORTED;
  357. c.channels = 3;
  358. } else if (c.color_type == 3) {
  359. if (c.bit_depth != 1 && c.bit_depth != 2 && c.bit_depth != 4 &&
  360. c.bit_depth != 8)
  361. return IMG_ERR_UNSUPPORTED;
  362. c.channels = 1;
  363. } else if (c.color_type == 4) {
  364. if (c.bit_depth != 8 && c.bit_depth != 16)
  365. return IMG_ERR_UNSUPPORTED;
  366. c.channels = 2;
  367. } else if (c.color_type == 6) {
  368. if (c.bit_depth != 8 && c.bit_depth != 16)
  369. return IMG_ERR_UNSUPPORTED;
  370. c.channels = 4;
  371. } else {
  372. return IMG_ERR_UNSUPPORTED;
  373. }
  374. for (i = 0; i < 256; i++)
  375. c.trns[i] = 255;
  376. /* Pre-IDAT chunks. */
  377. pos = 33;
  378. while (pos + 12 <= n) {
  379. unsigned int l = img_be32(d + pos);
  380. const unsigned char *type = d + pos + 4;
  381. const unsigned char *data = d + pos + 8;
  382. if (l > n - pos - 12)
  383. return IMG_ERR_CORRUPT;
  384. if (type[0] == 'I' && type[1] == 'D' && type[2] == 'A' &&
  385. type[3] == 'T') {
  386. have_idat = 1;
  387. break;
  388. }
  389. if (!png_chunk_crc(d + pos, l))
  390. return IMG_ERR_CORRUPT;
  391. if (type[0] == 'P' && type[1] == 'L' && type[2] == 'T' &&
  392. type[3] == 'E') {
  393. if (l == 0 || l % 3 != 0 || l > 768)
  394. return IMG_ERR_CORRUPT;
  395. c.palette_entries = l / 3;
  396. for (i = 0; i < c.palette_entries; i++) {
  397. c.palette[i][0] = data[i * 3];
  398. c.palette[i][1] = data[i * 3 + 1];
  399. c.palette[i][2] = data[i * 3 + 2];
  400. }
  401. have_plte = 1;
  402. } else if (type[0] == 't' && type[1] == 'R' && type[2] == 'N' &&
  403. type[3] == 'S') {
  404. c.has_trns = 1;
  405. if (c.color_type == 3) {
  406. if (l > c.palette_entries)
  407. return IMG_ERR_CORRUPT;
  408. for (i = 0; i < l; i++)
  409. c.trns[i] = data[i];
  410. } else if (c.color_type == 0) {
  411. if (l != 2)
  412. return IMG_ERR_CORRUPT;
  413. c.trns_gray = img_be16(data);
  414. } else if (c.color_type == 2) {
  415. if (l != 6)
  416. return IMG_ERR_CORRUPT;
  417. c.trns_r = img_be16(data);
  418. c.trns_g = img_be16(data + 2);
  419. c.trns_b = img_be16(data + 4);
  420. } else {
  421. return IMG_ERR_CORRUPT;
  422. }
  423. } else if (!(type[0] & 0x20)) {
  424. return IMG_ERR_UNSUPPORTED; /* unknown critical chunk */
  425. }
  426. pos += 12 + l;
  427. }
  428. if (!have_idat)
  429. return IMG_ERR_CORRUPT;
  430. if (c.color_type == 3 && !have_plte)
  431. return IMG_ERR_CORRUPT;
  432. if (c.color_type == 3 && c.palette_entries == 0)
  433. return IMG_ERR_CORRUPT;
  434. rcode = img_alloc(m, c.w, c.h, IMG_FMT_PNG,
  435. (c.color_type == 4 || c.color_type == 6 ||
  436. c.has_trns) != 0, out);
  437. if (rcode)
  438. return rcode;
  439. image_end = img_mem_mark(m);
  440. c.out = out;
  441. c.mem = m;
  442. c.max_rowbytes = (c.w * c.channels * c.bit_depth + 7) / 8;
  443. c.filter_bpp = (c.channels * c.bit_depth + 7) / 8;
  444. if (c.filter_bpp < 1)
  445. c.filter_bpp = 1;
  446. c.rowbuf = (unsigned char *)img_mem_alloc(m, c.max_rowbytes + 1, 1);
  447. c.prev = (unsigned char *)img_mem_alloc(m, c.max_rowbytes, 1);
  448. c.cur = (unsigned char *)img_mem_alloc(m, c.max_rowbytes, 1);
  449. zsize = sizeof(struct inflate);
  450. c.z = (struct inflate *)img_mem_alloc(m, zsize, 16);
  451. if (!c.rowbuf || !c.prev || !c.cur || !c.z) {
  452. img_mem_rewind(m, mark);
  453. return IMG_ERR_NOMEM;
  454. }
  455. {
  456. unsigned int k;
  457. for (k = 0; k < c.max_rowbytes; k++)
  458. c.prev[k] = 0;
  459. }
  460. c.pass = 0;
  461. png_setup_pass(&c);
  462. /* Feed the IDAT stream. The first chunk is already located at pos. */
  463. idat.p = d + pos;
  464. idat.end = d + n;
  465. idat.cur = 0;
  466. idat.cur_len = 0;
  467. idat.err = 0;
  468. /* The first IDAT chunk begins at pos; prime it. */
  469. if (idat.p + 12 > idat.end)
  470. goto png_fail;
  471. {
  472. unsigned int l = img_be32(idat.p);
  473. if (l > (unsigned int)(idat.end - (idat.p + 12)))
  474. goto png_fail;
  475. if (idat.p[4] != 'I' || idat.p[5] != 'D' || idat.p[6] != 'A' ||
  476. idat.p[7] != 'T')
  477. goto png_fail;
  478. if (!png_chunk_crc(idat.p, l))
  479. goto png_fail;
  480. idat.cur = idat.p + 8;
  481. idat.cur_len = l;
  482. idat.p += 12 + l;
  483. }
  484. /* The first segment is already handed over; subsequent IDAT chunks go
  485. * through png_idat_more(). */
  486. {
  487. struct inflate_src src;
  488. struct inflate_sink sink;
  489. src.p = idat.cur;
  490. src.len = idat.cur_len;
  491. idat.cur_len = 0; /* walker moves on when this is consumed */
  492. src.more = png_idat_more;
  493. src.ctx = &idat;
  494. sink.write = png_sink_write;
  495. sink.ctx = &c;
  496. inflate_init(c.z, &src, &sink);
  497. }
  498. DBG("png: inflate start at %u first=%u\n", (unsigned)(idat.cur - d),
  499. (unsigned)(idat.p - d));
  500. if (inflate_zlib(c.z, 0) != 0) {
  501. DBG("png: inflate failed total=%u finished=%u rowpos=%u pass=%u row=%u/%u err=%d\n",
  502. (unsigned)c.z->total, c.finished, c.rowpos, c.pass,
  503. c.pass_row, c.pass_h, idat.err);
  504. img_mem_rewind(m, mark);
  505. return idat.err ? IMG_ERR_CORRUPT : IMG_ERR_CORRUPT;
  506. }
  507. DBG("png: inflate ok total=%u finished=%u rowpos=%u pass=%u\n",
  508. (unsigned)c.z->total, c.finished, c.rowpos, c.pass);
  509. pos = (unsigned int)(idat.p - d);
  510. /* Expect IEND (ignore ancillary chunks). */
  511. while (pos + 12 <= n) {
  512. unsigned int l = img_be32(d + pos);
  513. const unsigned char *type = d + pos + 4;
  514. if (l > n - pos - 12)
  515. goto png_fail;
  516. if (!png_chunk_crc(d + pos, l))
  517. goto png_fail;
  518. if (type[0] == 'I' && type[1] == 'E' && type[2] == 'N' &&
  519. type[3] == 'D') {
  520. pos += 12 + l;
  521. break;
  522. }
  523. pos += 12 + l;
  524. }
  525. if (!c.finished || c.rowpos != 0)
  526. goto png_fail;
  527. img_mem_rewind(m, image_end);
  528. return 0;
  529. png_fail:
  530. img_mem_rewind(m, mark);
  531. return IMG_ERR_CORRUPT;
  532. }