Skip to content

Commit

Permalink
Merge pull request #32 from FoamyGuy/handle_longint_missing_error
Browse files Browse the repository at this point in the history
Fix for builds without longint
  • Loading branch information
evaherrada authored Jun 25, 2020
2 parents 21755ff + b62f51f commit 0d193cd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
7 changes: 6 additions & 1 deletion adafruit_imageload/bmp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ def load(file, *, bitmap=None, palette=None):
# print(bmp_header_length)
file.seek(0x12) # Width of the bitmap in pixels
width = int.from_bytes(file.read(4), "little")
height = int.from_bytes(file.read(4), "little")
try:
height = int.from_bytes(file.read(4), "little")
except OverflowError:
raise NotImplementedError(
"Negative height BMP files are not supported on builds without longint"
)
file.seek(0x1C) # Number of bits per pixel
color_depth = int.from_bytes(file.read(2), "little")
file.seek(0x1E) # Compression type
Expand Down
11 changes: 8 additions & 3 deletions adafruit_imageload/bmp/indexed.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"

import sys


def load(
file,
Expand Down Expand Up @@ -71,9 +73,12 @@ def load(
while colors > 2 ** minimum_color_depth:
minimum_color_depth *= 2

# convert unsigned int to signed int when height is negative
if height > 0x7FFFFFFF:
height = height - 4294967296
if sys.maxsize > 1073741823:
# pylint: disable=import-outside-toplevel
from .negative_height_check import negative_height_check

# convert unsigned int to signed int when height is negative
height = negative_height_check(height)
bitmap = bitmap(width, abs(height), colors)
file.seek(data_start)
line_size = width // (8 // color_depth)
Expand Down
12 changes: 12 additions & 0 deletions adafruit_imageload/bmp/negative_height_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""
Check for negative height on the BMP.
Seperated into it's own file to support builds
without longint.
"""


def negative_height_check(height):
"""Check the height return modified if negative."""
if height > 0x7FFFFFFF:
return height - 4294967296
return height

0 comments on commit 0d193cd

Please sign in to comment.