summaryrefslogtreecommitdiff
path: root/draw/porterduff.c
blob: 99e00e053d5b9711b52916d3fef6dc43167f180e (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
362
363
364
#include "fitz.h"

/*

The functions in this file implement various flavours of Porter-Duff blending.

We take the following as definitions:

	Cx = Color (from plane x)
	ax = Alpha (from plane x)
	cx = Cx.ax = Premultiplied color (from plane x)

The general PorterDuff blending equation is:

	Blend Z = X op Y	cz = Fx.cx + Fy. cy	where Fx and Fy depend on op

The two operations we use in this file are: '(X in Y) over Z' and
'S over Z'. The definitions of the 'over' and 'in' operations are as
follows:

	For S over Z,	Fs = 1, Fz = 1-as
	For X in Y,	Fx = ay, Fy = 0

We have 2 choices; we can either work with premultiplied data, or non
premultiplied data. Our

First the premultiplied case:

	Let S = (X in Y)
	Let R = (X in Y) over Z = S over Z

	cs	= cx.Fx + cy.Fy	(where Fx = ay, Fy = 0)
		= cx.ay
	as	= ax.Fx + ay.Fy
		= ax.ay

	cr	= cs.Fs + cz.Fz	(where Fs = 1, Fz = 1-as)
		= cs + cz.(1-as)
		= cx.ay + cz.(1-ax.ay)
	ar	= as.Fs + az.Fz
		= as + az.(1-as)
		= ax.ay + az.(1-ax.ay)

This has various nice properties, like not needing any divisions, and
being symmetric in color and alpha, so this is what we use. Because we
went through the pain of deriving the non premultiplied forms, we list
them here too, though they are not used.

Non Pre-multiplied case:

	Cs.as	= Fx.Cx.ax + Fy.Cy.ay	(where Fx = ay, Fy = 0)
		= Cx.ay.ax
	Cs	= (Cx.ay.ax)/(ay.ax)
		= Cx
	Cr.ar	= Fs.Cs.as + Fz.Cz.az	(where Fs = 1, Fz = 1-as)
		= Cs.as	+ (1-as).Cz.az
		= Cx.ax.ay + Cz.az.(1-ax.ay)
	Cr	= (Cx.ax.ay + Cz.az.(1-ax.ay))/(ax.ay + az.(1-ax-ay))

Much more complex, it seems. However, if we could restrict ourselves to
the case where we were always plotting onto an opaque background (i.e.
az = 1), then:

	Cr	= Cx.(ax.ay) + Cz.(1-ax.ay)
		= (Cx-Cz)*(1-ax.ay) + Cz	(a single MLA operation)
	ar	= 1

Sadly, this is not true in the general case, so we abandon this effort
and stick to using the premultiplied form.

*/

typedef unsigned char byte;

/*
 * Path drawing (the source colors are not premultiplied)
 */

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

static void
path_w2i1o2(byte *ga, byte * restrict src, byte cov, int len, byte * restrict dst)
{
	byte g = ga[0];
	int a = FZ_EXPAND(ga[1]);

	while (len--)
	{
		int ca;
		cov += *src; *src = 0; src++;
		ca = FZ_COMBINE(FZ_EXPAND(cov), a);
		dst[0] = FZ_BLEND(g, dst[0], ca);
		dst[1] = FZ_BLEND(255, dst[1], ca);
		dst += 2;
	}
}

static void
path_w4i1o4(byte *rgba, byte * restrict src, byte cov, int len, byte * restrict dst)
{
	byte r = rgba[0];
	byte g = rgba[1];
	byte b = rgba[2];
	int a = FZ_EXPAND(rgba[3]);
	while (len--)
	{
		int ca;
		cov += *src; *src = 0; src++;
		ca = FZ_COMBINE(FZ_EXPAND(cov), a);
		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;
	}
}

void (*fz_path_1o1)(byte*restrict,byte,int,byte*restrict) = path_1o1;
void (*fz_path_w2i1o2)(byte*,byte*restrict,byte,int,byte*restrict) = path_w2i1o2;
void (*fz_path_w4i1o4)(byte*,byte*restrict,byte,int,byte*restrict) = path_w4i1o4;

/*
 * Text drawing (the source colors are not premultiplied)
 */

static void
text_1o1(byte * restrict src, int srcw, byte * restrict dst, int dstw, int w0, int h)
{

	srcw -= w0;
	dstw -= w0;
	while (h--)
	{
		int w = w0;
		while (w--)
		{
			dst[0] = src[0] + fz_mul255(dst[0], 255 - src[0]);
			src++;
			dst++;
		}
		src += srcw;
		dst += dstw;
	}
}

static void
text_w2i1o2(byte *ga, byte * restrict src, int srcw, byte * restrict dst, int dstw, int w0, int h)
{
	byte g = ga[0];
	int a = FZ_EXPAND(ga[1]);

	srcw -= w0;
	dstw -= w0<<1;
	while (h--)
	{
		int w = w0;
		while (w--)
		{
			int c = FZ_COMBINE(FZ_EXPAND(src[0]), a);
			dst[0] = FZ_BLEND(g, dst[0], c);
			dst[1] = FZ_BLEND(255, dst[1], c);
			src ++;
			dst += 2;
		}
		src += srcw;
		dst += dstw;
	}
}

static void
text_w4i1o4(byte *rgba, byte * restrict src, int srcw, byte * restrict dst, int dstw, int w0, int h)
{
	byte r = rgba[0];
	byte g = rgba[1];
	byte b = rgba[2];
	int a = FZ_EXPAND(rgba[3]);

	srcw -= w0;
	dstw -= w0<<2;
	while (h--)
	{
		int w = w0;
		while (w--)
		{
			int c = FZ_COMBINE(FZ_EXPAND(src[0]), a);
			dst[0] = FZ_BLEND(r, dst[0], c);
			dst[1] = FZ_BLEND(g, dst[1], c);
			dst[2] = FZ_BLEND(b, dst[2], c);
			dst[3] = FZ_BLEND(255, dst[3], c);
			src ++;
			dst += 4;
		}
		src += srcw;
		dst += dstw;
	}
}

void (*fz_text_1o1)(byte*restrict,int,byte*restrict,int,int,int) = text_1o1;
void (*fz_text_w2i1o2)(byte*,byte*restrict,int,byte*restrict,int,int,int) = text_w2i1o2;
void (*fz_text_w4i1o4)(byte*,byte*restrict,int,byte*restrict,int,int,int) = text_w4i1o4;

/*
 * Pixmap blending
 */

void
fz_blendwithmask(byte * restrict dp, byte * restrict sp, byte * restrict mp, int n, int w)
{
	int k;

	switch (n)
	{
	case 2:
		while (w--)
		{
			int ma = *mp++;
			int masa = fz_mul255(sp[1], ma);
			int t = 255 - masa;
			dp[0] = fz_mul255(sp[0], ma) + fz_mul255(dp[0], t);
			sp += 2;
			dp += 2;
		}
		break;
	case 4:
		while (w--)
		{
			int ma = *mp++;
			int masa = fz_mul255(sp[3], ma);
			int t = 255 - masa;
			dp[0] = fz_mul255(sp[0], ma) + fz_mul255(dp[0], t);
			dp[1] = fz_mul255(sp[1], ma) + fz_mul255(dp[1], t);
			dp[2] = fz_mul255(sp[2], ma) + fz_mul255(dp[2], t);
			dp[3] = fz_mul255(sp[3], ma) + fz_mul255(dp[3], t);
			sp += 4;
			dp += 4;
		}
		break;
	default:
		while (w--)
		{
			int ma = *mp++;
			int masa = fz_mul255(sp[n-1], ma);
			int t = 255 - masa;
			for (k = 0; k < n; k++)
				dp[k] = fz_mul255(sp[k], ma) + fz_mul255(dp[k], t);
			sp += n;
			dp += n;
		}
	}
}

void
fz_blendwithalpha(byte * restrict dp, byte * restrict sp, int ma, int n, int w)
{
	int k;

	while (w--)
	{
		int masa = fz_mul255(sp[n-1], ma);
		int t = 255 - masa;
		for (k = 0; k < n; k++)
			dp[k] = fz_mul255(sp[k], ma) + fz_mul255(dp[k], t);
		sp += n;
		dp += n;
	}
}

void
fz_blendnormal(byte * restrict dp, byte * restrict sp, int n, int w)
{
	int k;

	while (w--)
	{
		int t = 255 - sp[n-1];
		for (k = 0; k < n; k++)
			dp[k] = sp[k] + fz_mul255(dp[k], t);
		sp += n;
		dp += n;
	}
}

void
fz_blendpixmapswithmask(fz_pixmap *dst, fz_pixmap *src, fz_pixmap *msk)
{
	unsigned char *sp, *dp, *mp;
	fz_bbox bbox;
	int x, y, w, h, n;

	bbox = fz_boundpixmap(dst);
	bbox = fz_intersectbbox(bbox, fz_boundpixmap(src));
	bbox = fz_intersectbbox(bbox, fz_boundpixmap(msk));

	x = bbox.x0;
	y = bbox.y0;
	w = bbox.x1 - bbox.x0;
	h = bbox.y1 - bbox.y0;

	n = src->n;
	sp = src->samples + ((y - src->y) * src->w + (x - src->x)) * src->n;
	mp = msk->samples + ((y - msk->y) * msk->w + (x - msk->x)) * msk->n;
	dp = dst->samples + ((y - dst->y) * dst->w + (x - dst->x)) * dst->n;

	assert(dst->n == src->n);
	assert(msk->n == 1);

	while (h--)
	{
		fz_blendwithmask(dp, sp, mp, n, w);
		sp += src->w * n;
		dp += dst->w * n;
		mp += msk->w;
	}
}

void
fz_blendpixmapswithalpha(fz_pixmap *dst, fz_pixmap *src, float alpha)
{
	unsigned char *sp, *dp;
	fz_bbox bbox;
	int x, y, w, h, n, a;

	bbox = fz_boundpixmap(dst);
	bbox = fz_intersectbbox(bbox, fz_boundpixmap(src));

	x = bbox.x0;
	y = bbox.y0;
	w = bbox.x1 - bbox.x0;
	h = bbox.y1 - bbox.y0;

	a = alpha * 255;
	n = src->n;
	sp = src->samples + ((y - src->y) * src->w + (x - src->x)) * src->n;
	dp = dst->samples + ((y - dst->y) * dst->w + (x - dst->x)) * dst->n;

	assert(dst->n == src->n);

	while (h--)
	{
		if (a == 255)
			fz_blendnormal(dp, sp, n, w);
		else
			fz_blendwithalpha(dp, sp, a, n, w);
		sp += src->w * n;
		dp += dst->w * n;
	}
}

void
fz_blendpixmaps(fz_pixmap *dst, fz_pixmap *src)
{
	fz_blendpixmapswithalpha(dst, src, 1);
}