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