From b1ccaf5c35bf8895c4167ee5e0d7921e3bc52a6a Mon Sep 17 00:00:00 2001 From: Kevin Matocha Date: Tue, 16 Mar 2021 10:05:09 -0500 Subject: [PATCH 1/4] update with option to use new bitmaptools.readinto --- adafruit_imageload/bmp/indexed.py | 41 ++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 12 deletions(-) mode change 100644 => 100755 adafruit_imageload/bmp/indexed.py diff --git a/adafruit_imageload/bmp/indexed.py b/adafruit_imageload/bmp/indexed.py old mode 100644 new mode 100755 index 53e1b00..3d07e6d --- a/adafruit_imageload/bmp/indexed.py +++ b/adafruit_imageload/bmp/indexed.py @@ -17,6 +17,11 @@ import sys +try: + from bitmaptools import readinto as _bitmap_readinto +except ImportError: + _bitmap_readinto = None # pylint: disable=invalid-name + def load( file, @@ -57,7 +62,7 @@ def load( minimum_color_depth *= 2 if sys.maxsize > 1073741823: - # pylint: disable=import-outside-toplevel + # pylint: disable=import-outside-toplevel, relative-beyond-top-level from .negative_height_check import negative_height_check # convert unsigned int to signed int when height is negative @@ -82,17 +87,28 @@ def load( if compression == 0: chunk = bytearray(line_size) - for y in range(range1, range2, range3): - file.readinto(chunk) - pixels_per_byte = 8 // color_depth - offset = y * width - - for x in range(width): - i = x // pixels_per_byte - pixel = ( - chunk[i] >> (8 - color_depth * (x % pixels_per_byte + 1)) - ) & mask - bitmap[offset + x] = pixel + + if _bitmap_readinto: + _bitmap_readinto( + bitmap, + file, + bits_per_pixel=color_depth, + element_size=4, + reverse_pixels_in_element=False, + reverse_rows=True, + ) + else: # use the standard file.readinto + for y in range(range1, range2, range3): + file.readinto(chunk) + pixels_per_byte = 8 // color_depth + offset = y * width + + for x in range(width): + i = x // pixels_per_byte + pixel = ( + chunk[i] >> (8 - color_depth * (x % pixels_per_byte + 1)) + ) & mask + bitmap[offset + x] = pixel elif compression in (1, 2): decode_rle( bitmap=bitmap, @@ -150,6 +166,7 @@ def decode_rle(bitmap, file, compression, y_range, width): # file is 15px wide but has data for 16px. width_remaining = width - x + print("doing this too") file.readinto(run_buf) if run_buf[0] == 0: From 98c57e6af0836feb1152eadbaaf8924287e8174f Mon Sep 17 00:00:00 2001 From: Kevin Matocha Date: Tue, 16 Mar 2021 10:07:59 -0500 Subject: [PATCH 2/4] remove debug statement --- adafruit_imageload/bmp/indexed.py | 1 - 1 file changed, 1 deletion(-) diff --git a/adafruit_imageload/bmp/indexed.py b/adafruit_imageload/bmp/indexed.py index 3d07e6d..2495db8 100755 --- a/adafruit_imageload/bmp/indexed.py +++ b/adafruit_imageload/bmp/indexed.py @@ -166,7 +166,6 @@ def decode_rle(bitmap, file, compression, y_range, width): # file is 15px wide but has data for 16px. width_remaining = width - x - print("doing this too") file.readinto(run_buf) if run_buf[0] == 0: From 2caf1875e7ffd38e2d6ce745af1f7fda581bd0be Mon Sep 17 00:00:00 2001 From: Kevin Matocha Date: Thu, 18 Mar 2021 21:52:56 -0500 Subject: [PATCH 3/4] trigger GitHub actions From e7165877a79411a05e55960d1adc95f41b9bfa7a Mon Sep 17 00:00:00 2001 From: Kevin Matocha Date: Fri, 19 Mar 2021 08:54:21 -0500 Subject: [PATCH 4/4] move memory chunk instance into correct location in if branch --- adafruit_imageload/bmp/indexed.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_imageload/bmp/indexed.py b/adafruit_imageload/bmp/indexed.py index 2495db8..c277475 100755 --- a/adafruit_imageload/bmp/indexed.py +++ b/adafruit_imageload/bmp/indexed.py @@ -86,7 +86,6 @@ def load( range3 = 1 if compression == 0: - chunk = bytearray(line_size) if _bitmap_readinto: _bitmap_readinto( @@ -98,6 +97,7 @@ def load( reverse_rows=True, ) else: # use the standard file.readinto + chunk = bytearray(line_size) for y in range(range1, range2, range3): file.readinto(chunk) pixels_per_byte = 8 // color_depth