Skip to content

Commit 140e426

Browse files
authored
Added USE_RAW_ALPHA (#8602)
Co-authored-by: Andrew Murray <[email protected]>
1 parent 93cdfeb commit 140e426

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

Tests/test_file_bmp.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,13 @@ def test_offset() -> None:
224224
# to exclude the palette size from the pixel data offset
225225
with Image.open("Tests/images/pal8_offset.bmp") as im:
226226
assert_image_equal_tofile(im, "Tests/images/bmp/g/pal8.bmp")
227+
228+
229+
def test_use_raw_alpha(monkeypatch: pytest.MonkeyPatch) -> None:
230+
with Image.open("Tests/images/bmp/g/rgb32.bmp") as im:
231+
assert im.info["compression"] == BmpImagePlugin.BmpImageFile.COMPRESSIONS["RAW"]
232+
assert im.mode == "RGB"
233+
234+
monkeypatch.setattr(BmpImagePlugin, "USE_RAW_ALPHA", True)
235+
with Image.open("Tests/images/bmp/g/rgb32.bmp") as im:
236+
assert im.mode == "RGBA"

src/PIL/BmpImagePlugin.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
32: ("RGB", "BGRX"),
4949
}
5050

51+
USE_RAW_ALPHA = False
52+
5153

5254
def _accept(prefix: bytes) -> bool:
5355
return prefix.startswith(b"BM")
@@ -242,7 +244,9 @@ def _bitmap(self, header: int = 0, offset: int = 0) -> None:
242244
msg = "Unsupported BMP bitfields layout"
243245
raise OSError(msg)
244246
elif file_info["compression"] == self.COMPRESSIONS["RAW"]:
245-
if file_info["bits"] == 32 and header == 22: # 32-bit .cur offset
247+
if file_info["bits"] == 32 and (
248+
header == 22 or USE_RAW_ALPHA # 32-bit .cur offset
249+
):
246250
raw_mode, self._mode = "BGRA", "RGBA"
247251
elif file_info["compression"] in (
248252
self.COMPRESSIONS["RLE8"],

0 commit comments

Comments
 (0)