diff --git a/adafruit_imageload/bmp/__init__.py b/adafruit_imageload/bmp/__init__.py index 5a72935..4eb9167 100644 --- a/adafruit_imageload/bmp/__init__.py +++ b/adafruit_imageload/bmp/__init__.py @@ -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 diff --git a/adafruit_imageload/bmp/indexed.py b/adafruit_imageload/bmp/indexed.py index 4d1178a..4bfe7ff 100644 --- a/adafruit_imageload/bmp/indexed.py +++ b/adafruit_imageload/bmp/indexed.py @@ -32,6 +32,8 @@ __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git" +import sys + def load( file, @@ -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) diff --git a/adafruit_imageload/bmp/negative_height_check.py b/adafruit_imageload/bmp/negative_height_check.py new file mode 100644 index 0000000..2d0f228 --- /dev/null +++ b/adafruit_imageload/bmp/negative_height_check.py @@ -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