summaryrefslogtreecommitdiff
path: root/draw/imagedraw.c
blob: c43f9f24f55157a444f929650fda77283f54073c (plain)
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
#include "fitz.h"

typedef unsigned char byte;

static inline byte
getmask(byte *s, int w, int h, 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[w * v + u];
}

static inline byte *
getrgba(byte *s, int w, int h, 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 + ((w * v + u) << 2);
}

static inline byte *
getga(byte *s, int w, int h, 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 + ((w * v + u) << 1);
}

static inline int
getcolor(byte *s, int w, int h, int n, int u, int v, int k)
{
	if (u < 0) u = 0;
	if (v < 0) v = 0;
	if (u >= w) u = w - 1;
	if (v >= h) v = h - 1;
	return s[w * v * n + u + k];
}

static inline int
lerp(int a, int b, int t)
{
	return a + (((b - a) * t) >> 16);
}

static inline void
lerpga(byte *dst, byte *a, byte *b, int t)
{
	dst[0] = lerp(a[0], b[0], t);
	dst[1] = lerp(a[1], b[1], t);
}

static inline void
lerprgba(byte *dst, byte *a, byte *b, int t)
{
	dst[0] = lerp(a[0], b[0], t);
	dst[1] = lerp(a[1], b[1], t);
	dst[2] = lerp(a[2], b[2], t);
	dst[3] = lerp(a[3], b[3], t);
}

static inline int
samplemask(byte *s, int w, int h, int u, int v)
{
	int ui = u >> 16;
	int vi = v >> 16;
	int ud = u & 0xFFFF;
	int vd = v & 0xFFFF;
	int a = getmask(s, w, h, ui, vi);
	int b = getmask(s, w, h, ui+1, vi);
	int c = getmask(s, w, h, ui, vi+1);
	int d = getmask(s, w, h, ui+1, vi+1);
	int ab = lerp(a, b, ud);
	int cd = lerp(c, d, ud);
	return lerp(ab, cd, vd);
}

static inline void
samplega(byte *s, int w, int h, int u, int v, byte *out)
{
	byte ab[2];
	byte cd[2];
	int ui = u >> 16;
	int vi = v >> 16;
	int ud = u & 0xFFFF;
	int vd = v & 0xFFFF;
	byte *a = getga(s, w, h, ui, vi);
	byte *b = getga(s, w, h, ui+1, vi);
	byte *c = getga(s, w, h, ui, vi+1);
	byte *d = getga(s, w, h, ui+1, vi+1);
	lerpga(ab, a, b, ud);
	lerpga(cd, c, d, ud);
	lerpga(out, ab, cd, vd);
}

static inline void
samplergba(byte *s, int w, int h, int u, int v, byte *out)
{
	byte ab[4];
	byte cd[4];
	int ui = u >> 16;
	int vi = v >> 16;
	int ud = u & 0xFFFF;
	int vd = v & 0xFFFF;
	byte *a = getrgba(s, w, h, ui, vi);
	byte *b = getrgba(s, w, h, ui+1, vi);
	byte *c = getrgba(s, w, h, ui, vi+1);
	byte *d = getrgba(s, w, h, ui+1, vi+1);
	lerprgba(ab, a, b, ud);
	lerprgba(cd, c, d, ud);
	lerprgba(out, ab, cd, vd);
}

static inline void
samplecolor(byte *s, int w, int h, int n, int u, int v, byte *out)
{
	int ui = u >> 16;
	int vi = v >> 16;
	int ud = u & 0xFFFF;
	int vd = v & 0xFFFF;
	int k;
	for (k = 0; k < n; k++)
	{
		int a = getcolor(s, w, h, n, ui, vi, k);
		int b = getcolor(s, w, h, n, ui+1, vi, k);
		int c = getcolor(s, w, h, n, ui, vi+1, k);
		int d = getcolor(s, w, h, n, ui+1, vi+1, k);
		int ab = lerp(a, b, ud);
		int cd = lerp(c, d, ud);
		out[k] = lerp(ab, cd, vd);
	}
}

static void
img_1o1(byte * restrict src, byte cov, int len, byte * restrict dst,
	fz_pixmap *image, int u, int v, int fa, int fb)
{
	byte *samples = image->samples;
	int w = image->w;
	int h = image->h;

	while (len--)
	{
		int sa;
		cov += *src; *src = 0; src++;
		if (cov != 0)
		{
			sa = samplemask(samples, w, h, u, v);
			sa = FZ_COMBINE(FZ_EXPAND(sa), FZ_EXPAND(cov));
			if (sa != 0)
			{
				dst[0] = FZ_BLEND(255, dst[0], sa);
			}
		}
		dst++;
		u += fa;
		v += fb;
	}
}

static void
img_2o2(byte * restrict src, byte cov, int len, byte * restrict dst,
	fz_pixmap *image, int u, int v, int fa, int fb)
{
	byte *samples = image->samples;
	int w = image->w;
	int h = image->h;
	byte ga[2];

	while (len--)
	{
		int sa;
		cov += *src; *src = 0; src++;
		if (cov != 0)
		{
			samplega(samples, w, h, u, v, ga);
			sa = FZ_COMBINE(FZ_EXPAND(ga[1]), FZ_EXPAND(cov));
			if (sa != 0)
			{
				dst[0] = FZ_BLEND(ga[0], dst[0], sa);
				dst[1] = FZ_BLEND(255, dst[1], sa);
			}
		}
		dst += 2;
		u += fa;
		v += fb;
	}
}

