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