Coverage Report

Created: 2025-01-11 06:55

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