-
Notifications
You must be signed in to change notification settings - Fork 7
/
gfxcon.py
executable file
·492 lines (454 loc) · 14.2 KB
/
gfxcon.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
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#!/usr/bin/env python
import os
if not os.path.isdir("vmsystem"):
print("changing to script location...")
os.chdir(os.path.dirname(os.path.abspath(__file__)))
import vmsystem.libbaltcalc as libbaltcalc
from vmsystem.libbaltcalc import btint
import vmsystem.libtextcon as tcon
import time
import sys
import vmsystem.iofuncts as iofuncts
import vmsystem.g2common as g2com
try:
import pygame
except ImportError:
sys.exit("ERROR: pygame not found. (REQUIRED)")
#just point xrange to range in python3.
vers=sys.version_info[0]
if vers==3:
xrange=range
def packart_maker(imagepath, notrailnew=0):
print("converting image...")
image=pygame.image.load(imagepath)
xsize=image.get_width()
ysize=image.get_height()
#print(image.get_height())
#print(ysize)
size=2
outf=open(imagepath.rsplit(".")[0]+".tas0", 'w')
outf.write('''#SBTCVM Gen2-9 GFXCON: ternary-packed art conversion.
#image file: ''' + imagepath + "\n")
outf.write("head-nspin=stdnsp\nfopset1;>io.ttywr\nfopset2;>io.packart\n")
for coly in xrange(0, ysize):
buffx=""
for linex in xrange(0, xsize):
pixcol = image.get_at((linex, coly))
level = (pixcol[0] + pixcol[1] + pixcol[2])//3
if level<85:
buffx+="-"
elif level<170:
buffx+="0"
else:
buffx+="+"
if len(buffx)==9:
outf.write("fopwri2;" + buffx + "\n")
size+=1
buffx=""
if linex>=81:
print("WARNING: IMAGE BEYOND MAXIMUM TERNARY PACKED ART WIDTH \n ALLOWED BY PYGAME FRONTEND (81)")
while len(buffx)<9 and buffx!="":
buffx+="-"
if buffx!="":
outf.write("fopwri2;" + buffx + "\n")
size+=1
if coly<ysize-1 or notrailnew==0:
outf.write("fopwri1;:\\n\n")
size+=1
#print(coly)
outf.close()
print("Done.")
print(g2com.nonetformatted_smart(size))
return
def colart_maker(imagepath, notrailnew=0):
print("converting image...")
image=pygame.image.load(imagepath)
xsize=image.get_width()
ysize=image.get_height()
#print(image.get_height())
#print(ysize)
size=3
outf=open(imagepath.rsplit(".")[0]+".tas0", 'w')
outf.write('''#SBTCVM Gen2-9 GFXCON: ternary-packed art conversion.
#image file: ''' + imagepath + "\n")
outf.write("head-nspin=stdnsp\nfopset1;>io.ttywr\nfopset2;>io.cpack\n")
for coly in xrange(0, ysize):
buffx=""
for linex in xrange(0, xsize):
pixcol = image.get_at((linex, coly))
for level in (pixcol[0], pixcol[1], pixcol[2]):
if level<85:
buffx+="-"
elif level<170:
buffx+="0"
else:
buffx+="+"
if len(buffx)==9:
outf.write("fopwri2;" + buffx + "\n")
size+=1
buffx=""
if linex>=81:
print("WARNING: IMAGE BEYOND MAXIMUM TERNARY PACKED ART WIDTH \n ALLOWED BY PYGAME FRONTEND (81)")
while len(buffx)<9 and buffx!="":
buffx+="-"
if buffx!="":
outf.write("fopwri2;" + buffx + "\n")
size+=1
if coly<ysize-1 or notrailnew==0:
outf.write("fopwri1;:\\n\n")
size+=1
#print(coly)
outf.write('fopset2;>io.packart\n')
outf.close()
print("Done.")
print(g2com.nonetformatted_smart(size))
return
def colart_maker_RLE(imagepath, notrailnew=0):
print("converting image...")
image=pygame.image.load(imagepath)
xsize=image.get_width()
ysize=image.get_height()
if xsize>=79:
print("Using Full-TTY-width CPRLE Encoding")
else:
print("Image is narrower than TTY. using line-broken CPRLE.")
#print(image.get_height())
#print(ysize)
size=1
pbuff=None
bufflen=0
outf=open(imagepath.rsplit(".")[0]+".tas0", 'w')
outf.write('''#SBTCVM Gen2-9 GFXCON: ternary-packed art conversion. uses Run-length Encoding.
#Needs to be decoded manually.
#image file: ''' + imagepath + "\n"
'''null;>.DATASIZE
''')
for coly in xrange(0, ysize):
buffx=""
for linex in xrange(0, xsize):
pixcol = image.get_at((linex, coly))
for level in (pixcol[0], pixcol[1], pixcol[2]):
if level<85:
buffx+="-"
elif level<170:
buffx+="0"
else:
buffx+="+"
if len(buffx)==9:
if buffx==pbuff:
bufflen+=1
buffx=""
elif pbuff==None:
pbuff=buffx
buffx=""
size+=1
bufflen=0
else:
outf.write("raw;10x" + str(bufflen) + "," + pbuff + "\n")
pbuff=buffx
buffx=""
size+=1
bufflen=0
buffx=""
if linex>=81:
print("WARNING: IMAGE BEYOND MAXIMUM TERNARY PACKED ART WIDTH \n ALLOWED BY PYGAME FRONTEND (81)")
while len(buffx)<9 and buffx!="":
buffx+="---"
linex+=1
if linex<80 or coly==ysize-1:
outf.write("raw;10x" + str(bufflen) + "," + pbuff + "\n")
if buffx!="":
outf.write("raw;10x" + str(bufflen) + "," + buffx + "\n")
pbuff=None
size+=1
bufflen=0
if (coly<ysize-1) and linex<80:
outf.write("raw;-,0\n")
elif coly==ysize-1 and notrailnew==0:
outf.write("raw;-,0\n")
outf.write("###LINE DIV\n")
#print(coly)
outf.write("null;;.DATASIZE\n")
outf.close()
print("Done.")
print(g2com.nonetformatted_smart(size))
return
def plot_BIN_RLE(imagepath, lineinterpol=0):
print("converting image into BINRLE plotter image format...")
image=pygame.image.load(imagepath)
xsize=image.get_width()
ysize=image.get_height()
pixcol=None
#print(image.get_height())
#print(ysize)
size=1
pbuff=None
bufflen=0
bank=1
#find initial color state
if lineinterpol==0:
color_bool=binencode(image.get_at((0, 0)), offset=0)
elif lineinterpol==2:
color_bool=binencode(image.get_at((0, 0)), offset=-51-25)
else:
color_bool=binencode(image.get_at((0, 0)), offset=42)
outf=open(imagepath.rsplit(".")[0]+".tas0", 'w')
outf.write('''#SBTCVM Gen2-9 GFXCON: run-length encoded tritmap
#Needs to be decoded manually.
#image file: ''' + imagepath + "\n"
'''raw;10x''' + str(xsize) + ''',10x''' + str(color_bool) + '''
''')
for coly in xrange(0, ysize):
for linex in xrange(0, xsize):
pixcol = image.get_at((linex, coly))
if lineinterpol==0:
buffx=binencode(pixcol, offset=0)
elif lineinterpol==2:
if coly % 4 == 0:
buffx=binencode(pixcol, offset=-51-25)
elif coly % 4 == 1:
buffx=binencode(pixcol, offset=-51+25)
elif coly % 4 == 2:
buffx=binencode(pixcol, offset=51-25)
else:
buffx=binencode(pixcol, offset=51+25)
else:
if coly % 2:
buffx=binencode(pixcol, offset=-42)
else:
buffx=binencode(pixcol, offset=42)
if buffx==pbuff:
bufflen+=1
elif pbuff==None:
pbuff=buffx
bufflen=0
else:
bufflen, bank, size = binformat(bufflen, bank, outf, size)
pbuff=buffx
bufflen=0
if pbuff!=None:
bufflen, bank, size = binformat(bufflen, bank, outf, size)
#write exit code
if bank==0:
#if last raw statement unfinished, finish it with terminator zero.
outf.write("10x0\n")
size+=1
else:
#else, finish it with blank null as terminator.
outf.write("null\n")
size+=1
outf.close()
print("Done.")
print(g2com.nonetformatted_smart(size))
return
def binformat(bufflen, bank, outf, size):
splitup=True
bufflen+=1
start=True
while splitup:
#DO NOT USE 9841! it will integer-overflow the BINRLE module's decoder algorithm!
if bufflen>9041:
bufflen-=9041
if start==True:
presetval="9041"
start=0
else:
presetval="-9041"
if bank==1:
bank=0
outf.write("raw;10x" + presetval + ",")
else:
bank=1
outf.write("10x" + presetval + "\n")
size+=1
if bufflen<=9841 and bufflen>0:
splitup=False
if bank==1:
bank=0
outf.write("raw;10x" + str(-bufflen) + ",")
else:
bank=1
outf.write("10x" + str(-bufflen) + "\n")
elif bufflen>0:
splitup=False
if bank==1:
bank=0
outf.write("raw;10x" + str(bufflen) + ",")
else:
bank=1
outf.write("10x" + str(bufflen) + "\n")
size+=1
bufflen=0
if bufflen<=0:
splitup=False
return bufflen, bank, size
def binencode(rgb, offset=0):
r=rgb[0]+offset
g=rgb[1]+offset
b=rgb[2]+offset
mono=(r+g+b)/3.0
if mono>127:
return 1
else:
return 0
def plot_RLE(imagepath, lineinterpol=0, threshold=None):
print("converting image into PLRLE plotter image format...")
if threshold!=None:
try:
threshold=int(threshold)
except ValueError:
sys.exit("INVALID THRESHOLD ARGUMENT!")
if threshold<0:
sys.exit("INVALID THRESHOLD ARGUMENT!")
image=pygame.image.load(imagepath)
xsize=image.get_width()
ysize=image.get_height()
pixcol=None
#print(image.get_height())
#print(ysize)
size=1
pbuff=None
bufflen=0
outf=open(imagepath.rsplit(".")[0]+".tas0", 'w')
outf.write('''#SBTCVM Gen2-9 GFXCON: run-length encoded tritmap
#Needs to be decoded manually.
#image file: ''' + imagepath + "\n"
'''null;>.DATASIZE
''')
for coly in xrange(0, ysize):
buffx=""
for linex in xrange(0, xsize):
prevpix = pixcol
pixcol = image.get_at((linex, coly))
if threshold!=None and prevpix!=None:
if abs(prevpix[0]-pixcol[0])<threshold and abs(prevpix[1]-pixcol[1])<threshold and abs(prevpix[2]-pixcol[2])<threshold:
pixcol = prevpix
if lineinterpol==0:
buffx=bin8totrit3(pixcol[0])+bin8totrit3(pixcol[1])+bin8totrit3(pixcol[2])
else:
if coly % 2:
buffx=bin8totrit3(pixcol[0]-5)+bin8totrit3(pixcol[1]-5)+bin8totrit3(pixcol[2]-5)
else:
buffx=bin8totrit3(pixcol[0])+bin8totrit3(pixcol[1])+bin8totrit3(pixcol[2])
if buffx==pbuff:
bufflen+=1
buffx=""
elif pbuff==None:
pbuff=buffx
buffx=""
size+=1
bufflen=0
else:
outf.write("raw;10x" + str(bufflen) + "," + pbuff + "\n")
pbuff=buffx
buffx=""
size+=1
bufflen=0
if pbuff!=None:
outf.write("raw;10x" + str(bufflen) + "," + pbuff + "\n")
pbuff=None
buffx=""
size+=1
bufflen=0
outf.write("raw;-,0\n")
outf.write("###LINE DIV\n")
#print(coly)
outf.write("null;;.DATASIZE\n")
outf.close()
print("Done.")
print(g2com.nonetformatted_smart(size))
return
def bin8totrit3(level):
if level<0:
level=0
if level>255:
level=255
#print(level)
xtemp=btint(int(level/9.44444444)-13)
#print(xtemp.intval)
xtemp.changeval(xtemp.dectrunk(3))
return xtemp.bttrunk(3)
if __name__=="__main__":
try:
cmd=sys.argv[1]
except IndexError:
cmd=None
try:
arg=sys.argv[2]
except IndexError:
arg=None
try:
arg2=sys.argv[3]
except IndexError:
arg2=None
if cmd in ["-h", "--help"]:
print('''SBTCVM Gen2-9 gfx conversion utility.
-p(n) [image] : convert an image into 3-color ternary-packed art,
and place it in a tas0 file. append n for no trailing newline.
-cp(n) [image] : convert an image into 27-color ternary packed art,
and place it in a tas0 file. append n for no trailing newline.
-cprle(n) [image] : convert an image into 27-color cprle compressed
packed art image data.
-plrle(i) [image] (threshold) : convert an image into 9-trit (PLRLE format) rle compressed.
tritmap image. append i for line-based striped interpolation.
follow image name by an OPTIONAL threshold integer value a color channel must change for encoder to change colors.
(at cost of detail)
-binrle [image] : boolean (2 tone) RLE-compressed tritmap format. uses BINRLE module.
-binrlei [image] : same as -binrle, only enable 2-line interpolation.
-binrlei4 [image] : same as -binrle, only enable 4-line interpolation.''')
elif cmd in ["-v", "--version"]:
print("SBTCVM Gen2-9 gfx conversion utility. v1.0.0\n" + "part of SBTCVM-Gen2-9 v2.1.0.alpha")
elif cmd in ["-a", "--about"]:
print('''SBTCVM Gen2-9 gfx conversion utility.
v1.0.0
part of SBTCVM-Gen2-9 (v2.1.0.alpha)
Copyright (c) 2016-2022 Thomas Leathers and Contributors
see readme.md for more information and licensing of media.
SBTCVM Gen2-9 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 3 of the License, or
(at your option) any later version.
SBTCVM Gen2-9 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 SBTCVM Gen2-9. If not, see <http://www.gnu.org/licenses/>
''')
elif cmd in ["-p"]:
print("Ternary-Packed art encoder.")
packart_maker(iofuncts.findtrom(arg, ext=".png", exitonfail=1, exitmsg="image file was not found. STOP", dirauto=1))
elif cmd in ["-cp"]:
print("Color Ternary-Packed art encoder.")
colart_maker(iofuncts.findtrom(arg, ext=".png", exitonfail=1, exitmsg="image file was not found. STOP", dirauto=1))
elif cmd in ["-pn"]:
print("Ternary-Packed art encoder.")
packart_maker(iofuncts.findtrom(arg, ext=".png", exitonfail=1, exitmsg="image file was not found. STOP", dirauto=1), 1)
elif cmd in ["-cpn"]:
print("Color Ternary-Packed art encoder.")
colart_maker(iofuncts.findtrom(arg, ext=".png", exitonfail=1, exitmsg="image file was not found. STOP", dirauto=1), 1)
elif cmd in ["-cprle"]:
print("Color Ternary-Packed art encoder (RLE compressed).")
colart_maker_RLE(iofuncts.findtrom(arg, ext=".png", exitonfail=1, exitmsg="image file was not found. STOP", dirauto=1))
elif cmd in ["-cprlen"]:
print("Color Ternary-Packed art encoder (RLE compressed).")
colart_maker_RLE(iofuncts.findtrom(arg, ext=".png", exitonfail=1, exitmsg="image file was not found. STOP", dirauto=1), 1)
elif cmd in ["-plrle"]:
print("RLE-encoded 9-trit RGB tritmap.")
plot_RLE(iofuncts.findtrom(arg, ext=".png", exitonfail=1, exitmsg="image file was not found. STOP", dirauto=1), lineinterpol=0, threshold=arg2)
elif cmd in ["-plrlei"]:
print("RLE-encoded 9-trit RGB tritmap. (line interpolated)")
plot_RLE(iofuncts.findtrom(arg, ext=".png", exitonfail=1, exitmsg="image file was not found. STOP", dirauto=1), lineinterpol=1, threshold=arg2)
elif cmd in ["-binrle"]:
print("RLE-encoded boolean tritmap.")
plot_BIN_RLE(iofuncts.findtrom(arg, ext=".png", exitonfail=1, exitmsg="image file was not found. STOP", dirauto=1), lineinterpol=0)
elif cmd in ["-binrlei"]:
print("RLE-encoded boolean tritmap. (line interpolated)")
plot_BIN_RLE(iofuncts.findtrom(arg, ext=".png", exitonfail=1, exitmsg="image file was not found. STOP", dirauto=1), lineinterpol=1)
elif cmd in ["-binrlei4"]:
print("RLE-encoded boolean tritmap. (4 line interpolated)")
plot_BIN_RLE(iofuncts.findtrom(arg, ext=".png", exitonfail=1, exitmsg="image file was not found. STOP", dirauto=1), lineinterpol=2)
elif cmd == None:
print("Tip: try gfxcon.py -h for help.")
elif cmd.startswith("-"):
print("Unknown option: '" + cmd + "' try gfxcon.py -h for help.")