summaryrefslogtreecommitdiff
path: root/draw/imagedraw.c
blob: 9f6d93bfff4618fb38246e22dd5f172f8cf13a90 (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
#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 *
getargb(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 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
lerpargb(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
sampleargb(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 = getargb(s, w, h, ui, vi);
	byte *b = getargb(s, w, h, ui+1, vi);
	byte *c = getargb(s, w, h, ui, vi+1);
	byte *d = getargb(s, w, h, ui+1, vi+1);
	lerpargb(ab, a, b, ud);
	lerpargb(cd, c, d, ud);
	lerpargb(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_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 argb[4];

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

static void
img_w4i1o4(byte *argb, 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;
	int alpha = FZ_EXPAND(argb[0]);
	byte r = argb[1];
	byte g = argb[2];
	byte b = argb[3];

	if (alpha == 0)
		return;
	if (alpha != 256)
	{
		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, alpha);
				if (ca != 0)
				{
					dst[0] = FZ_BLEND(255, dst[0], ca);
					dst[1] = FZ_BLEND(r, dst[1], ca);
					dst[2] = FZ_BLEND(g, dst[2], ca);
					dst[3] = FZ_BLEND(b, 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(255, dst[0], ca);
					dst[1] = FZ_BLEND(r, dst[1], ca);
					dst[2] = FZ_BLEND(g, dst[2], ca);
					dst[3] = FZ_BLEND(b, dst[3], ca);
				}
			}
			dst += 4;
			u += fa;
			v += fb;
		}
	}
}

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