Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/PIL/CurImagePlugin.py: 91%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

32 statements  

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.startswith(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 assert self.fp is not None 

42 offset = self.fp.tell() 

43 

44 # check magic 

45 s = self.fp.read(6) 

46 if not _accept(s): 

47 msg = "not a CUR file" 

48 raise SyntaxError(msg) 

49 

50 # pick the largest cursor in the file 

51 m = b"" 

52 for i in range(i16(s, 4)): 

53 s = self.fp.read(16) 

54 if not m: 

55 m = s 

56 elif s[0] > m[0] and s[1] > m[1]: 

57 m = s 

58 if not m: 

59 msg = "No cursors were found" 

60 raise TypeError(msg) 

61 

62 # load as bitmap 

63 self._bitmap(i32(m, 12) + offset) 

64 

65 # patch up the bitmap height 

66 self._size = self.size[0], self.size[1] // 2 

67 self.tile = [self.tile[0]._replace(extents=(0, 0) + self.size)] 

68 

69 

70# 

71# -------------------------------------------------------------------- 

72 

73Image.register_open(CurImageFile.format, CurImageFile, _accept) 

74 

75Image.register_extension(CurImageFile.format, ".cur")