CurImagePlugin.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 print_function
  19. from . import BmpImagePlugin, Image
  20. from ._binary import i8, i16le as i16, i32le as i32
  21. # __version__ is deprecated and will be removed in a future version. Use
  22. # PIL.__version__ instead.
  23. __version__ = "0.1"
  24. #
  25. # --------------------------------------------------------------------
  26. def _accept(prefix):
  27. return prefix[:4] == b"\0\0\2\0"
  28. ##
  29. # Image plugin for Windows Cursor files.
  30. class CurImageFile(BmpImagePlugin.BmpImageFile):
  31. format = "CUR"
  32. format_description = "Windows Cursor"
  33. def _open(self):
  34. offset = self.fp.tell()
  35. # check magic
  36. s = self.fp.read(6)
  37. if not _accept(s):
  38. raise SyntaxError("not a CUR file")
  39. # pick the largest cursor in the file
  40. m = b""
  41. for i in range(i16(s[4:])):
  42. s = self.fp.read(16)
  43. if not m:
  44. m = s
  45. elif i8(s[0]) > i8(m[0]) and i8(s[1]) > i8(m[1]):
  46. m = s
  47. if not m:
  48. raise TypeError("No cursors were found")
  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. return
  56. #
  57. # --------------------------------------------------------------------
  58. Image.register_open(CurImageFile.format, CurImageFile, _accept)
  59. Image.register_extension(CurImageFile.format, ".cur")