summaryrefslogtreecommitdiff
path: root/fitzdraw/porterduff.c
blob: 755ba43423e8e8cc6351f69afae9077804e4f67d (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
351
352
353
354
355
356
357
358
359
360
361
#include "fitz_base.h"
#include "fitz_tree.h"
#include "fitz_draw.h"

typedef unsigned char byte;

/*
 * Blend pixmap regions
 */

/* dst = src over dst */
static void
duff_non(byte * restrict sp0, int sw, int sn, byte * restrict dp0, int dw, int w0, int h)
{
	int k;
	while (h--)
	{
		byte *sp = sp0;
		byte *dp = dp0;
		int w = w0;
		while (w--)
		{
			byte sa = sp[0];
			byte ssa = 255 - sa;
			for (k = 0; k < sn; k++)
			{
				dp[k] = sp[k] + fz_mul255(dp[k], ssa);
			}
			sp += sn;
			dp += sn;
		}
		sp0 += sw;
		dp0 += dw;
	}
}

/* dst = src in msk */
static void
duff_nimcn(byte * restrict sp0, int sw, int sn, byte * restrict mp0, int mw, int mn, byte * restrict dp0, int dw, int w0, int h)
{
	int k;
	while (h--)
	{
		byte *sp = sp0;
		byte *mp = mp0;
		byte *dp = dp0;
		int w = w0;
		while (w--)
		{
			byte ma = mp[0];
			for (k = 0; k < sn; k++)
				dp[k] = fz_mul255(sp[k], ma);
			sp += sn;
			mp += mn;
			dp += sn;
		}
		sp0 += sw;
		mp0 += mw;
		dp0 += dw;
	}
}

/* dst = src in msk over dst */
static void
duff_nimon(byte * restrict sp0, int sw, int sn, byte * restrict mp0, int mw, int mn, byte * restrict dp0, int dw, int w0, int h)
{
	int k;
	while (h--)
	{
		byte *sp = sp0;
		byte *mp = mp0;
		byte *dp = dp0;
		int w = w0;
		while (w--)
		{
			/* TODO: validate this */
			byte ma = mp[0];
			byte sa = fz_mul255(sp[0], ma);
			byte ssa = 255 - sa;
			for (k = 0; k < sn; k++)
			{
				dp[k] = fz_mul255(sp[k], ma) + fz_mul255(dp[k], ssa);
			}
			sp += sn;
			mp += mn;
			dp += sn;
		}
		sp0 += sw;
		mp0 += mw;
		dp0 += dw;
	}
}

static void duff_1o1(byte * restrict sp0, int sw, byte * restrict dp0, int dw, int w0, int h)
{
	/* duff_non(sp0, sw, 1, dp0, dw, w0, h); */
	while (h--)
	{
		byte *sp = sp0;
		byte *dp = dp0;
		int w = w0;
		while (w--)
		{
			dp[0] = sp[0] + fz_mul255(dp[0], 255 - sp[0]);
			sp ++;
			dp ++;
		}
		sp0 += sw;
		dp0 += dw;
	}
}

static void duff_4o4(byte *sp0, int sw, byte *dp0, int dw, int w0, int h)
{
	/* duff_non(sp0, sw, 4, dp0, dw, w0, h); */
	while (h--)
	{
		byte *sp = sp0;
		byte *dp = dp0;
		int w = w0;
		while (w--)
		{
			byte ssa = 255 - sp[0];
			dp[0] = sp[0] + fz_mul255(dp[0], ssa);
			dp[1] = sp[1] + fz_mul255(dp[1], ssa);
			dp[2] = sp[2] + fz_mul255(dp[2], ssa);
			dp[3] = sp[3] + fz_mul255(dp[3], ssa);
			sp += 4;
			dp += 4;
		}
		sp0 += sw;
		dp0 += dw;
	}
}

static void duff_1i1c1(byte * restrict sp0, int sw, byte * restrict mp0, int mw, byte * restrict dp0, int dw, int w0, int h)
{
	/* duff_nimcn(sp0, sw, 1, mp0, mw, 1, dp0, dw, w0, h); */
	while (h--)
	{
		byte *sp = sp0;
		byte *mp = mp0;
		byte *dp = dp0;
		int w = w0;
		while (w--)
		{
			dp[0] = fz_mul255(sp[0], mp[0]);
			sp ++;
			mp ++;
			dp ++;
		}
		sp0 += sw;
		mp0 += mw;
		dp0 += dw;
	}
}

static void duff_4i1c4(byte * restrict sp0, int sw, byte * restrict mp0, int mw, byte * restrict dp0, int dw, int w0, int h)
{
	/* duff_nimcn(sp0, sw, 4, mp0, mw, 1, dp0, dw, w0, h); */
	while (h--)
	{
		byte *sp = sp0;
		byte *mp = mp0;
		byte *dp = dp0;
		int w = w0;
		while (w--)
		{
			byte ma = mp[0];
			dp[0] = fz_mul255(sp[0], ma);
			dp[1] = fz_mul255(sp[1], ma);
			dp[2] = fz_mul255(sp[2], ma);
			dp[3] = fz_mul255(sp[3], ma);
			sp += 4;
			mp += 1;
			dp += 4;
		}
		sp0 += sw;
		mp0 += mw;
		dp0 += dw;
	}
}

static void duff_1i1o1(byte * restrict sp0, int sw, byte * restrict mp0, int mw, byte * restrict dp0, int dw, int w0, int h)
{
	/* duff_nimon(sp0, sw, 1, mp0, mw, 1, dp0, dw, w0, h); */
	while (h--)
	{
		byte *sp = sp0;
		byte *mp = mp0;
		byte *dp = dp0;
		int w = w0;
		while (w--)
		{
			byte ma = mp[0];
			byte sa = fz_mul255(sp[0], ma);
			byte ssa = 255 - sa;
			dp[0] = fz_mul255(sp[0], ma) + fz_mul255(dp[0], ssa);
			sp ++;
			mp ++;
			dp ++;
		}
		sp0 += sw;
		mp0 += mw;
		dp0 += dw;
	}
}

static void duff_4i1o4(byte * restrict sp0, int sw, byte * restrict mp0, int mw, byte * restrict dp0, int dw, int w0, int h)
{
	/* duff_nimon(sp0, sw, 4, mp0, mw, 1, dp0, dw, w0, h); */
	while (h--)
	{
		byte *sp = sp0;
		byte *mp = mp0;
		byte *dp = dp0;
		int w = w0;
		while (w--)
		{
			byte ma = mp[0];
			byte sa = fz_mul255(sp[0], ma);
			byte ssa = 255 - sa;
			dp[0] = fz_mul255(sp[0], ma) + fz_mul255(dp[0], ssa);
			dp[1] = fz_mul255(sp[1], ma) + fz_mul255(dp[1], ssa);
			dp[2] = fz_mul255(sp[2], ma) + fz_mul255(dp[2], ssa);
			dp[3] = fz_mul255(sp[3], ma) + fz_mul255(dp[3], ssa);
			sp += 4;
			mp += 1;
			dp += 4;
		}
		sp0 += sw;
		mp0 += mw;
		dp0 += dw;
	}
}

/*
 * Path and text masks
 */

static void path_1c1(byte * restrict src, byte cov, int len, byte * restrict dst)
{
	while (len--)
	{
		cov += *src; *src = 0; src++;
		*dst++ = cov;
	}
}

static void path_1o1(byte * restrict src, byte cov, int len, byte * restrict dst)
{
	while (len--)
	{
		cov += *src; *src = 0; src++;
		dst[0] = cov + fz_mul255(dst[0], 255 - cov);
		dst++;
	}
}

// With 4 In 1 Over 4
static void path_w4i1o4(byte * restrict argb, byte * restrict src, byte cov, int len, byte * restrict dst)
{
	byte alpha = argb[0];
	byte r = argb[4];
	byte g = argb[5];
	byte b = argb[6];
	while (len--)
	{
		byte ca;
		cov += *src; *src = 0; src++;
		ca = fz_mul255(cov, alpha);
		dst[0] = ca + fz_mul255(dst[0], 255 - ca);
		dst[1] = fz_mul255((short)r - dst[1], ca) + dst[1];
		dst[2] = fz_mul255((short)g - dst[2], ca) + dst[2];
		dst[3] = fz_mul255((short)b - dst[3], ca) + dst[3];
		dst += 4;
	}
}

static void text_1c1(byte * restrict src0, int srcw, byte * restrict dst0, int dstw, int w0, int h)
{
	while (h--)
	{
		byte * restrict src = src0;
		byte * restrict dst = dst0;
		int w = w0;
		while (w--)
		{
			*dst++ = *src++;
		}
		src0 += srcw;
		dst0 += dstw;
	}
}

static void text_1o1(byte * restrict src0, int srcw, byte * restrict dst0, int dstw, int w0, int h)
{
	while (h--)
	{
		byte *src = src0;
		byte *dst = dst0;
		int w = w0;
		while (w--)
		{
			dst[0] = src[0] + fz_mul255(dst[0], 255 - src[0]);
			src++;
			dst++;
		}
		src0 += srcw;
		dst0 += dstw;
	}
}

static void text_w4i1o4(byte * restrict argb, byte * restrict src0, int srcw, byte * restrict dst0, int dstw, int w0, int h)
{
	unsigned char alpha = argb[0];
	unsigned char r = argb[4];
	unsigned char g = argb[5];
	unsigned char b = argb[6];
	while (h--)
	{
		byte *src = src0;
		byte *dst = dst0;
		int w = w0;
		while (w--)
		{
			byte ca = fz_mul255(src[0], alpha);
			dst[0] = ca + fz_mul255(dst[0], 255 - ca);
			dst[1] = fz_mul255((short)r - dst[1], ca) + dst[1];
			dst[2] = fz_mul255((short)g - dst[2], ca) + dst[2];
			dst[3] = fz_mul255((short)b - dst[3], ca) + dst[3];
			src ++;
			dst += 4;
		}
		src0 += srcw;
		dst0 += dstw;
	}
}

/*
 * ... and the function pointers
 */

void (*fz_duff_non)(byte*,int,int,byte*,int,int,int) = duff_non;
void (*fz_duff_nimcn)(byte*,int,int,byte*,int,int,byte*,int,int,int) = duff_nimcn;
void (*fz_duff_nimon)(byte*,int,int,byte*,int,int,byte*,int,int,int) = duff_nimon;
void (*fz_duff_1o1)(byte*,int,byte*,int,int,int) = duff_1o1;
void (*fz_duff_4o4)(byte*,int,byte*,int,int,int) = duff_4o4;
void (*fz_duff_1i1c1)(byte*,int,byte*,int,byte*,int,int,int) = duff_1i1c1;
void (*fz_duff_4i1c4)(byte*,int,byte*,int,byte*,int,int,int) = duff_4i1c4;
void (*fz_duff_1i1o1)(byte*,int,byte*,int,byte*,int,int,int) = duff_1i1o1;
void (*fz_duff_4i1o4)(byte*,int,byte*,int,byte*,int,int,int) = duff_4i1o4;

void (*fz_path_1c1)(byte*,byte,int,byte*) = path_1c1;
void (*fz_path_1o1)(byte*,byte,int,byte*) = path_1o1;
void (*fz_path_w4i1o4)(byte*,byte*,byte,int,byte*) = path_w4i1o4;

void (*fz_text_1c1)(byte*,int,byte*,int,int,int) = text_1c1;
void (*fz_text_1o1)(byte*,int,byte*,int,int,int) = text_1o1;
void (*fz_text_w4i1o4)(byte*,byte*,int,byte*,int,int,int) = text_w4i1o4;