load-png.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. // Copyright (C) 2004-2021 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include "pixmap-imp.h"
  24. #include "z-imp.h"
  25. #include <limits.h>
  26. #include <string.h>
  27. struct info
  28. {
  29. unsigned int width, height, depth, n;
  30. enum fz_colorspace_type type;
  31. int interlace, indexed;
  32. size_t size;
  33. unsigned char *samples;
  34. unsigned char palette[256*4];
  35. int transparency;
  36. int trns[3];
  37. int xres, yres;
  38. fz_colorspace *cs;
  39. };
  40. static inline unsigned int getuint(const unsigned char *p)
  41. {
  42. return p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3];
  43. }
  44. static inline int getcomp(const unsigned char *line, int x, int bpc)
  45. {
  46. switch (bpc)
  47. {
  48. case 1: return (line[x >> 3] >> ( 7 - (x & 7) ) ) & 1;
  49. case 2: return (line[x >> 2] >> ( ( 3 - (x & 3) ) << 1 ) ) & 3;
  50. case 4: return (line[x >> 1] >> ( ( 1 - (x & 1) ) << 2 ) ) & 15;
  51. case 8: return line[x];
  52. case 16: return line[x << 1] << 8 | line[(x << 1) + 1];
  53. }
  54. return 0;
  55. }
  56. static inline void putcomp(unsigned char *line, int x, int bpc, int value)
  57. {
  58. int maxval = (1 << bpc) - 1;
  59. switch (bpc)
  60. {
  61. case 1: line[x >> 3] &= ~(maxval << (7 - (x & 7))); break;
  62. case 2: line[x >> 2] &= ~(maxval << ((3 - (x & 3)) << 1)); break;
  63. case 4: line[x >> 1] &= ~(maxval << ((1 - (x & 1)) << 2)); break;
  64. }
  65. switch (bpc)
  66. {
  67. case 1: line[x >> 3] |= value << (7 - (x & 7)); break;
  68. case 2: line[x >> 2] |= value << ((3 - (x & 3)) << 1); break;
  69. case 4: line[x >> 1] |= value << ((1 - (x & 1)) << 2); break;
  70. case 8: line[x] = value; break;
  71. case 16: line[x << 1] = value >> 8; line[(x << 1) + 1] = value & 0xFF; break;
  72. }
  73. }
  74. static const unsigned char png_signature[8] =
  75. {
  76. 137, 80, 78, 71, 13, 10, 26, 10
  77. };
  78. static inline int paeth(int a, int b, int c)
  79. {
  80. /* The definitions of ac and bc are correct, not a typo. */
  81. int ac = b - c, bc = a - c, abcc = ac + bc;
  82. int pa = (ac < 0 ? -ac : ac);
  83. int pb = (bc < 0 ? -bc : bc);
  84. int pc = (abcc < 0 ? -abcc : abcc);
  85. return pa <= pb && pa <= pc ? a : pb <= pc ? b : c;
  86. }
  87. static void
  88. png_predict(unsigned char *samples, unsigned int width, unsigned int height, unsigned int n, unsigned int depth)
  89. {
  90. unsigned int stride = (width * n * depth + 7) / 8;
  91. unsigned int bpp = (n * depth + 7) / 8;
  92. unsigned int i, row;
  93. for (row = 0; row < height; row ++)
  94. {
  95. unsigned char *src = samples + (unsigned int)((stride + 1) * row);
  96. unsigned char *dst = samples + (unsigned int)(stride * row);
  97. unsigned char *a = dst;
  98. unsigned char *b = dst - stride;
  99. unsigned char *c = dst - stride;
  100. switch (*src++)
  101. {
  102. default:
  103. case 0: /* None */
  104. for (i = 0; i < stride; i++)
  105. *dst++ = *src++;
  106. break;
  107. case 1: /* Sub */
  108. for (i = 0; i < bpp; i++)
  109. *dst++ = *src++;
  110. for (i = bpp; i < stride; i++)
  111. *dst++ = *src++ + *a++;
  112. break;
  113. case 2: /* Up */
  114. if (row == 0)
  115. for (i = 0; i < stride; i++)
  116. *dst++ = *src++;
  117. else
  118. for (i = 0; i < stride; i++)
  119. *dst++ = *src++ + *b++;
  120. break;
  121. case 3: /* Average */
  122. if (row == 0)
  123. {
  124. for (i = 0; i < bpp; i++)
  125. *dst++ = *src++;
  126. for (i = bpp; i < stride; i++)
  127. *dst++ = *src++ + (*a++ >> 1);
  128. }
  129. else
  130. {
  131. for (i = 0; i < bpp; i++)
  132. *dst++ = *src++ + (*b++ >> 1);
  133. for (i = bpp; i < stride; i++)
  134. *dst++ = *src++ + ((*b++ + *a++) >> 1);
  135. }
  136. break;
  137. case 4: /* Paeth */
  138. if (row == 0)
  139. {
  140. for (i = 0; i < bpp; i++)
  141. *dst++ = *src++ + paeth(0, 0, 0);
  142. for (i = bpp; i < stride; i++)
  143. *dst++ = *src++ + paeth(*a++, 0, 0);
  144. }
  145. else
  146. {
  147. for (i = 0; i < bpp; i++)
  148. *dst++ = *src++ + paeth(0, *b++, 0);
  149. for (i = bpp; i < stride; i++)
  150. *dst++ = *src++ + paeth(*a++, *b++, *c++);
  151. }
  152. break;
  153. }
  154. }
  155. }
  156. static const unsigned int adam7_ix[7] = { 0, 4, 0, 2, 0, 1, 0 };
  157. static const unsigned int adam7_dx[7] = { 8, 8, 4, 4, 2, 2, 1 };
  158. static const unsigned int adam7_iy[7] = { 0, 0, 4, 0, 2, 0, 1 };
  159. static const unsigned int adam7_dy[7] = { 8, 8, 8, 4, 4, 2, 2 };
  160. static void
  161. png_deinterlace_passes(fz_context *ctx, struct info *info, unsigned int *w, unsigned int *h, unsigned int *ofs)
  162. {
  163. int p, bpp = info->depth * info->n;
  164. ofs[0] = 0;
  165. for (p = 0; p < 7; p++)
  166. {
  167. w[p] = (info->width + adam7_dx[p] - adam7_ix[p] - 1) / adam7_dx[p];
  168. h[p] = (info->height + adam7_dy[p] - adam7_iy[p] - 1) / adam7_dy[p];
  169. if (w[p] == 0) h[p] = 0;
  170. if (h[p] == 0) w[p] = 0;
  171. if (w[p] && h[p])
  172. ofs[p + 1] = ofs[p] + h[p] * (1 + (w[p] * bpp + 7) / 8);
  173. else
  174. ofs[p + 1] = ofs[p];
  175. }
  176. }
  177. static void
  178. png_deinterlace(fz_context *ctx, struct info *info, unsigned int *passw, unsigned int *passh, unsigned int *passofs)
  179. {
  180. unsigned int n = info->n;
  181. unsigned int depth = info->depth;
  182. size_t stride = ((size_t)info->width * n * depth + 7) / 8;
  183. unsigned char *output;
  184. unsigned int p, x, y, k;
  185. if (info->height > UINT_MAX / stride)
  186. fz_throw(ctx, FZ_ERROR_LIMIT, "image too large");
  187. output = Memento_label(fz_malloc(ctx, info->height * stride), "png_deinterlace");
  188. for (p = 0; p < 7; p++)
  189. {
  190. unsigned char *sp = info->samples + (passofs[p]);
  191. unsigned int w = passw[p];
  192. unsigned int h = passh[p];
  193. png_predict(sp, w, h, n, depth);
  194. for (y = 0; y < h; y++)
  195. {
  196. for (x = 0; x < w; x++)
  197. {
  198. int outx = x * adam7_dx[p] + adam7_ix[p];
  199. int outy = y * adam7_dy[p] + adam7_iy[p];
  200. unsigned char *dp = output + outy * stride;
  201. for (k = 0; k < n; k++)
  202. {
  203. int v = getcomp(sp, x * n + k, depth);
  204. putcomp(dp, outx * n + k, depth, v);
  205. }
  206. }
  207. sp += (w * depth * n + 7) / 8;
  208. }
  209. }
  210. fz_free(ctx, info->samples);
  211. info->samples = output;
  212. }
  213. static void
  214. png_read_ihdr(fz_context *ctx, struct info *info, const unsigned char *p, unsigned int size)
  215. {
  216. int color, compression, filter;
  217. if (size != 13)
  218. fz_throw(ctx, FZ_ERROR_FORMAT, "IHDR chunk is the wrong size");
  219. info->width = getuint(p + 0);
  220. info->height = getuint(p + 4);
  221. info->depth = p[8];
  222. color = p[9];
  223. compression = p[10];
  224. filter = p[11];
  225. info->interlace = p[12];
  226. if (info->width <= 0)
  227. fz_throw(ctx, FZ_ERROR_FORMAT, "image width must be > 0");
  228. if (info->height <= 0)
  229. fz_throw(ctx, FZ_ERROR_FORMAT, "image height must be > 0");
  230. if (info->depth != 1 && info->depth != 2 && info->depth != 4 &&
  231. info->depth != 8 && info->depth != 16)
  232. fz_throw(ctx, FZ_ERROR_FORMAT, "image bit depth must be one of 1, 2, 4, 8, 16");
  233. if (color == 2 && info->depth < 8)
  234. fz_throw(ctx, FZ_ERROR_FORMAT, "illegal bit depth for truecolor");
  235. if (color == 3 && info->depth > 8)
  236. fz_throw(ctx, FZ_ERROR_FORMAT, "illegal bit depth for indexed");
  237. if (color == 4 && info->depth < 8)
  238. fz_throw(ctx, FZ_ERROR_FORMAT, "illegal bit depth for grayscale with alpha");
  239. if (color == 6 && info->depth < 8)
  240. fz_throw(ctx, FZ_ERROR_FORMAT, "illegal bit depth for truecolor with alpha");
  241. info->indexed = 0;
  242. if (color == 0) /* gray */
  243. info->n = 1, info->type = FZ_COLORSPACE_GRAY;
  244. else if (color == 2) /* rgb */
  245. info->n = 3, info->type = FZ_COLORSPACE_RGB;
  246. else if (color == 4) /* gray alpha */
  247. info->n = 2, info->type = FZ_COLORSPACE_GRAY;
  248. else if (color == 6) /* rgb alpha */
  249. info->n = 4, info->type = FZ_COLORSPACE_RGB;
  250. else if (color == 3) /* indexed */
  251. {
  252. info->type = FZ_COLORSPACE_RGB; /* after colorspace expansion it will be */
  253. info->indexed = 1;
  254. info->n = 1;
  255. }
  256. else
  257. fz_throw(ctx, FZ_ERROR_FORMAT, "unknown color type");
  258. if (compression != 0)
  259. fz_throw(ctx, FZ_ERROR_FORMAT, "unknown compression method");
  260. if (filter != 0)
  261. fz_throw(ctx, FZ_ERROR_FORMAT, "unknown filter method");
  262. if (info->interlace != 0 && info->interlace != 1)
  263. fz_throw(ctx, FZ_ERROR_FORMAT, "interlace method not supported");
  264. if (info->height > UINT_MAX / info->width / info->n / (info->depth / 8 + 1))
  265. fz_throw(ctx, FZ_ERROR_LIMIT, "image dimensions might overflow");
  266. }
  267. static void
  268. png_read_plte(fz_context *ctx, struct info *info, const unsigned char *p, unsigned int size)
  269. {
  270. int n = size / 3;
  271. int i;
  272. if (n > 256)
  273. {
  274. fz_warn(ctx, "too many samples in palette");
  275. n = 256;
  276. }
  277. for (i = 0; i < n; i++)
  278. {
  279. info->palette[i * 4] = p[i * 3];
  280. info->palette[i * 4 + 1] = p[i * 3 + 1];
  281. info->palette[i * 4 + 2] = p[i * 3 + 2];
  282. }
  283. /* Fill in any missing palette entries */
  284. for (; i < 256; i++)
  285. {
  286. info->palette[i * 4] = 0;
  287. info->palette[i * 4 + 1] = 0;
  288. info->palette[i * 4 + 2] = 0;
  289. }
  290. }
  291. static void
  292. png_read_trns(fz_context *ctx, struct info *info, const unsigned char *p, unsigned int size)
  293. {
  294. unsigned int i;
  295. info->transparency = 1;
  296. if (info->indexed)
  297. {
  298. if (size > 256)
  299. {
  300. fz_warn(ctx, "too many samples in transparency table");
  301. size = 256;
  302. }
  303. for (i = 0; i < size; i++)
  304. info->palette[i * 4 + 3] = p[i];
  305. /* Fill in any missing entries */
  306. for (; i < 256; i++)
  307. info->palette[i * 4 + 3] = 255;
  308. }
  309. else
  310. {
  311. if (size != info->n * 2)
  312. fz_throw(ctx, FZ_ERROR_FORMAT, "tRNS chunk is the wrong size");
  313. for (i = 0; i < info->n; i++)
  314. info->trns[i] = (p[i * 2] << 8 | p[i * 2 + 1]) & ((1 << info->depth) - 1);
  315. }
  316. }
  317. static void
  318. png_read_icc(fz_context *ctx, struct info *info, const unsigned char *p, unsigned int size)
  319. {
  320. #if FZ_ENABLE_ICC
  321. fz_stream *mstm = NULL, *zstm = NULL;
  322. fz_colorspace *cs = NULL;
  323. fz_buffer *buf = NULL;
  324. size_t m = fz_mini(80, size);
  325. size_t n = fz_strnlen((const char *)p, m);
  326. if (n + 2 > m)
  327. {
  328. fz_warn(ctx, "invalid ICC profile name");
  329. return;
  330. }
  331. fz_var(mstm);
  332. fz_var(zstm);
  333. fz_var(buf);
  334. fz_try(ctx)
  335. {
  336. mstm = fz_open_memory(ctx, p + n + 2, size - n - 2);
  337. zstm = fz_open_flated(ctx, mstm, 15);
  338. buf = fz_read_all(ctx, zstm, 0);
  339. cs = fz_new_icc_colorspace(ctx, info->type, 0, NULL, buf);
  340. fz_drop_colorspace(ctx, info->cs);
  341. info->cs = cs;
  342. }
  343. fz_always(ctx)
  344. {
  345. fz_drop_buffer(ctx, buf);
  346. fz_drop_stream(ctx, zstm);
  347. fz_drop_stream(ctx, mstm);
  348. }
  349. fz_catch(ctx)
  350. {
  351. fz_rethrow_if(ctx, FZ_ERROR_SYSTEM);
  352. fz_report_error(ctx);
  353. fz_warn(ctx, "ignoring embedded ICC profile in PNG");
  354. }
  355. #endif
  356. }
  357. static void
  358. png_read_idat(fz_context *ctx, struct info *info, const unsigned char *p, unsigned int size, z_stream *stm)
  359. {
  360. int code;
  361. stm->next_in = (Bytef*)p;
  362. stm->avail_in = size;
  363. code = inflate(stm, Z_SYNC_FLUSH);
  364. if (code != Z_OK && code != Z_STREAM_END)
  365. fz_throw(ctx, FZ_ERROR_LIBRARY, "zlib error: %s", stm->msg);
  366. if (stm->avail_in != 0)
  367. {
  368. if (stm->avail_out == 0)
  369. fz_throw(ctx, FZ_ERROR_FORMAT, "ran out of output before input");
  370. fz_throw(ctx, FZ_ERROR_FORMAT, "inflate did not consume buffer (%d remaining)", stm->avail_in);
  371. }
  372. }
  373. static void
  374. png_read_phys(fz_context *ctx, struct info *info, const unsigned char *p, unsigned int size)
  375. {
  376. if (size != 9)
  377. fz_throw(ctx, FZ_ERROR_FORMAT, "pHYs chunk is the wrong size");
  378. if (p[8] == 1)
  379. {
  380. info->xres = (getuint(p) * 254 + 5000) / 10000;
  381. info->yres = (getuint(p + 4) * 254 + 5000) / 10000;
  382. }
  383. }
  384. static void
  385. png_read_image(fz_context *ctx, struct info *info, const unsigned char *p, size_t total, int only_metadata)
  386. {
  387. unsigned int passw[7], passh[7], passofs[8];
  388. unsigned int code, size;
  389. z_stream stm;
  390. memset(info, 0, sizeof (struct info));
  391. memset(info->palette, 255, sizeof(info->palette));
  392. info->xres = 96;
  393. info->yres = 96;
  394. /* Read signature */
  395. if (total < 8 + 12 || memcmp(p, png_signature, 8))
  396. fz_throw(ctx, FZ_ERROR_FORMAT, "not a png image (wrong signature)");
  397. p += 8;
  398. total -= 8;
  399. /* Read IHDR chunk (must come first) */
  400. size = getuint(p);
  401. if (size > total - 12)
  402. fz_throw(ctx, FZ_ERROR_FORMAT, "premature end of data in png image");
  403. if (!memcmp(p + 4, "IHDR", 4))
  404. png_read_ihdr(ctx, info, p + 8, size);
  405. else
  406. fz_throw(ctx, FZ_ERROR_FORMAT, "png file must start with IHDR chunk");
  407. p += size + 12;
  408. total -= size + 12;
  409. /* Prepare output buffer */
  410. if (!only_metadata)
  411. {
  412. if (!info->interlace)
  413. {
  414. info->size = info->height * (1 + ((size_t) info->width * info->n * info->depth + 7) / 8);
  415. }
  416. else
  417. {
  418. png_deinterlace_passes(ctx, info, passw, passh, passofs);
  419. info->size = passofs[7];
  420. }
  421. info->samples = Memento_label(fz_malloc(ctx, info->size), "png_samples");
  422. stm.zalloc = fz_zlib_alloc;
  423. stm.zfree = fz_zlib_free;
  424. stm.opaque = ctx;
  425. stm.next_out = info->samples;
  426. stm.avail_out = (uInt)info->size;
  427. code = inflateInit(&stm);
  428. if (code != Z_OK)
  429. fz_throw(ctx, FZ_ERROR_LIBRARY, "zlib error: %s", stm.msg);
  430. }
  431. fz_try(ctx)
  432. {
  433. /* Read remaining chunks until IEND */
  434. while (total > 8)
  435. {
  436. size = getuint(p);
  437. if (total < 12 || size > total - 12)
  438. fz_throw(ctx, FZ_ERROR_FORMAT, "premature end of data in png image");
  439. if (!memcmp(p + 4, "PLTE", 4) && !only_metadata)
  440. png_read_plte(ctx, info, p + 8, size);
  441. if (!memcmp(p + 4, "tRNS", 4) && !only_metadata)
  442. png_read_trns(ctx, info, p + 8, size);
  443. if (!memcmp(p + 4, "pHYs", 4))
  444. png_read_phys(ctx, info, p + 8, size);
  445. if (!memcmp(p + 4, "IDAT", 4) && !only_metadata)
  446. png_read_idat(ctx, info, p + 8, size, &stm);
  447. if (!memcmp(p + 4, "iCCP", 4))
  448. png_read_icc(ctx, info, p + 8, size);
  449. if (!memcmp(p + 4, "IEND", 4))
  450. break;
  451. p += size + 12;
  452. total -= size + 12;
  453. }
  454. if (!only_metadata && stm.avail_out != 0)
  455. {
  456. memset(stm.next_out, 0xff, stm.avail_out);
  457. fz_warn(ctx, "missing pixel data in png image; possibly truncated");
  458. }
  459. else if (total <= 8)
  460. fz_warn(ctx, "missing IEND chunk in png image; possibly truncated");
  461. }
  462. fz_catch(ctx)
  463. {
  464. if (!only_metadata)
  465. {
  466. inflateEnd(&stm);
  467. fz_free(ctx, info->samples);
  468. info->samples = NULL;
  469. }
  470. fz_rethrow(ctx);
  471. }
  472. if (!only_metadata)
  473. {
  474. code = inflateEnd(&stm);
  475. if (code != Z_OK)
  476. {
  477. fz_free(ctx, info->samples);
  478. info->samples = NULL;
  479. fz_throw(ctx, FZ_ERROR_LIBRARY, "zlib error: %s", stm.msg);
  480. }
  481. /* Apply prediction filter and deinterlacing */
  482. fz_try(ctx)
  483. {
  484. if (!info->interlace)
  485. png_predict(info->samples, info->width, info->height, info->n, info->depth);
  486. else
  487. png_deinterlace(ctx, info, passw, passh, passofs);
  488. }
  489. fz_catch(ctx)
  490. {
  491. fz_free(ctx, info->samples);
  492. info->samples = NULL;
  493. fz_rethrow(ctx);
  494. }
  495. }
  496. if (info->cs && fz_colorspace_type(ctx, info->cs) != info->type)
  497. {
  498. fz_warn(ctx, "embedded ICC profile does not match PNG colorspace");
  499. fz_drop_colorspace(ctx, info->cs);
  500. info->cs = NULL;
  501. }
  502. if (info->cs == NULL)
  503. {
  504. if (info->n == 3 || info->n == 4 || info->indexed)
  505. info->cs = fz_keep_colorspace(ctx, fz_device_rgb(ctx));
  506. else
  507. info->cs = fz_keep_colorspace(ctx, fz_device_gray(ctx));
  508. }
  509. }
  510. static fz_pixmap *
  511. png_expand_palette(fz_context *ctx, struct info *info, fz_pixmap *src)
  512. {
  513. fz_pixmap *dst = fz_new_pixmap(ctx, info->cs, src->w, src->h, NULL, info->transparency);
  514. unsigned char *sp = src->samples;
  515. unsigned char *dp = dst->samples;
  516. unsigned int x, y;
  517. size_t dstride = dst->stride - dst->w * (size_t)dst->n;
  518. size_t sstride = src->stride - src->w * (size_t)src->n;
  519. dst->xres = src->xres;
  520. dst->yres = src->yres;
  521. for (y = info->height; y > 0; y--)
  522. {
  523. for (x = info->width; x > 0; x--)
  524. {
  525. int v = *sp << 2;
  526. *dp++ = info->palette[v];
  527. *dp++ = info->palette[v + 1];
  528. *dp++ = info->palette[v + 2];
  529. if (info->transparency)
  530. *dp++ = info->palette[v + 3];
  531. ++sp;
  532. }
  533. sp += sstride;
  534. dp += dstride;
  535. }
  536. fz_drop_pixmap(ctx, src);
  537. return dst;
  538. }
  539. static void
  540. png_mask_transparency(struct info *info, fz_pixmap *dst)
  541. {
  542. unsigned int stride = (info->width * info->n * info->depth + 7) / 8;
  543. unsigned int depth = info->depth;
  544. unsigned int n = info->n;
  545. unsigned int x, y, k, t;
  546. for (y = 0; y < info->height; y++)
  547. {
  548. unsigned char *sp = info->samples + (unsigned int)(y * stride);
  549. unsigned char *dp = dst->samples + (unsigned int)(y * dst->stride);
  550. for (x = 0; x < info->width; x++)
  551. {
  552. t = 1;
  553. for (k = 0; k < n; k++)
  554. if (getcomp(sp, x * n + k, depth) != info->trns[k])
  555. t = 0;
  556. if (t)
  557. dp[x * dst->n + dst->n - 1] = 0;
  558. }
  559. }
  560. }
  561. fz_pixmap *
  562. fz_load_png(fz_context *ctx, const unsigned char *p, size_t total)
  563. {
  564. fz_pixmap *image = NULL;
  565. struct info png;
  566. size_t stride;
  567. int alpha;
  568. fz_var(image);
  569. fz_try(ctx)
  570. {
  571. png_read_image(ctx, &png, p, total, 0);
  572. stride = ((size_t) png.width * png.n * png.depth + 7) / 8;
  573. alpha = (png.n == 2 || png.n == 4 || png.transparency);
  574. if (png.indexed)
  575. {
  576. image = fz_new_pixmap(ctx, NULL, png.width, png.height, NULL, 1);
  577. fz_unpack_tile(ctx, image, png.samples, png.n, png.depth, stride, 1);
  578. image = png_expand_palette(ctx, &png, image);
  579. }
  580. else
  581. {
  582. image = fz_new_pixmap(ctx, png.cs, png.width, png.height, NULL, alpha);
  583. fz_unpack_tile(ctx, image, png.samples, png.n, png.depth, stride, 0);
  584. if (png.transparency)
  585. png_mask_transparency(&png, image);
  586. }
  587. if (alpha)
  588. fz_premultiply_pixmap(ctx, image);
  589. fz_set_pixmap_resolution(ctx, image, png.xres, png.yres);
  590. }
  591. fz_always(ctx)
  592. {
  593. fz_drop_colorspace(ctx, png.cs);
  594. fz_free(ctx, png.samples);
  595. }
  596. fz_catch(ctx)
  597. {
  598. fz_drop_pixmap(ctx, image);
  599. fz_rethrow(ctx);
  600. }
  601. return image;
  602. }
  603. void
  604. fz_load_png_info(fz_context *ctx, const unsigned char *p, size_t total, int *wp, int *hp, int *xresp, int *yresp, fz_colorspace **cspacep)
  605. {
  606. struct info png;
  607. fz_try(ctx)
  608. png_read_image(ctx, &png, p, total, 1);
  609. fz_catch(ctx)
  610. {
  611. fz_drop_colorspace(ctx, png.cs);
  612. fz_rethrow(ctx);
  613. }
  614. *cspacep = png.cs;
  615. *wp = png.width;
  616. *hp = png.height;
  617. *xresp = png.xres;
  618. *yresp = png.xres;
  619. }