static void
img_4o4(byte * restrict src, byte cov, int len, byte * restrict dst,
	fz_pixmap *image, int u, int v, int fa, int fb)
{
	byte *samples = image->samples;
	int w = image->w;
	int h = image->h;
	byte rgba[4];

	while (len--)
	{
		int sa;
		cov += *src; *src = 0; src++;
		if (cov != 0)
		{
			samplergba(samples, w, h, u, v, rgba);
			sa = FZ_COMBINE(FZ_EXPAND(rgba[3]), FZ_EXPAND(cov));
			if (sa != 0)
			{
				dst[0] = FZ_BLEND(rgba[0], dst[0], sa);
				dst[1] = FZ_BLEND(rgba[1], dst[1], sa);
				dst[2] = FZ_BLEND(rgba[2], dst[2], sa);
				dst[3] = FZ_BLEND(255, dst[3], sa);
			}
		}
		dst += 4;
		u += fa;
		v += fb;
	}
}

static void
img_w2i1o2(byte *ga, byte * restrict src, byte cov, int len, byte * restrict dst,
	fz_pixmap *image, int u, int v, int fa, int fb)
{
	byte *samples = image->samples;
	int w = image->w;
	int h = image->h;
	byte g = ga[0];
	byte a = ga[1];

	if (a == 0)
		return;
	if (a != 255)
	{
		while (len--)
		{
			int ca;
			cov += *src; *src = 0; src++;
			if (cov != 0)
			{
				ca = samplemask(samples, w, h, u, v);
				ca = FZ_COMBINE(FZ_EXPAND(cov), FZ_EXPAND(ca));
				ca = FZ_COMBINE(ca, FZ_EXPAND(a));
				if (ca != 0)
				{
					dst[0] = FZ_BLEND(g, dst[0], ca);
					dst[1] = FZ_BLEND(255, dst[1], ca);
				}
			}
			dst += 2;
			u += fa;
			v += fb;
		}
	}
	else
	{
		while (len--)
		{
			int ca;
			cov += *src; *src = 0; src++;
			if (cov != 0)
			{
				ca = samplemask(samples, w, h, u, v);
				ca = FZ_COMBINE(FZ_EXPAND(cov), FZ_EXPAND(ca));
				if (ca != 0)
				{
					dst[0] = FZ_BLEND(g, dst[0], ca);
					dst[1] = FZ_BLEND(255, dst[1], ca);
				}
			}
			dst += 2;
			u += fa;
			v += fb;
		}
	}
}

static void
img_w4i1o4(byte *rgba, byte * restrict src, byte cov, int len, byte * restrict dst,
	fz_pixmap *image, int u, int v, int fa, int fb)
{
	byte *samples = image->samples;
	int w = image->w;
	int h = image->h;
	byte r = rgba[0];
	byte g = rgba[1];
	byte b = rgba[2];
	byte a = rgba[3];

	if (a == 0)
		return;
	if (a != 255)
	{
		while (len--)
		{
			int ca;
			cov += *src; *src = 0; src++;
			if (cov != 0)
			{
				ca = samplemask(samples, w, h, u, v);
				ca = FZ_COMBINE(FZ_EXPAND(cov), FZ_EXPAND(ca));
				ca = FZ_COMBINE(ca, FZ_EXPAND(a));
				if (ca != 0)
				{
					dst[0] = FZ_BLEND(r, dst[0], ca);
					dst[1] = FZ_BLEND(g, dst[1], ca);
					dst[2] = FZ_BLEND(b, dst[2], ca);
					dst[3] = FZ_BLEND(255, dst[3], ca);
				}
			}
			dst += 4;
			u += fa;
			v += fb;
		}
	}
	else
	{
		while (len--)
		{
			int ca;
			cov += *src; *src = 0; src++;
			if (cov != 0)
			{
				ca = samplemask(samples, w, h, u, v);
				ca = FZ_COMBINE(FZ_EXPAND(cov), FZ_EXPAND(ca));
				if (ca != 0)
				{
					dst[0] = FZ_BLEND(r, dst[0], ca);
					dst[1] = FZ_BLEND(g, dst[1], ca);
					dst[2] = FZ_BLEND(b, dst[2], ca);
					dst[3] = FZ_BLEND(255, dst[3], ca);
				}
			}
			dst += 4;
			u += fa;
			v += fb;
		}
	}
}

void (*fz_img_1o1)(byte*restrict, byte, int, byte*restrict, fz_pixmap *image, int u, int v, int fa, int fb) = img_1o1;
void (*fz_img_2o2)(byte*restrict, byte, int, byte*restrict, fz_pixmap *image, int u, int v, int fa, int fb) = img_2o2;
void (*fz_img_4o4)(byte*restrict, byte, int, byte*restrict, fz_pixmap *image, int u, int v, int fa, int fb) = img_4o4;
void (*fz_img_w2i1o2)(byte*, byte*restrict, byte, int, byte*restrict, fz_pixmap *image, int u, int v, int fa, int fb) = img_w2i1o2;
void (*fz_img_w4i1o4)(byte*, byte*restrict, byte, int, byte*restrict, fz_pixmap *image, int u, int v, int fa, int fb) = img_w4i1o4;