From 75ebd6974b94ffd368db5199bc788cd51df3943b Mon Sep 17 00:00:00 2001 From: Teemu Rytilahti Date: Sat, 30 Nov 2024 20:57:46 +0100 Subject: [PATCH] Add script to parse tapovac map data --- devtools/parse_tapovac_map.py | 96 +++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 devtools/parse_tapovac_map.py diff --git a/devtools/parse_tapovac_map.py b/devtools/parse_tapovac_map.py new file mode 100644 index 00000000..f55bc900 --- /dev/null +++ b/devtools/parse_tapovac_map.py @@ -0,0 +1,96 @@ +# /// script +# dependencies = [ +# "mashumaro", +# "pillow", +# "lz4", +# ] +# /// + +"""Simple tool to parse tapovac maps. + +Use either get_image(), or +show_image( Image: + """Return image object for getMapData response.""" + data = data["result"] + d = MapData.from_dict(data) + + # TODO: move assert checks to mashumaro checks + + assert len(d.map_data) == d.pix_lz4len + assert d.width * d.height == d.pix_len + + print(f"{d.resolution=} {d.resolution_unit}") + print(f"{d.width=} x {d.height=} = {d.width * d.height} ({d.pix_len=}) {d.bitnum=}") + print(f"{d.origin_coor=} {d.real_origin_coor=}") + print(f"{d.auto_area_flag=}") + print(f"{d.area_list=}") + print(f"{d.bit_list=}") + + # TODO: handle orientation + # TODO: parse arealist for rooms/virtualwalls/other + # TODO: use nicer palette + + img_data = lz4.block.decompress(d.map_data, uncompressed_size=d.pix_len) + assert d.map_hash == hashlib.md5(img_data).hexdigest().upper() + + assert d.bitnum == 8 + mode = "L" # L = 8bit gray + + img = Image.frombytes(mode, (d.width, d.height), data=img_data) + + return img + + +def show_image(filename): + """Load and show image from getMapData json file.""" + with open(filename) as f: + data = json.load(f) + img = get_image(data) + final = img.resize((4 * img.width, 4 * img.height)) + ImageShow.show(final) + + +if __name__ == "__main__": + show_image(sys.argv[1])