From 34dae3aef5d3eed5874849720823a10d49e33472 Mon Sep 17 00:00:00 2001 From: Dan Cogliano Date: Thu, 1 Aug 2019 15:44:24 -0400 Subject: [PATCH 1/3] Support negative height in BMP files The BMP file format standard allows for the bitmap height to be defined as a negative number. When negative, it means that instead of the origin being at the lower left it is instead at the upper left when the height is negative. This file change supports those images where the origin is in the upper left. --- adafruit_imageload/bmp/indexed.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/adafruit_imageload/bmp/indexed.py b/adafruit_imageload/bmp/indexed.py index b1943f6..797c229 100644 --- a/adafruit_imageload/bmp/indexed.py +++ b/adafruit_imageload/bmp/indexed.py @@ -59,7 +59,13 @@ def load(file, width, height, data_start, colors, color_depth, *, bitmap=None, p while colors > 2 ** minimum_color_depth: minimum_color_depth *= 2 - bitmap = bitmap(width, height, colors) + #convert unsigned int to signed int when height is negative + if height > 0x7fffffff: + height = height - 4294967296 + theight = height + if theight < 0: + theight = 0 - theight + bitmap = bitmap(width, theight, colors) file.seek(data_start) line_size = width // (8 // color_depth) if width % (8 // color_depth) != 0: @@ -69,8 +75,15 @@ def load(file, width, height, data_start, colors, color_depth, *, bitmap=None, p chunk = bytearray(line_size) mask = (1 << minimum_color_depth) - 1 - - for y in range(height - 1, -1, -1): + if height > 0: + range1 = height - 1 + range2 = -1 + range3 = -1 + else: + range1 = 0 + range2 = abs(height) + range3 = 1 + for y in range(range1, range2, range3): file.readinto(chunk) pixels_per_byte = 8 // color_depth offset = y * width From 67cff5aa91413353f7179e535abf68d95d325c78 Mon Sep 17 00:00:00 2001 From: Dan Cogliano Date: Thu, 1 Aug 2019 23:29:40 -0400 Subject: [PATCH 2/3] Fixed indendation issue from PyLint --- adafruit_imageload/bmp/indexed.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_imageload/bmp/indexed.py b/adafruit_imageload/bmp/indexed.py index 797c229..b6ddae1 100644 --- a/adafruit_imageload/bmp/indexed.py +++ b/adafruit_imageload/bmp/indexed.py @@ -61,7 +61,7 @@ def load(file, width, height, data_start, colors, color_depth, *, bitmap=None, p #convert unsigned int to signed int when height is negative if height > 0x7fffffff: - height = height - 4294967296 + height = height - 4294967296 theight = height if theight < 0: theight = 0 - theight From 238f5ac1f637342c54f751c60b98b8998ff428b2 Mon Sep 17 00:00:00 2001 From: Dan Cogliano Date: Fri, 2 Aug 2019 10:16:46 -0400 Subject: [PATCH 3/3] Another tab fix --- adafruit_imageload/bmp/indexed.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_imageload/bmp/indexed.py b/adafruit_imageload/bmp/indexed.py index b6ddae1..3317e0a 100644 --- a/adafruit_imageload/bmp/indexed.py +++ b/adafruit_imageload/bmp/indexed.py @@ -61,7 +61,7 @@ def load(file, width, height, data_start, colors, color_depth, *, bitmap=None, p #convert unsigned int to signed int when height is negative if height > 0x7fffffff: - height = height - 4294967296 + height = height - 4294967296 theight = height if theight < 0: theight = 0 - theight