Coverage Report

Created: 2024-05-20 06:23

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