Skip to content

Add option for using bitmaptools.readinto for indexed bitmap loads #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions adafruit_imageload/bmp/indexed.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -81,18 +86,29 @@ def load(
range3 = 1

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
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
elif compression in (1, 2):
decode_rle(
bitmap=bitmap,
Expand Down