Skip to content

Commit 5caff1f

Browse files
authored
Merge pull request #18 from cogliano/patch-1
Support negative height in BMP files
2 parents 8d5ec07 + 238f5ac commit 5caff1f

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

adafruit_imageload/bmp/indexed.py

+16-3
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)