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.

PcxImagePlugin.py 5.9KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # PCX file handling
  6. #
  7. # This format was originally used by ZSoft's popular PaintBrush
  8. # program for the IBM PC. It is also supported by many MS-DOS and
  9. # Windows applications, including the Windows PaintBrush program in
  10. # Windows 3.
  11. #
  12. # history:
  13. # 1995-09-01 fl Created
  14. # 1996-05-20 fl Fixed RGB support
  15. # 1997-01-03 fl Fixed 2-bit and 4-bit support
  16. # 1999-02-03 fl Fixed 8-bit support (broken in 1.0b1)
  17. # 1999-02-07 fl Added write support
  18. # 2002-06-09 fl Made 2-bit and 4-bit support a bit more robust
  19. # 2002-07-30 fl Seek from to current position, not beginning of file
  20. # 2003-06-03 fl Extract DPI settings (info["dpi"])
  21. #
  22. # Copyright (c) 1997-2003 by Secret Labs AB.
  23. # Copyright (c) 1995-2003 by Fredrik Lundh.
  24. #
  25. # See the README file for information on usage and redistribution.
  26. #
  27. import io
  28. import logging
  29. from . import Image, ImageFile, ImagePalette
  30. from ._binary import i16le as i16
  31. from ._binary import o8
  32. from ._binary import o16le as o16
  33. logger = logging.getLogger(__name__)
  34. def _accept(prefix):
  35. return prefix[0] == 10 and prefix[1] in [0, 2, 3, 5]
  36. ##
  37. # Image plugin for Paintbrush images.
  38. class PcxImageFile(ImageFile.ImageFile):
  39. format = "PCX"
  40. format_description = "Paintbrush"
  41. def _open(self):
  42. # header
  43. s = self.fp.read(128)
  44. if not _accept(s):
  45. msg = "not a PCX file"
  46. raise SyntaxError(msg)
  47. # image
  48. bbox = i16(s, 4), i16(s, 6), i16(s, 8) + 1, i16(s, 10) + 1
  49. if bbox[2] <= bbox[0] or bbox[3] <= bbox[1]:
  50. msg = "bad PCX image size"
  51. raise SyntaxError(msg)
  52. logger.debug("BBox: %s %s %s %s", *bbox)
  53. # format
  54. version = s[1]
  55. bits = s[3]
  56. planes = s[65]
  57. provided_stride = i16(s, 66)
  58. logger.debug(
  59. "PCX version %s, bits %s, planes %s, stride %s",
  60. version,
  61. bits,
  62. planes,
  63. provided_stride,
  64. )
  65. self.info["dpi"] = i16(s, 12), i16(s, 14)
  66. if bits == 1 and planes == 1:
  67. mode = rawmode = "1"
  68. elif bits == 1 and planes in (2, 4):
  69. mode = "P"
  70. rawmode = "P;%dL" % planes
  71. self.palette = ImagePalette.raw("RGB", s[16:64])
  72. elif version == 5 and bits == 8 and planes == 1:
  73. mode = rawmode = "L"
  74. # FIXME: hey, this doesn't work with the incremental loader !!!
  75. self.fp.seek(-769, io.SEEK_END)
  76. s = self.fp.read(769)
  77. if len(s) == 769 and s[0] == 12:
  78. # check if the palette is linear greyscale
  79. for i in range(256):
  80. if s[i * 3 + 1 : i * 3 + 4] != o8(i) * 3:
  81. mode = rawmode = "P"
  82. break
  83. if mode == "P":
  84. self.palette = ImagePalette.raw("RGB", s[1:])
  85. self.fp.seek(128)
  86. elif version == 5 and bits == 8 and planes == 3:
  87. mode = "RGB"
  88. rawmode = "RGB;L"
  89. else:
  90. msg = "unknown PCX mode"
  91. raise OSError(msg)
  92. self.mode = mode
  93. self._size = bbox[2] - bbox[0], bbox[3] - bbox[1]
  94. # Don't trust the passed in stride.
  95. # Calculate the approximate position for ourselves.
  96. # CVE-2020-35653
  97. stride = (self._size[0] * bits + 7) // 8
  98. # While the specification states that this must be even,
  99. # not all images follow this
  100. if provided_stride != stride:
  101. stride += stride % 2
  102. bbox = (0, 0) + self.size
  103. logger.debug("size: %sx%s", *self.size)
  104. self.tile = [("pcx", bbox, self.fp.tell(), (rawmode, planes * stride))]
  105. # --------------------------------------------------------------------
  106. # save PCX files
  107. SAVE = {
  108. # mode: (version, bits, planes, raw mode)
  109. "1": (2, 1, 1, "1"),
  110. "L": (5, 8, 1, "L"),
  111. "P": (5, 8, 1, "P"),
  112. "RGB": (5, 8, 3, "RGB;L"),
  113. }
  114. def _save(im, fp, filename):
  115. try:
  116. version, bits, planes, rawmode = SAVE[im.mode]
  117. except KeyError as e:
  118. msg = f"Cannot save {im.mode} images as PCX"
  119. raise ValueError(msg) from e
  120. # bytes per plane
  121. stride = (im.size[0] * bits + 7) // 8
  122. # stride should be even
  123. stride += stride % 2
  124. # Stride needs to be kept in sync with the PcxEncode.c version.
  125. # Ideally it should be passed in in the state, but the bytes value
  126. # gets overwritten.
  127. logger.debug(
  128. "PcxImagePlugin._save: xwidth: %d, bits: %d, stride: %d",
  129. im.size[0],
  130. bits,
  131. stride,
  132. )
  133. # under windows, we could determine the current screen size with
  134. # "Image.core.display_mode()[1]", but I think that's overkill...
  135. screen = im.size
  136. dpi = 100, 100
  137. # PCX header
  138. fp.write(
  139. o8(10)
  140. + o8(version)
  141. + o8(1)
  142. + o8(bits)
  143. + o16(0)
  144. + o16(0)
  145. + o16(im.size[0] - 1)
  146. + o16(im.size[1] - 1)
  147. + o16(dpi[0])
  148. + o16(dpi[1])
  149. + b"\0" * 24
  150. + b"\xFF" * 24
  151. + b"\0"
  152. + o8(planes)
  153. + o16(stride)
  154. + o16(1)
  155. + o16(screen[0])
  156. + o16(screen[1])
  157. + b"\0" * 54
  158. )
  159. assert fp.tell() == 128
  160. ImageFile._save(im, fp, [("pcx", (0, 0) + im.size, 0, (rawmode, bits * planes))])
  161. if im.mode == "P":
  162. # colour palette
  163. fp.write(o8(12))
  164. palette = im.im.getpalette("RGB", "RGB")
  165. palette += b"\x00" * (768 - len(palette))
  166. fp.write(palette) # 768 bytes
  167. elif im.mode == "L":
  168. # greyscale palette
  169. fp.write(o8(12))
  170. for i in range(256):
  171. fp.write(o8(i) * 3)
  172. # --------------------------------------------------------------------
  173. # registry
  174. Image.register_open(PcxImageFile.format, PcxImageFile, _accept)
  175. Image.register_save(PcxImageFile.format, _save)
  176. Image.register_extension(PcxImageFile.format, ".pcx")
  177. Image.register_mime(PcxImageFile.format, "image/x-pcx")