1#
2# The Python Imaging Library.
3# $Id$
4#
5# Windows Cursor support for PIL
6#
7# notes:
8# uses BmpImagePlugin.py to read the bitmap data.
9#
10# history:
11# 96-05-27 fl Created
12#
13# Copyright (c) Secret Labs AB 1997.
14# Copyright (c) Fredrik Lundh 1996.
15#
16# See the README file for information on usage and redistribution.
17#
18from __future__ import annotations
19
20from . import BmpImagePlugin, Image
21from ._binary import i16le as i16
22from ._binary import i32le as i32
23
24#
25# --------------------------------------------------------------------
26
27
28def _accept(prefix: bytes) -> bool:
29 return prefix[:4] == b"\0\0\2\0"
30
31
32##
33# Image plugin for Windows Cursor files.
34
35
36class CurImageFile(BmpImagePlugin.BmpImageFile):
37 format = "CUR"
38 format_description = "Windows Cursor"
39
40 def _open(self) -> None:
41 offset = self.fp.tell()
42
43 # check magic
44 s = self.fp.read(6)
45 if not _accept(s):
46 msg = "not a CUR file"
47 raise SyntaxError(msg)
48
49 # pick the largest cursor in the file
50 m = b""
51 for i in range(i16(s, 4)):
52 s = self.fp.read(16)
53 if not m:
54 m = s
55 elif s[0] > m[0] and s[1] > m[1]:
56 m = s
57 if not m:
58 msg = "No cursors were found"
59 raise TypeError(msg)
60
61 # load as bitmap
62 self._bitmap(i32(m, 12) + offset)
63
64 # patch up the bitmap height
65 self._size = self.size[0], self.size[1] // 2
66 d, e, o, a = self.tile[0]
67 self.tile[0] = d, (0, 0) + self.size, o, a
68
69
70#
71# --------------------------------------------------------------------
72
73Image.register_open(CurImageFile.format, CurImageFile, _accept)
74
75Image.register_extension(CurImageFile.format, ".cur")