Skip to content

Commit 0d193cd

Browse files
authored
Merge pull request #32 from FoamyGuy/handle_longint_missing_error
Fix for builds without longint
2 parents 21755ff + b62f51f commit 0d193cd

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

adafruit_imageload/bmp/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ def load(file, *, bitmap=None, palette=None):
5050
# print(bmp_header_length)
5151
file.seek(0x12) # Width of the bitmap in pixels
5252
width = int.from_bytes(file.read(4), "little")
53-
height = int.from_bytes(file.read(4), "little")
53+
try:
54+
height = int.from_bytes(file.read(4), "little")
55+
except OverflowError:
56+
raise NotImplementedError(
57+
"Negative height BMP files are not supported on builds without longint"
58+
)
5459
file.seek(0x1C) # Number of bits per pixel
5560
color_depth = int.from_bytes(file.read(2), "little")
5661
file.seek(0x1E) # Compression type

adafruit_imageload/bmp/indexed.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
__version__ = "0.0.0-auto.0"
3333
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"
3434

35+
import sys
36+
3537

3638
def load(
3739
file,
@@ -71,9 +73,12 @@ def load(
7173
while colors > 2 ** minimum_color_depth:
7274
minimum_color_depth *= 2
7375

74-
# convert unsigned int to signed int when height is negative
75-
if height > 0x7FFFFFFF:
76-
height = height - 4294967296
76+
if sys.maxsize > 1073741823:
77+
# pylint: disable=import-outside-toplevel
78+
from .negative_height_check import negative_height_check
79+
80+
# convert unsigned int to signed int when height is negative
81+
height = negative_height_check(height)
7782
bitmap = bitmap(width, abs(height), colors)
7883
file.seek(data_start)
7984
line_size = width // (8 // color_depth)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
Check for negative height on the BMP.
3+
Seperated into it's own file to support builds
4+
without longint.
5+
"""
6+
7+
8+
def negative_height_check(height):
9+
"""Check the height return modified if negative."""
10+
if height > 0x7FFFFFFF:
11+
return height - 4294967296
12+
return height

0 commit comments

Comments
 (0)