2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# The Python Imaging Library.
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
# PCX file handling
|
|
|
|
#
|
|
|
|
# This format was originally used by ZSoft's popular PaintBrush
|
|
|
|
# program for the IBM PC. It is also supported by many MS-DOS and
|
|
|
|
# Windows applications, including the Windows PaintBrush program in
|
|
|
|
# Windows 3.
|
|
|
|
#
|
|
|
|
# history:
|
|
|
|
# 1995-09-01 fl Created
|
|
|
|
# 1996-05-20 fl Fixed RGB support
|
|
|
|
# 1997-01-03 fl Fixed 2-bit and 4-bit support
|
|
|
|
# 1999-02-03 fl Fixed 8-bit support (broken in 1.0b1)
|
|
|
|
# 1999-02-07 fl Added write support
|
|
|
|
# 2002-06-09 fl Made 2-bit and 4-bit support a bit more robust
|
|
|
|
# 2002-07-30 fl Seek from to current position, not beginning of file
|
|
|
|
# 2003-06-03 fl Extract DPI settings (info["dpi"])
|
|
|
|
#
|
|
|
|
# Copyright (c) 1997-2003 by Secret Labs AB.
|
|
|
|
# Copyright (c) 1995-2003 by Fredrik Lundh.
|
|
|
|
#
|
|
|
|
# See the README file for information on usage and redistribution.
|
|
|
|
#
|
|
|
|
|
2019-01-13 05:05:46 +03:00
|
|
|
import io
|
2015-02-06 21:58:07 +03:00
|
|
|
import logging
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2017-01-17 16:22:18 +03:00
|
|
|
from . import Image, ImageFile, ImagePalette
|
2020-09-01 20:16:46 +03:00
|
|
|
from ._binary import i16le as i16
|
|
|
|
from ._binary import o8
|
|
|
|
from ._binary import o16le as o16
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-02-06 21:58:07 +03:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def _accept(prefix):
|
2020-05-08 19:48:02 +03:00
|
|
|
return prefix[0] == 10 and prefix[1] in [0, 2, 3, 5]
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
##
|
|
|
|
# Image plugin for Paintbrush images.
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
class PcxImageFile(ImageFile.ImageFile):
|
|
|
|
|
|
|
|
format = "PCX"
|
|
|
|
format_description = "Paintbrush"
|
|
|
|
|
|
|
|
def _open(self):
|
|
|
|
|
|
|
|
# header
|
|
|
|
s = self.fp.read(128)
|
|
|
|
if not _accept(s):
|
2012-10-11 07:52:53 +04:00
|
|
|
raise SyntaxError("not a PCX file")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# image
|
2019-03-21 16:28:20 +03:00
|
|
|
bbox = i16(s, 4), i16(s, 6), i16(s, 8) + 1, i16(s, 10) + 1
|
2010-07-31 06:52:47 +04:00
|
|
|
if bbox[2] <= bbox[0] or bbox[3] <= bbox[1]:
|
2012-10-11 07:52:53 +04:00
|
|
|
raise SyntaxError("bad PCX image size")
|
2015-02-06 21:58:07 +03:00
|
|
|
logger.debug("BBox: %s %s %s %s", *bbox)
|
2014-03-04 08:29:14 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
# format
|
2020-05-08 19:48:02 +03:00
|
|
|
version = s[1]
|
|
|
|
bits = s[3]
|
|
|
|
planes = s[65]
|
2021-01-21 11:29:11 +03:00
|
|
|
provided_stride = i16(s, 66)
|
2019-03-21 16:28:20 +03:00
|
|
|
logger.debug(
|
|
|
|
"PCX version %s, bits %s, planes %s, stride %s",
|
|
|
|
version,
|
|
|
|
bits,
|
|
|
|
planes,
|
2021-01-21 11:29:11 +03:00
|
|
|
provided_stride,
|
2019-03-21 16:28:20 +03:00
|
|
|
)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
self.info["dpi"] = i16(s, 12), i16(s, 14)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
if bits == 1 and planes == 1:
|
|
|
|
mode = rawmode = "1"
|
|
|
|
|
|
|
|
elif bits == 1 and planes in (2, 4):
|
|
|
|
mode = "P"
|
|
|
|
rawmode = "P;%dL" % planes
|
|
|
|
self.palette = ImagePalette.raw("RGB", s[16:64])
|
|
|
|
|
|
|
|
elif version == 5 and bits == 8 and planes == 1:
|
|
|
|
mode = rawmode = "L"
|
|
|
|
# FIXME: hey, this doesn't work with the incremental loader !!!
|
2019-01-13 05:05:46 +03:00
|
|
|
self.fp.seek(-769, io.SEEK_END)
|
2010-07-31 06:52:47 +04:00
|
|
|
s = self.fp.read(769)
|
2020-05-08 19:48:02 +03:00
|
|
|
if len(s) == 769 and s[0] == 12:
|
2010-07-31 06:52:47 +04:00
|
|
|
# check if the palette is linear greyscale
|
|
|
|
for i in range(256):
|
2019-03-21 16:28:20 +03:00
|
|
|
if s[i * 3 + 1 : i * 3 + 4] != o8(i) * 3:
|
2010-07-31 06:52:47 +04:00
|
|
|
mode = rawmode = "P"
|
|
|
|
break
|
|
|
|
if mode == "P":
|
|
|
|
self.palette = ImagePalette.raw("RGB", s[1:])
|
|
|
|
self.fp.seek(128)
|
|
|
|
|
|
|
|
elif version == 5 and bits == 8 and planes == 3:
|
|
|
|
mode = "RGB"
|
|
|
|
rawmode = "RGB;L"
|
|
|
|
|
|
|
|
else:
|
2019-09-30 17:56:31 +03:00
|
|
|
raise OSError("unknown PCX mode")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
self.mode = mode
|
2019-03-21 16:28:20 +03:00
|
|
|
self._size = bbox[2] - bbox[0], bbox[3] - bbox[1]
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2021-01-21 11:29:11 +03:00
|
|
|
# Don't trust the passed in stride.
|
|
|
|
# Calculate the approximate position for ourselves.
|
2021-01-20 12:43:00 +03:00
|
|
|
# CVE-2020-35653
|
2020-12-17 02:17:53 +03:00
|
|
|
stride = (self._size[0] * bits + 7) // 8
|
2021-01-21 11:29:11 +03:00
|
|
|
|
|
|
|
# While the specification states that this must be even,
|
|
|
|
# not all images follow this
|
|
|
|
if provided_stride != stride:
|
|
|
|
stride += stride % 2
|
2020-12-17 02:17:53 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
bbox = (0, 0) + self.size
|
2015-02-06 21:58:07 +03:00
|
|
|
logger.debug("size: %sx%s", *self.size)
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
self.tile = [("pcx", bbox, self.fp.tell(), (rawmode, planes * stride))]
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# save PCX files
|
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
SAVE = {
|
|
|
|
# mode: (version, bits, planes, raw mode)
|
|
|
|
"1": (2, 1, 1, "1"),
|
|
|
|
"L": (5, 8, 1, "L"),
|
|
|
|
"P": (5, 8, 1, "P"),
|
|
|
|
"RGB": (5, 8, 3, "RGB;L"),
|
|
|
|
}
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2017-11-24 13:34:58 +03:00
|
|
|
def _save(im, fp, filename):
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
try:
|
|
|
|
version, bits, planes, rawmode = SAVE[im.mode]
|
2020-06-21 13:13:35 +03:00
|
|
|
except KeyError as e:
|
2020-07-16 12:43:29 +03:00
|
|
|
raise ValueError(f"Cannot save {im.mode} images as PCX") from e
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# bytes per plane
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
stride = (im.size[0] * bits + 7) // 8
|
2014-03-04 11:03:36 +04:00
|
|
|
# stride should be even
|
2014-05-10 08:36:15 +04:00
|
|
|
stride += stride % 2
|
2014-03-04 11:03:36 +04:00
|
|
|
# Stride needs to be kept in sync with the PcxEncode.c version.
|
|
|
|
# Ideally it should be passed in in the state, but the bytes value
|
2014-08-26 17:47:10 +04:00
|
|
|
# gets overwritten.
|
2014-03-04 11:03:36 +04:00
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
logger.debug(
|
|
|
|
"PcxImagePlugin._save: xwidth: %d, bits: %d, stride: %d",
|
|
|
|
im.size[0],
|
|
|
|
bits,
|
|
|
|
stride,
|
|
|
|
)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# under windows, we could determine the current screen size with
|
|
|
|
# "Image.core.display_mode()[1]", but I think that's overkill...
|
|
|
|
|
|
|
|
screen = im.size
|
|
|
|
|
|
|
|
dpi = 100, 100
|
|
|
|
|
|
|
|
# PCX header
|
|
|
|
fp.write(
|
2019-03-21 16:28:20 +03:00
|
|
|
o8(10)
|
|
|
|
+ o8(version)
|
|
|
|
+ o8(1)
|
|
|
|
+ o8(bits)
|
|
|
|
+ o16(0)
|
|
|
|
+ o16(0)
|
|
|
|
+ o16(im.size[0] - 1)
|
|
|
|
+ o16(im.size[1] - 1)
|
|
|
|
+ o16(dpi[0])
|
|
|
|
+ o16(dpi[1])
|
|
|
|
+ b"\0" * 24
|
|
|
|
+ b"\xFF" * 24
|
|
|
|
+ b"\0"
|
|
|
|
+ o8(planes)
|
|
|
|
+ o16(stride)
|
|
|
|
+ o16(1)
|
|
|
|
+ o16(screen[0])
|
|
|
|
+ o16(screen[1])
|
|
|
|
+ b"\0" * 54
|
|
|
|
)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
assert fp.tell() == 128
|
|
|
|
|
2019-03-21 16:28:20 +03:00
|
|
|
ImageFile._save(im, fp, [("pcx", (0, 0) + im.size, 0, (rawmode, bits * planes))])
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
if im.mode == "P":
|
|
|
|
# colour palette
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
fp.write(o8(12))
|
2022-06-22 10:27:49 +03:00
|
|
|
palette = im.im.getpalette("RGB", "RGB")
|
|
|
|
palette += b"\x00" * (768 - len(palette))
|
|
|
|
fp.write(palette) # 768 bytes
|
2010-07-31 06:52:47 +04:00
|
|
|
elif im.mode == "L":
|
|
|
|
# greyscale palette
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
fp.write(o8(12))
|
2010-07-31 06:52:47 +04:00
|
|
|
for i in range(256):
|
2019-03-21 16:28:20 +03:00
|
|
|
fp.write(o8(i) * 3)
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# registry
|
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2015-07-04 16:29:58 +03:00
|
|
|
Image.register_open(PcxImageFile.format, PcxImageFile, _accept)
|
|
|
|
Image.register_save(PcxImageFile.format, _save)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-07-04 16:29:58 +03:00
|
|
|
Image.register_extension(PcxImageFile.format, ".pcx")
|
2018-12-31 09:41:41 +03:00
|
|
|
|
|
|
|
Image.register_mime(PcxImageFile.format, "image/x-pcx")
|