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
|
#include "fitz.h"
fz_pixmap *
fz_newpixmap(int x, int y, int w, int h, int n)
{
fz_pixmap *pix;
pix = fz_malloc(sizeof(fz_pixmap));
pix->x = x;
pix->y = y;
pix->w = w;
pix->h = h;
pix->n = n;
pix->samples = fz_malloc(pix->w * pix->h * pix->n * sizeof(fz_sample));
return pix;
}
fz_pixmap *
fz_newpixmapwithrect(fz_irect r, int n)
{
return fz_newpixmap(r.x0, r.y0, r.x1 - r.x0, r.y1 - r.y0, n);
}
fz_pixmap *
fz_newpixmapcopy(fz_pixmap *old)
{
fz_pixmap *pix = fz_newpixmap(old->x, old->y, old->w, old->h, old->n);
memcpy(pix->samples, old->samples, old->w * old->h * old->n);
return pix;
}
void
fz_freepixmap(fz_pixmap *pix)
{
fz_free(pix->samples);
fz_free(pix);
}
void
fz_clearpixmap(fz_pixmap *pix, unsigned char value)
{
memset(pix->samples, value, pix->w * pix->h * pix->n * sizeof(fz_sample));
}
void
fz_gammapixmap(fz_pixmap *pix, float gamma)
{
unsigned char table[256];
int n = pix->w * pix->h * pix->n;
unsigned char *p = pix->samples;
int i;
for (i = 0; i < 256; i++)
table[i] = CLAMP(pow(i / 255.0, gamma) * 255.0, 0, 255);
while (n--)
{
*p = table[*p];
p++;
}
}
void
fz_debugpixmap(fz_pixmap *pix, char *prefix)
{
static int counter = 0;
char colorname[40];
char alphaname[40];
FILE *color = NULL;
FILE *alpha = NULL;
int x, y;
sprintf(alphaname, "%s-%04d-alpha.pgm", prefix, counter);
alpha = fopen(alphaname, "wb");
if (!alpha)
goto cleanup;
if (pix->n > 1)
{
if (pix->n > 2)
sprintf(colorname, "%s-%04d-color.ppm", prefix, counter);
else
sprintf(colorname, "%s-%04d-color.pgm", prefix, counter);
color = fopen(colorname, "wb");
if (!color)
goto cleanup;
}
counter ++;
if (pix->n == 5)
{
fprintf(alpha, "P5\n%d %d\n255\n", pix->w, pix->h);
fprintf(color, "P6\n%d %d\n255\n", pix->w, pix->h);
for (y = 0; y < pix->h; y++)
{
for (x = 0; x < pix->w; x++)
{
int a = pix->samples[x * pix->n + y * pix->w * pix->n + 0];
int cc = pix->samples[x * pix->n + y * pix->w * pix->n + 1];
int mm = pix->samples[x * pix->n + y * pix->w * pix->n + 2];
int yy = pix->samples[x * pix->n + y * pix->w * pix->n + 3];
int kk = pix->samples[x * pix->n + y * pix->w * pix->n + 4];
int r = 255 - MIN(cc + kk, 255);
int g = 255 - MIN(mm + kk, 255);
int b = 255 - MIN(yy + kk, 255);
fputc(a, alpha);
fputc(r, color);
fputc(g, color);
fputc(b, color);
}
}
}
else if (pix->n == 4)
{
fprintf(alpha, "P5\n%d %d\n255\n", pix->w, pix->h);
fprintf(color, "P6\n%d %d\n255\n", pix->w, pix->h);
for (y = 0; y < pix->h; y++)
{
for (x = 0; x < pix->w; x++)
{
int a = pix->samples[x * pix->n + y * pix->w * pix->n + 0];
int r = pix->samples[x * pix->n + y * pix->w * pix->n + 1];
int g = pix->samples[x * pix->n + y * pix->w * pix->n + 2];
int b = pix->samples[x * pix->n + y * pix->w * pix->n + 3];
fputc(a, alpha);
fputc(r, color);
fputc(g, color);
fputc(b, color);
}
}
}
else if (pix->n == 2)
{
fprintf(alpha, "P5\n%d %d\n255\n", pix->w, pix->h);
fprintf(color, "P5\n%d %d\n255\n", pix->w, pix->h);
for (y = 0; y < pix->h; y++)
{
for (x = 0; x < pix->w; x++)
{
int a = pix->samples[x * pix->n + y * pix->w * pix->n + 0];
int g = pix->samples[x * pix->n + y * pix->w * pix->n + 1];
fputc(a, alpha);
fputc(g, color);
}
}
}
else if (pix->n == 1)
{
fprintf(alpha, "P5\n%d %d\n255\n", pix->w, pix->h);
for (y = 0; y < pix->h; y++)
{
for (x = 0; x < pix->w; x++)
{
int g = pix->samples[x * pix->n + y * pix->w * pix->n + 0];
fputc(g, alpha);
}
}
}
cleanup:
if (alpha) fclose(alpha);
if (color) fclose(color);
}
|