CurImagePlugin.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 i8
  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. raise SyntaxError("not a CUR file")
  37. # pick the largest cursor in the file
  38. m = b""
  39. for i in range(i16(s[4:])):
  40. s = self.fp.read(16)
  41. if not m:
  42. m = s
  43. elif i8(s[0]) > i8(m[0]) and i8(s[1]) > i8(m[1]):
  44. m = s
  45. if not m:
  46. raise TypeError("No cursors were found")
  47. # load as bitmap
  48. self._bitmap(i32(m[12:]) + offset)
  49. # patch up the bitmap height
  50. self._size = self.size[0], self.size[1] // 2
  51. d, e, o, a = self.tile[0]
  52. self.tile[0] = d, (0, 0) + self.size, o, a
  53. return
  54. #
  55. # --------------------------------------------------------------------
  56. Image.register_open(CurImageFile.format, CurImageFile, _accept)
  57. Image.register_extension(CurImageFile.format, ".cur")