From 8d29c3b5cf7b37692c613cbc2645535752c36888 Mon Sep 17 00:00:00 2001 From: Arthur Bonnaudet Date: Thu, 12 Oct 2023 11:37:14 +0200 Subject: [PATCH] Fix icon2glyph script icon2glyph.py crashed when encountering a 2BPP image --- lib_nbgl/tools/icon2glyph.py | 8 ++++++-- lib_nbgl/tools/nbgl_rle.py | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib_nbgl/tools/icon2glyph.py b/lib_nbgl/tools/icon2glyph.py index 60f2d5084..d17373a79 100755 --- a/lib_nbgl/tools/icon2glyph.py +++ b/lib_nbgl/tools/icon2glyph.py @@ -113,13 +113,14 @@ def image_to_packed_buffer(img, bpp: int): # Compressions functions -def rle_compress(im: Image, bpp) -> bytes: +def rle_compress(im: Image, bpp) -> Optional[bytes]: """ Run RLE compression on input image """ if bpp == 1: return Rle1bpp.rle_1bpp(im)[1] elif bpp == 2: + # No compression supports BPP2 return None elif bpp == 4: return Rle4bpp.rle_4bpp(im)[1] @@ -167,6 +168,9 @@ def compress(im: Image, bpp) -> Tuple[NbglFileCompression, bytes]: min_comp = NbglFileCompression.NoCompression for compression, buffer in compressed_bufs.items(): + if buffer is None: + continue + final_length = len(buffer) if compression != NbglFileCompression.NoCompression: final_length += NBGL_IMAGE_FILE_HEADER_SIZE @@ -341,7 +345,7 @@ def main(): except Exception as e: sys.stderr.write( - "Exception while processing {} {}\n".format(file), e) + "Exception while processing {}: {}\n".format(file, e)) try: traceback.print_tb() except: diff --git a/lib_nbgl/tools/nbgl_rle.py b/lib_nbgl/tools/nbgl_rle.py index 5bc23420b..c1655c814 100644 --- a/lib_nbgl/tools/nbgl_rle.py +++ b/lib_nbgl/tools/nbgl_rle.py @@ -147,7 +147,7 @@ def occurrences_to_rle(cls, occurrences: Tuple[int, int], bpp: int) -> bytes: return result @classmethod - def rle_4bpp(cls, img) -> bytes: + def rle_4bpp(cls, img) -> Tuple[int, bytes]: bpp = 4 pixels = cls.image_to_pixels(img, bpp) occurrences = cls.pixels_to_occurrences(pixels) @@ -314,7 +314,7 @@ def decode_pass2(cls, data): # ------------------------------------------------------------------------- @classmethod - def rle_1bpp(cls, img) -> bytes: + def rle_1bpp(cls, img) -> Tuple[int, bytes]: """ Input: image to compress - convert the picture to an array of pixels