Skip to content
Open
Show file tree
Hide file tree
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
Binary file removed Tests/images/pal8_offset.bmp
Binary file not shown.
22 changes: 16 additions & 6 deletions Tests/test_file_bmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_fallback_if_mmap_errors() -> None:
# This image has been truncated,
# so that the buffer is not large enough when using mmap
with Image.open("Tests/images/mmap_error.bmp") as im:
assert_image_equal_tofile(im, "Tests/images/pal8_offset.bmp")
assert_image_equal_tofile(im, "Tests/images/bmp/g/pal8.bmp")


def test_save_to_bytes() -> None:
Expand Down Expand Up @@ -238,11 +238,21 @@ def test_unsupported_bmp_bitfields_layout() -> None:
Image.open(fp)


def test_offset() -> None:
# This image has been hexedited
# to exclude the palette size from the pixel data offset
with Image.open("Tests/images/pal8_offset.bmp") as im:
assert_image_equal_tofile(im, "Tests/images/bmp/g/pal8.bmp")
@pytest.mark.parametrize(
"offset, path",
(
(26, "pal8os2.bmp"),
(54, "pal8.bmp"),
),
)
def test_offset(offset: int, path: str) -> None:
image_path = "Tests/images/bmp/g/" + path
# Exclude the palette size from the pixel data offset
with open(image_path, "rb") as fp:
data = fp.read()
data = data[:10] + o32(offset) + data[14:]
with Image.open(io.BytesIO(data)) as im:
assert_image_equal_tofile(im, image_path)


def test_use_raw_alpha(monkeypatch: pytest.MonkeyPatch) -> None:
Expand Down
11 changes: 4 additions & 7 deletions src/PIL/BmpImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,12 @@ def _bitmap(self, header: int = 0, offset: int = 0) -> None:

# ------- If color count was not found in the header, compute from bits
assert isinstance(file_info["bits"], int)
file_info["colors"] = (
file_info["colors"]
if file_info.get("colors", 0)
else (1 << file_info["bits"])
)
if not file_info.get("colors", 0):
file_info["colors"] = 1 << file_info["bits"]
assert isinstance(file_info["palette_padding"], int)
assert isinstance(file_info["colors"], int)
if offset == 14 + file_info["header_size"] and file_info["bits"] <= 8:
offset += 4 * file_info["colors"]
offset += file_info["palette_padding"] * file_info["colors"]

# ---------------------- Check bit depth for unusual unsupported values
self._mode, raw_mode = BIT2MODE.get(file_info["bits"], ("", ""))
Expand Down Expand Up @@ -265,7 +263,6 @@ def _bitmap(self, header: int = 0, offset: int = 0) -> None:
msg = f"Unsupported BMP Palette size ({file_info['colors']})"
raise OSError(msg)
else:
assert isinstance(file_info["palette_padding"], int)
padding = file_info["palette_padding"]
palette = read(padding * file_info["colors"])
grayscale = True
Expand Down
Loading