XpmImagePlugin.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 i8, o8
  19. # __version__ is deprecated and will be removed in a future version. Use
  20. # PIL.__version__ instead.
  21. __version__ = "0.2"
  22. # XPM header
  23. xpm_head = re.compile(b'"([0-9]*) ([0-9]*) ([0-9]*) ([0-9]*)')
  24. def _accept(prefix):
  25. return prefix[:9] == b"/* XPM */"
  26. ##
  27. # Image plugin for X11 pixel maps.
  28. class XpmImageFile(ImageFile.ImageFile):
  29. format = "XPM"
  30. format_description = "X11 Pixel Map"
  31. def _open(self):
  32. if not _accept(self.fp.read(9)):
  33. raise SyntaxError("not an XPM file")
  34. # skip forward to next string
  35. while True:
  36. s = self.fp.readline()
  37. if not s:
  38. raise SyntaxError("broken XPM file")
  39. m = xpm_head.match(s)
  40. if m:
  41. break
  42. self._size = int(m.group(1)), int(m.group(2))
  43. pal = int(m.group(3))
  44. bpp = int(m.group(4))
  45. if pal > 256 or bpp != 1:
  46. raise ValueError("cannot read this XPM file")
  47. #
  48. # load palette description
  49. palette = [b"\0\0\0"] * 256
  50. for i in range(pal):
  51. s = self.fp.readline()
  52. if s[-2:] == b"\r\n":
  53. s = s[:-2]
  54. elif s[-1:] in b"\r\n":
  55. s = s[:-1]
  56. c = i8(s[1])
  57. s = s[2:-2].split()
  58. for i in range(0, len(s), 2):
  59. if s[i] == b"c":
  60. # process colour key
  61. rgb = s[i + 1]
  62. if rgb == b"None":
  63. self.info["transparency"] = c
  64. elif rgb[0:1] == b"#":
  65. # FIXME: handle colour names (see ImagePalette.py)
  66. rgb = int(rgb[1:], 16)
  67. palette[c] = (
  68. o8((rgb >> 16) & 255) + o8((rgb >> 8) & 255) + o8(rgb & 255)
  69. )
  70. else:
  71. # unknown colour
  72. raise ValueError("cannot read this XPM file")
  73. break
  74. else:
  75. # missing colour key
  76. raise ValueError("cannot read this XPM file")
  77. self.mode = "P"
  78. self.palette = ImagePalette.raw("RGB", b"".join(palette))
  79. self.tile = [("raw", (0, 0) + self.size, self.fp.tell(), ("P", 0, 1))]
  80. def load_read(self, bytes):
  81. #
  82. # load all image data in one chunk
  83. xsize, ysize = self.size
  84. s = [None] * ysize
  85. for i in range(ysize):
  86. s[i] = self.fp.readline()[1 : xsize + 1].ljust(xsize)
  87. return b"".join(s)
  88. #
  89. # Registry
  90. Image.register_open(XpmImageFile.format, XpmImageFile, _accept)
  91. Image.register_extension(XpmImageFile.format, ".xpm")
  92. Image.register_mime(XpmImageFile.format, "image/xpm")