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
|
#include "fitz.h"
typedef unsigned char byte;
static inline float roundup(float x)
{
return (x < 0) ? floorf(x) : ceilf(x);
}
static inline int lerp(int a, int b, int t)
{
return a + (((b - a) * t) >> 16);
}
static inline int bilerp(int a, int b, int c, int d, int u, int v)
{
return lerp(lerp(a, b, u), lerp(c, d, u), v);
}
static inline byte *samplenearest(byte *s, int w, int h, int n, int u, int v)
{
if (u < 0) u = 0;
if (v < 0) v = 0;
if (u >= w) u = w - 1;
if (v >= h) v = h - 1;
return s + (v * w + u) * n;
}
/* Blend premultiplied source image in constant alpha over destination */
static inline void
fz_paintaffinealphaNlerp(byte *dp, byte *sp, int sw, int sh, int u, int v, int fa, int fb, int w, int n, int alpha)
{
int k;
while (w--)
{
int ui = u >> 16;
int vi = v >> 16;
if (ui >= 0 && ui < sw && vi >= 0 && vi < sh)
{
int uf = u & 0xffff;
int vf = v & 0xffff;
byte *a = samplenearest(sp, sw, sh, n, ui, vi);
byte *b = samplenearest(sp, sw, sh, n, ui+1, vi);
byte *c = samplenearest(sp, sw, sh, n, ui, vi+1);
byte *d = samplenearest(sp, sw, sh, n, ui+1, vi+1);
int x = bilerp(a[n-1], b[n-1], c[n-1], d[n-1], uf, vf);
int t = 255 - fz_mul255(x, alpha);
for (k = 0; k < n; k++)
{
x = bilerp(a[k], b[k], c[k], d[k], uf, vf);
dp[k] = fz_mul255(x, alpha) + fz_mul255(dp[k], t);
}
}
dp += n;
u += fa;
v += fb;
}
}
static inline void
fz_paintaffinealphaNnear(byte *dp, byte *sp, int sw, int sh, int u, int v, int fa, int fb, int w, int n, int alpha)
{
int k;
while (w--)
{
int ui = u >> 16;
int vi = v >> 16;
if (ui >= 0 && ui < sw && vi >= 0 && vi < sh)
{
byte *sample = sp + ((vi * sw + ui) * n);
int t = 255 - fz_mul255(sample[n-1], alpha);
for (k = 0; k < n; k++)
dp[k] = fz_mul255(sample[k], alpha) + fz_mul255(dp[k], t);
}
dp += n;
u += fa;
v += fb;
}
}
/* Blend premultiplied source image over destination */
static inline void
fz_paintaffineNlerp(byte *dp, byte *sp, int sw, int sh, int u, int v, int fa, int fb, int w, int n)
{
int k;
while (w--)
{
int ui = u >> 16;
int vi = v >> 16;
if (ui >= 0 && ui < sw && vi >= 0 && vi < sh)
{
int uf = u & 0xffff;
int vf = v & 0xffff;
byte *a = samplenearest(sp, sw, sh, n, ui, vi);
byte *b = samplenearest(sp, sw, sh, n, ui+1, vi);
byte *c = samplenearest(sp, sw, sh, n, ui, vi+1);
byte *d = samplenearest(sp, sw, sh, n, ui+1, vi+1);
int t = 255 - bilerp(a[n-1], b[n-1], c[n-1], d[n-1], uf, vf);
for (k = 0; k < n; k++)
{
int x = bilerp(a[k], b[k], c[k], d[k], uf, vf);
dp[k] = x + fz_mul255(dp[k], t);
}
}
dp += n;
u += fa;
v += fb;
}
}
static inline void
fz_paintaffineNnear(byte *dp, byte *sp, int sw, int sh, int u, int v, int fa, int fb, int w, int n)
{
int k;
while (w--)
{
int ui = u >> 16;
int vi = v >> 16;
if (ui >= 0 && ui < sw && vi >= 0 && vi < sh)
{
byte *sample = sp + ((vi * sw + ui) * n);
int t = 255 - sample[n-1];
for (k = 0; k < n; k++)
dp[k] = sample[k] + fz_mul255(dp[k], t);
}
dp += n;
u += fa;
v += fb;
}
}
/* Blend non-premultiplied color in source image mask over destination */
static inline void
fz_paintaffinecolorNlerp(byte *dp, byte *sp, int sw, int sh, int u, int v, int fa, int fb, int w, int n, byte *color)
{
int sa = color[n-1];
int k;
while (w--)
{
int ui = u >> 16;
int vi = v >> 16;
if (ui >= 0 && ui < sw && vi >= 0 && vi < sh)
{
int uf = u & 0xffff;
int vf = v & 0xffff;
byte *a = samplenearest(sp, sw, sh, 1, ui, vi);
byte *b = samplenearest(sp, sw, sh, 1, ui+1, vi);
byte *c = samplenearest(sp, sw, sh, 1, ui, vi+1);
byte *d = samplenearest(sp, sw, sh, 1, ui+1, vi+1);
int ma = bilerp(a[0], b[0], c[0], d[0], uf, vf);
int masa = FZ_COMBINE(FZ_EXPAND(ma), sa);
for (k = 0; k < n - 1; k++)
dp[k] = FZ_BLEND(color[k], dp[k], masa);
dp[k] = FZ_BLEND(255, dp[k], masa);
}
dp += n;
u += fa;
v += fb;
}
}
static inline void
fz_paintaffinecolorNnear(byte *dp, byte *sp, int sw, int sh, int u, int v, int fa, int fb, int w, int n, byte *color)
{
int sa = color[n-1];
int k;
while (w--)
{
int ui = u >> 16;
int vi = v >> 16;
if (ui >= 0 && ui < sw && vi >= 0 && vi < sh)
{
int ma = sp[vi * sw + ui];
int masa = FZ_COMBINE(FZ_EXPAND(ma), sa);
for (k = 0; k < n - 1; k++)
dp[k] = FZ_BLEND(color[k], dp[k], masa);
dp[k] = FZ_BLEND(255, dp[k], masa);
}
dp += n;
u += fa;
v += fb;
}
}
void
fz_paintaffinelerp(byte *dp, byte *sp, int sw, int sh, int u, int v, int fa, int fb, int w, int n, int alpha)
{
if (alpha == 255)
{
switch (n)
{
case 1: fz_paintaffineNlerp(dp, sp, sw, sh, u, v, fa, fb, w, 1); break;
case 2: fz_paintaffineNlerp(dp, sp, sw, sh, u, v, fa, fb, w, 2); break;
case 4: fz_paintaffineNlerp(dp, sp, sw, sh, u, v, fa, fb, w, 4); break;
default: fz_paintaffineNlerp(dp, sp, sw, sh, u, v, fa, fb, w, n); break;
}
}
else if (alpha > 0)
{
switch (n)
{
case 1: fz_paintaffinealphaNlerp(dp, sp, sw, sh, u, v, fa, fb, w, 1, alpha); break;
case 2: fz_paintaffinealphaNlerp(dp, sp, sw, sh, u, v, fa, fb, w, 2, alpha); break;
case 4: fz_paintaffinealphaNlerp(dp, sp, sw, sh, u, v, fa, fb, w, 4, alpha); break;
default: fz_paintaffinealphaNlerp(dp, sp, sw, sh, u, v, fa, fb, w, n, alpha); break;
}
}
}
void
fz_paintaffinenear(byte *dp, byte *sp, int sw, int sh, int u, int v, int fa, int fb, int w, int n, int alpha)
{
if (alpha == 255)
{
switch (n)
{
case 1: fz_paintaffineNnear(dp, sp, sw, sh, u, v, fa, fb, w, 1); break;
case 2: fz_paintaffineNnear(dp, sp, sw, sh, u, v, fa, fb, w, 2); break;
case 4: fz_paintaffineNnear(dp, sp, sw, sh, u, v, fa, fb, w, 4); break;
default: fz_paintaffineNnear(dp, sp, sw, sh, u, v, fa, fb, w, n); break;
}
}
else if (alpha > 0)
{
switch (n)
{
case 1: fz_paintaffinealphaNnear(dp, sp, sw, sh, u, v, fa, fb, w, 1, alpha); break;
case 2: fz_paintaffinealphaNnear(dp, sp, sw, sh, u, v, fa, fb, w, 2, alpha); break;
case 4: fz_paintaffinealphaNnear(dp, sp, sw, sh, u, v, fa, fb, w, 4, alpha); break;
default: fz_paintaffinealphaNnear(dp, sp, sw, sh, u, v, fa, fb, w, n, alpha); break;
}
}
}
void
fz_paintaffinecolorlerp(byte *dp, byte *sp, int sw, int sh, int u, int v, int fa, int fb, int w, int n, byte *color)
{
switch (n)
{
case 2: fz_paintaffinecolorNlerp(dp, sp, sw, sh, u, v, fa, fb, w, 2, color); break;
case 4: fz_paintaffinecolorNlerp(dp, sp, sw, sh, u, v, fa, fb, w, 4, color); break;
default: fz_paintaffinecolorNlerp(dp, sp, sw, sh, u, v, fa, fb, w, n, color); break;
}
}
void
fz_paintaffinecolornear(byte *dp, byte *sp, int sw, int sh, int u, int v, int fa, int fb, int w, int n, byte *color)
{
switch (n)
{
case 2: fz_paintaffinecolorNnear(dp, sp, sw, sh, u, v, fa, fb, w, 2, color); break;
case 4: fz_paintaffinecolorNnear(dp, sp, sw, sh, u, v, fa, fb, w, 4, color); break;
default: fz_paintaffinecolorNnear(dp, sp, sw, sh, u, v, fa, fb, w, n, color); break;
}
}
/* Draw an image with an affine transform on destination */
static void
fz_paintimageimp(fz_pixmap *dst, fz_bbox scissor, fz_pixmap *img, fz_matrix ctm, byte *color, int alpha)
{
byte *dp, *sp;
int u, v, fa, fb, fc, fd;
int x, y, w, h;
int sw, sh, n;
fz_matrix inv;
fz_bbox bbox;
int dolerp;
/* grid fit the image */
if (fz_isrectilinear(ctm))
{
ctm.a = roundup(ctm.a);
ctm.b = roundup(ctm.b);
ctm.c = roundup(ctm.c);
ctm.d = roundup(ctm.d);
ctm.e = floorf(ctm.e);
ctm.f = floorf(ctm.f);
}
/* turn on interpolation for upscaled and non-rectilinear transforms */
dolerp = 0;
if (!fz_isrectilinear(ctm))
dolerp = 1;
if (sqrtf(ctm.a * ctm.a + ctm.b * ctm.b) > img->w)
dolerp = 1;
if (sqrtf(ctm.c * ctm.c + ctm.d * ctm.d) > img->h)
dolerp = 1;
bbox = fz_roundrect(fz_transformrect(ctm, fz_unitrect));
bbox = fz_intersectbbox(bbox, scissor);
x = bbox.x0;
y = bbox.y0;
w = bbox.x1 - bbox.x0;
h = bbox.y1 - bbox.y0;
/* map from screen space (x,y) to image space (u,v) */
inv = fz_scale(1.0f / img->w, -1.0f / img->h);
inv = fz_concat(inv, fz_translate(0, 1));
inv = fz_concat(inv, ctm);
inv = fz_invertmatrix(inv);
fa = inv.a * 65536;
fb = inv.b * 65536;
fc = inv.c * 65536;
fd = inv.d * 65536;
/* Calculate initial texture positions. Do a half step to start. */
u = (fa * x) + (fc * y) + inv.e * 65536 + ((fa+fc)>>1);
v = (fb * x) + (fd * y) + inv.f * 65536 + ((fb+fd)>>1);
dp = dst->samples + ((y - dst->y) * dst->w + (x - dst->x)) * dst->n;
n = dst->n;
sp = img->samples;
sw = img->w;
sh = img->h;
/* TODO: if (fb == 0 && fa == 1) call fz_paintspan */
while (h--)
{
if (dolerp)
{
if (color)
fz_paintaffinecolorlerp(dp, sp, sw, sh, u, v, fa, fb, w, n, color);
else
fz_paintaffinelerp(dp, sp, sw, sh, u, v, fa, fb, w, n, alpha);
}
else
{
if (color)
fz_paintaffinecolornear(dp, sp, sw, sh, u, v, fa, fb, w, n, color);
else
fz_paintaffinenear(dp, sp, sw, sh, u, v, fa, fb, w, n, alpha);
}
dp += dst->w * n;
u += fc;
v += fd;
}
}
void
fz_paintimagecolor(fz_pixmap *dst, fz_bbox scissor, fz_pixmap *img, fz_matrix ctm, byte *color)
{
assert(img->n == 1);
fz_paintimageimp(dst, scissor, img, ctm, color, 255);
}
void
fz_paintimage(fz_pixmap *dst, fz_bbox scissor, fz_pixmap *img, fz_matrix ctm, int alpha)
{
assert(dst->n == img->n);
fz_paintimageimp(dst, scissor, img, ctm, nil, alpha);
}
|