Skip to content

Support negative height in BMP files #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 6, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions adafruit_imageload/bmp/indexed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you just declare the bitmap as below, and remove lines 65-67?

bitmap = bitmap(width, abs(height), colors)

This would save the (very limited) memory and time spent allocating the object "theight", since it isn't used again.

Edit: I got the line numbers wrong the first time. I tested the PR with and without this change, and both worked.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I should have seen that. I just made a pull request.

file.seek(data_start)
line_size = width // (8 // color_depth)
if width % (8 // color_depth) != 0:
Expand All @@ -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
Expand Down