From a95868307123fa0a8e7c8d4f464ddf8d1e8693e0 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 27 Apr 2023 18:17:11 +0200 Subject: [PATCH 1/3] Make png2c.py work with Python 3.9+ array.tostring() was removed in 3.9, so use tobytes() instead. This makes the script incompatible with Python 2, but this shouldn't be much of a concern any more these days. --- misc/scripts/png2c.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/misc/scripts/png2c.py b/misc/scripts/png2c.py index aedb81e13f..8d527f04b4 100755 --- a/misc/scripts/png2c.py +++ b/misc/scripts/png2c.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # This script is a slightly modified version of the original found at # @@ -50,7 +50,7 @@ for path in sys.argv[1:]: # Each PNG file starts with a 8 byte signature that should be followed # by IHDR chunk which is always 13 bytes in length so the first 16 # bytes are fixed (or at least we expect them to be). - if bytes[0:16].tostring() != b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR': + if bytes[0:16].tobytes() != b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR': print('"%s" doesn\'t seem to be a valid PNG file.' % filename) continue From ff206213aa1e396a559a984bfde8a379041e7eae Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 27 Apr 2023 18:30:37 +0200 Subject: [PATCH 2/3] Output warnings about skipping filename to stderr Considering that the output of this script is typically redirected, it really doesn't make sense to send error messages to the generated .c file. --- misc/scripts/png2c.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misc/scripts/png2c.py b/misc/scripts/png2c.py index 8d527f04b4..73f09fc0d4 100755 --- a/misc/scripts/png2c.py +++ b/misc/scripts/png2c.py @@ -37,7 +37,7 @@ for path in sys.argv[1:]: # Allow only filenames that make sense as C variable names if not(m): - print("Skipped file (unsuitable filename): " + filename) + print("Skipped file (unsuitable filename): " + filename, file=sys.stderr) continue # Read PNG file as character array From 303b244a9983059f4857a013ea0a61eec8db74cb Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 27 Apr 2023 18:31:23 +0200 Subject: [PATCH 3/3] Allow using "@" symbols in PNG files names This is needed when dealing with @2x versions of the files. --- misc/scripts/png2c.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/misc/scripts/png2c.py b/misc/scripts/png2c.py index 73f09fc0d4..d15ba811b0 100755 --- a/misc/scripts/png2c.py +++ b/misc/scripts/png2c.py @@ -25,6 +25,9 @@ if len(sys.argv) < 2: r = re.compile("^([a-zA-Z._][a-zA-Z._0-9]*)[.][pP][nN][gG]$") +# Automatically replace some symbols in the filenames. +sanitize = str.maketrans('-@', '__') + with_size = 0 size_suffix = '' for path in sys.argv[1:]: @@ -32,7 +35,7 @@ for path in sys.argv[1:]: with_size = 1 continue - filename = os.path.basename(path).replace('-','_') + filename = os.path.basename(path).translate(sanitize) m = r.match(filename) # Allow only filenames that make sense as C variable names