Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XpmImagePlugin.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. msg = "not an XPM file"
  31. raise SyntaxError(msg)
  32. # skip forward to next string
  33. while True:
  34. s = self.fp.readline()
  35. if not s:
  36. msg = "broken XPM file"
  37. raise SyntaxError(msg)
  38. m = xpm_head.match(s)
  39. if m:
  40. break
  41. self._size = int(m.group(1)), int(m.group(2))
  42. pal = int(m.group(3))
  43. bpp = int(m.group(4))
  44. if pal > 256 or bpp != 1:
  45. msg = "cannot read this XPM file"
  46. raise ValueError(msg)
  47. #
  48. # load palette description
  49. palette = [b"\0\0\0"] * 256
  50. for _ 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 = 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[: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. msg = "cannot read this XPM file"
  73. raise ValueError(msg)
  74. break
  75. else:
  76. # missing colour key
  77. msg = "cannot read this XPM file"
  78. raise ValueError(msg)
  79. self.mode = "P"
  80. self.palette = ImagePalette.raw("RGB", b"".join(palette))
  81. self.tile = [("raw", (0, 0) + self.size, self.fp.tell(), ("P", 0, 1))]
  82. def load_read(self, bytes):
  83. #
  84. # load all image data in one chunk
  85. xsize, ysize = self.size
  86. s = [None] * ysize
  87. for i in range(ysize):
  88. s[i] = self.fp.readline()[1 : xsize + 1].ljust(xsize)
  89. return b"".join(s)
  90. #
  91. # Registry
  92. Image.register_open(XpmImageFile.format, XpmImageFile, _accept)
  93. Image.register_extension(XpmImageFile.format, ".xpm")
  94. Image.register_mime(XpmImageFile.format, "image/xpm")