comparison mupdf-source/source/fitz/load-png.c @ 2:b50eed0cc0ef upstream

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