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
|
#include "fitz.h"
#define noLINEAR
typedef unsigned char byte;
static inline float roundup(float x)
{
return (x < 0) ? floorf(x) : ceilf(x);
}
#ifdef LINEAR
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;
}
#endif
/* Blend premultiplied source image in constant alpha over destination */
static inline void
fz_paintaffinealphaN(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)
{
#ifdef LINEAR
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);
}
#else
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);
#endif
}
dp += n;
u += fa;
v += fb;
}
}
/* Blend premultiplied source image over destination */
static inline void
fz_paintaffineN(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)
{
#ifdef LINEAR
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);
}
#else
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);
#endif
}
dp += n;
u += fa;
v += fb;
}
}
/* Blend non-premultiplied color in source image mask over destination */
static inline void
fz_paintaffinecolorN(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)
{
#ifdef LINEAR
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);
#else
int ma = sp[vi * sw + ui];
#endif
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_paintaffine(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_paintaffineN(dp, sp, sw, sh, u, v, fa, fb, w, 1); break;
case 2: fz_paintaffineN(dp, sp, sw, sh, u, v, fa, fb, w, 2); break;
case 4: fz_paintaffineN(dp, sp, sw, sh, u, v, fa, fb, w, 4); break;
default: fz_paintaffineN(dp, sp, sw, sh, u, v, fa, fb, w, n); break;
}
}
else if (alpha > 0)
{
switch (n)
{
case 1: fz_paintaffinealphaN(dp, sp, sw, sh, u, v, fa, fb, w, 1, alpha); break;
case 2: fz_paintaffinealphaN(dp, sp, sw, sh, u, v, fa, fb, w, 2, alpha); break;
case 4: fz_paintaffinealphaN(dp, sp, sw, sh, u, v, fa, fb, w, 4, alpha); break;
default: fz_paintaffinealphaN(dp, sp, sw, sh, u, v, fa, fb, w, n, alpha); break;
}
}
}
void
fz_paintaffinecolor(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_paintaffinecolorN(dp, sp, sw, sh, u, v, fa, fb, w, 2, color); break;
case 4: fz_paintaffinecolorN(dp, sp, sw, sh, u, v, fa, fb, w, 4, color); break;
default: fz_paintaffinecolorN(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;
/* 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);// + 0.5f;
ctm.f = floorf(ctm.f);// + 0.5f;
}
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. */
/* Also, convert from texture space to sample space (subtract 1/2) */
u = (fa * x) + (fc * y) + inv.e * 65536 + ((fa+fc)>>1) - 32768;
v = (fb * x) + (fd * y) + inv.f * 65536 + ((fb+fd)>>1) - 32768;
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 (color)
fz_paintaffinecolor(dp, sp, sw, sh, u, v, fa, fb, w, n, color);
else
fz_paintaffine(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);
}
|