CurImagePlugin.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #
  18. from __future__ import annotations
  19. from . import BmpImagePlugin, Image
  20. from ._binary import i16le as i16
  21. from ._binary import i32le as i32
  22. #
  23. # --------------------------------------------------------------------
  24. def _accept(prefix):
  25. return prefix[:4] == b"\0\0\2\0"
  26. ##
  27. # Image plugin for Windows Cursor files.
  28. class CurImageFile(BmpImagePlugin.BmpImageFile):
  29. format = "CUR"
  30. format_description = "Windows Cursor"
  31. def _open(self):
  32. offset = self.fp.tell()
  33. # check magic
  34. s = self.fp.read(6)
  35. if not _accept(s):
  36. msg = "not a CUR file"
  37. raise SyntaxError(msg)
  38. # pick the largest cursor in the file
  39. m = b""
  40. for i in range(i16(s, 4)):
  41. s = self.fp.read(16)
  42. if not m:
  43. m = s
  44. elif s[0] > m[0] and s[1] > m[1]:
  45. m = s
  46. if not m:
  47. msg = "No cursors were found"
  48. raise TypeError(msg)
  49. # load as bitmap
  50. self._bitmap(i32(m, 12) + offset)
  51. # patch up the bitmap height
  52. self._size = self.size[0], self.size[1] // 2
  53. d, e, o, a = self.tile[0]
  54. self.tile[0] = d, (0, 0) + self.size, o, a
  55. #
  56. # --------------------------------------------------------------------
  57. Image.register_open(CurImageFile.format, CurImageFile, _accept)
  58. Image.register_extension(CurImageFile.format, ".cur")