Skip to content

Commit 42fda8a

Browse files
committed
refactor negative height check into its own file
1 parent 1fc176d commit 42fda8a

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

adafruit_imageload/bmp/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@ 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+
)
59+
5460
file.seek(0x1C) # Number of bits per pixel
5561
color_depth = int.from_bytes(file.read(2), "little")
5662
file.seek(0x1E) # Compression type

adafruit_imageload/bmp/indexed.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929
3030
"""
3131

32-
__version__ = "0.0.0-auto.0"
32+
__version__ = "0.10.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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""
2+
Check for negative height on the BMP.
3+
Seperated into it's own file to support builds
4+
without longint.
5+
"""
6+
def negative_height_check(height):
7+
"""Check the height return modified if negative."""
8+
if height > 0x7FFFFFFF:
9+
return height - 4294967296
10+
return height

0 commit comments

Comments
 (0)