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
|
#include <fitz.h>
fz_error *
fz_newpixmap(fz_pixmap **pixp, int x, int y, int w, int h, int n)
{
fz_pixmap *pix;
pix = *pixp = fz_malloc(sizeof(fz_pixmap));
if (!pix)
return fz_outofmem;
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));
if (!pix->samples) {
fz_free(pix);
return fz_outofmem;
}
memset(pix->samples, 0, pix->w * pix->h * pix->n * sizeof(fz_sample));
return nil;
}
void
fz_droppixmap(fz_pixmap *pix)
{
fz_free(pix->samples);
fz_free(pix);
}
void
fz_clearpixmap(fz_pixmap *pix)
{
memset(pix->samples, 0, pix->w * pix->h * pix->n * sizeof(fz_sample));
}
fz_error *
fz_convertpixmap(fz_pixmap **dstp, fz_pixmap *src, fz_colorspace *srcs, fz_colorspace *dsts)
{
fz_error *error;
fz_pixmap *dst;
float srcv[32];
float dstv[32];
int y, x, k;
error = fz_newpixmap(&dst, src->x, src->y, src->w, src->h, dsts->n + 1);
if (error)
return error;
unsigned char *s = src->samples;
unsigned char *d = dst->samples;
printf("convert pixmap from %s to %s\n", srcs->name, dsts->name);
for (y = 0; y < src->h; y++)
{
for (x = 0; x < src->w; x++)
{
*d++ = *s++;
for (k = 0; k < src->n - 1; k++)
srcv[k] = *s++ / 255.0;
fz_convertcolor(srcs, srcv, dsts, dstv);
for (k = 0; k < dst->n - 1; k++)
*d++ = dstv[k] * 255;
}
}
*dstp = dst;
return nil;
}
void
fz_blendover(fz_pixmap *src, fz_pixmap *dst)
{
int x, y, k;
assert(dst->n == src->n || src->n == 1);
assert(dst->w == src->w);
assert(dst->h == src->h);
unsigned char *s = src->samples;
unsigned char *d = dst->samples;
if (dst->n == src->n)
{
for (y = 0; y < dst->h; y++)
{
for (x = 0; x < dst->w; x++)
{
int sa = s[0];
int ssa = 255 - sa;
for (k = 0; k < dst->n; k++)
d[k] = s[k] + fz_mul255(d[k], ssa);
s += src->n;
d += dst->n;
}
}
}
else if (src->n == 1)
{
for (y = 0; y < dst->h; y++)
{
for (x = 0; x < dst->w; x++)
{
int sa = s[0];
int ssa = 255 - sa;
d[0] = s[0] + fz_mul255(d[0], ssa);
for (k = 1; k < dst->n; k++)
d[k] = 0 + fz_mul255(d[k], ssa);
s += src->n;
d += dst->n;
}
}
}
}
void
fz_blendmask(fz_pixmap *dst, fz_pixmap *src, fz_pixmap *msk)
{
int x, y, k;
assert(src->n == dst->n);
unsigned char *d = dst->samples;
unsigned char *s = src->samples;
unsigned char *m = msk->samples;
for (y = 0; y < dst->h; y++)
{
for (x = 0; x < dst->w; x++)
{
for (k = 0; k < dst->n; k++)
{
*d++ = fz_mul255(*s++, *m);
}
m += msk->n;
}
}
}
void
fz_gammapixmap(fz_pixmap *pix, float gamma)
{
int i;
unsigned char table[255];
for (i = 0; i < 256; i++)
table[i] = CLAMP(pow(i / 255.0, gamma) * 255.0, 0, 255);
int n = pix->w * pix->h * pix->n;
unsigned char *p = pix->samples;
while (n--)
{
*p = table[*p]; p++;
}
}
void
fz_debugpixmap(fz_pixmap *pix)
{
if (pix->n == 4)
{
int x, y;
FILE *ppm = fopen("out.ppm", "w");
FILE *pgm = fopen("out.pgm", "w");
fprintf(ppm, "P6\n%d %d\n255\n", pix->w, pix->h);
fprintf(pgm, "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 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];
putc(a, pgm);
putc(r, ppm);
putc(g, ppm);
putc(b, ppm);
// putc(((r * a) / 255) + (255 - a), ppm);
// putc(((g * a) / 255) + (255 - a), ppm);
// putc(((b * a) / 255) + (255 - a), ppm);
}
fclose(ppm);
fclose(pgm);
}
else if (pix->n == 1)
{
FILE *pgm = fopen("out.pgm", "w");
fprintf(pgm, "P5\n%d %d\n255\n", pix->w, pix->h);
fwrite(pix->samples, 1, pix->w * pix->h, pgm);
fclose(pgm);
}
}
|