NetBoot/Icons/BMP2Asm.py

39 lines
858 B
Python
Raw Normal View History

#!/usr/bin/env python3
import sys
import argparse
import struct
args = argparse.ArgumentParser(description='32x32 BMP to assembly icon (non-B&W becomes transparent)')
args.add_argument('bmp', help='BMP file')
args = args.parse_args()
bmp = open(args.bmp, 'rb').read()
2021-03-15 02:08:33 +00:00
sig, fsize, pixoffset, w, h = struct.unpack_from('<HLxxxxLxxxxLl', bmp)
h = abs(h)
2021-03-15 02:08:33 +00:00
if w != 32: sys.exit('width must be 32')
2021-03-15 02:08:33 +00:00
pixels = []
mask = []
2021-03-15 02:08:33 +00:00
for row in range(h):
row_pix = ''
row_mask = ''
2021-03-15 02:08:33 +00:00
for col in range(w):
px, = struct.unpack_from('<L', bmp, pixoffset + row*w*4 + col*4)
2021-03-15 02:08:33 +00:00
white = bool(px & 0x00FFFFFF)
opaque = bool(px & 0xFF000000)
row_pix += str(int(opaque and not white))
row_mask += str(int(opaque))
pixels.append(row_pix)
mask.append(row_mask)
for i in pixels+mask:
print(' dc.l %' + i)