retroforth/tools/retro-embedimage.py
crc 9912a4b501 nga-python: implement support for run length encoding in image (#44)
FossilOrigin-Name: a132117aafc756384eb9a63b55678fefa7020b8a07ded9121828bb09658d4770
2021-02-02 15:22:17 +00:00

39 lines
915 B
Python
Executable file

#!/usr/bin/env python3
# retro-embedimage
#
# This takes an ngaImage and generates a version that provides the
# image as a Python list. Output is written to stdout.
#
# Copyright (c) 2020, Charles Childers
#
# Usage:
#
# retro-embedimage.py [image]
import os, sys, struct
from struct import pack, unpack
if __name__ == "__main__":
cells = int(os.path.getsize(sys.argv[1]) / 4)
f = open(sys.argv[1], "rb")
memory = list(struct.unpack(cells * "i", f.read()))
f.close()
count = 0
print("InitialImage = [")
rl = 0
for cell in memory:
if cell == 0:
rl = rl + 1
if rl > 0 and cell != 0:
print("[{0}]".format(rl), end=", ")
rl = 0
count = count + 1
if cell != 0:
print(cell, end=", ")
count = count + 1
if count > 10:
print("")
count = 0
print("]")