1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-11-27 01:51:35 +00:00
8bitworkshop/tools/pbm_to_c.py

38 lines
912 B
Python
Raw Normal View History

2017-01-25 17:30:05 +00:00
#!/usr/bin/python
import sys
2017-01-25 17:30:05 +00:00
# reverse byte
def rev(n):
return int('{:08b}'.format(n)[::-1], 2)
# output bits in given range
def out(i, pix, lb, hb, reverse=0, shift=0):
x = (pix >> lb) & ((1<<(hb-lb))-1)
if reverse:
x = rev(x)
if shift:
x = x << shift
assert(x>=0 and x<=255)
output[i].append(x)
# read PBM (binary P4 format) file
with open(sys.argv[1],'rb') as f:
# read PBM header
header = f.readline().strip()
assert(header == 'P4')
dims = f.readline().strip()
while dims[0] == '#':
2017-01-25 17:30:05 +00:00
dims = f.readline().strip()
width,height = map(int, dims.split())
wbytes = (width+7)/8
data = f.read()
2018-11-22 19:11:56 +00:00
print("{%d,%d," % (wbytes,height), end='')
2017-01-25 17:30:05 +00:00
for i in range(0,len(data)):
if i>0:
sys.stdout.write(",")
ofs = i+wbytes-(i%wbytes)*2-1
sys.stdout.write( "0x%02x" % ord(data[ofs]) )
2018-11-22 19:11:56 +00:00
print("}")
2017-01-25 17:30:05 +00:00