PcxImagePlugin.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. raise SyntaxError("not a PCX file")
  46. # image
  47. bbox = i16(s, 4), i16(s, 6), i16(s, 8) + 1, i16(s, 10) + 1
  48. if bbox[2] <= bbox[0] or bbox[3] <= bbox[1]:
  49. raise SyntaxError("bad PCX image size")
  50. logger.debug("BBox: %s %s %s %s", *bbox)
  51. # format
  52. version = s[1]
  53. bits = s[3]
  54. planes = s[65]
  55. provided_stride = i16(s, 66)
  56. logger.debug(
  57. "PCX version %s, bits %s, planes %s, stride %s",
  58. version,
  59. bits,
  60. planes,
  61. provided_stride,
  62. )
  63. self.info["dpi"] = i16(s, 12), i16(s, 14)
  64. if bits == 1 and planes == 1:
  65. mode = rawmode = "1"
  66. elif bits == 1 and planes in (2, 4):
  67. mode = "P"
  68. rawmode = "P;%dL" % planes
  69. self.palette = ImagePalette.raw("RGB", s[16:64])
  70. elif version == 5 and bits == 8 and planes == 1:
  71. mode = rawmode = "L"
  72. # FIXME: hey, this doesn't work with the incremental loader !!!
  73. self.fp.seek(-769, io.SEEK_END)
  74. s = self.fp.read(769)
  75. if len(s) == 769 and s[0] == 12:
  76. # check if the palette is linear greyscale
  77. for i in range(256):
  78. if s[i * 3 + 1 : i * 3 + 4] != o8(i) * 3:
  79. mode = rawmode = "P"
  80. break
  81. if mode == "P":
  82. self.palette = ImagePalette.raw("RGB", s[1:])
  83. self.fp.seek(128)
  84. elif version == 5 and bits == 8 and planes == 3:
  85. mode = "RGB"
  86. rawmode = "RGB;L"
  87. else:
  88. raise OSError("unknown PCX mode")
  89. self.mode = mode
  90. self._size = bbox[2] - bbox[0], bbox[3] - bbox[1]
  91. # Don't trust the passed in stride.
  92. # Calculate the approximate position for ourselves.
  93. # CVE-2020-35653
  94. stride = (self._size[0] * bits + 7) // 8
  95. # While the specification states that this must be even,
  96. # not all images follow this
  97. if provided_stride != stride:
  98. stride += stride % 2
  99. bbox = (0, 0) + self.size
  100. logger.debug("size: %sx%s", *self.size)
  101. self.tile = [("pcx", bbox, self.fp.tell(), (rawmode, planes * stride))]
  102. # --------------------------------------------------------------------
  103. # save PCX files
  104. SAVE = {
  105. # mode: (version, bits, planes, raw mode)
  106. "1": (2, 1, 1, "1"),
  107. "L": (5, 8, 1, "L"),
  108. "P": (5, 8, 1, "P"),
  109. "RGB": (5, 8, 3, "RGB;L"),
  110. }
  111. def _save(im, fp, filename):
  112. try:
  113. version, bits, planes, rawmode = SAVE[im.mode]
  114. except KeyError as e:
  115. raise ValueError(f"Cannot save {im.mode} images as PCX") from e
  116. # bytes per plane
  117. stride = (im.size[0] * bits + 7) // 8
  118. # stride should be even
  119. stride += stride % 2
  120. # Stride needs to be kept in sync with the PcxEncode.c version.
  121. # Ideally it should be passed in in the state, but the bytes value
  122. # gets overwritten.
  123. logger.debug(
  124. "PcxImagePlugin._save: xwidth: %d, bits: %d, stride: %d",
  125. im.size[0],
  126. bits,
  127. stride,
  128. )
  129. # under windows, we could determine the current screen size with
  130. # "Image.core.display_mode()[1]", but I think that's overkill...
  131. screen = im.size
  132. dpi = 100, 100
  133. # PCX header
  134. fp.write(
  135. o8(10)
  136. + o8(version)
  137. + o8(1)
  138. + o8(bits)
  139. + o16(0)
  140. + o16(0)
  141. + o16(im.size[0] - 1)
  142. + o16(im.size[1] - 1)
  143. + o16(dpi[0])
  144. + o16(dpi[1])
  145. + b"\0" * 24
  146. + b"\xFF" * 24
  147. + b"\0"
  148. + o8(planes)
  149. + o16(stride)
  150. + o16(1)
  151. + o16(screen[0])
  152. + o16(screen[1])
  153. + b"\0" * 54
  154. )
  155. assert fp.tell() == 128
  156. ImageFile._save(im, fp, [("pcx", (0, 0) + im.size, 0, (rawmode, bits * planes))])
  157. if im.mode == "P":
  158. # colour palette
  159. fp.write(o8(12))
  160. fp.write(im.im.getpalette("RGB", "RGB")) # 768 bytes
  161. elif im.mode == "L":
  162. # greyscale palette
  163. fp.write(o8(12))
  164. for i in range(256):
  165. fp.write(o8(i) * 3)
  166. # --------------------------------------------------------------------
  167. # registry
  168. Image.register_open(PcxImageFile.format, PcxImageFile, _accept)
  169. Image.register_save(PcxImageFile.format, _save)
  170. Image.register_extension(PcxImageFile.format, ".pcx")
  171. Image.register_mime(PcxImageFile.format, "image/x-pcx")