summaryrefslogtreecommitdiffstats
path: root/src/libnr/nr-compose-transform.cpp
blob: bb5022a74f078b13af2582c5b7fa87811e73cb4f (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
#define __NR_COMPOSE_TRANSFORM_C__

/*
 * Pixel buffer rendering library
 *
 * Authors:
 *   Lauris Kaplinski <lauris@kaplinski.com>
 *
 * This code is in public domain
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "nr-pixops.h"
#include "nr-matrix.h"


#ifdef WITH_MMX
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/* fixme: */
int nr_have_mmx (void);
void nr_mmx_R8G8B8A8_P_R8G8B8A8_P_R8G8B8A8_N_TRANSFORM_0 (unsigned char *px, int w, int h, int rs,
							  const unsigned char *spx, int sw, int sh, int srs,
							  const long *FFd2s, unsigned int alpha);
void nr_mmx_R8G8B8A8_P_R8G8B8A8_P_R8G8B8A8_N_TRANSFORM_n (unsigned char *px, int w, int h, int rs,
							  const unsigned char *spx, int sw, int sh, int srs,
							  const long *FFd2s, const long *FF_S, unsigned int alpha, int dbits);
#define NR_PIXOPS_MMX (1 && nr_have_mmx ())
#ifdef __cplusplus
}
#endif //__cplusplus
#endif

/* fixme: Implement missing (Lauris) */
/* fixme: PREMUL colors before calculating average (Lauris) */

/* Fixed point precision */
#define FBITS 12

void nr_R8G8B8A8_N_EMPTY_R8G8B8A8_N_TRANSFORM (unsigned char *px, int w, int h, int rs,
					       const unsigned char *spx, int sw, int sh, int srs,
					       const NR::Matrix &d2s, unsigned int alpha, int xd, int yd);
void nr_R8G8B8A8_N_EMPTY_R8G8B8A8_P_TRANSFORM (unsigned char *px, int w, int h, int rs,
					       const unsigned char *spx, int sw, int sh, int srs,
					       const NR::Matrix &d2s, unsigned int alpha, int xd, int yd);
void nr_R8G8B8A8_P_EMPTY_R8G8B8A8_N_TRANSFORM (unsigned char *px, int w, int h, int rs,
					       const unsigned char *spx, int sw, int sh, int srs,
					       const NR::Matrix &d2s, unsigned int alpha, int xd, int yd);
void nr_R8G8B8A8_P_EMPTY_R8G8B8A8_P_TRANSFORM (unsigned char *px, int w, int h, int rs,
					       const unsigned char *spx, int sw, int sh, int srs,
					       const NR::Matrix &d2s, unsigned int alpha, int xd, int yd);

void
nr_R8G8B8A8_N_R8G8B8A8_N_R8G8B8A8_N_TRANSFORM (unsigned char *px, int w, int h, int rs,
					       const unsigned char *spx, int sw, int sh, int srs,
					       const NR::Matrix &d2s, unsigned int alpha, int xd, int yd)
{
	int xsize, ysize, size, dbits;
	long FFs_x_x, FFs_x_y, FFs_y_x, FFs_y_y, FFs__x, FFs__y;
	long FFs_x_x_S, FFs_x_y_S, FFs_y_x_S, FFs_y_y_S;
	/* Subpixel positions */
	int FF_sx_S[256];
	int FF_sy_S[256];
	unsigned char *d0;
	int FFsx0, FFsy0;
	int x, y;

	if (alpha == 0) return;

	xsize = (1 << xd);
	ysize = (1 << yd);
	size = xsize * ysize;
	dbits = xd + yd;

	/* Set up fixed point matrix */
	FFs_x_x = (long) (d2s[0] * (1 << FBITS) + 0.5);
	FFs_x_y = (long) (d2s[1] * (1 << FBITS) + 0.5);
	FFs_y_x = (long) (d2s[2] * (1 << FBITS) + 0.5);
	FFs_y_y = (long) (d2s[3] * (1 << FBITS) + 0.5);
	FFs__x = (long) (d2s[4] * (1 << FBITS) + 0.5);
	FFs__y = (long) (d2s[5] * (1 << FBITS) + 0.5);

	FFs_x_x_S = FFs_x_x >> xd;
	FFs_x_y_S = FFs_x_y >> xd;
	FFs_y_x_S = FFs_y_x >> yd;
	FFs_y_y_S = FFs_y_y >> yd;

	/* Set up subpixel matrix */
	/* fixme: We can calculate that in floating point (Lauris) */
	for (y = 0; y < ysize; y++) {
		for (x = 0; x < xsize; x++) {
			FF_sx_S[y * xsize + x] = FFs_x_x_S * x + FFs_y_x_S * y;
			FF_sy_S[y * xsize + x] = FFs_x_y_S * x + FFs_y_y_S * y;
		}
	}

	d0 = px;
	FFsx0 = FFs__x;
	FFsy0 = FFs__y;

	for (y = 0; y < h; y++) {
		unsigned char *d;
		long FFsx, FFsy;
		d = d0;
		FFsx = FFsx0;
		FFsy = FFsy0;
		for (x = 0; x < w; x++) {
			unsigned int r, g, b, a;
			long sx, sy;
			int i;
			r = g = b = a = 0;
			for (i = 0; i < size; i++) {
				sx = (FFsx + FF_sx_S[i]) >> FBITS;
				if ((sx >= 0) && (sx < sw)) {
					sy = (FFsy + FF_sy_S[i]) >> FBITS;
					if ((sy >= 0) && (sy < sh)) {
						const unsigned char *s;
						unsigned int ca;
						s = spx + sy * srs + sx * 4;
						ca = NR_PREMUL (s[3], alpha);
						r += NR_PREMUL (s[0], ca);
						g += NR_PREMUL (s[1], ca);
						b += NR_PREMUL (s[2], ca);
						a += ca;
					}
				}
			}
			a >>= dbits;
			if (a != 0) {
				r = r >> dbits;
				g = g >> dbits;
				b = b >> dbits;
				if (a == 255) {
					/* Transparent BG, premul src */
					d[0] = r;
					d[1] = g;
					d[2] = b;
					d[3] = a;
				} else {
					unsigned int ca;
					/* Full composition */
					ca = 65025 - (255 - a) * (255 - d[3]);
					d[0] = NR_COMPOSENNN_A7 (r, a, d[0], d[3], ca);
					d[1] = NR_COMPOSENNN_A7 (g, a, d[1], d[3], ca);
					d[2] = NR_COMPOSENNN_A7 (b, a, d[2], d[3], ca);
					d[3] = (ca + 127) / 255;
				}
			}
			/* Advance pointers */
			FFsx += FFs_x_x;
			FFsy += FFs_x_y;
			d += 4;
		}
		FFsx0 += FFs_y_x;
		FFsy0 += FFs_y_y;
		d0 += rs;
	}
}

