XpmImagePlugin.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # XPM File handling
  6. #
  7. # History:
  8. # 1996-12-29 fl Created
  9. # 2001-02-17 fl Use 're' instead of 'regex' (Python 2.1) (0.7)
  10. #
  11. # Copyright (c) Secret Labs AB 1997-2001.
  12. # Copyright (c) Fredrik Lundh 1996-2001.
  13. #
  14. # See the README file for information on usage and redistribution.
  15. #
  16. import re
  17. from . import Image, ImageFile, ImagePalette
  18. from ._binary import o8
  19. # XPM header
  20. xpm_head = re.compile(b'"([0-9]*) ([0-9]*) ([0-9]*) ([0-9]*)')
  21. def _accept(prefix):
  22. return prefix[:9] == b"/* XPM */"
  23. ##
  24. # Image plugin for X11 pixel maps.
  25. class XpmImageFile(ImageFile.ImageFile):
  26. format = "XPM"
  27. format_description = "X11 Pixel Map"
  28. def _open(self):
  29. if not _accept(self.fp.read(9)):
  30. raise SyntaxError("not an XPM file")
  31. # skip forward to next string
  32. while True:
  33. s = self.fp.readline()
  34. if not s:
  35. raise SyntaxError("broken XPM file")
  36. m = xpm_head.match(s)
  37. if m:
  38. break
  39. self._size = int(m.group(1)), int(m.group(2))
  40. pal = int(m.group(3))
  41. bpp = int(m.group(4))
  42. if pal > 256 or bpp != 1:
  43. raise ValueError("cannot read this XPM file")
  44. #
  45. # load palette description
  46. palette = [b"\0\0\0"] * 256
  47. for i in range(pal):
  48. s = self.fp.readline()
  49. if s[-2:] == b"\r\n":
  50. s = s[:-2]
  51. elif s[-1:] in b"\r\n":
  52. s = s[:-1]
  53. c = s[1]
  54. s = s[2:-2].split()
  55. for i in range(0, len(s), 2):
  56. if s[i] == b"c":
  57. # process colour key
  58. rgb = s[i + 1]
  59. if rgb == b"None":
  60. self.info["transparency"] = c
  61. elif rgb[0:1] == b"#":
  62. # FIXME: handle colour names (see ImagePalette.py)
  63. rgb = int(rgb[1:], 16)
  64. palette[c] = (
  65. o8((rgb >> 16) & 255) + o8((rgb >> 8) & 255) + o8(rgb & 255)
  66. )
  67. else:
  68. # unknown colour
  69. raise ValueError("cannot read this XPM file")
  70. break
  71. else:
  72. # missing colour key
  73. raise ValueError("cannot read this XPM file")
  74. self.mode = "P"
  75. self.palette = ImagePalette.raw("RGB", b"".join(palette))
  76. self.tile = [("raw", (0, 0) + self.size, self.fp.tell(), ("P", 0, 1))]
  77. def load_read(self, bytes):
  78. #
  79. # load all image data in one chunk
  80. xsize, ysize = self.size
  81. s = [None] * ysize
  82. for i in range(ysize):
  83. s[i] = self.fp.readline()[1 : xsize + 1].ljust(xsize)
  84. return b"".join(s)
  85. #
  86. # Registry
  87. Image.register_open(XpmImageFile.format, XpmImageFile, _accept)
  88. Image.register_extension(XpmImageFile.format, ".xpm")
  89. Image.register_mime(XpmImageFile.format, "image/xpm")