Files
lucky-cat/tools/extract_lcd_assets.py
T

77 lines
2.4 KiB
Python

import os
import re
import struct
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
LCD_ROOT = Path(r"D:\Code\chaowan\lcd\main")
OUT_DIR = ROOT / "components" / "platform" / "assets" / "lcd"
IMAGE_SOURCES = {
"qianzhiwuxing": ["qianzhiwuxing0"],
"shui": ["shui1", "shui2"],
"mu": ["mu1", "mu2"],
"huo": ["huo1", "huo2"],
"tu": ["tu1", "tu2"],
"jin": ["jin1", "jin2"],
"caiwei": ["bei0", "dongbei0", "dong0", "dongnan0", "nan0", "xinan0", "xi0", "xibei0"],
"chouqian_qianzhi": ["chouqianqianzhi0", "chouqianqianzhi1", "chouqianqianzhi2"],
"chouqian_buyun": ["shangshangqian0", "buyunqian1", "buyunqian2"],
"chouqian_heping": [None, "hepingqian1", "hepingqian2"],
"chouqian_shangshang": [None, "shangshangqian1", "shangshangqian2"],
"chouqian_xiaoji": [None, "xiaojiqian1", "xiaojiqian2"],
"chouqian_zhongshang": [None, "zhongshangqian1", "zhongshangqian2"],
}
def extract_arrays(source_text):
arrays = {}
pattern = re.compile(
r"static\s+const\s+(?:rle_structure_t|eye_image_frame_t)\s+([A-Za-z0-9_]+)\[\]\s*=\s*\{",
re.MULTILINE,
)
for match in pattern.finditer(source_text):
name = match.group(1)
cursor = match.end()
depth = 1
while cursor < len(source_text) and depth > 0:
if source_text[cursor] == "{":
depth += 1
elif source_text[cursor] == "}":
depth -= 1
cursor += 1
arrays[name] = source_text[match.end(): cursor - 1]
return arrays
def parse_runs(body):
runs = []
for color, count in re.findall(r"\{\s*0x([0-9A-Fa-f]+)\s*,\s*(\d+)\s*\}", body):
runs.append((int(color, 16), int(count)))
if not runs:
raise ValueError("no runs parsed")
return runs
def write_rle(path, runs):
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("wb") as f:
for color, count in runs:
f.write(struct.pack("<HH", color, count))
def main():
eye_arrays = extract_arrays((LCD_ROOT / "eye_image.c").read_text(encoding="utf-8"))
OUT_DIR.mkdir(parents=True, exist_ok=True)
for asset_dir, frames in IMAGE_SOURCES.items():
for index, array_name in enumerate(frames):
if array_name is None:
continue
write_rle(OUT_DIR / f"{asset_dir}{index}.rle", parse_runs(eye_arrays[array_name]))
if __name__ == "__main__":
main()