tools/client/png_benchmark/prepare.py (1439B)
1 """Build the standalone decoder and prepare the locally captured game PNGs.""" 2 import gzip 3 import json 4 import pathlib 5 import shutil 6 import subprocess 7 8 root = pathlib.Path(__file__).resolve().parents[3] 9 source = pathlib.Path(__file__).resolve().parent 10 output = root / 'local/png-benchmark' 11 output.mkdir(exist_ok=True) 12 13 # The proprietary corpus stays local; preserve the original decoded reference. 14 captured = json.loads((root / 'local/png-decoding/captured.json').read_text()) 15 images = list({image['input']: image for image in captured}.values()) 16 assert len(images) == 180 17 assert {image['bits'] for image in images} == {24, 32} 18 corpus = [{key: image[key] for key in ['input', 'pixels', 'width', 'height', 'bits']} for image in images] 19 (output / 'corpus.json.gz').write_bytes(gzip.compress(json.dumps(corpus).encode())) 20 21 # Use the game's pinned libpng port and its decoder optimization level. 22 subprocess.run([ 23 str(root / 'toolchains/emsdk/upstream/emscripten/emcc'), str(source / 'decoder.c'), 24 '-O2', '-flto=thin', '-sSUPPORT_LONGJMP=wasm', '-sUSE_LIBPNG=1', 25 '-sALLOW_MEMORY_GROWTH', '-sMODULARIZE', '-sEXPORT_NAME=createPNG', 26 '-sEXPORTED_FUNCTIONS=_decode,_pack_pixels,_malloc,_free', 27 '-sEXPORTED_RUNTIME_METHODS=HEAPU8,HEAPU32', '-o', str(output / 'decoder.js'), 28 ], check=True) 29 for name in ['index.html', 'worker.js']: 30 shutil.copyfile(source / name, output / name) 31 print(f'Prepared {len(images)} PNGs in {output}')