From 3a6248361216294cc2a3492f2455ab6bcf1af96c Mon Sep 17 00:00:00 2001 From: ch4nsuk3 Date: Fri, 26 Jul 2024 18:19:56 -0500 Subject: [PATCH 1/5] Initial Testing Initial testing of the transparency block. --- adafruit_imageload/png.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/adafruit_imageload/png.py b/adafruit_imageload/png.py index 34b32f0..93d0e21 100644 --- a/adafruit_imageload/png.py +++ b/adafruit_imageload/png.py @@ -61,6 +61,10 @@ def load( height = 0 while True: size, chunk = struct.unpack(">I4s", file.read(8)) + print("==================") + print(size) + print(chunk) + print('====================') if chunk == b"IHDR": ( width, @@ -86,6 +90,9 @@ def load( pal = palette(pal_size) for i in range(pal_size): pal[i] = file.read(3) + elif chunk == b"tRNS": + transparent = file.read(size) + print(transparent) elif chunk == b"IDAT": data.extend(file.read(size)) elif chunk == b"IEND": From 0aed0e9c1ccd84085e3f6fa545ad82112064dd52 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 <134003603+ch4nsuk3@users.noreply.github.com> Date: Wed, 31 Jul 2024 15:02:30 -0500 Subject: [PATCH 2/5] Initial Implementation Transparency checking works, but there still may be efficient methods. --- adafruit_imageload/png.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/adafruit_imageload/png.py b/adafruit_imageload/png.py index 93d0e21..130579c 100644 --- a/adafruit_imageload/png.py +++ b/adafruit_imageload/png.py @@ -61,10 +61,6 @@ def load( height = 0 while True: size, chunk = struct.unpack(">I4s", file.read(8)) - print("==================") - print(size) - print(chunk) - print('====================') if chunk == b"IHDR": ( width, @@ -91,8 +87,10 @@ def load( for i in range(pal_size): pal[i] = file.read(3) elif chunk == b"tRNS": - transparent = file.read(size) - print(transparent) + trns_list = list(file.read(size)) + indices = [i for i, x in enumerate(trns_list) if x == 0] + for index in indices: + pal.make_transparent(index) elif chunk == b"IDAT": data.extend(file.read(size)) elif chunk == b"IEND": From ce523ff41fe4446f0d2926815a55391b2c424570 Mon Sep 17 00:00:00 2001 From: ch4nsuk3 Date: Thu, 1 Aug 2024 15:41:56 -0500 Subject: [PATCH 3/5] Final Implementation Various testing shows using a for loop is the quickest method, beating enumeration and list comprehension. --- adafruit_imageload/png.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/adafruit_imageload/png.py b/adafruit_imageload/png.py index 130579c..8e0d481 100644 --- a/adafruit_imageload/png.py +++ b/adafruit_imageload/png.py @@ -48,7 +48,7 @@ def load( :param object palette: Type to store the palette. Must have API similar to `displayio.Palette`. Will be skipped if None. """ - # pylint: disable=too-many-locals,too-many-branches + # pylint: disable=too-many-locals,too-many-branches, consider-using-enumerate, too-many-statements header = file.read(8) if header != b"\x89PNG\r\n\x1a\n": raise ValueError("Not a PNG file") @@ -87,10 +87,12 @@ def load( for i in range(pal_size): pal[i] = file.read(3) elif chunk == b"tRNS": - trns_list = list(file.read(size)) - indices = [i for i, x in enumerate(trns_list) if x == 0] - for index in indices: - pal.make_transparent(index) + if size > len(pal): + raise ValueError("More transparency entries than palette entries") + trns_data = file.read(size) + for i in range(len(trns_data)): + if trns_data[i] == 0: + pal.make_transparent(i) elif chunk == b"IDAT": data.extend(file.read(size)) elif chunk == b"IEND": From a8809b03a6f40a2f96402d5ec9d864edddc5f27e Mon Sep 17 00:00:00 2001 From: ch4nsuk3 Date: Thu, 1 Aug 2024 15:44:40 -0500 Subject: [PATCH 4/5] Updated author info --- adafruit_imageload/png.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/adafruit_imageload/png.py b/adafruit_imageload/png.py index 8e0d481..870313c 100644 --- a/adafruit_imageload/png.py +++ b/adafruit_imageload/png.py @@ -1,5 +1,6 @@ # SPDX-FileCopyrightText: 2022 Radomir Dopieralski # SPDX-FileCopyrightText: 2023 Matt Land +# SPDX-FileCopyrightText: 2024 Channing Ramos # # SPDX-License-Identifier: MIT @@ -10,7 +11,7 @@ Load pixel values (indices or colors) into a bitmap and colors into a palette from a PNG file. -* Author(s): Radomir Dopieralski, Matt Land +* Author(s): Radomir Dopieralski, Matt Land, Channing Ramos """ From a7ac0db3a7f5c39808718afe165b7a2f1a985dbb Mon Sep 17 00:00:00 2001 From: ch4nsuk3 Date: Fri, 2 Aug 2024 16:24:59 -0500 Subject: [PATCH 5/5] Del trans_data Deletes the trans_data list after its no longer needed. --- adafruit_imageload/png.py | 1 + 1 file changed, 1 insertion(+) diff --git a/adafruit_imageload/png.py b/adafruit_imageload/png.py index 870313c..dbc0abf 100644 --- a/adafruit_imageload/png.py +++ b/adafruit_imageload/png.py @@ -94,6 +94,7 @@ def load( for i in range(len(trns_data)): if trns_data[i] == 0: pal.make_transparent(i) + del trns_data elif chunk == b"IDAT": data.extend(file.read(size)) elif chunk == b"IEND":