nga-python: implement run length encoding for arbitrary value sequences in image (from Arland)

FossilOrigin-Name: 57120962dce31f709627ed2f3ea857b0fe56e662624dab27d16083e9fe79f9a7
This commit is contained in:
crc 2021-02-02 22:28:27 +00:00
parent 0904125b14
commit 6ea19bbdd2
3 changed files with 805 additions and 937 deletions

View file

@ -6,6 +6,7 @@
# image as a Python list. Output is written to stdout.
#
# Copyright (c) 2020, Charles Childers
# Copyright (c) 2021, Arland Childers
#
# Usage:
#
@ -15,25 +16,35 @@
import os, sys, struct
from struct import pack, unpack
def prints(length, priv, end=", "):
if priv != None:
if length == 1:
print(priv, end=end)
else:
print("[{},{}]".format(length, priv), end=end)
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
count = -1 # This is counts for the extra loop at the beginning
print("InitialImage = [", end="\n ")
length = 1
priv = None
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("")
if cell == priv:
length += 1
else:
prints(length, priv)
priv = cell
length = 1
count += 1
if count >= 10:
print(end="\n ")
count = 0
print("]")
prints(length, priv, end="")
print("\n]")

File diff suppressed because it is too large Load diff

View file

@ -19,7 +19,7 @@ class Memory(list):
for cell in initial:
if type(cell) == list:
for v in range(0, cell[0]):
self[i] = 0
self[i] = cell[1]
i = i + 1
else:
self[i] = cell