Skip to content
Open
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
29 changes: 13 additions & 16 deletions src/PIL/BmpImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
__lazy_modules__ = {"PIL._binary"}

import os
from functools import cache

from . import Image, ImageFile, ImagePalette
from ._binary import i16le as i16
Expand Down Expand Up @@ -64,6 +65,17 @@ def _dib_accept(prefix: bytes) -> bool:
return i32(prefix) in [12, 40, 52, 56, 64, 108, 124]


@cache
def _grayscale_palette(colors: int, padding: int) -> bytearray:
palette = bytearray()
indices = (0, 255) if colors == 2 else range(colors)
for val in indices:
palette += bytes((val,)) * 3
if padding == 4:
palette += bytes((0,))
return palette


# =============================================================================
# Image plugin for the Windows BMP format.
# =============================================================================
Expand Down Expand Up @@ -273,22 +285,7 @@ def _bitmap(self, header: int = 0, offset: int = 0) -> None:
padding = file_info["palette_padding"]
palette = read(padding * file_info["colors"])

if file_info["colors"] == 2:
if padding == 4:
# b"".join(bytes((val,) * 3 + (0,)) for val in (0, 255))
grayscale_palette = b"\x00\x00\x00\x00\xff\xff\xff\x00"
else:
# b"".join(bytes((val,) * 3) for val in (0, 255))
grayscale_palette = b"\x00\x00\x00\xff\xff\xff"
else:
if padding == 4:
# b"".join(bytes((val,) * 3 + (0,)) for val in range(256))
grayscale_palette = b"\x00\x00\x00\x00\x01\x01\x01\x00\x02\x02\x02\x00\x03\x03\x03\x00\x04\x04\x04\x00\x05\x05\x05\x00\x06\x06\x06\x00\x07\x07\x07\x00\x08\x08\x08\x00\t\t\t\x00\n\n\n\x00\x0b\x0b\x0b\x00\x0c\x0c\x0c\x00\r\r\r\x00\x0e\x0e\x0e\x00\x0f\x0f\x0f\x00\x10\x10\x10\x00\x11\x11\x11\x00\x12\x12\x12\x00\x13\x13\x13\x00\x14\x14\x14\x00\x15\x15\x15\x00\x16\x16\x16\x00\x17\x17\x17\x00\x18\x18\x18\x00\x19\x19\x19\x00\x1a\x1a\x1a\x00\x1b\x1b\x1b\x00\x1c\x1c\x1c\x00\x1d\x1d\x1d\x00\x1e\x1e\x1e\x00\x1f\x1f\x1f\x00 \x00!!!\x00\"\"\"\x00###\x00$$$\x00%%%\x00&&&\x00'''\x00(((\x00)))\x00***\x00+++\x00,,,\x00---\x00...\x00///\x00000\x00111\x00222\x00333\x00444\x00555\x00666\x00777\x00888\x00999\x00:::\x00;;;\x00<<<\x00===\x00>>>\x00???\x00@@@\x00AAA\x00BBB\x00CCC\x00DDD\x00EEE\x00FFF\x00GGG\x00HHH\x00III\x00JJJ\x00KKK\x00LLL\x00MMM\x00NNN\x00OOO\x00PPP\x00QQQ\x00RRR\x00SSS\x00TTT\x00UUU\x00VVV\x00WWW\x00XXX\x00YYY\x00ZZZ\x00[[[\x00\\\\\\\x00]]]\x00^^^\x00___\x00```\x00aaa\x00bbb\x00ccc\x00ddd\x00eee\x00fff\x00ggg\x00hhh\x00iii\x00jjj\x00kkk\x00lll\x00mmm\x00nnn\x00ooo\x00ppp\x00qqq\x00rrr\x00sss\x00ttt\x00uuu\x00vvv\x00www\x00xxx\x00yyy\x00zzz\x00{{{\x00|||\x00}}}\x00~~~\x00\x7f\x7f\x7f\x00\x80\x80\x80\x00\x81\x81\x81\x00\x82\x82\x82\x00\x83\x83\x83\x00\x84\x84\x84\x00\x85\x85\x85\x00\x86\x86\x86\x00\x87\x87\x87\x00\x88\x88\x88\x00\x89\x89\x89\x00\x8a\x8a\x8a\x00\x8b\x8b\x8b\x00\x8c\x8c\x8c\x00\x8d\x8d\x8d\x00\x8e\x8e\x8e\x00\x8f\x8f\x8f\x00\x90\x90\x90\x00\x91\x91\x91\x00\x92\x92\x92\x00\x93\x93\x93\x00\x94\x94\x94\x00\x95\x95\x95\x00\x96\x96\x96\x00\x97\x97\x97\x00\x98\x98\x98\x00\x99\x99\x99\x00\x9a\x9a\x9a\x00\x9b\x9b\x9b\x00\x9c\x9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\xe2\x00\xe3\xe3\xe3\x00\xe4\xe4\xe4\x00\xe5\xe5\xe5\x00\xe6\xe6\xe6\x00\xe7\xe7\xe7\x00\xe8\xe8\xe8\x00\xe9\xe9\xe9\x00\xea\xea\xea\x00\xeb\xeb\xeb\x00\xec\xec\xec\x00\xed\xed\xed\x00\xee\xee\xee\x00\xef\xef\xef\x00\xf0\xf0\xf0\x00\xf1\xf1\xf1\x00\xf2\xf2\xf2\x00\xf3\xf3\xf3\x00\xf4\xf4\xf4\x00\xf5\xf5\xf5\x00\xf6\xf6\xf6\x00\xf7\xf7\xf7\x00\xf8\xf8\xf8\x00\xf9\xf9\xf9\x00\xfa\xfa\xfa\x00\xfb\xfb\xfb\x00\xfc\xfc\xfc\x00\xfd\xfd\xfd\x00\xfe\xfe\xfe\x00\xff\xff\xff\x00" # noqa: E501
else:
# b"".join(bytes((val,) * 3) for val in range(256))
grayscale_palette = b"\x00\x00\x00\x01\x01\x01\x02\x02\x02\x03\x03\x03\x04\x04\x04\x05\x05\x05\x06\x06\x06\x07\x07\x07\x08\x08\x08\t\t\t\n\n\n\x0b\x0b\x0b\x0c\x0c\x0c\r\r\r\x0e\x0e\x0e\x0f\x0f\x0f\x10\x10\x10\x11\x11\x11\x12\x12\x12\x13\x13\x13\x14\x14\x14\x15\x15\x15\x16\x16\x16\x17\x17\x17\x18\x18\x18\x19\x19\x19\x1a\x1a\x1a\x1b\x1b\x1b\x1c\x1c\x1c\x1d\x1d\x1d\x1e\x1e\x1e\x1f\x1f\x1f !!!\"\"\"###$$$%%%&&&'''((()))***+++,,,---...///000111222333444555666777888999:::;;;<<<===>>>???@@@AAABBBCCCDDDEEEFFFGGGHHHIIIJJJKKKLLLMMMNNNOOOPPPQQQRRRSSSTTTUUUVVVWWWXXXYYYZZZ[[[\\\\\\]]]^^^___```aaabbbcccdddeeefffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuuvvvwwwxxxyyyzzz{{{|||}}}~~~\x7f\x7f\x7f\x80\x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xdc\xdd\xdd\xdd\xde\xde\xde\xdf\xdf\xdf\xe0\xe0\xe0\xe1\xe1\xe1\xe2\xe2\xe2\xe3\xe3\xe3\xe4\xe4\xe4\xe5\xe5\xe5\xe6\xe6\xe6\xe7\xe7\xe7\xe8\xe8\xe8\xe9\xe9\xe9\xea\xea\xea\xeb\xeb\xeb\xec\xec\xec\xed\xed\xed\xee\xee\xee\xef\xef\xef\xf0\xf0\xf0\xf1\xf1\xf1\xf2\xf2\xf2\xf3\xf3\xf3\xf4\xf4\xf4\xf5\xf5\xf5\xf6\xf6\xf6\xf7\xf7\xf7\xf8\xf8\xf8\xf9\xf9\xf9\xfa\xfa\xfa\xfb\xfb\xfb\xfc\xfc\xfc\xfd\xfd\xfd\xfe\xfe\xfe\xff\xff\xff" # noqa: E501

if grayscale_palette.startswith(palette):
if _grayscale_palette(file_info["colors"], padding).startswith(palette):
# ------- If all colors are gray, white or black, ditch palette
self._mode = "1" if file_info["colors"] <= 2 else "L"
raw_mode = self.mode
Expand Down