Skip to content

Commit 396755e

Browse files
authored
Test largest CUR cursor (#9191)
2 parents 274a65b + 0675697 commit 396755e

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

Tests/test_file_cur.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
from __future__ import annotations
22

3+
from io import BytesIO
4+
35
import pytest
46

57
from PIL import CurImagePlugin, Image
8+
from PIL._binary import o8
9+
from PIL._binary import o16le as o16
10+
from PIL._binary import o32le as o32
611

712
TEST_FILE = "Tests/images/deerstalker.cur"
813

@@ -17,6 +22,24 @@ def test_sanity() -> None:
1722
assert im.getpixel((16, 16)) == (84, 87, 86, 255)
1823

1924

25+
def test_largest_cursor() -> None:
26+
magic = b"\x00\x00\x02\x00"
27+
sizes = ((1, 1), (8, 8), (4, 4))
28+
data = magic + o16(len(sizes))
29+
for w, h in sizes:
30+
image_offset = 6 + len(sizes) * 16 if (w, h) == max(sizes) else 0
31+
data += o8(w) + o8(h) + o8(0) * 10 + o32(image_offset)
32+
data += (
33+
o32(12) # header size
34+
+ o16(8) # width
35+
+ o16(16) # height
36+
+ o16(0) # planes
37+
+ o16(1) # bits
38+
)
39+
with Image.open(BytesIO(data)) as im:
40+
assert im.size == (8, 8)
41+
42+
2043
def test_invalid_file() -> None:
2144
invalid_file = "Tests/images/flower.jpg"
2245

@@ -26,6 +49,7 @@ def test_invalid_file() -> None:
2649
no_cursors_file = "Tests/images/no_cursors.cur"
2750

2851
cur = CurImagePlugin.CurImageFile(TEST_FILE)
52+
assert cur.fp is not None
2953
cur.fp.close()
3054
with open(no_cursors_file, "rb") as cur.fp:
3155
with pytest.raises(TypeError):

src/PIL/CurImagePlugin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#
1818
from __future__ import annotations
1919

20-
from . import BmpImagePlugin, Image, ImageFile
20+
from . import BmpImagePlugin, Image
2121
from ._binary import i16le as i16
2222
from ._binary import i32le as i32
2323

@@ -38,6 +38,7 @@ class CurImageFile(BmpImagePlugin.BmpImageFile):
3838
format_description = "Windows Cursor"
3939

4040
def _open(self) -> None:
41+
assert self.fp is not None
4142
offset = self.fp.tell()
4243

4344
# check magic
@@ -63,8 +64,7 @@ def _open(self) -> None:
6364

6465
# patch up the bitmap height
6566
self._size = self.size[0], self.size[1] // 2
66-
d, e, o, a = self.tile[0]
67-
self.tile[0] = ImageFile._Tile(d, (0, 0) + self.size, o, a)
67+
self.tile = [self.tile[0]._replace(extents=(0, 0) + self.size)]
6868

6969

7070
#

0 commit comments

Comments
 (0)