-
Notifications
You must be signed in to change notification settings - Fork 0
/
imgtools.h
159 lines (133 loc) · 2.93 KB
/
imgtools.h
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
#define passto_if(x) if(__builtin_expect(x, 1))
#define goto_if(x) if(__builtin_expect(x, 0))
static inline s32int
rcp17(s32int pshift, s32int q)
{
s32int tag, z;
if(q==0){
return 1<<pshift;
}
z = 1 << (pshift-(31-__builtin_clz(q)));
tag = q >> ((31-2)-__builtin_clz(q));
if(tag == 7)
z = z>>1;
if(tag == 5 || tag == 6 || q == 3)
z = (z|(z>>1))>>1;
z = z + (((z<<pshift)-q*z*z)>>pshift);
z = z + (((z<<pshift)-q*z*z)>>pshift);
z = z + (((z<<pshift)-q*z*z)>>pshift);
return z;
}
static inline u32int
rcp32(int pshift, u32int q)
{
s32int tag;
s64int z;
if(q == 0){
return 1<<pshift;
}
z = ((s64int)1 << (pshift-(31-__builtin_clz(q))));
tag = q >> ((31-2)-__builtin_clz(q));
if(tag == 7)
z = z>>1;
if(tag == 5 || tag == 6 || q == 3)
z = (z|(z>>1))>>1;
z = z + (((z<<pshift)-q*z*z)>>pshift);
z = z + (((z<<pshift)-q*z*z)>>pshift);
z = z + (((z<<pshift)-q*z*z)>>pshift);
return z;
}
static inline u32int
blend32(u32int dval, u32int sval, u32int mval)
{
u32int dtmp, stmp, tmp1, tmp2;
/* multiply red and blue at the same time */
dtmp = dval & 0x00ff00ff;
stmp = sval & 0x00ff00ff;
tmp1 = dtmp * 255;
tmp1 += stmp * mval;
/* divide by 255 */
tmp1 += 0x00010001;
tmp1 += (tmp1>>8) & 0x00ff00ff;
tmp1 >>= 8;
tmp1 = tmp1 & 0x00ff00ff;
/* multiply green and alpha at the same time */
dtmp = (dval>>8) & 0x00ff00ff;
stmp = (sval>>8) & 0x00ff00ff;
tmp2 = dtmp * 255;
tmp2 += stmp * mval;
/* divide by 255 */
tmp2 += 0x00010001;
tmp2 += (tmp2>>8) & 0x00ff00ff;
tmp2 = tmp2 & 0xff00ff00;
return tmp1 | tmp2;
}
static inline u32int
blend32_under(u32int dval, u32int sval)
{
u32int mval;
mval = 255 - (dval >> 24);
return blend32(dval, sval, mval);
}
static inline u32int
blend32_over(u32int dval, u32int sval)
{
u32int mval;
mval = 255 - (sval >> 24);
return blend32(sval, dval, mval);
}
static inline u32int
blend32_sub(u32int dval, u32int sval)
{
u32int mval;
mval = 255 - (sval >> 24);
return blend32(0, dval, mval);
}
static inline u32int
blend32_mask(u32int dval, u32int sval)
{
u32int mval;
mval = sval >> 24;
return blend32(0, dval, mval);
}
static inline u32int *
img_uvstart(Image *img, int uoff, int voff, int isdst)
{
int u0, v0;
if(isdst){
u0 = uoff;
v0 = voff;
} else {
while(uoff < 0)
uoff += rectw(&img->r);
while(voff < 0)
voff += rectw(&img->r);
u0 = (uoff % rectw(&img->r));
v0 = (voff % recth(&img->r));
}
return (u32int*)(img->img + img->stride*v0 + 4*u0);
}
static inline u32int *
img_vstart(Image *img, int uoff)
{
int u0, v0;
while(uoff < 0)
uoff += rectw(&img->r);
//u0 = img->r.u0 + (uoff % rectw(&img->r));
u0 = (uoff % rectw(&img->r));
v0 = img->r.v0;
return (u32int*)(img->img + img->stride*v0 + 4*u0);
}
static inline int
img_uwrap(Image *img, int uoff)
{
while(uoff < 0)
uoff += rectw(&img->r);
uoff %= rectw(&img->r);
return rectw(&img->r) - uoff;
}
static inline u32int *
img_end(Image *img)
{
return (u32int*)(img->img + img->stride*img->r.vend);
}