forked from n0spaces/py-super-xbr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
superxbr.pyx
416 lines (340 loc) · 17.2 KB
/
superxbr.pyx
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
from __future__ import print_function
import numpy as np
from PIL import Image
from cpython cimport bool
cimport numpy as np
# This is a port of https://pastebin.com/cbH8ZQQT
# *** Super-xBR code begins here - MIT LICENSE ***
# ******* Super XBR Scaler *******
#
# Copyright (c) 2016 Hyllian - [email protected]
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
cdef int rb(col):
"""Get red byte"""
return col >> 0 & 0xff
cdef int gb(col):
"""Get green byte"""
return col >> 8 & 0xff
cdef int bb(col):
"""Get blue byte"""
return col >> 16 & 0xff
cdef int ab(col):
"""Get alpha byte"""
return col >> 24 & 0xff
# weights
cdef double wgt1 = 0.129633
cdef double wgt2 = 0.175068
cdef double w1 = -wgt1
cdef double w2 = wgt1 + 0.5
cdef double w3 = -wgt2
cdef double w4 = wgt2 + 0.5
cdef double df(double x, double y):
"""Get absolute difference of two values"""
return abs(x - y)
cdef double clamp(double x, double low, double high):
"""Clamp x between low and high"""
return max(min(x, high), low)
'''
P1
|P0|B |C |P1| C F4 |a0|b1|c2|d3|
|D |E |F |F4| B F I4 |b0|c1|d2|e3| |e1|i1|i2|e2|
|G |H |I |I4| P0 E A I P3 |c0|d1|e2|f3| |e3|i3|i4|e4|
|P2|H5|I5|P3| D H I5 |d0|e1|f2|g3|
G H5
P2
sx, sy
-1 -1 | -2 0 (x+y) (x-y) -3 1 (x+y-1) (x-y+1)
-1 0 | -1 -1 -2 0
-1 1 | 0 -2 -1 -1
-1 2 | 1 -3 0 -2
0 -1 | -1 1 (x+y) (x-y) ... ... ...
0 0 | 0 0
0 1 | 1 -1
0 2 | 2 -2
1 -1 | 0 2 ...
1 0 | 1 1
1 1 | 2 0
1 2 | 3 -1
2 -1 | 1 3 ...
2 0 | 2 2
2 1 | 3 1
2 2 | 4 0
'''
# noinspection PyUnresolvedReferences
cdef int diagonal_edge(np.ndarray[double, ndim=2] mat, tuple wp):
cdef int dw1 = (
wp[0] * (df(mat[0, 2], mat[1, 1]) + df(mat[1, 1], mat[2, 0]) +
df(mat[1, 3], mat[2, 2]) + df(mat[2, 2], mat[3, 1])) +
wp[1] * (df(mat[0, 3], mat[1, 2]) + df(mat[2, 1], mat[3, 0])) +
wp[2] * (df(mat[0, 3], mat[2, 1]) + df(mat[1, 2], mat[3, 0])) +
wp[3] * df(mat[1, 2], mat[2, 1]) +
wp[4] * (df(mat[0, 2], mat[2, 0]) + df(mat[1, 3], mat[3, 1])) +
wp[5] * (df(mat[0, 1], mat[1, 0]) + df(mat[2, 3], mat[3, 2]))
)
cdef int dw2 = (
wp[0] * (df(mat[0, 1], mat[1, 2]) + df(mat[1, 2], mat[2, 3]) +
df(mat[1, 0], mat[2, 1]) + df(mat[2, 1], mat[3, 2])) +
wp[1] * (df(mat[0, 0], mat[1, 1]) + df(mat[2, 2], mat[3, 3])) +
wp[2] * (df(mat[0, 0], mat[2, 2]) + df(mat[1, 1], mat[3, 3])) +
wp[3] * df(mat[1, 1], mat[2, 2]) +
wp[4] * (df(mat[1, 0], mat[3, 2]) + df(mat[0, 1], mat[2, 3])) +
wp[5] * (df(mat[0, 2], mat[1, 3]) + df(mat[2, 0], mat[3, 1]))
)
return dw1 - dw2
# Super-xBR scaling
cdef bytes scale_from_buffer(bytes buffer, int w, int h, bool display_progress=True):
cdef np.ndarray[np.uint32_t, ndim=1] data = np.frombuffer(buffer, np.uint32)
# only scaling by a factor of 2 is supported
cdef int f = 2
cdef int outw = w * f
cdef int outh = h * f
# progress feedback
cdef int progress_current = 0
cdef int progress_total = (w * h * 2) + (outh * outw)
# output image buffer array
cdef np.ndarray[np.uint32_t] out = np.empty(outw * outh, np.uint32)
cdef tuple wp = (2, 1, -1, 4, -1, 1)
cdef np.ndarray[double, ndim=2] r = np.empty((4, 4), float)
cdef np.ndarray[double, ndim=2] g = np.empty((4, 4), float)
cdef np.ndarray[double, ndim=2] b = np.empty((4, 4), float)
cdef np.ndarray[double, ndim=2] a = np.empty((4, 4), float)
cdef np.ndarray[double, ndim=2] luma = np.empty((4, 4), float)
# declare cython var types for much improved performance
cdef np.uint32_t sample
cdef int cx, cy, csx, csy
cdef double min_r_sample, min_g_sample, min_b_sample, min_a_sample
cdef double max_r_sample, max_g_sample, max_b_sample, max_a_sample
cdef double rf, gf, bf, af
cdef int ri, gi, bi, ai
# first pass
for y in range(0, outh, 2):
for x in range(0, outw, 2):
# central pixels on original image
cx = x // f
cy = y // f
# sample supporting pixels in original image
for sx in range(-1, 3):
for sy in range(-1, 3):
# clamp pixel locations
csy = int(clamp(sy + cy, 0, h - 1))
csx = int(clamp(sx + cx, 0, w - 1))
# sample & add weight components
sample = data[csy * w + csx]
r[sx + 1, sy + 1] = rb(sample)
g[sx + 1, sy + 1] = gb(sample)
b[sx + 1, sy + 1] = bb(sample)
a[sx + 1, sy + 1] = ab(sample)
luma[sx + 1, sy + 1] = (0.2126 * r[sx + 1, sy + 1] +
0.7152 * g[sx + 1, sy + 1] +
0.0722 * b[sx + 1, sy + 1])
min_r_sample = min(r[1, 1], r[1, 2], r[2, 1], r[2, 2])
min_g_sample = min(g[1, 1], g[1, 2], g[2, 1], g[2, 2])
min_b_sample = min(b[1, 1], b[1, 2], b[2, 1], b[2, 2])
min_a_sample = min(a[1, 1], a[1, 2], a[2, 1], a[2, 2])
max_r_sample = max(r[1, 1], r[1, 2], r[2, 1], r[2, 2])
max_g_sample = max(g[1, 1], g[1, 2], g[2, 1], g[2, 2])
max_b_sample = max(b[1, 1], b[1, 2], b[2, 1], b[2, 2])
max_a_sample = max(a[1, 1], a[1, 2], a[2, 1], a[2, 2])
d_edge = diagonal_edge(luma, wp)
# generate and write result
if d_edge <= 0:
rf = w1 * (r[0, 3] + r[3, 0]) + w2 * (r[1, 2] + r[2, 1])
gf = w1 * (g[0, 3] + g[3, 0]) + w2 * (g[1, 2] + g[2, 1])
bf = w1 * (b[0, 3] + b[3, 0]) + w2 * (b[1, 2] + b[2, 1])
af = w1 * (a[0, 3] + a[3, 0]) + w2 * (a[1, 2] + a[2, 1])
else:
rf = w1 * (r[0, 0] + r[3, 3]) + w2 * (r[1, 1] + r[2, 2])
gf = w1 * (g[0, 0] + g[3, 3]) + w2 * (g[1, 1] + g[2, 2])
bf = w1 * (b[0, 0] + b[3, 3]) + w2 * (b[1, 1] + b[2, 2])
af = w1 * (a[0, 0] + a[3, 3]) + w2 * (a[1, 1] + a[2, 2])
# anti-ringing, clamp
rf = clamp(rf, min_r_sample, max_r_sample)
gf = clamp(gf, min_g_sample, max_g_sample)
bf = clamp(bf, min_b_sample, max_b_sample)
af = clamp(af, min_a_sample, max_a_sample)
ri = int(clamp(np.ceil(rf), 0, 255))
gi = int(clamp(np.ceil(gf), 0, 255))
bi = int(clamp(np.ceil(bf), 0, 255))
ai = int(clamp(np.ceil(af), 0, 255))
out[y * outw + x] = out[y * outw + x + 1] = out[(y + 1) * outw + x] = data[cy * w + cx]
out[(y + 1) * outw + x + 1] = (ai << 24) | (bi << 16) | (gi << 8) | ri
if display_progress:
progress_current += 1
print('({}/{})'.format(progress_current, progress_total), end='\r')
# second pass
wp = (2, 0, 0, 0, 0, 0)
for y in range(0, outh, 2):
for x in range(0, outw, 2):
# sample supporting pixels in original image
for sx in range(-1, 3):
for sy in range(-1, 3):
# clamp pixel locations
csy = int(clamp(sx - sy + y, 0, f * h - 1))
csx = int(clamp(sx + sy + x, 0, f * w - 1))
# sample & add weighted components
sample = out[csy * outw + csx]
r[sx + 1, sy + 1] = rb(sample)
g[sx + 1, sy + 1] = gb(sample)
b[sx + 1, sy + 1] = bb(sample)
a[sx + 1, sy + 1] = ab(sample)
luma[sx + 1, sy + 1] = (0.2126 * r[sx + 1, sy + 1] +
0.7152 * g[sx + 1, sy + 1] +
0.0722 * b[sx + 1, sy + 1])
min_r_sample = min(r[1, 1], r[1, 2], r[2, 1], r[2, 2])
min_g_sample = min(g[1, 1], g[1, 2], g[2, 1], g[2, 2])
min_b_sample = min(b[1, 1], b[1, 2], b[2, 1], b[2, 2])
min_a_sample = min(a[1, 1], a[1, 2], a[2, 1], a[2, 2])
max_r_sample = max(r[1, 1], r[1, 2], r[2, 1], r[2, 2])
max_g_sample = max(g[1, 1], g[1, 2], g[2, 1], g[2, 2])
max_b_sample = max(b[1, 1], b[1, 2], b[2, 1], b[2, 2])
max_a_sample = max(a[1, 1], a[1, 2], a[2, 1], a[2, 2])
d_edge = diagonal_edge(luma, wp)
# generate and write result
if d_edge <= 0:
rf = w1 * (r[0, 3] + r[3, 0]) + w2 * (r[1, 2] + r[2, 1])
gf = w1 * (g[0, 3] + g[3, 0]) + w2 * (g[1, 2] + g[2, 1])
bf = w1 * (b[0, 3] + b[3, 0]) + w2 * (b[1, 2] + b[2, 1])
af = w1 * (a[0, 3] + a[3, 0]) + w2 * (a[1, 2] + a[2, 1])
else:
rf = w1 * (r[0, 0] + r[3, 3]) + w2 * (r[1, 1] + r[2, 2])
gf = w1 * (g[0, 0] + g[3, 3]) + w2 * (g[1, 1] + g[2, 2])
bf = w1 * (b[0, 0] + b[3, 3]) + w2 * (b[1, 1] + b[2, 2])
af = w1 * (a[0, 0] + a[3, 3]) + w2 * (a[1, 1] + a[2, 2])
# anti-ringing, clamp
rf = clamp(rf, min_r_sample, max_r_sample)
gf = clamp(gf, min_g_sample, max_g_sample)
bf = clamp(bf, min_b_sample, max_b_sample)
af = clamp(af, min_a_sample, max_a_sample)
ri = int(clamp(np.ceil(rf), 0, 255))
gi = int(clamp(np.ceil(gf), 0, 255))
bi = int(clamp(np.ceil(bf), 0, 255))
ai = int(clamp(np.ceil(af), 0, 255))
out[y * outw + x + 1] = (ai << 24) | (bi << 16) | (gi << 8) | ri
for sx in range(-1, 3):
for sy in range(-1, 3):
# clamp pixel locations
csy = int(clamp(sx - sy + 1 + y, 0, f * h - 1))
csx = int(clamp(sx + sy - 1 + x, 0, f * w - 1))
# sample and add weighted components
sample = out[csy * outw + csx]
r[sx + 1, sy + 1] = rb(sample)
g[sx + 1, sy + 1] = gb(sample)
b[sx + 1, sy + 1] = bb(sample)
a[sx + 1, sy + 1] = ab(sample)
luma[sx + 1, sy + 1] = (0.2126 * r[sx + 1, sy + 1] +
0.7152 * g[sx + 1, sy + 1] +
0.0722 * b[sx + 1, sy + 1])
d_edge = diagonal_edge(luma, wp)
# generate and write result
if d_edge <= 0:
rf = w1 * (r[0, 3] + r[3, 0]) + w2 * (r[1, 2] + r[2, 1])
gf = w1 * (g[0, 3] + g[3, 0]) + w2 * (g[1, 2] + g[2, 1])
bf = w1 * (b[0, 3] + b[3, 0]) + w2 * (b[1, 2] + b[2, 1])
af = w1 * (a[0, 3] + a[3, 0]) + w2 * (a[1, 2] + a[2, 1])
else:
rf = w1 * (r[0, 0] + r[3, 3]) + w2 * (r[1, 1] + r[2, 2])
gf = w1 * (g[0, 0] + g[3, 3]) + w2 * (g[1, 1] + g[2, 2])
bf = w1 * (b[0, 0] + b[3, 3]) + w2 * (b[1, 1] + b[2, 2])
af = w1 * (a[0, 0] + a[3, 3]) + w2 * (a[1, 1] + a[2, 2])
# anti-ringing, clamp
rf = clamp(rf, min_r_sample, max_r_sample)
gf = clamp(gf, min_g_sample, max_g_sample)
bf = clamp(bf, min_b_sample, max_b_sample)
af = clamp(af, min_a_sample, max_a_sample)
ri = int(clamp(np.ceil(rf), 0, 255))
gi = int(clamp(np.ceil(gf), 0, 255))
bi = int(clamp(np.ceil(bf), 0, 255))
ai = int(clamp(np.ceil(af), 0, 255))
out[(y + 1) * outw + x] = (ai << 24) | (bi << 16) | (gi << 8) | ri
if display_progress:
progress_current += 1
print('({}/{})'.format(progress_current, progress_total), end='\r')
# third pass
wp = (2, 1, -1, 4, -1, 1)
for y in range(outh - 1, -1, -1):
for x in range(outw - 1, -1, -1):
for sx in range(-2, 2):
for sy in range(-2, 2):
# clamp pixel locations
csy = int(clamp(sy + y, 0, f * h - 1))
csx = int(clamp(sx + x, 0, f * w - 1))
# sample & add weighted components
sample = out[csy * outw + csx]
r[sx + 2, sy + 2] = rb(sample)
g[sx + 2, sy + 2] = gb(sample)
b[sx + 2, sy + 2] = bb(sample)
a[sx + 2, sy + 2] = ab(sample)
luma[sx + 2, sy + 2] = (0.2126 * r[sx + 1, sy + 1] +
0.7152 * g[sx + 1, sy + 1] +
0.0722 * b[sx + 1, sy + 1])
min_r_sample = min(r[1, 1], r[1, 2], r[2, 1], r[2, 2])
min_g_sample = min(g[1, 1], g[1, 2], g[2, 1], g[2, 2])
min_b_sample = min(b[1, 1], b[1, 2], b[2, 1], b[2, 2])
min_a_sample = min(a[1, 1], a[1, 2], a[2, 1], a[2, 2])
max_r_sample = max(r[1, 1], r[1, 2], r[2, 1], r[2, 2])
max_g_sample = max(g[1, 1], g[1, 2], g[2, 1], g[2, 2])
max_b_sample = max(b[1, 1], b[1, 2], b[2, 1], b[2, 2])
max_a_sample = max(a[1, 1], a[1, 2], a[2, 1], a[2, 2])
d_edge = diagonal_edge(luma, wp)
# generate and write result
if d_edge <= 0:
rf = w1 * (r[0, 3] + r[3, 0]) + w2 * (r[1, 2] + r[2, 1])
gf = w1 * (g[0, 3] + g[3, 0]) + w2 * (g[1, 2] + g[2, 1])
bf = w1 * (b[0, 3] + b[3, 0]) + w2 * (b[1, 2] + b[2, 1])
af = w1 * (a[0, 3] + a[3, 0]) + w2 * (a[1, 2] + a[2, 1])
else:
rf = w1 * (r[0, 0] + r[3, 3]) + w2 * (r[1, 1] + r[2, 2])
gf = w1 * (g[0, 0] + g[3, 3]) + w2 * (g[1, 1] + g[2, 2])
bf = w1 * (b[0, 0] + b[3, 3]) + w2 * (b[1, 1] + b[2, 2])
af = w1 * (a[0, 0] + a[3, 3]) + w2 * (a[1, 1] + a[2, 2])
# anti-ringing, clamp
rf = clamp(rf, min_r_sample, max_r_sample)
gf = clamp(gf, min_g_sample, max_g_sample)
bf = clamp(bf, min_b_sample, max_b_sample)
af = clamp(af, min_a_sample, max_a_sample)
ri = int(clamp(np.ceil(rf), 0, 255))
gi = int(clamp(np.ceil(gf), 0, 255))
bi = int(clamp(np.ceil(bf), 0, 255))
ai = int(clamp(np.ceil(af), 0, 255))
out[y * outw + x] = (ai << 24) | (bi << 16) | (gi << 8) | ri
if display_progress:
progress_current += 1
print('({}/{})'.format(progress_current, progress_total), end='\r')
if display_progress:
print()
return out.tobytes()
# *** Super-xBR code ends here - MIT LICENSE ***
def scale(im: Image.Image, int passes=1, print_progress=False) -> Image.Image:
"""
Apply the Super-xBR upscale filter to an image.
:param im: The PIL Image object to apply the filter to. For best results, it should be a small pixel-art image.
:param passes: The number of times to apply the filter. The image scale is doubled each time the filter is applied.
:param print_progress: If True, progress will be displayed while the filter is being applied. This is good when
working with multiple passes or large images.
:return: A larger image with the filter applied.
"""
if im.mode != 'RGBA':
im = im.convert('RGBA')
cdef bytes im_buffer = im.tobytes()
for p in range(passes):
if print_progress:
print('pass {} of {}...'.format(p + 1, passes))
im_buffer = scale_from_buffer(im_buffer, im.width * (2 ** p), im.height * (2 ** p), print_progress)
return Image.frombuffer('RGBA', (int(im.width * (2 ** passes)), int(im.height * (2 ** passes))), im_buffer)