XbmImagePlugin.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # XBM File handling
  6. #
  7. # History:
  8. # 1995-09-08 fl Created
  9. # 1996-11-01 fl Added save support
  10. # 1997-07-07 fl Made header parser more tolerant
  11. # 1997-07-22 fl Fixed yet another parser bug
  12. # 2001-02-17 fl Use 're' instead of 'regex' (Python 2.1) (0.4)
  13. # 2001-05-13 fl Added hotspot handling (based on code from Bernhard Herzog)
  14. # 2004-02-24 fl Allow some whitespace before first #define
  15. #
  16. # Copyright (c) 1997-2004 by Secret Labs AB
  17. # Copyright (c) 1996-1997 by Fredrik Lundh
  18. #
  19. # See the README file for information on usage and redistribution.
  20. #
  21. from __future__ import annotations
  22. import re
  23. from . import Image, ImageFile
  24. TYPE_CHECKING = False
  25. if TYPE_CHECKING:
  26. from typing import IO
  27. # XBM header
  28. xbm_head = re.compile(
  29. rb"\s*#define[ \t]+.*_width[ \t]+(?P<width>[0-9]+)[\r\n]+"
  30. b"#define[ \t]+.*_height[ \t]+(?P<height>[0-9]+)[\r\n]+"
  31. b"(?P<hotspot>"
  32. b"#define[ \t]+[^_]*_x_hot[ \t]+(?P<xhot>[0-9]+)[\r\n]+"
  33. b"#define[ \t]+[^_]*_y_hot[ \t]+(?P<yhot>[0-9]+)[\r\n]+"
  34. b")?"
  35. rb"[\000-\377]*_bits\[]"
  36. )
  37. def _accept(prefix: bytes) -> bool:
  38. return prefix.startswith((b"#define", b"\n#define", b"/*"))
  39. ##
  40. # Image plugin for X11 bitmaps.
  41. class XbmImageFile(ImageFile.ImageFile):
  42. format = "XBM"
  43. format_description = "X11 Bitmap"
  44. def _open(self) -> None:
  45. assert self.fp is not None
  46. width = None
  47. height = None
  48. x_hot = None
  49. offset = None
  50. starting = True
  51. in_block_comment = False
  52. while True:
  53. line = self.fp.readline(75)
  54. if not line:
  55. break
  56. line = line.rstrip()
  57. if in_block_comment:
  58. if line.endswith(b"*/"):
  59. in_block_comment = False
  60. continue
  61. if line.startswith(b"/*"):
  62. in_block_comment = not line.endswith(b"*/")
  63. continue
  64. if starting and not line:
  65. starting = False
  66. continue
  67. if not line.startswith(b"#define "):
  68. if line.endswith(b"_bits[] = {"):
  69. offset = self.fp.tell()
  70. break
  71. value = int(line.split().pop())
  72. if b"width " in line:
  73. width = value
  74. elif b"height " in line:
  75. height = value
  76. elif b"_x_hot " in line:
  77. x_hot = value
  78. elif b"_y_hot " in line:
  79. if x_hot is not None:
  80. self.info["hotspot"] = x_hot, value
  81. if width is None or height is None or offset is None:
  82. msg = "not a XBM file"
  83. raise SyntaxError(msg)
  84. self._mode = "1"
  85. self._size = width, height
  86. self.tile = [ImageFile._Tile("xbm", (0, 0, *self.size), offset)]
  87. def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
  88. if im.mode != "1":
  89. msg = f"cannot write mode {im.mode} as XBM"
  90. raise OSError(msg)
  91. fp.write(f"#define im_width {im.size[0]}\n".encode("ascii"))
  92. fp.write(f"#define im_height {im.size[1]}\n".encode("ascii"))
  93. hotspot = im.encoderinfo.get("hotspot")
  94. if hotspot:
  95. fp.write(f"#define im_x_hot {hotspot[0]}\n".encode("ascii"))
  96. fp.write(f"#define im_y_hot {hotspot[1]}\n".encode("ascii"))
  97. fp.write(b"static char im_bits[] = {\n")
  98. ImageFile._save(im, fp, [ImageFile._Tile("xbm", (0, 0, *im.size))])
  99. fp.write(b"};\n")
  100. Image.register_open(XbmImageFile.format, XbmImageFile, _accept)
  101. Image.register_save(XbmImageFile.format, _save)
  102. Image.register_extension(XbmImageFile.format, ".xbm")
  103. Image.register_mime(XbmImageFile.format, "image/xbm")