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 added Tests/images/mask_1.cur
Binary file not shown.
Binary file added Tests/images/mask_L.cur
Binary file not shown.
16 changes: 16 additions & 0 deletions Tests/test_file_cur.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ def test_largest_cursor() -> None:
assert im.size == (8, 8)


@pytest.mark.parametrize("mode", ("1", "L"))
def test_mask(mode: str) -> None:
with Image.open("Tests/images/mask_" + mode + ".cur") as im:
assert im.mode == "LA"

for i, value in enumerate(
[
(0, 255), # AND 0 XOR 0 is black
(255, 255), # AND 0 XOR 1 is white
(0, 0), # AND 1 XOR 0 is transparent
(0, 0), # AND 1 XOR 1 is transparent
]
):
assert im.getpixel((0, i)) == value


def test_invalid_file() -> None:
invalid_file = "Tests/images/flower.jpg"

Expand Down
34 changes: 31 additions & 3 deletions src/PIL/CurImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#
from __future__ import annotations

from . import BmpImagePlugin, Image
from . import BmpImagePlugin, Image, ImageOps
from ._binary import i16le as i16
from ._binary import i32le as i32

Expand Down Expand Up @@ -61,10 +61,38 @@ def _open(self) -> None:

# load as bitmap
self._bitmap(i32(m, 12) + offset)
self._masked = self.mode in ("1", "L")
if self._masked:
self._rawmode = self.mode
self._mode = "LA"

# patch up the bitmap height
self._size = self.size[0], self.size[1] // 2
self.tile = [self.tile[0]._replace(extents=(0, 0) + self.size)]
self._size = self.width, self.height // 2
if not self._masked:
self.tile = [self.tile[0]._replace(extents=(0, 0) + self.size)]

def load_prepare(self) -> None:
if self._masked:
self._mode = self._rawmode
self._size = self.width, self.height * 2
super().load_prepare()

def load_end(self) -> None:
if not self._masked:
return
self._mode = "LA"
new_height = self.height // 2

and_mask = self.im.crop((0, 0, self.width, new_height))
xor_mask = self.im.crop((0, new_height, self.width, self.height))

self._size = self.width, new_height
self._im = Image.core.fill(self.mode, self.size)
self._im.paste(
xor_mask.convert(self.mode),
(0, 0) + self.size,
ImageOps.invert(Image.Image()._new(and_mask)).im,
)


#
Expand Down
1 change: 1 addition & 0 deletions src/libImaging/Convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,7 @@ static struct {
{IMAGING_MODE_1, IMAGING_MODE_L, bit2l},
{IMAGING_MODE_1, IMAGING_MODE_I, bit2i},
{IMAGING_MODE_1, IMAGING_MODE_F, bit2f},
{IMAGING_MODE_1, IMAGING_MODE_LA, bit2rgb},
{IMAGING_MODE_1, IMAGING_MODE_RGB, bit2rgb},
{IMAGING_MODE_1, IMAGING_MODE_RGBA, bit2rgb},
{IMAGING_MODE_1, IMAGING_MODE_RGBX, bit2rgb},
Expand Down
Loading