unroll / tools /pack_bin.py
SharpMeow's picture
Faster first load: ship the int16 geometry as byte planes + gzip (scroll.i16p.gz, 1.62 MB instead of 3.48 MB), unpacked in the browser to the exact scroll.bin bytes with a fallback to scroll.bin; tools/pack_bin.py builds it, tests/pack_bin.mjs checks the round trip
77b49a9
Raw History Blame Contribute Delete
870 Bytes
# SPDX-License-Identifier: Apache-2.0
"""Pack data/scroll.bin (little-endian int16) as byte planes + gzip: data/scroll.i16p.gz.
The page fetches the packed file first and unpacks it to the exact original bytes
(lib/formats.js unpackInt16), falling back to scroll.bin. Run after tools/build.py.
"""
import gzip, os, sys
import numpy as np
ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')
def pack(raw: bytes) -> bytes:
a = np.frombuffer(raw, np.uint8).reshape(-1, 2)
return gzip.compress(a.T.copy().tobytes(), 9, mtime=0)
if __name__ == '__main__':
src = sys.argv[1] if len(sys.argv) > 1 else f'{ROOT}/data/scroll.bin'
dst = sys.argv[2] if len(sys.argv) > 2 else f'{ROOT}/data/scroll.i16p.gz'
raw = open(src, 'rb').read(); out = pack(raw)
open(dst, 'wb').write(out)
print(f'{src}: {len(raw)} -> {dst}: {len(out)} bytes')