-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilter.c
285 lines (262 loc) · 7.19 KB
/
filter.c
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
/*
* filter.c
* Copyright (C) 2003 Florian Schulze <[email protected]>
*
* This file is part of Jump 'n Bump.
*
* Jump 'n Bump is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Jump 'n Bump is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
The following scaling filter is called advancedmame2x.
The implementation found here was possible because of the great ideas of
Lucas Pope.
*/
typedef unsigned char byte;
static int scale2x_inited = 0;
static byte lookup_map[4 * 16];
void init_scale2x(void)
{
int i;
if (scale2x_inited)
return;
//-------------------------------------------------------------------------
// scale2x takes the following source:
// A B C
// D E F
// G H I
//
// and doubles the size of E to produce:
// E0 E1
// E2 E3
//
// E0 = D == B && B != F && D != H ? D : E;
// E1 = B == F && B != D && F != H ? F : E;
// E2 = D == H && D != B && H != F ? D : E;
// E3 = H == F && D != H && B != F ? F : E;
//
// to make this comparison regimen faster, we encode source color
// equivalency into a single byte with the getCode() macro
//
// #define getCode(b,f,h,d) ( (b == f)<<0 | (f == h)<<1 | (h == d)<<2 | (d == b)<<3 )
// encode the scale2x conditionals into a lookup code
for (i = 0; i < 16; i++) {
// E0 = D == B && B != F && D != H ? D : E; // 10-0 => 1000 or 1010 => 8 or A
lookup_map[0 * 16 + i] = (i == 0x8 || i == 0xA) ? 0 : 1;
// E1 = B == F && B != D && F != H ? F : E; // 0-01 => 0101 or 0001 => 5 or 1
lookup_map[1 * 16 + i] = (i == 0x5 || i == 0x1) ? 2 : 1;
// E2 = D == H && D != B && H != F ? D : E; // 010- => 0101 or 0100 => 5 or 4
lookup_map[2 * 16 + i] = (i == 0x4 || i == 0x5) ? 0 : 1;
// E3 = H == F && D != H && B != F ? F : E; // -010 => 1010 or 0010 => A or 2
lookup_map[3 * 16 + i] = (i == 0xA || i == 0x2) ? 2 : 1;
}
}
void do_scale2x(unsigned char *src,
int src_width,
int src_height,
unsigned char *dst)
{
int x;
int y;
int dst_width = src_width * 2;
int code;
byte rowColors[3];
byte *e0;
byte *e1;
byte *e2;
byte *e3;
if (!scale2x_inited)
init_scale2x();
// special top case - b is always unknown
{
byte *d;
byte *e;
byte *f;
byte *h;
e0 = &dst[0];
e1 = &dst[1];
e2 = &dst[dst_width];
e3 = &dst[dst_width + 1];
e = &src[0];
f = &src[1];
h = &src[src_width];
// special left case - d is unknown
rowColors[0] = *e;
rowColors[1] = *e;
rowColors[2] = *f;
code = ((*f == *h) << 1);
*e0 = rowColors[lookup_map[0 * 16 + code]];
*e1 = rowColors[lookup_map[1 * 16 + code]];
*e2 = rowColors[lookup_map[2 * 16 + code]];
*e3 = rowColors[lookup_map[3 * 16 + code]];
e++;
f++;
h++;
d = &src[src_width]; // (src_width - 1) + 1
e0 += 2;
e1 += 2;
e2 += 2;
e3 += 2;
// normal case
for (x = 1; x < (src_width - 1); x++) {
rowColors[0] = *d;
rowColors[1] = *e;
rowColors[2] = *f;
code = ((*f == *h) << 1 | (*h == *d) << 2);
*e0 = rowColors[lookup_map[0 * 16 + code]];
*e1 = rowColors[lookup_map[1 * 16 + code]];
*e2 = rowColors[lookup_map[2 * 16 + code]];
*e3 = rowColors[lookup_map[3 * 16 + code]];
d++;
e++;
f++;
h++;
e0 += 2;
e1 += 2;
e2 += 2;
e3 += 2;
}
// special right case - f is unknown
rowColors[0] = *d;
rowColors[1] = *e;
rowColors[2] = *e;
code = ((*h == *d) << 2);
*e0 = rowColors[lookup_map[0 * 16 + code]];
*e1 = rowColors[lookup_map[1 * 16 + code]];
*e2 = rowColors[lookup_map[2 * 16 + code]];
*e3 = rowColors[lookup_map[3 * 16 + code]];
}
// top and bottom always known
for (y = 1; y < (src_height - 1); y++) {
byte *b;
byte *d;
byte *e;
byte *f;
byte *h;
e0 = &dst[y * dst_width * 2];
e1 = &dst[y * dst_width * 2 + 1];
e2 = &dst[y * dst_width * 2 + dst_width];
e3 = &dst[y * dst_width * 2 + dst_width + 1];
b = &src[y * src_width - src_width];
e = &src[y * src_width];
f = &src[y * src_width + 1];
h = &src[y * src_width + src_width];
// special left case - d is unknown
rowColors[0] = *e;
rowColors[1] = *e;
rowColors[2] = *f;
code = ((*b == *f) << 0 | (*f == *h) << 1);
*e0 = rowColors[lookup_map[0 * 16 + code]];
*e1 = rowColors[lookup_map[1 * 16 + code]];
*e2 = rowColors[lookup_map[2 * 16 + code]];
*e3 = rowColors[lookup_map[3 * 16 + code]];
b++;
e++;
f++;
h++;
d = &src[y * src_width]; // (y * src_width - 1) + 1
e0 += 2;
e1 += 2;
e2 += 2;
e3 += 2;
// normal case
for (x = 1; x < (src_width - 1); x++) {
rowColors[0] = *d;
rowColors[1] = *e;
rowColors[2] = *f;
code = ((*b == *f) << 0 | (*f == *h) << 1 | (*h == *d) << 2 | (*d == *b) << 3);
*e0 = rowColors[lookup_map[0 * 16 + code]];
*e1 = rowColors[lookup_map[1 * 16 + code]];
*e2 = rowColors[lookup_map[2 * 16 + code]];
*e3 = rowColors[lookup_map[3 * 16 + code]];
b++;
d++;
e++;
f++;
h++;
e0 += 2;
e1 += 2;
e2 += 2;
e3 += 2;
}
// special right case - f is unknown
rowColors[0] = *d;
rowColors[1] = *e;
rowColors[2] = *e;
code = ((*h == *d) << 2 | (*d == *b) << 3);
*e0 = rowColors[lookup_map[0 * 16 + code]];
*e1 = rowColors[lookup_map[1 * 16 + code]];
*e2 = rowColors[lookup_map[2 * 16 + code]];
*e3 = rowColors[lookup_map[3 * 16 + code]];
}
// special bottom case - h is always unknown
{
byte *b;
byte *d;
byte *e;
byte *f;
e0 = &dst[y * dst_width * 2];
e1 = &dst[y * dst_width * 2 + 1];
e2 = &dst[y * dst_width * 2 + dst_width];
e3 = &dst[y * dst_width * 2 + dst_width + 1];
b = &src[y * src_width - src_width];
e = &src[y * src_width];
f = &src[y * src_width + 1];
// special left case - d is unknown
rowColors[0] = *e;
rowColors[1] = *e;
rowColors[2] = *f;
code = ((*b == *f) << 0);
*e0 = rowColors[lookup_map[0 * 16 + code]];
*e1 = rowColors[lookup_map[1 * 16 + code]];
*e2 = rowColors[lookup_map[2 * 16 + code]];
*e3 = rowColors[lookup_map[3 * 16 + code]];
b++;
e++;
f++;
d = &src[y * src_width]; // (y * src_width - 1) + 1
e0 += 2;
e1 += 2;
e2 += 2;
e3 += 2;
// normal case
for (x = 1; x < (src_width - 1); x++) {
rowColors[0] = *d;
rowColors[1] = *e;
rowColors[2] = *f;
code = ((*b == *f) << 0 | (*d == *b) << 3);
*e0 = rowColors[lookup_map[0 * 16 + code]];
*e1 = rowColors[lookup_map[1 * 16 + code]];
*e2 = rowColors[lookup_map[2 * 16 + code]];
*e3 = rowColors[lookup_map[3 * 16 + code]];
b++;
d++;
e++;
f++;
e0 += 2;
e1 += 2;
e2 += 2;
e3 += 2;
}
// special right case - f is unknown
rowColors[0] = *d;
rowColors[1] = *e;
rowColors[2] = *e;
code = ((*d == *b) << 3);
*e0 = rowColors[lookup_map[0 * 16 + code]];
*e1 = rowColors[lookup_map[1 * 16 + code]];
*e2 = rowColors[lookup_map[2 * 16 + code]];
*e3 = rowColors[lookup_map[3 * 16 + code]];
}
}