Skip to content

Commit d8d2895

Browse files
committed
refactor negative height check into its own file.
Catch overflow on devices without longint
1 parent 21755ff commit d8d2895

3 files changed

Lines changed: 27 additions & 2 deletions

File tree

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: 9 additions & 1 deletion
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,
@@ -72,8 +74,14 @@ def load(
7274
minimum_color_depth *= 2
7375

7476
# convert unsigned int to signed int when height is negative
75-
if height > 0x7FFFFFFF:
77+
if sys.maxsize > 1073741823:
78+
# pylint: disable=import-outside-toplevel
79+
from .negative_height_check import negative_height_check
80+
7681
height = height - 4294967296
82+
83+
# convert unsigned int to signed int when height is negative
84+
height = negative_height_check(height)
7785
bitmap = bitmap(width, abs(height), colors)
7886
file.seek(data_start)
7987
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)