-
Notifications
You must be signed in to change notification settings - Fork 0
/
overlaymanager.py
350 lines (282 loc) · 13.3 KB
/
overlaymanager.py
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
import os
import pathlib
from shutil import copy
from PIL import Image, ImageDraw
def writeConfig(outputfile, templatefile, formats):
of = open(outputfile, 'wt', encoding='utf-8')
with open(templatefile) as tf:
line = tf.readline()
while line:
of.write(line.format(**formats))
line = tf.readline()
of.close
def getTemplate(filename, folder, default, logger):
fullfilename = folder + filename
fulldefaultname = folder + default
if (os.path.exists(fullfilename)):
logger.debug("Using specific template file: %s" % fullfilename)
return fullfilename
elif (os.path.exists(fulldefaultname)):
logger.debug("Using default template file: %s" % fulldefaultname)
return fulldefaultname
else:
logger.error("Missing configuration file: %s" % fulldefaultname)
exit(-1)
def writeLayoutCfg(gamename, layoutcfgfilepath, imagename, viewport_width, viewport_height, viewport_x, viewport_y, bezel_width, bezel_height, logger):
layoutcfgfilename = gamename + ".lay"
layoutcfgfilefullpath = layoutcfgfilepath + layoutcfgfilename
formats = {'imagename': imagename, 'viewport_height': viewport_height, 'viewport_width': viewport_width, 'viewport_x': viewport_x,
'viewport_y': viewport_y, 'bezel_width': bezel_width, 'bezel_height': bezel_height}
template = getTemplate(gamename + ".lay", "templates\\layouts\\", "_default.lay", logger)
writeConfig(layoutcfgfilefullpath, template, formats)
#writeConfig(layoutcfgfilefullpath, "templates\\layouts\\template.lay", formats)
logger.info("Layout configuration file %s written" % layoutcfgfilefullpath)
def writeOverlayCfg(gamename, overlaycfgfilepath, imagename, logger):
overlaycfgfilename = gamename + ".cfg"
overlaycfgfilefullpath = overlaycfgfilepath + overlaycfgfilename
formats = {'imagename': imagename}
template = getTemplate(gamename + ".cfg", "templates\\overlays\\", "_default.cfg", logger)
#writeConfig(overlaycfgfilefullpath, "templates\\overlays\\template.cfg", formats)
writeConfig(overlaycfgfilefullpath, template, formats)
logger.info("Overlay configuration file %s written" % overlaycfgfilefullpath)
def writeCore(gamename, corecfgfilepath, realoverlaybasedir, viewport_width, viewport_height, viewport_x, viewport_y, logger):
corecfgfilename = gamename + ".cfg"
corecfgfilefullpath = corecfgfilepath + corecfgfilename
formats = {'viewport_height': viewport_height, 'viewport_width': viewport_width, 'viewport_x': viewport_x,
'viewport_y': viewport_y, 'realoverlaybasedir': realoverlaybasedir, 'corecfgfilename': corecfgfilename}
template = getTemplate(gamename + ".cfg", "templates\\config\\", "_default.cfg", logger)
#writeConfig(corecfgfilefullpath, "templates\\config\\template.cfg", formats)
writeConfig(corecfgfilefullpath, template, formats)
logger.info("Core configuration file %s written" % corecfgfilefullpath)
def writeShader(gamename, shadercfgfilepath, logger):
shadercfgfilename = gamename + ".cgp"
shadercfgfilefullpath = shadercfgfilepath + shadercfgfilename
formats = {}
template = getTemplate(gamename + ".cgp", "templates\\shaders\\", "_default.cgp", logger)
#writeConfig(shadercfgfilefullpath, "templates\\shaders\\template.cgp", formats)
writeConfig(shadercfgfilefullpath, template, formats)
logger.info("Shader configuration file %s written" % shadercfgfilefullpath)
def upscale():
gamex = 320
gamey = 224
screenx = 1920
screeny = 1080
z = 0
tx = 0
ty = 0
while (z < 10):
tx = gamex * z
ty = gamey * z
if ((tx > screenx) or (ty > screeny)):
z = z - 1
break
z = z + 1
targetx = gamex * z
targety = gamey * z
gamer = targetx / float(targety)
logger.info("Game size: %d * %d, ratio %.10f" % (gamex, gamey, gamex / float(gamey)))
logger.info("Target size: %d * %d, ratio %.10f (Multiplied X%d)" % (targetx, targety, gamer, z))
cips = 4 / float(3)
logger.info("4/3 = %.10f" % cips)
def ___getViewportAxisRange(axis, im, tt, padding, logger):
if (axis == "x"):
stop = im.size[0]
fixed = im.size[1] / 2
else:
stop = im.size[1]
fixed = im.size[0] / 2
start = 0
firsttransparent = 0
firstcounter = 0
logger.debug("Viewport scan on %s axis : fixed dimension at: %d" % (axis, fixed))
for i in range (start, stop):
if (axis == "x"):
pix = im.getpixel((i, fixed))
else:
pix = im.getpixel((fixed, i))
if pix[3] >= tt and firstcounter < padding:
firsttransparent = 0
lasttransparent = 0
firstcounter = 0
if pix[3] < tt and firsttransparent == 0 and firstcounter >= padding:
firsttransparent = i
logger.debug("Got first trasparent on %s axis at : %d" % (axis, firsttransparent))
if pix[3] < tt and firsttransparent != 0:
lasttransparent = i
if pix[3] < tt:
firstcounter = firstcounter +1
logger.debug("Got last trasparent on %s axis at : %d" % (axis, lasttransparent))
viewportsize = lasttransparent - firsttransparent
logger.debug("Viewport scan on %s axis : %d to %d (size %d)" % (axis, firsttransparent, lasttransparent, viewportsize))
return viewportsize, firsttransparent
def getViewportAxisRange(axis, im, tt, padding, logger):
if (axis == "x"):
stop = im.size[0]
fixed = im.size[1] / 2
else:
stop = im.size[1]
fixed = im.size[0] / 2
start = 0
firsttransparent = 0
lasttransparent = stop
firstcounter = 0
logger.debug("Viewport scan on %s axis from %d to %d, fixed dimension at: %d" % (axis, start, stop, fixed))
for i in range (start, stop):
if (axis == "x"):
pix = im.getpixel((i, fixed))
else:
pix = im.getpixel((fixed, i))
if pix[3] < tt and firsttransparent == 0:
firsttransparent = i
logger.debug("Got first trasparent on %s axis at : %d" % (axis, firsttransparent))
if pix[3] >= tt and firsttransparent != 0 and lasttransparent == stop:
lasttransparent = i-1
logger.debug("Got last trasparent on %s axis at : %d" % (axis, lasttransparent))
lasttransparent = lasttransparent - padding
firsttransparent = firsttransparent + padding
viewportsize = lasttransparent - firsttransparent
logger.debug("Viewport scan on %s axis : %d to %d (size %d)" % (axis, firsttransparent, lasttransparent, viewportsize))
return viewportsize, firsttransparent
def getViewportRange(im, tt, padding, logger):
xsize, firstxtransparent = getViewportAxisRange("x", im, tt, padding, logger)
ysize, firstytransparent = getViewportAxisRange("y", im, tt, padding, logger)
return firstxtransparent, firstytransparent, xsize, ysize
def resize(core, gamename, maxwidth, maxheight, marginx, marginy, customx, customy, mode, bc, tt, padding, config, logger):
imagename = gamename + ".png"
imagefilename = config['resize']['inputresizebasedir'] + imagename
overlaydir = config['resize']['outputresizebasedir']
os.makedirs(overlaydir, exist_ok=True)
logger.info("Resizing image: %s with mode %s" % (imagefilename, mode))
logger.debug('Max width / height: %s %s' % (maxwidth, maxheight))
if (not os.path.exists(imagefilename)):
logger.error('Missing image file: %s' % imagefilename)
exit(-1)
im = Image.open(imagefilename)
logger.debug("Original image data: %s %s %s %s %s" % (im.format, im.size, im.mode, mode, im.getbands()))
if (maxwidth == 0) :
maxwidth = im.width
if (maxheight == 0) :
maxheight = im.height
if (customx == 0) :
customx = im.width
if (customy == 0) :
customy = im.height
if (mode == 'custom') and (customx > maxwidth) :
logger.error("Custom X viewport size %d must be smaller than X target size %d ." % (customx, maxwidth))
return -1
if (mode == 'custom') and (customy > maxheight) :
logger.error("Custom Y viewport size %s must be smaller than Y target size %d." % (customy, maxheight))
return -1
if (maxwidth == im.width and maxheight == im.height and mode == 'outer') :
logger.info("No resizing needed")
copy(imagefilename, overlaydir + imagename)
else:
#get size of the viewport of the original overlay
viewport_x, viewport_y, viewport_width, viewport_height = getViewportRange(im, tt, padding, logger)
logger.info("Original viewport size: x %d y %d w %d h %d" % (viewport_x, viewport_y, viewport_width, viewport_height))
#sanity checks
if (viewport_width < 1 or viewport_height < 1) :
logger.error("No transparent viewport found.")
return -1
#calculate target size and area to base the resize operation depending on mode
if (mode == 'inner') :
areax = viewport_width
areay = viewport_height
resizewidth = maxwidth - marginx
resizeheight = maxheight - marginy
elif (mode == 'outer') :
areax = im.width
areay = im.height
resizewidth = maxwidth - marginx
resizeheight = maxheight - marginy
elif (mode == 'custom') :
areax = viewport_width
areay = viewport_height
resizewidth = customx
resizeheight = customy
else :
logger.error('Resize mode %s not supported' % mode)
#calculate the actual new ratio, width and height for the resized image
ratio = min(resizewidth/areax, resizeheight/areay)
new_width = round(im.width * ratio)
new_height = round(im.height * ratio)
#resize the image
newim = im.resize((new_width, new_height), Image.ANTIALIAS)
logger.debug("Resized image data: %s %s %s %s" % (im.format, newim.size, newim.mode, newim.getbands()))
#####newim.save(overlaydir + "resized_" + imagename, "PNG")
#get size of the viewport of the resized overlay
rviewport_x, rviewport_y, rviewport_width, rviewport_height = getViewportRange(newim, tt, padding, logger)
logger.info("Resized viewport size: x %d y %d w %d h %d" % (rviewport_x, rviewport_y, rviewport_width, rviewport_height))
#calculate offset for pasting the resized image and the mask, depending on resize mode
if (mode == 'inner') :
offsetx = round((maxwidth - rviewport_width) / 2 - rviewport_x)
offsety = round((maxheight - rviewport_height) / 2 -rviewport_y)
elif (mode == 'outer') :
offsetx = round((maxwidth - new_width) / 2)
offsety = round((maxheight - new_height) / 2)
elif (mode == 'custom') :
offsetx = round((maxwidth - rviewport_width) / 2 - rviewport_x)
offsety = round((maxheight - rviewport_height) / 2 -rviewport_y)
else :
logger.error('Resize mode %s not supported' % mode)
#create background image
backim = Image.new("RGBA", (maxwidth, maxheight), "#" + bc)
#####backim.save(overlaydir + "bkg_" + imagename, "PNG")
#create transparency mask
newmask = Image.new('RGBA', (new_width, new_height), (255, 255, 255, 0))
#paste mask over background
backim.paste(newmask, (offsetx, offsety))
#####backim.save(overlaydir + "mask_" + imagename, "PNG")
#paste resized image over backgound and mask
backim.paste(newim, (offsetx, offsety))
#####backim.save(overlaydir + "composite_" + imagename, "PNG")
#save overlay
backim.save(overlaydir + imagename, "PNG")
logger.info("Resided bezel %s written" % (overlaydir + imagename))
def generateOverlay(core, gamename, config, logger):
imagename = gamename + ".png"
imagefilename = config['overlay']['inputoverlaybasedir'] + imagename
overlaydir = config['overlay']['outputoverlaybasedir']
os.makedirs(overlaydir, exist_ok=True)
im = Image.open(imagefilename)
logger.debug("Overlay image data: %s %s %s %s" % (im.format, im.size, im.mode, im.getbands()))
#write overlay config file
writeOverlayCfg(gamename, overlaydir, imagename, logger)
def generateLayout(core, gamename, tt, padding, config, logger):
imagename = gamename + ".png"
imagefilename = config['layout']['inputlayoutbasedir'] + imagename
layoutdir = config['layout']['outputlayoutbasedir']
os.makedirs(layoutdir, exist_ok=True)
im = Image.open(imagefilename)
logger.debug("Layout image data: %s %s %s %s" % (im.format, im.size, im.mode, im.getbands()))
viewport_x, viewport_y, viewport_width, viewport_height = getViewportRange(im, tt, padding, logger)
#write layout config file
writeLayoutCfg(gamename, layoutdir, imagename, viewport_width, viewport_height, viewport_x, viewport_y, im.width, im.height, logger)
def generateCfg(core, gamename, tt, padding, config, logger):
imagename = gamename + ".png"
imagefilename = config['config']['inputoverlaybasedir'] + imagename
coredir = config['config']['outputcorebasedir'] + core + "\\"
im = Image.open(imagefilename)
logger.debug("Image data: %s %s %s %s" % (im.format, im.size, im.mode, im.getbands()))
viewport_x, viewport_y, viewport_width, viewport_height = getViewportRange(im, tt, padding, logger)
logger.info("Viewport: %s %s %s %s" % (viewport_x, viewport_y, viewport_width, viewport_height))
#print("Test new viewport size: ", newxsize, newysize)
if (viewport_width < 1 or viewport_height < 1) :
logger.error("No transparent viewport found.")
return -1
viewport_r = viewport_height / float(viewport_width)
logger.debug("Viewport size: [%d * %d], ratio %.10f" % (viewport_width, viewport_height, viewport_r))
realoverlaybasedir = config['general']['realoverlaybasedir']
os.makedirs(coredir, exist_ok=True)
writeCore(gamename, coredir, realoverlaybasedir, viewport_width, viewport_height, viewport_x, viewport_y, logger)
def generateShader(core, gamename, config, logger):
shaderdir = config['shader']['outputshaderbasedir'] + core + "\\"
os.makedirs(shaderdir, exist_ok=True)
writeShader(gamename, shaderdir, logger)
def copyOverlay(core, gamename, config):
imagename = gamename + ".png"
inputdir = config['general']['inputbasedir']
overlaydir = config['general']['outputoverlaybasedir']
os.makedirs(overlaydir, exist_ok=True)
copy(inputdir + imagename, overlaydir + imagename)
writeOverlay(gamename, overlaydir, imagename, logger)