Skip to content

Commit 34dae3a

Browse files
authored
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.
1 parent 8d5ec07 commit 34dae3a

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

adafruit_imageload/bmp/indexed.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ def load(file, width, height, data_start, colors, color_depth, *, bitmap=None, p
5959
while colors > 2 ** minimum_color_depth:
6060
minimum_color_depth *= 2
6161

62-
bitmap = bitmap(width, height, colors)
62+
#convert unsigned int to signed int when height is negative
63+
if height > 0x7fffffff:
64+
height = height - 4294967296
65+
theight = height
66+
if theight < 0:
67+
theight = 0 - theight
68+
bitmap = bitmap(width, theight, colors)
6369
file.seek(data_start)
6470
line_size = width // (8 // color_depth)
6571
if width % (8 // color_depth) != 0:
@@ -69,8 +75,15 @@ def load(file, width, height, data_start, colors, color_depth, *, bitmap=None, p
6975

7076
chunk = bytearray(line_size)
7177
mask = (1 << minimum_color_depth) - 1
72-
73-
for y in range(height - 1, -1, -1):
78+
if height > 0:
79+
range1 = height - 1
80+
range2 = -1
81+
range3 = -1
82+
else:
83+
range1 = 0
84+
range2 = abs(height)
85+
range3 = 1
86+
for y in range(range1, range2, range3):
7487
file.readinto(chunk)
7588
pixels_per_byte = 8 // color_depth
7689
offset = y * width

0 commit comments

Comments
 (0)