Skip to content

Commit ce523ff

Browse files
committed
Final Implementation
Various testing shows using a for loop is the quickest method, beating enumeration and list comprehension.
1 parent 0aed0e9 commit ce523ff

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

adafruit_imageload/png.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def load(
4848
:param object palette: Type to store the palette. Must have API similar to
4949
`displayio.Palette`. Will be skipped if None.
5050
"""
51-
# pylint: disable=too-many-locals,too-many-branches
51+
# pylint: disable=too-many-locals,too-many-branches, consider-using-enumerate, too-many-statements
5252
header = file.read(8)
5353
if header != b"\x89PNG\r\n\x1a\n":
5454
raise ValueError("Not a PNG file")
@@ -87,10 +87,12 @@ def load(
8787
for i in range(pal_size):
8888
pal[i] = file.read(3)
8989
elif chunk == b"tRNS":
90-
trns_list = list(file.read(size))
91-
indices = [i for i, x in enumerate(trns_list) if x == 0]
92-
for index in indices:
93-
pal.make_transparent(index)
90+
if size > len(pal):
91+
raise ValueError("More transparency entries than palette entries")
92+
trns_data = file.read(size)
93+
for i in range(len(trns_data)):
94+
if trns_data[i] == 0:
95+
pal.make_transparent(i)
9496
elif chunk == b"IDAT":
9597
data.extend(file.read(size))
9698
elif chunk == b"IEND":

0 commit comments

Comments
 (0)