Merge branch 'png2c-fixes'

Improve png2c script, notably make it work with Python 3.9+.

See #23500.
This commit is contained in:
Vadim Zeitlin 2023-04-30 00:58:12 +02:00
commit ca7605d9af

View file

@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/env python
# This script is a slightly modified version of the original found at
#
@ -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,12 +35,12 @@ 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
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
@ -50,7 +53,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