From fdb53d106ef573cd63ea39a4f1f6b5116370e4c8 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Thu, 1 Jun 2023 22:19:47 +0100 Subject: Convert PNG pixel data to a binary .o --- tools/pngpix.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100755 tools/pngpix.py (limited to 'tools') diff --git a/tools/pngpix.py b/tools/pngpix.py new file mode 100755 index 0000000..e803b82 --- /dev/null +++ b/tools/pngpix.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 + +from argparse import ArgumentParser +import subprocess +import os +from PIL import Image + +__version__ = "1.0" + +ld = os.environ.get("LD", "i586-pc-msdosdjgpp-ld") +strip = os.environ.get("STRIP", "i586-pc-msdosdjgpp-strip") + + +def main(): + parser = ArgumentParser( + description="PNG to pixel data .o", + epilog="Copyright (C) 2023 Juan J Martinez ", + ) + + parser.add_argument( + "--version", action="version", version="%(prog)s " + __version__ + ) + parser.add_argument("image", help="image to convert") + parser.add_argument("output", help="object name for the pixel data") + + args = parser.parse_args() + + try: + image = Image.open(args.image) + except IOError: + parser.error("failed to open the image") + + if image.mode != "P": + parser.error("not an indexed image (no palette)") + + data = image.getdata() + if not data: + parser.error("failed to extract the pixel data") + + tmp = args.output.rstrip(".o") + with open(tmp, "wb") as fd: + fd.write(bytearray(data)) + fd.flush() + + # create an object file from the binary + rc = subprocess.call( + [ + ld, + "-r", + "-b", + "binary", + "-o", + args.output, + tmp, + ] + ) + os.unlink(tmp) + if rc != 0: + parser.error("Failed to run %s" % ld) + + # strip unwanted symbols + rc = subprocess.call([strip, "-w", "-K", "*_%s_*" % tmp, args.output]) + if rc != 0: + parser.error("Failed to run %s" % ld) + + +if __name__ == "__main__": + main() -- cgit v1.2.3