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.5KB

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