void nr_R8G8B8A8_N_R8G8B8A8_N_R8G8B8A8_P_TRANSFORM (unsigned char *px, int w, int h, int rs,
						    const unsigned char *spx, int sw, int sh, int srs,
						    const NR::Matrix &d2s, unsigned int alpha, int xd, int yd);

static void
nr_R8G8B8A8_P_R8G8B8A8_P_R8G8B8A8_N_TRANSFORM_0 (unsigned char *px, int w, int h, int rs,
						 const unsigned char *spx, int sw, int sh, int srs,
						 const long *FFd2s, unsigned int alpha)
{
	unsigned char *d0;
	int FFsx0, FFsy0;
	int x, y;

	d0 = px;
	FFsx0 = FFd2s[4];
	FFsy0 = FFd2s[5];

	for (y = 0; y < h; y++) {
		unsigned char *d;
		long FFsx, FFsy;
		d = d0;
		FFsx = FFsx0;
		FFsy = FFsy0;
		for (x = 0; x < w; x++) {
			long sx, sy;
			sx = FFsx >> FBITS;
			if ((sx >= 0) && (sx < sw)) {
				sy = FFsy >> FBITS;
				if ((sy >= 0) && (sy < sh)) {
					const unsigned char *s;
					unsigned int a;
					s = spx + sy * srs + sx * 4;
					a = NR_PREMUL (s[3], alpha);
					if (a != 0) {
						if ((a == 255) || (d[3] == 0)) {
							/* Transparent BG, premul src */
							d[0] = NR_PREMUL (s[0], a);
							d[1] = NR_PREMUL (s[1], a);
							d[2] = NR_PREMUL (s[2], a);
							d[3] = a;
						} else {
							d[0] = NR_COMPOSENPP (s[0], a, d[0], d[3]);
							d[1] = NR_COMPOSENPP (s[1], a, d[1], d[3]);
							d[2] = NR_COMPOSENPP (s[2], a, d[2], d[3]);
							d[3] = (65025 - (255 - a) * (255 - d[3]) + 127) / 255;
						}
					}
				}
			}
			/* Advance pointers */
			FFsx += FFd2s[0];
			FFsy += FFd2s[1];
			d += 4;
		}
		FFsx0 += FFd2s[2];
		FFsy0 += FFd2s[3];
		d0 += rs;
	}
}

