cosmo/terminal/tools/fb_check.py

188 lines
8.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env python3
"""核对抓屏结果(screen_shot.py dump 出来的 320x240 RGB565 大端裸数据)。
# 颜色普查 + 列表页三张卡的图标身份(加色混合反算,残差 0 = 就是这张图)
python3 terminal/tools/fb_check.py /tmp/r_list
# 逐像素核对中文字模:位置=字模左上角,fg=前景色 hex,后面跟要显示的中文
python3 terminal/tools/fb_check.py /tmp/r_info --glyph 103,176,b77d,木星,太阳系中最大的行
# 核对屏上贴的是不是后台「平台管理」里维护的图标(裸点阵从接口直接拉)
curl -s -H "Authorization: Bearer $TOKEN" -o /tmp/ic_599_40.bin \
'http://127.0.0.1:8000/api/terminal/bodies/599/icon?size=40'
python3 terminal/tools/fb_check.py /tmp/r_list --raw 13,32,40,/tmp/ic_599_40.bin
用来回答两件事:中文有没有画成空心方框,图标有没有走错兜底图。
"""
import os, re, sys
import numpy as np
SRC = os.path.join(os.path.dirname(__file__), '..', 'firmware',
'cosmo_terminal', 'src')
ICON_NAMES = 'planet jupiter saturn mars earth earth_icon venus neptune sun star ' \
'comet halley galaxy satellite probe shuttle rover'.split()
BG = {0x0063, 0x10A4, 0x10E4, 0x0105, 0x1966, 0x330C, 0x6201} # 底色/边框/星点
def load_icons():
txt = open(os.path.join(SRC, 'icons.cpp')).read()
out = {}
for m in re.finditer(r'static const uint16_t px_(\w+)_(list|detail)\[\d+\]\s*=\s*\{(.*?)\};', txt, re.S):
v = np.array([int(x, 16) for x in re.findall(r'0x([0-9A-Fa-f]{4})', m.group(3))], np.uint16)
out['ic_%s_%s' % (m.group(1), m.group(2))] = v
return out
def load_font():
txt = open(os.path.join(SRC, 'font_cjk.cpp')).read()
cps = [int(x, 16) for x in re.findall(r'0x([0-9A-Fa-f]{4})',
txt[txt.index('s_codepoints[]'):txt.index('s_glyphs[')])]
raw = re.search(r's_glyphs\[\]\[\d+\]\s*=\s*\{(.*?)\n\};', txt, re.S).group(1)
b = np.array([int(x, 16) for x in re.findall(r'0x([0-9A-Fa-f]{2})', raw)], np.uint8)
g = [b[i:i + 32] for i in range(0, len(b) - 31, 32)]
assert len(cps) == len(g), (len(cps), len(g))
return dict(zip(cps, g))
def glyph_bits(font, ch):
d = font.get(ord(ch))
if d is None:
return None
# 2 字节/行,MSB 为最左像素(与 display.cpp 的 glyph_blit 一致)
return np.array([[(d[r * 2 + c // 8] >> (7 - c % 8)) & 1 for c in range(16)]
for r in range(16)], np.uint8)
_box_rows = np.array([0xFF, 0xFF] + [0x80, 0x01] * 14 + [0xFF, 0xFF], np.uint8)
BOX = np.array([[((_box_rows[r * 2 + c // 8]) >> (7 - c % 8)) & 1 for c in range(16)]
for r in range(16)], np.uint8)
def satadd(bg, ic, w, h):
ic = ic.reshape(h, w).astype(np.int32)
b = bg.astype(np.int32)
r = np.minimum(31, ((b >> 11) & 31) + ((ic >> 11) & 31))
g = np.minimum(63, ((b >> 5) & 63) + ((ic >> 5) & 63))
l = np.minimum(31, (b & 31) + (ic & 31))
return (((r << 11) | (g << 5) | l) & 0xFFFF).astype(np.uint16)
def check_icons(img, icons):
"""列表页 3 张卡:卡底 + 内置图标加色混合,逐个候选反算,残差 0 即为所用图"""
for n, y0 in enumerate((22, 87, 152)):
reg = img[y0 + 10:y0 + 50, 13:53].astype(np.uint16)
if not reg.any():
continue
res = []
for name, px in icons.items():
if not name.endswith('_list') or px.size != 1600:
continue
for bgc in (0x10E4, 0x10A4):
e = satadd(np.full((40, 40), bgc, np.uint16), px, 40, 40)
res.append((int(np.abs(e.astype(int) - reg.astype(int)).mean()), name, bgc))
res.sort()
d0, n0, _ = res[0]
if d0 > 50:
print(' card%d y=%3d -> 不属于任何内置图标'
' (后台维护的图标已覆盖,用 --raw 核对)'
% (n, y0)); continue
print(' card%d y=%3d -> %-18s 残差%d' % (n, y0, n0, d0))
def check_raw(img, raws):
"""和外部给的裸点阵(后端 /terminal/bodies/{id}/icon 拉下来的 RGB565 大端)对。
叠加混合下 obs = 底色 + 图标(逐通道饱和相加),底色不止面板一种(详情页图标底下
还有雷达刻度),所以逐像素在已知底色集合里取最近的那个再比。图标贴对了每像素误差
必然是 0,贴错则整片笔画都对不上。
"""
bad = 0
for x, y, size, path in raws:
blob = open(path, 'rb').read()
if len(blob) != size * size * 2:
print(' %s: %d 字节,和 %dx%d 不符' % (path, len(blob), size, size)); bad += 1; continue
px = np.frombuffer(blob, '>u2')
reg = img[y:y + size, x:x + size].astype(np.uint16).reshape(-1)
cands = np.array(sorted(BG), np.uint16) # 面板/选中/底/星点/走线/雷达/金底
# 逐像素在所有候选底色里取最接近的,再看最大偏差:贴对了必然处处为 0
worst = int(np.minimum.reduce([np.abs(satadd(np.full((size, size), bgc, np.uint16),
px, size, size).astype(int).reshape(-1)
- reg.astype(int)) for bgc in cands]).max())
ink = int((px != 0).sum())
print(' %-16s @%d,%d %dx%d 墨点%d 最大逐像素误差%s -> %s'
% (os.path.basename(path), x, y, size, size, ink, worst,
'命中(就是这张后台维护的图)' if worst == 0 else '不是这张图'))
bad += worst != 0
return bad
def check_glyphs(img, font, spec):
x, y, fg = spec[0], spec[1], spec[2]
bad = drawn = 0
for ch in spec[3]:
if ord(ch) < 0x80:
x += 9 # ASCII 字模不在本工具职责内,跳过
continue
e = glyph_bits(font, ch)
if e is None:
print(' x=%-3d %r 字库里没有' % (x, ch)); bad += 1; x += 17; continue
c = img[y:y + 16, x:x + 16]
miss = int((c[e == 1] != fg).sum())
leak = int((c[e == 0] == fg).sum())
isbox = bool((e == BOX).all())
note = 'OK'
if miss == 0 and leak == 0 and isbox:
note = '空心方框(缺字)'
elif miss or leak:
note = '不匹配 miss=%d leak=%d' % (miss, leak)
print(' x=%-3d %s ink=%2d %s' % (x, ch, int(e.sum()), note))
bad += miss + leak
if isbox:
bad += 1
drawn += 1
x += 17
print(' 核对 %d 字:%s' % (drawn, '全部逐像素命中' if bad == 0 else '%d 处不对' % bad))
return bad
def main():
args = sys.argv[1:]
glyph = None
if '--glyph' in args:
i = args.index('--glyph')
x, y, fg, s = args[i + 1].split(',', 3)
glyph = [int(x), int(y), int(fg, 16), s]
args = args[:i] + args[i + 2:]
if not args:
print(__doc__); return 2
raws = []
while '--raw' in args:
i = args.index('--raw')
x, y, n, f = args[i + 1].split(',')
raws.append((int(x), int(y), int(n), f))
args = args[:i] + args[i + 2:]
icons, font = load_icons(), load_font()
rc = 0
for path in args:
raw = open(path, 'rb').read()
# 整屏裸数据按长度反推尺寸,换板子不用加参数
W, H = next(((w, h) for w, h in ((320, 240), (240, 240), (172, 640), (640, 172))
if len(raw) == w * h * 2), (0, 0))
if not W:
print('%s: %d 字节,不是任何一款屏的整屏' % (path, len(raw))); rc = 1; continue
print('%s: %dx%d' % (path, W, H))
img = np.frombuffer(raw, '>u2').reshape(H, W).astype(np.uint16)
top = sorted(((int((img == c).sum()), c) for c in np.unique(img)), reverse=True)[:8]
print('%s 非背景像素 %d' % (path, int((~np.isin(img, list(BG))).sum())))
print(' 主色:', ' '.join('%04X:%d' % (c, n) for n, c in top))
check_icons(img, icons)
if raws:
rc |= check_raw(img, raws) != 0
if glyph:
rc |= check_glyphs(img, font, glyph) != 0
return rc
if __name__ == '__main__':
sys.exit(main())