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#
27from __future__ import annotations
28
29import io
30import logging
31from typing import IO
32
33from . import Image, ImageFile, ImagePalette
34from ._binary import i16le as i16
35from ._binary import o8
36from ._binary import o16le as o16
37
38logger = logging.getLogger(__name__)
39
40
41def _accept(prefix: bytes) -> bool:
42 return prefix[0] == 10 and prefix[1] in [0, 2, 3, 5]
43
44
45##
46# Image plugin for Paintbrush images.
47
48
49class PcxImageFile(ImageFile.ImageFile):
50 format = "PCX"
51 format_description = "Paintbrush"
52
53 def _open(self) -> None:
54 # header
55 assert self.fp is not None
56
57 s = self.fp.read(128)
58 if not _accept(s):
59 msg = "not a PCX file"
60 raise SyntaxError(msg)
61
62 # image
63 bbox = i16(s, 4), i16(s, 6), i16(s, 8) + 1, i16(s, 10) + 1
64 if bbox[2] <= bbox[0] or bbox[3] <= bbox[1]:
65 msg = "bad PCX image size"
66 raise SyntaxError(msg)
67 logger.debug("BBox: %s %s %s %s", *bbox)
68
69 # format
70 version = s[1]
71 bits = s[3]
72 planes = s[65]
73 provided_stride = i16(s, 66)
74 logger.debug(
75 "PCX version %s, bits %s, planes %s, stride %s",
76 version,
77 bits,
78 planes,
79 provided_stride,
80 )
81
82 self.info["dpi"] = i16(s, 12), i16(s, 14)
83
84 if bits == 1 and planes == 1:
85 mode = rawmode = "1"
86
87 elif bits == 1 and planes in (2, 4):
88 mode = "P"
89 rawmode = "P;%dL" % planes
90 self.palette = ImagePalette.raw("RGB", s[16:64])
91
92 elif version == 5 and bits == 8 and planes == 1:
93 mode = rawmode = "L"
94 # FIXME: hey, this doesn't work with the incremental loader !!!
95 self.fp.seek(-769, io.SEEK_END)
96 s = self.fp.read(769)
97 if len(s) == 769 and s[0] == 12:
98 # check if the palette is linear grayscale
99 for i in range(256):
100 if s[i * 3 + 1 : i * 3 + 4] != o8(i) * 3:
101 mode = rawmode = "P"
102 break
103 if mode == "P":
104 self.palette = ImagePalette.raw("RGB", s[1:])
105 self.fp.seek(128)
106
107 elif version == 5 and bits == 8 and planes == 3:
108 mode = "RGB"
109 rawmode = "RGB;L"
110
111 else:
112 msg = "unknown PCX mode"
113 raise OSError(msg)
114
115 self._mode = mode
116 self._size = bbox[2] - bbox[0], bbox[3] - bbox[1]
117
118 # Don't trust the passed in stride.
119 # Calculate the approximate position for ourselves.
120 # CVE-2020-35653
121 stride = (self._size[0] * bits + 7) // 8
122
123 # While the specification states that this must be even,
124 # not all images follow this
125 if provided_stride != stride:
126 stride += stride % 2
127
128 bbox = (0, 0) + self.size
129 logger.debug("size: %sx%s", *self.size)
130
131 self.tile = [("pcx", bbox, self.fp.tell(), (rawmode, planes * stride))]
132
133
134# --------------------------------------------------------------------
135# save PCX files
136
137
138SAVE = {
139 # mode: (version, bits, planes, raw mode)
140 "1": (2, 1, 1, "1"),
141 "L": (5, 8, 1, "L"),
142 "P": (5, 8, 1, "P"),
143 "RGB": (5, 8, 3, "RGB;L"),
144}
145
146
147def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
148 try:
149 version, bits, planes, rawmode = SAVE[im.mode]
150 except KeyError as e:
151 msg = f"Cannot save {im.mode} images as PCX"
152 raise ValueError(msg) from e
153
154 # bytes per plane
155 stride = (im.size[0] * bits + 7) // 8
156 # stride should be even
157 stride += stride % 2
158 # Stride needs to be kept in sync with the PcxEncode.c version.
159 # Ideally it should be passed in in the state, but the bytes value
160 # gets overwritten.
161
162 logger.debug(
163 "PcxImagePlugin._save: xwidth: %d, bits: %d, stride: %d",
164 im.size[0],
165 bits,
166 stride,
167 )
168
169 # under windows, we could determine the current screen size with
170 # "Image.core.display_mode()[1]", but I think that's overkill...
171
172 screen = im.size
173
174 dpi = 100, 100
175
176 # PCX header
177 fp.write(
178 o8(10)
179 + o8(version)
180 + o8(1)
181 + o8(bits)
182 + o16(0)
183 + o16(0)
184 + o16(im.size[0] - 1)
185 + o16(im.size[1] - 1)
186 + o16(dpi[0])
187 + o16(dpi[1])
188 + b"\0" * 24
189 + b"\xFF" * 24
190 + b"\0"
191 + o8(planes)
192 + o16(stride)
193 + o16(1)
194 + o16(screen[0])
195 + o16(screen[1])
196 + b"\0" * 54
197 )
198
199 assert fp.tell() == 128
200
201 ImageFile._save(im, fp, [("pcx", (0, 0) + im.size, 0, (rawmode, bits * planes))])
202
203 if im.mode == "P":
204 # colour palette
205 assert im.im is not None
206
207 fp.write(o8(12))
208 palette = im.im.getpalette("RGB", "RGB")
209 palette += b"\x00" * (768 - len(palette))
210 fp.write(palette) # 768 bytes
211 elif im.mode == "L":
212 # grayscale palette
213 fp.write(o8(12))
214 for i in range(256):
215 fp.write(o8(i) * 3)
216
217
218# --------------------------------------------------------------------
219# registry
220
221
222Image.register_open(PcxImageFile.format, PcxImageFile, _accept)
223Image.register_save(PcxImageFile.format, _save)
224
225Image.register_extension(PcxImageFile.format, ".pcx")
226
227Image.register_mime(PcxImageFile.format, "image/x-pcx")