static void
nr_R8G8B8A8_P_R8G8B8A8_P_R8G8B8A8_N_TRANSFORM_n (unsigned char *px, int w, int h, int rs,
						 const unsigned char *spx, int sw, int sh, int srs,
						 const long *FFd2s, const long *FF_S, unsigned int alpha, int dbits)
{
	int size;
	unsigned char *d0;
	int FFsx0, FFsy0;
	int x, y;

	size = (1 << dbits);
    unsigned alpha_rounding_fix = size * 255;
    unsigned rgb_rounding_fix = size * (255 * 256);
    if (alpha > 127) ++alpha;

	d0 = px;
	FFsx0 = FFd2s[4];
	FFsy0 = FFd2s[5];

	for (y = 0; y < h; y++) {
		unsigned char *d;
		long FFsx, FFsy;
		d = d0;
		FFsx = FFsx0;
		FFsy = FFsy0;
		for (x = 0; x < w; x++) {
			unsigned int r, g, b, a;
			int i;
			r = g = b = a = 0;
			for (i = 0; i < size; i++) {
				long sx, sy;
				sx = (FFsx + FF_S[2 * i]) >> FBITS;
				if ((sx >= 0) && (sx < sw)) {
					sy = (FFsy + FF_S[2 * i + 1]) >> FBITS;
					if ((sy >= 0) && (sy < sh)) {
						const unsigned char *s;
						unsigned int ca;
						s = spx + sy * srs + sx * 4;
						ca = s[3] * alpha;
						r += s[0] * ca;
						g += s[1] * ca;
						b += s[2] * ca;
						a += ca;
					}
				}
			}
			a = (a + alpha_rounding_fix) >> (8 + dbits);
			if (a != 0) {
				r = (r + rgb_rounding_fix) >> (16 + dbits);
				g = (g + rgb_rounding_fix) >> (16 + dbits);
				b = (b + rgb_rounding_fix) >> (16 + dbits);
				if ((a == 255) || (d[3] == 0)) {
					/* Transparent BG, premul src */
					d[0] = r;
					d[1] = g;
					d[2] = b;
					d[3] = a;
				} else {
					d[0] = NR_COMPOSEPPP (r, a, d[0], d[3]);
					d[1] = NR_COMPOSEPPP (g, a, d[1], d[3]);
					d[2] = NR_COMPOSEPPP (b, a, d[2], d[3]);
					d[3] = (65025 - (255 - a) * (255 - d[3]) + 127) / 255;
				}
			}
			/* Advance pointers */
			FFsx += FFd2s[0];
			FFsy += FFd2s[1];
			d += 4;
		}
		FFsx0 += FFd2s[2];
		FFsy0 += FFd2s[3];
		d0 += rs;
	}
}

void nr_R8G8B8A8_P_R8G8B8A8_P_R8G8B8A8_N_TRANSFORM (unsigned char *px, int w, int h, int rs,
						    const unsigned char *spx, int sw, int sh, int srs,
						    const NR::Matrix &d2s, unsigned int alpha, int xd, int yd)
{
	int dbits;
	long FFd2s[6];
	int i;

	if (alpha == 0) return;

	dbits = xd + yd;

	for (i = 0; i < 6; i++) {
		FFd2s[i] = (long) (d2s[i] * (1 << FBITS) + 0.5);
	}

	if (dbits == 0) {
#ifdef WITH_MMX
		if (NR_PIXOPS_MMX) {
			/* WARNING: MMX composer REQUIRES w > 0 and h > 0 */
			nr_mmx_R8G8B8A8_P_R8G8B8A8_P_R8G8B8A8_N_TRANSFORM_0 (px, w, h, rs, spx, sw, sh, srs, FFd2s, alpha);
			return;
		}
#endif
		nr_R8G8B8A8_P_R8G8B8A8_P_R8G8B8A8_N_TRANSFORM_0 (px, w, h, rs, spx, sw, sh, srs, FFd2s, alpha);
	} else {
		int xsize, ysize;
		long FFs_x_x_S, FFs_x_y_S, FFs_y_x_S, FFs_y_y_S;
		long FF_S[2 * 256];
		int x, y;

		xsize = (1 << xd);
		ysize = (1 << yd);

		FFs_x_x_S = FFd2s[0] >> xd;
		FFs_x_y_S = FFd2s[1] >> xd;
		FFs_y_x_S = FFd2s[2] >> yd;
		FFs_y_y_S = FFd2s[3] >> yd;

		/* Set up subpixel matrix */
		/* fixme: We can calculate that in floating point (Lauris) */
		for (y = 0; y < ysize; y++) {
			for (x = 0; x < xsize; x++) {
				FF_S[2 * (y * xsize + x)] = FFs_x_x_S * x + FFs_y_x_S * y;
				FF_S[2 * (y * xsize + x) + 1] = FFs_x_y_S * x + FFs_y_y_S * y;
			}
		}

#ifdef WITH_MMX
		if (NR_PIXOPS_MMX) {
			/* WARNING: MMX composer REQUIRES w > 0 and h > 0 */
			nr_mmx_R8G8B8A8_P_R8G8B8A8_P_R8G8B8A8_N_TRANSFORM_n (px, w, h, rs, spx, sw, sh, srs, FFd2s, FF_S, alpha, dbits);
			return;
		}
#endif
		nr_R8G8B8A8_P_R8G8B8A8_P_R8G8B8A8_N_TRANSFORM_n (px, w, h, rs, spx, sw, sh, srs, FFd2s, FF_S, alpha, dbits);
	}
}

void nr_R8G8B8A8_P_R8G8B8A8_P_R8G8B8A8_P_TRANSFORM (unsigned char *px, int w, int h, int rs,
						    const unsigned char *spx, int sw, int sh, int srs,
						    const NR::Matrix &d2s, unsigned int alpha, int xd, int yd);