1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
|
#include "fitz.h"
static int fz_memory_limit = 256 << 20;
static int fz_memory_used = 0;
fz_pixmap *
fz_new_pixmap_with_data(fz_colorspace *colorspace, int w, int h, unsigned char *samples)
{
fz_pixmap *pix;
pix = fz_malloc(sizeof(fz_pixmap));
pix->refs = 1;
pix->x = 0;
pix->y = 0;
pix->w = w;
pix->h = h;
pix->mask = NULL;
pix->interpolate = 1;
pix->xres = 96;
pix->yres = 96;
pix->colorspace = NULL;
pix->n = 1;
if (colorspace)
{
pix->colorspace = fz_keep_colorspace(colorspace);
pix->n = 1 + colorspace->n;
}
if (samples)
{
pix->samples = samples;
pix->free_samples = 0;
}
else
{
fz_memory_used += pix->w * pix->h * pix->n;
pix->samples = fz_calloc(pix->h, pix->w * pix->n);
pix->free_samples = 1;
}
return pix;
}
fz_pixmap *
fz_new_pixmap_with_limit(fz_colorspace *colorspace, int w, int h)
{
int n = colorspace ? colorspace->n + 1 : 1;
int size = w * h * n;
if (fz_memory_used + size > fz_memory_limit)
{
fz_warn("pixmap memory exceeds soft limit %dM + %dM > %dM",
fz_memory_used/(1<<20), size/(1<<20), fz_memory_limit/(1<<20));
return NULL;
}
return fz_new_pixmap_with_data(colorspace, w, h, NULL);
}
fz_pixmap *
fz_new_pixmap(fz_colorspace *colorspace, int w, int h)
{
return fz_new_pixmap_with_data(colorspace, w, h, NULL);
}
fz_pixmap *
fz_new_pixmap_with_rect(fz_colorspace *colorspace, fz_bbox r)
{
fz_pixmap *pixmap;
pixmap = fz_new_pixmap(colorspace, r.x1 - r.x0, r.y1 - r.y0);
pixmap->x = r.x0;
pixmap->y = r.y0;
return pixmap;
}
fz_pixmap *
fz_new_pixmap_with_rect_and_data(fz_colorspace *colorspace, fz_bbox r, unsigned char *samples)
{
fz_pixmap *pixmap;
pixmap = fz_new_pixmap_with_data(colorspace, r.x1 - r.x0, r.y1 - r.y0, samples);
pixmap->x = r.x0;
pixmap->y = r.y0;
return pixmap;
}
fz_pixmap *
fz_keep_pixmap(fz_pixmap *pix)
{
pix->refs++;
return pix;
}
void
fz_drop_pixmap(fz_pixmap *pix)
{
if (pix && --pix->refs == 0)
{
fz_memory_used -= pix->w * pix->h * pix->n;
if (pix->mask)
fz_drop_pixmap(pix->mask);
if (pix->colorspace)
fz_drop_colorspace(pix->colorspace);
if (pix->free_samples)
fz_free(pix->samples);
fz_free(pix);
}
}
fz_bbox
fz_bound_pixmap(fz_pixmap *pix)
{
fz_bbox bbox;
bbox.x0 = pix->x;
bbox.y0 = pix->y;
bbox.x1 = pix->x + pix->w;
bbox.y1 = pix->y + pix->h;
return bbox;
}
void
fz_clear_pixmap(fz_pixmap *pix)
{
memset(pix->samples, 0, pix->w * pix->h * pix->n);
}
void
fz_clear_pixmap_with_color(fz_pixmap *pix, int value)
{
if (value == 255)
memset(pix->samples, 255, pix->w * pix->h * pix->n);
else
{
int k, x, y;
unsigned char *s = pix->samples;
for (y = 0; y < pix->h; y++)
{
for (x = 0; x < pix->w; x++)
{
for (k = 0; k < pix->n - 1; k++)
*s++ = value;
*s++ = 255;
}
}
}
}
void
fz_copy_pixmap_rect(fz_pixmap *dest, fz_pixmap *src, fz_bbox r)
{
const unsigned char *srcp;
unsigned char *destp;
int y, w, destspan, srcspan;
r = fz_intersect_bbox(r, fz_bound_pixmap(dest));
r = fz_intersect_bbox(r, fz_bound_pixmap(src));
w = r.x1 - r.x0;
y = r.y1 - r.y0;
if (w <= 0 || y <= 0)
return;
w *= src->n;
srcspan = src->w * src->n;
srcp = src->samples + srcspan * (r.y0 - src->y) + src->n * (r.x0 - src->x);
destspan = dest->w * dest->n;
destp = dest->samples + destspan * (r.y0 - dest->y) + dest->n * (r.x0 - dest->x);
do
{
memcpy(destp, srcp, w);
srcp += srcspan;
destp += destspan;
}
while (--y);
}
void
fz_clear_pixmap_rect_with_color(fz_pixmap *dest, int value, fz_bbox r)
{
unsigned char *destp;
int x, y, w, k, destspan;
r = fz_intersect_bbox(r, fz_bound_pixmap(dest));
w = r.x1 - r.x0;
y = r.y1 - r.y0;
if (w <= 0 || y <= 0)
return;
destspan = dest->w * dest->n;
destp = dest->samples + destspan * (r.y0 - dest->y) + dest->n * (r.x0 - dest->x);
if (value == 255)
do
{
memset(destp, 255, w * dest->n);
destp += destspan;
}
while (--y);
else
do
{
unsigned char *s = destp;
for (x = 0; x < w; x++)
{
for (k = 0; k < dest->n - 1; k++)
*s++ = value;
*s++ = 255;
}
destp += destspan;
}
while (--y);
}
void
fz_premultiply_pixmap(fz_pixmap *pix)
{
unsigned char *s = pix->samples;
unsigned char a;
int k, x, y;
for (y = 0; y < pix->h; y++)
{
for (x = 0; x < pix->w; x++)
{
a = s[pix->n - 1];
for (k = 0; k < pix->n - 1; k++)
s[k] = fz_mul255(s[k], a);
s += pix->n;
}
}
}
fz_pixmap *
fz_alpha_from_gray(fz_pixmap *gray, int luminosity)
{
fz_pixmap *alpha;
unsigned char *sp, *dp;
int len;
assert(gray->n == 2);
alpha = fz_new_pixmap_with_rect(NULL, fz_bound_pixmap(gray));
dp = alpha->samples;
sp = gray->samples;
if (!luminosity)
sp ++;
len = gray->w * gray->h;
while (len--)
{
*dp++ = sp[0];
sp += 2;
}
return alpha;
}
void
fz_invert_pixmap(fz_pixmap *pix)
{
unsigned char *s = pix->samples;
int k, x, y;
for (y = 0; y < pix->h; y++)
{
for (x = 0; x < pix->w; x++)
{
for (k = 0; k < pix->n - 1; k++)
s[k] = 255 - s[k];
s += pix->n;
}
}
}
void
fz_gamma_pixmap(fz_pixmap *pix, float gamma)
{
unsigned char gamma_map[256];
unsigned char *s = pix->samples;
int k, x, y;
for (k = 0; k < 256; k++)
gamma_map[k] = pow(k / 255.0f, gamma) * 255;
for (y = 0; y < pix->h; y++)
{
for (x = 0; x < pix->w; x++)
{
for (k = 0; k < pix->n - 1; k++)
s[k] = gamma_map[s[k]];
s += pix->n;
}
}
}
/*
* Write pixmap to PNM file (without alpha channel)
*/
fz_error
fz_write_pnm(fz_pixmap *pixmap, char *filename)
{
FILE *fp;
unsigned char *p;
int len;
if (pixmap->n != 1 && pixmap->n != 2 && pixmap->n != 4)
return fz_error_make("pixmap must be grayscale or rgb to write as pnm");
fp = fopen(filename, "wb");
if (!fp)
return fz_error_make("cannot open file '%s': %s", filename, strerror(errno));
if (pixmap->n == 1 || pixmap->n == 2)
fprintf(fp, "P5\n");
if (pixmap->n == 4)
fprintf(fp, "P6\n");
fprintf(fp, "%d %d\n", pixmap->w, pixmap->h);
fprintf(fp, "255\n");
len = pixmap->w * pixmap->h;
p = pixmap->samples;
switch (pixmap->n)
{
case 1:
fwrite(p, 1, len, fp);
break;
case 2:
while (len--)
{
putc(p[0], fp);
p += 2;
}
break;
case 4:
while (len--)
{
putc(p[0], fp);
putc(p[1], fp);
putc(p[2], fp);
p += 4;
}
}
fclose(fp);
return fz_okay;
}
/*
* Write pixmap to PAM file (with or without alpha channel)
*/
fz_error
fz_write_pam(fz_pixmap *pixmap, char *filename, int savealpha)
{
unsigned char *sp;
int y, w, k;
FILE *fp;
int sn = pixmap->n;
int dn = pixmap->n;
if (!savealpha && dn > 1)
dn--;
fp = fopen(filename, "wb");
if (!fp)
return fz_error_make("cannot open file '%s': %s", filename, strerror(errno));
fprintf(fp, "P7\n");
fprintf(fp, "WIDTH %d\n", pixmap->w);
fprintf(fp, "HEIGHT %d\n", pixmap->h);
fprintf(fp, "DEPTH %d\n", dn);
fprintf(fp, "MAXVAL 255\n");
if (pixmap->colorspace)
fprintf(fp, "# COLORSPACE %s\n", pixmap->colorspace->name);
switch (dn)
{
case 1: fprintf(fp, "TUPLTYPE GRAYSCALE\n"); break;
case 2: if (sn == 2) fprintf(fp, "TUPLTYPE GRAYSCALE_ALPHA\n"); break;
case 3: if (sn == 4) fprintf(fp, "TUPLTYPE RGB\n"); break;
case 4: if (sn == 4) fprintf(fp, "TUPLTYPE RGB_ALPHA\n"); break;
}
fprintf(fp, "ENDHDR\n");
sp = pixmap->samples;
for (y = 0; y < pixmap->h; y++)
{
w = pixmap->w;
while (w--)
{
for (k = 0; k < dn; k++)
putc(sp[k], fp);
sp += sn;
}
}
fclose(fp);
return fz_okay;
}
/*
* Write pixmap to PNG file (with or without alpha channel)
*/
#include <zlib.h>
static inline void big32(unsigned char *buf, unsigned int v)
{
buf[0] = (v >> 24) & 0xff;
buf[1] = (v >> 16) & 0xff;
buf[2] = (v >> 8) & 0xff;
buf[3] = (v) & 0xff;
}
static inline void put32(unsigned int v, FILE *fp)
{
putc(v >> 24, fp);
putc(v >> 16, fp);
putc(v >> 8, fp);
putc(v, fp);
}
static void putchunk(char *tag, unsigned char *data, int size, FILE *fp)
{
unsigned int sum;
put32(size, fp);
fwrite(tag, 1, 4, fp);
fwrite(data, 1, size, fp);
sum = crc32(0, NULL, 0);
sum = crc32(sum, (unsigned char*)tag, 4);
sum = crc32(sum, data, size);
put32(sum, fp);
}
fz_error
fz_write_png(fz_pixmap *pixmap, char *filename, int savealpha)
{
static const unsigned char pngsig[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
FILE *fp;
unsigned char head[13];
unsigned char *udata, *cdata, *sp, *dp;
uLong usize, csize;
int y, x, k, sn, dn;
int color;
int err;
if (pixmap->n != 1 && pixmap->n != 2 && pixmap->n != 4)
return fz_error_make("pixmap must be grayscale or rgb to write as png");
sn = pixmap->n;
dn = pixmap->n;
if (!savealpha && dn > 1)
dn--;
switch (dn)
{
default:
case 1: color = 0; break;
case 2: color = 4; break;
case 3: color = 2; break;
case 4: color = 6; break;
}
usize = (pixmap->w * dn + 1) * pixmap->h;
csize = compressBound(usize);
udata = fz_malloc(usize);
cdata = fz_malloc(csize);
sp = pixmap->samples;
dp = udata;
for (y = 0; y < pixmap->h; y++)
{
*dp++ = 1; /* sub prediction filter */
for (x = 0; x < pixmap->w; x++)
{
for (k = 0; k < dn; k++)
{
if (x == 0)
dp[k] = sp[k];
else
dp[k] = sp[k] - sp[k-sn];
}
sp += sn;
dp += dn;
}
}
err = compress(cdata, &csize, udata, usize);
if (err != Z_OK)
{
fz_free(udata);
fz_free(cdata);
return fz_error_make("cannot compress image data");
}
fp = fopen(filename, "wb");
if (!fp)
{
fz_free(udata);
fz_free(cdata);
return fz_error_make("cannot open file '%s': %s", filename, strerror(errno));
}
big32(head+0, pixmap->w);
big32(head+4, pixmap->h);
head[8] = 8; /* depth */
head[9] = color;
head[10] = 0; /* compression */
head[11] = 0; /* filter */
head[12] = 0; /* interlace */
fwrite(pngsig, 1, 8, fp);
putchunk("IHDR", head, 13, fp);
putchunk("IDAT", cdata, csize, fp);
putchunk("IEND", head, 0, fp);
fclose(fp);
fz_free(udata);
fz_free(cdata);
return fz_okay;
}
|