-
Notifications
You must be signed in to change notification settings - Fork 13
/
scale2x.cpp
291 lines (257 loc) · 7.16 KB
/
scale2x.cpp
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
/*
* This file is part of the Scale2x project.
*
* Copyright (C) 2003, 2013 Andrea Mazzoleni
*
* This program 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.
*
* This program 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.
*/
/*
* This is part of the source of a simple command line tool which uses the reference
* implementation of the Scale effects.
*
* You can find an high level description of the effects at :
*
* http://www.scale2x.it/
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef char pixel_t;
pixel_t pixel_get(int x, int y, const unsigned char* pix, unsigned slice, unsigned pixel, int dx, int dy, int opt_tes)
{
const unsigned char* p;
unsigned i;
pixel_t v;
if (opt_tes) {
if (x < 0)
x += dx;
if (x >= dx)
x -= dx;
if (y < 0)
y += dy;
if (y >= dy)
y -= dy;
}
else {
if (x < 0)
x = 0;
if (x >= dx)
x = dx - 1;
if (y < 0)
y = 0;
if (y >= dy)
y = dy - 1;
}
p = pix + (y * slice) + (x * pixel);
v = 0;
for (i = 0; i<pixel; ++i)
v |= ((pixel_t)p[i]) << (i * 8);
return v;
}
void pixel_put(int x, int y, unsigned char* pix, unsigned slice, unsigned pixel, int dx, int dy, pixel_t v)
{
unsigned char* p;
unsigned i;
if (x < 0 || x >= dx)
return;
if (y < 0 || y >= dy)
return;
p = pix + (y * slice) + (x * pixel);
for (i = 0; i<pixel; ++i) {
p[i] = v >> (i * 8);
}
}
pixel_t lerp(pixel_t v0, pixel_t v1, double f)
{
unsigned r0, g0, b0;
unsigned r1, g1, b1;
r0 = v0 & 0xFF;
g0 = (v0 >> 8) & 0xFF;
b0 = (v0 >> 16) & 0xFF;
r1 = v1 & 0xFF;
g1 = (v1 >> 8) & 0xFF;
b1 = (v1 >> 16) & 0xFF;
r0 = (unsigned)(r0 * f + r1 * (1.0 - f));
g0 = (unsigned)(g0 * f + g1 * (1.0 - f));
b0 = (unsigned)(b0 * f + b1 * (1.0 - f));
if (r0 > 255) r0 = 255;
if (g0 > 255) g0 = 255;
if (b0 > 255) b0 = 255;
v0 = r0 + (g0 << 8) + (b0 << 16);
return v0 | 0xFF000000;
}
void scale2x(unsigned char* dst_ptr, const unsigned char* src_ptr, int width, int height)
{
unsigned pixel = 1; // size of 1 pixel in src
unsigned src_slice = pixel * width; // size of 1 line in src
unsigned dst_slice = src_slice * 2; // size of 1 line in dst
int x;
int y;
int opt_tes = 1; // flag 1 or 0
int opt_ver = 1; // version 0 to 4
for (y = 0; y<height; ++y) {
for (x = 0; x<width; ++x) {
pixel_t E0, E1, E2, E3;
pixel_t A, B, C, D, E, F, G, H, I;
A = pixel_get(x - 1, y - 1, src_ptr, src_slice, pixel, width, height, opt_tes);
B = pixel_get(x, y - 1, src_ptr, src_slice, pixel, width, height, opt_tes);
C = pixel_get(x + 1, y - 1, src_ptr, src_slice, pixel, width, height, opt_tes);
D = pixel_get(x - 1, y, src_ptr, src_slice, pixel, width, height, opt_tes);
E = pixel_get(x, y, src_ptr, src_slice, pixel, width, height, opt_tes);
F = pixel_get(x + 1, y, src_ptr, src_slice, pixel, width, height, opt_tes);
G = pixel_get(x - 1, y + 1, src_ptr, src_slice, pixel, width, height, opt_tes);
H = pixel_get(x, y + 1, src_ptr, src_slice, pixel, width, height, opt_tes);
I = pixel_get(x + 1, y + 1, src_ptr, src_slice, pixel, width, height, opt_tes);
/*
ABC
DEF
GHI
E0E1
E2E3
*/
switch (opt_ver) {
default:
case 0:
/* version 0, normal scaling */
E0 = E;
E1 = E;
E2 = E;
E3 = E;
break;
case 1:
/* default */
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;
break;
case 2:
/* scalek */
#define SCALE2K_BASE(A,B,C,D,E,F,G,H,I,E0,E1,E2,E3) \
if (D == B && B != E && D != E) { \
/* diagonal */ \
if (B == C && D == G) { \
/* square block */ \
} else if (B == C) { \
/* horizontal slope */ \
E0 = lerp(D, E0, 0.75); \
E1 = lerp(D, E1, 0.25); \
} else if (D == G) { \
/* vertical slope */ \
E0 = lerp(D, E0, 0.75); \
E2 = lerp(D, E2, 0.25); \
} else { \
/* pure diagonal */ \
E0 = lerp(E0,D,0.5); \
} \
}
/* Used by AdvanceMAME */
#define SCALE2K(A,B,C,D,E,F,G,H,I,E0,E1,E2,E3) \
if (D == B && B != E && D != E) { \
/* diagonal */ \
if (B == C && D == G) { \
/* square block */ \
if (A != E) { \
/* no star */ \
E0 = lerp(D, E0, 0.75); \
E1 = lerp(D, E1, 0.25); \
E2 = lerp(D, E2, 0.25); \
} \
} else if (B == C && C != F) { \
/* horizontal slope */ \
E0 = lerp(D, E0, 0.75); \
E1 = lerp(D, E1, 0.25); \
} else if (D == G && G != H) { \
/* vertical slope */ \
E0 = lerp(D, E0, 0.75); \
E2 = lerp(D, E2, 0.25); \
} else { \
/* pure diagonal */ \
E0 = lerp(E0,D,0.5); \
} \
}
#define SCALE2K_SPIKE(A,B,C,D,E,F,G,H,I,E0,E1,E2,E3) \
if (B != E && D != E) { \
if (D == B) { \
/* diagonal */ \
if (B == C && D == G) { \
/* square block */ \
} else if (B == C) { \
/* horizontal slope */ \
E0 = lerp(D, E0, 0.75); \
E1 = lerp(D, E1, 0.25); \
} else if (D == G) { \
/* vertical slope */ \
E0 = lerp(D, E0, 0.75); \
E2 = lerp(D, E2, 0.25); \
} else { \
/* pure diagonal */ \
E0 = lerp(E0,D,0.5); \
} \
} else if (A == B && G == E) { \
/* horizontal spike */ \
E0 = lerp(D, E0, 0.5); \
} else if (A == D && C == E) { \
/* vertical spike */ \
E0 = lerp(B, E0, 0.5); \
} \
}
#define SCALE2K_ALTERNATE(A,B,C,D,E,F,G,H,I,E0,E1,E2,E3) \
if (D == B && B != F && D != H) { \
/* diagonal */ \
if (B == C && D == G) { \
/* square block */ \
if (A != E) { \
E0 = lerp(D, E0, 0.75); \
E1 = lerp(D, E1, 0.25); \
E2 = lerp(D, E2, 0.25); \
} \
} else if (B == C && C != F) { \
/* horizontal slope */ \
E0 = lerp(D, E0, 0.75); \
E1 = lerp(D, E1, 0.25); \
} else if (D == G && G != H) { \
/* vertical slope */ \
E0 = lerp(D, E0, 0.75); \
E2 = lerp(D, E2, 0.25); \
} else { \
/* pure diagonal */ \
E0 = lerp(E0,D,0.5); \
} \
}
E0 = E1 = E2 = E3 = E;
SCALE2K(A, B, C, D, E, F, G, H, I, E0, E1, E2, E3);
SCALE2K(G, D, A, H, E, B, I, F, C, E2, E0, E3, E1);
SCALE2K(I, H, G, F, E, D, C, B, A, E3, E2, E1, E0);
SCALE2K(C, F, I, B, E, H, A, D, G, E1, E3, E0, E2);
break;
case 3:
/* rejected */
E0 = D == B && ((B != F && D != H) || D == A) ? D : E;
E1 = B == F && ((B != D && F != H) || B == C) ? F : E;
E2 = D == H && ((D != B && H != F) || D == G) ? D : E;
E3 = H == F && ((D != H && B != F) || H == I) ? F : E;
break;
case 4:
/* rejected, loses isolated pixels */
E0 = D == B && A != E ? D : E;
E1 = B == F && C != E ? F : E;
E2 = D == H && G != E ? D : E;
E3 = H == F && I != E ? F : E;
break;
}
pixel_put(x * 2, y * 2, dst_ptr, dst_slice, pixel, width * 2, height * 2, E0);
pixel_put(x * 2 + 1, y * 2, dst_ptr, dst_slice, pixel, width * 2, height * 2, E1);
pixel_put(x * 2, y * 2 + 1, dst_ptr, dst_slice, pixel, width * 2, height * 2, E2);
pixel_put(x * 2 + 1, y * 2 + 1, dst_ptr, dst_slice, pixel, width * 2, height * 2, E3);
}
}
}