aboutsummaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2023-06-01 22:19:47 +0100
committerJuan J. Martinez <jjm@usebox.net>2023-06-01 22:19:47 +0100
commitfdb53d106ef573cd63ea39a4f1f6b5116370e4c8 (patch)
tree72dcd57dd5dd0e5f9b061e2c4610849732c7fd07 /tools
parent2d95775ec893c3b27ebb2fd319ddd4758a4b4f10 (diff)
downloadgold-mine-run-fdb53d106ef573cd63ea39a4f1f6b5116370e4c8.tar.gz
gold-mine-run-fdb53d106ef573cd63ea39a4f1f6b5116370e4c8.zip
Convert PNG pixel data to a binary .o
Diffstat (limited to 'tools')
-rwxr-xr-xtools/pngpix.py68
1 files changed, 68 insertions, 0 deletions
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 <jjm@usebox.net>",
+ )
+
+ 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()