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 . 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. msg = "not a CUR file"
  36. raise SyntaxError(msg)
  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 s[0] > m[0] and s[1] > m[1]:
  44. m = s
  45. if not m:
  46. msg = "No cursors were found"
  47. raise TypeError(msg)
  48. # load as bitmap
  49. self._bitmap(i32(m, 12) + offset)
  50. # patch up the bitmap height
  51. self._size = self.size[0], self.size[1] // 2
  52. d, e, o, a = self.tile[0]
  53. self.tile[0] = d, (0, 0) + self.size, o, a
  54. return
  55. #
  56. # --------------------------------------------------------------------
  57. Image.register_open(CurImageFile.format, CurImageFile, _accept)
  58. Image.register_extension(CurImageFile.format, ".cur")