kodiak/chr.py

33 lines
1.0 KiB
Python
Executable File

#!/usr/bin/env python
from sys import argv, stderr, exit
if __name__ == "__main__":
if len(argv[1:]) != 1:
print('usage: %s PATH' % argv[0])
exit(0)
path = argv[1]
# we index each pixel into this list to see what value to use
# this should be exactly 4 values in the range 0-3.
# normally you won't want two duplicate values, since this
# will lose information.
translate = [3, 1, 2, 0]
for i, line in enumerate(open(path, 'r')):
toks = line.strip().split()
if len(toks) != 16:
stderr.write('line %d is invalid: %r\n' % (i + 1, line))
exit(1)
bytes = [int(t, 16) for t in toks]
output = [0] * 16
for i in range(0, 8):
for j in range(0, 8):
x = (bytes[i] >> j) & 1
y = (bytes[i + 8] >> j) & 1
z = translate[x + y * 2]
output[i] |= ((z & 1) << j)
output[i + 8] |= (((z & 2) << j) // 2)
print(' '.join(['%02x' % o for o in output]))