aboutsummaryrefslogtreecommitdiffstats
path: root/apply-glyph.h
blob: 09cb9aa1a1ad53a09bba9a2b89115d23745c2104 (plain) (blame)
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
/* See LICENSE file for copyright and license details. */

#ifndef NARROW_FLOAT_TYPE
# define NARROW_FLOAT_TYPE double
# define NARROW_FLOAT_TYPE_AUTO
#endif
#ifndef WIDE_FLOAT_TYPE
# define WIDE_FLOAT_TYPE NARROW_FLOAT_TYPE
# define WIDE_FLOAT_TYPE_AUTO
#endif
#ifndef TYPE
# define TYPE NARROW_FLOAT_TYPE
# define TYPE_AUTO
# define TYPEMAX 1
#else
# define TYPEMAX ((TYPE)~(TYPE)0)
#endif
#ifndef RMAP
# define RMAP(Y) ((NARROW_FLOAT_TYPE)((WIDE_FLOAT_TYPE)(Y) / TYPEMAX))
# define RMAP_AUTO
#endif
#define MAP(Y) (TYPE)((WIDE_FLOAT_TYPE)(Y) * TYPEMAX + (WIDE_FLOAT_TYPE)0.5f)
#define R      (&((TYPE *)&img[i])[settings.rpos])
#define G      (&((TYPE *)&img[i])[settings.gpos])
#define B      (&((TYPE *)&img[i])[settings.bpos])
#define A      (&((TYPE *)&img[i])[settings.apos])

do {
	NARROW_FLOAT_TYPE opacity   = (NARROW_FLOAT_TYPE)colour->opacity / 255;
	NARROW_FLOAT_TYPE al, alpha = (NARROW_FLOAT_TYPE)colour->alpha   / 255;
	NARROW_FLOAT_TYPE re, red   = (NARROW_FLOAT_TYPE)colour->red     / 255;
	NARROW_FLOAT_TYPE gr, green = (NARROW_FLOAT_TYPE)colour->green   / 255;
	NARROW_FLOAT_TYPE bl, blue  = (NARROW_FLOAT_TYPE)colour->blue    / 255;
	NARROW_FLOAT_TYPE transparency;

	if (settings.apos < 0) {
		if (gly_psize > 1) {
			for (img = img_start, r = startr; r < endr; r++, img += img_linesize, gly += gly_linesize) {
				for (c = startc, i = 0; c < endc; c += gly_psize, i += psize) {
					*R = MAP(RMAP(*R) * (1 - opacity * gly[c + ri]) + gly[c + ri] * red);
					*G = MAP(RMAP(*G) * (1 - opacity * gly[c + gi]) + gly[c + gi] * green);
					*B = MAP(RMAP(*B) * (1 - opacity * gly[c + bi]) + gly[c + bi] * blue);
				}
			}
		} else {
			for (img = img_start, r = startr; r < endr; r++, img += img_linesize, gly += gly_linesize) {
				for (c = startc, i = 0; c < endc; c += gly_psize, i += psize) {
					transparency = 1 - opacity * gly[c + ri];
					*R = MAP(RMAP(*R) * transparency + gly[c + ri] * red);
					*G = MAP(RMAP(*G) * transparency + gly[c + gi] * green);
					*B = MAP(RMAP(*B) * transparency + gly[c + bi] * blue);
				}
			}
		}
	} else if (image->premultiplied) {
		if (gly_psize > 1) {
			for (img = img_start, r = startr; r < endr; r++, img += img_linesize, gly += gly_linesize) {
				for (c = startc, i = 0; c < endc; c += gly_psize, i += psize) {
					high = gly[c + ri] > gly[c + gi] ? gly[c + ri] : gly[c + gi];
					high = high        > gly[c + bi] ? high        : gly[c + bi];
					*R = MAP(RMAP(*R) * (1 - opacity * gly[c + ri]) + gly[c + ri] * red);
					*G = MAP(RMAP(*G) * (1 - opacity * gly[c + gi]) + gly[c + gi] * green);
					*B = MAP(RMAP(*B) * (1 - opacity * gly[c + bi]) + gly[c + bi] * blue);
					*A = MAP(RMAP(*A) * (1 - opacity * high)        + high        * alpha);
				}
			}
		} else {
			for (img = img_start, r = startr; r < endr; r++, img += img_linesize, gly += gly_linesize) {
				for (c = startc, i = 0; c < endc; c += gly_psize, i += psize) {
					transparency = 1 - opacity * gly[c + ri];
					*R = MAP(RMAP(*R) * transparency + gly[c + ri] * red);
					*G = MAP(RMAP(*G) * transparency + gly[c + ri] * green);
					*B = MAP(RMAP(*B) * transparency + gly[c + ri] * blue);
					*A = MAP(RMAP(*A) * transparency + gly[c + ri] * alpha);
				}
			}
		}
	} else {
		if (gly_psize > 1) {
			for (img = img_start, r = startr; r < endr; r++, img += img_linesize, gly += gly_linesize) {
				for (c = startc, i = 0; c < endc; c += gly_psize, i += psize) {
					high = gly[c + ri] > gly[c + gi] ? gly[c + ri] : gly[c + gi];
					high = high        > gly[c + bi] ? high        : gly[c + bi];
					re = RMAP(*R) * RMAP(*A) * (1 - opacity * gly[c + ri]) + gly[c + ri] * red;
					gr = RMAP(*G) * RMAP(*A) * (1 - opacity * gly[c + gi]) + gly[c + gi] * green;
					bl = RMAP(*B) * RMAP(*A) * (1 - opacity * gly[c + bi]) + gly[c + bi] * blue;
					al = RMAP(*A) * (1 - opacity * high) + high * alpha;
					if (fpclassify(al) != FP_ZERO) {
						*R = MAP(re);
						*G = MAP(gr);
						*B = MAP(bl);
						*A = MAP(al);
					} else {
						*R = *G = *B = *A = 0;
					}
				}
			}
		} else {
			for (img = img_start, r = startr; r < endr; r++, img += img_linesize, gly += gly_linesize) {
				for (c = startc, i = 0; c < endc; c += gly_psize, i += psize) {
					transparency = 1 - opacity * gly[c + ri];
					re = RMAP(*R) * RMAP(*A) * transparency + gly[c + ri] * red;
					gr = RMAP(*G) * RMAP(*A) * transparency + gly[c + ri] * green;
					bl = RMAP(*B) * RMAP(*A) * transparency + gly[c + ri] * blue;
					al = RMAP(*A) * transparency + gly[c + ri] * alpha;
					if (fpclassify(al) != FP_ZERO) {
						*R = MAP(re);
						*G = MAP(gr);
						*B = MAP(bl);
						*A = MAP(al);
					} else {
						*R = *G = *B = *A = 0;
					}
				}
			}
		}
	}
} while (0);

#ifdef NARROW_FLOAT_TYPE_AUTO
# undef NARROW_FLOAT_TYPE_AUTO
# undef NARROW_FLOAT_TYPE
#endif
#ifdef WIDE_FLOAT_TYPE_AUTO
# undef WIDE_FLOAT_TYPE_AUTO
# undef WIDE_FLOAT_TYPE
#endif
#ifdef TYPE_AUTO
# undef TYPE_AUTO
# undef TYPE
#endif
#undef TYPEMAX
#ifdef RMAP_AUTO
# undef RMAP_AUTO
# undef RMAP
#endif
#undef MAP
#undef R
#undef G
#undef B
#undef A