2020-12-24 16:01:26 +01:00
|
|
|
#!/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
|
2021-02-02 23:28:27 +01:00
|
|
|
# Copyright (c) 2021, Arland Childers
|
2020-12-24 16:01:26 +01:00
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
|
|
|
# retro-embedimage.py [image]
|
|
|
|
|
|
|
|
|
|
|
|
import os, sys, struct
|
|
|
|
from struct import pack, unpack
|
|
|
|
|
2021-02-02 23:28:27 +01:00
|
|
|
|
|
|
|
def prints(length, priv, end=", "):
|
|
|
|
if priv != None:
|
|
|
|
if length == 1:
|
|
|
|
print(priv, end=end)
|
|
|
|
else:
|
|
|
|
print("[{},{}]".format(length, priv), end=end)
|
|
|
|
|
|
|
|
|
2020-12-24 16:01:26 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
cells = int(os.path.getsize(sys.argv[1]) / 4)
|
2020-12-25 05:30:14 +01:00
|
|
|
f = open(sys.argv[1], "rb")
|
2020-12-24 16:01:26 +01:00
|
|
|
memory = list(struct.unpack(cells * "i", f.read()))
|
|
|
|
f.close()
|
2021-02-02 23:28:27 +01:00
|
|
|
count = -1 # This is counts for the extra loop at the beginning
|
|
|
|
print("InitialImage = [", end="\n ")
|
|
|
|
length = 1
|
|
|
|
priv = None
|
|
|
|
|
2020-12-24 16:01:26 +01:00
|
|
|
for cell in memory:
|
2021-02-02 23:28:27 +01:00
|
|
|
if cell == priv:
|
|
|
|
length += 1
|
|
|
|
else:
|
|
|
|
prints(length, priv)
|
|
|
|
priv = cell
|
|
|
|
length = 1
|
|
|
|
count += 1
|
|
|
|
if count >= 10:
|
|
|
|
print(end="\n ")
|
2020-12-24 16:01:26 +01:00
|
|
|
count = 0
|
2021-02-02 23:28:27 +01:00
|
|
|
prints(length, priv, end="")
|
|
|
|
print("\n]")
|