From 735d02584b4db8a1b923d546b7be94de28f0876a Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 19 Mar 2026 10:38:28 +1100 Subject: [PATCH] Allow for different palette entry sizes when correcting offset --- Tests/test_file_bmp.py | 16 ++++++++++++---- src/PIL/BmpImagePlugin.py | 4 ++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Tests/test_file_bmp.py b/Tests/test_file_bmp.py index b61497812..c8ac46524 100644 --- a/Tests/test_file_bmp.py +++ b/Tests/test_file_bmp.py @@ -238,13 +238,21 @@ def test_unsupported_bmp_bitfields_layout() -> None: Image.open(fp) -def test_offset() -> None: +@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("Tests/images/bmp/g/pal8.bmp", "rb") as fp: + with open(image_path, "rb") as fp: data = fp.read() - data = data[:10] + o32(54) + data[14:] + data = data[:10] + o32(offset) + data[14:] with Image.open(io.BytesIO(data)) as im: - assert_image_equal_tofile(im, "Tests/images/bmp/g/pal8.bmp") + assert_image_equal_tofile(im, image_path) def test_use_raw_alpha(monkeypatch: pytest.MonkeyPatch) -> None: diff --git a/src/PIL/BmpImagePlugin.py b/src/PIL/BmpImagePlugin.py index 00975624a..a6724cab4 100644 --- a/src/PIL/BmpImagePlugin.py +++ b/src/PIL/BmpImagePlugin.py @@ -181,9 +181,10 @@ class BmpImageFile(ImageFile.ImageFile): assert isinstance(file_info["bits"], int) 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"], ("", "")) @@ -262,7 +263,6 @@ class BmpImageFile(ImageFile.ImageFile): 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