mirror of
https://github.com/laanwj/k210-sdk-stuff.git
synced 2024-11-22 09:26:21 +04:00
rust: k210-console: Fixes for colorfont generation script
This commit is contained in:
parent
0636964b39
commit
5d20ac05a0
@ -1,6 +1,8 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
'''
|
'''
|
||||||
Slice an image into 8x8 chunks (tiles) to create a color font.
|
Slice an image (or multiple images) into 8x8 chunks (tiles) and deduplicate
|
||||||
|
them to create a color font and a sequence of characters that represents the
|
||||||
|
original image(s).
|
||||||
'''
|
'''
|
||||||
import sys
|
import sys
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
@ -8,7 +10,7 @@ import struct
|
|||||||
|
|
||||||
BW=8 # tile width
|
BW=8 # tile width
|
||||||
BH=8 # tile height
|
BH=8 # tile height
|
||||||
BG=(0,0,0) # RGB color for background
|
BG=(0,0,0) # RGB color for background (hardcoded, could be configurable)
|
||||||
|
|
||||||
def rgb565(color):
|
def rgb565(color):
|
||||||
'''Truncate RGB888[8] color to RGB565'''
|
'''Truncate RGB888[8] color to RGB565'''
|
||||||
@ -37,6 +39,7 @@ def encode_block(block):
|
|||||||
rgb565(block[yi][xi*2 + 1]))
|
rgb565(block[yi][xi*2 + 1]))
|
||||||
return tuple(out)
|
return tuple(out)
|
||||||
|
|
||||||
|
assert(len(sys.argv) >= 3)
|
||||||
infiles = sys.argv[1:-1]
|
infiles = sys.argv[1:-1]
|
||||||
outfile = sys.argv[-1]
|
outfile = sys.argv[-1]
|
||||||
|
|
||||||
@ -49,6 +52,7 @@ charset = {}
|
|||||||
empty_block = encode_block([[BG]*BW]*BH)
|
empty_block = encode_block([[BG]*BW]*BH)
|
||||||
charset[empty_block] = 0
|
charset[empty_block] = 0
|
||||||
|
|
||||||
|
# sequence of characters to represent every image
|
||||||
seq = []
|
seq = []
|
||||||
for (infile, img) in images:
|
for (infile, img) in images:
|
||||||
blocks_x = (img.size[0] + (BW-1))//BW
|
blocks_x = (img.size[0] + (BW-1))//BW
|
||||||
@ -61,15 +65,16 @@ for (infile, img) in images:
|
|||||||
row = []
|
row = []
|
||||||
for bx in range(0, blocks_x):
|
for bx in range(0, blocks_x):
|
||||||
bd = encode_block(extract_block(img, (bx * BW, by * BH)))
|
bd = encode_block(extract_block(img, (bx * BW, by * BH)))
|
||||||
# add character to character set
|
|
||||||
try:
|
try:
|
||||||
|
# re-use existing matching character
|
||||||
ch = charset[bd]
|
ch = charset[bd]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
|
# add character to character set
|
||||||
ch = len(charset)
|
ch = len(charset)
|
||||||
charset[bd] = ch
|
charset[bd] = ch
|
||||||
row.append(ch)
|
row.append(ch)
|
||||||
out.append(row)
|
out.append(row)
|
||||||
seq.append(out)
|
seq.append((out, blocks_x, blocks_y))
|
||||||
|
|
||||||
m = len(empty_block)
|
m = len(empty_block)
|
||||||
n = len(charset)
|
n = len(charset)
|
||||||
@ -93,7 +98,7 @@ with open(outfile, 'w') as f:
|
|||||||
f.write('\n')
|
f.write('\n')
|
||||||
|
|
||||||
# TODO: output sequence; RLE encoding of some kind?
|
# TODO: output sequence; RLE encoding of some kind?
|
||||||
for (i,out) in enumerate(seq):
|
for (i,(out,blocks_x,blocks_y)) in enumerate(seq):
|
||||||
f.write('#[rustfmt::skip]\n')
|
f.write('#[rustfmt::skip]\n')
|
||||||
f.write(f'pub static SEQ{i}: [[u16; {blocks_x}]; {blocks_y}] = [\n')
|
f.write(f'pub static SEQ{i}: [[u16; {blocks_x}]; {blocks_y}] = [\n')
|
||||||
for subseq in out:
|
for subseq in out:
|
||||||
|
Loading…
Reference in New Issue
Block a user