Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/PIL/PcxImagePlugin.py: 18%

83 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

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 

28import io 

29import logging 

30 

31from . import Image, ImageFile, ImagePalette 

32from ._binary import i16le as i16 

33from ._binary import o8 

34from ._binary import o16le as o16 

35 

36logger = logging.getLogger(__name__) 

37 

38 

39def _accept(prefix): 

40 return prefix[0] == 10 and prefix[1] in [0, 2, 3, 5] 

41 

42 

43## 

44# Image plugin for Paintbrush images. 

45 

46 

47class PcxImageFile(ImageFile.ImageFile): 

48 

49 format = "PCX" 

50 format_description = "Paintbrush" 

51 

52 def _open(self): 

53 

54 # header 

55 s = self.fp.read(128) 

56 if not _accept(s): 

57 raise SyntaxError("not a PCX file") 

58 

59 # image 

60 bbox = i16(s, 4), i16(s, 6), i16(s, 8) + 1, i16(s, 10) + 1 

61 if bbox[2] <= bbox[0] or bbox[3] <= bbox[1]: 

62 raise SyntaxError("bad PCX image size") 

63 logger.debug("BBox: %s %s %s %s", *bbox) 

64 

65 # format 

66 version = s[1] 

67 bits = s[3] 

68 planes = s[65] 

69 provided_stride = i16(s, 66) 

70 logger.debug( 

71 "PCX version %s, bits %s, planes %s, stride %s", 

72 version, 

73 bits, 

74 planes, 

75 provided_stride, 

76 ) 

77 

78 self.info["dpi"] = i16(s, 12), i16(s, 14) 

79 

80 if bits == 1 and planes == 1: 

81 mode = rawmode = "1" 

82 

83 elif bits == 1 and planes in (2, 4): 

84 mode = "P" 

85 rawmode = "P;%dL" % planes 

86 self.palette = ImagePalette.raw("RGB", s[16:64]) 

87 

88 elif version == 5 and bits == 8 and planes == 1: 

89 mode = rawmode = "L" 

90 # FIXME: hey, this doesn't work with the incremental loader !!! 

91 self.fp.seek(-769, io.SEEK_END) 

92 s = self.fp.read(769) 

93 if len(s) == 769 and s[0] == 12: 

94 # check if the palette is linear greyscale 

95 for i in range(256): 

96 if s[i * 3 + 1 : i * 3 + 4] != o8(i) * 3: 

97 mode = rawmode = "P" 

98 break 

99 if mode == "P": 

100 self.palette = ImagePalette.raw("RGB", s[1:]) 

101 self.fp.seek(128) 

102 

103 elif version == 5 and bits == 8 and planes == 3: 

104 mode = "RGB" 

105 rawmode = "RGB;L" 

106 

107 else: 

108 raise OSError("unknown PCX mode") 

109 

110 self.mode = mode 

111 self._size = bbox[2] - bbox[0], bbox[3] - bbox[1] 

112 

113 # Don't trust the passed in stride. 

114 # Calculate the approximate position for ourselves. 

115 # CVE-2020-35653 

116 stride = (self._size[0] * bits + 7) // 8 

117 

118 # While the specification states that this must be even, 

119 # not all images follow this 

120 if provided_stride != stride: 

121 stride += stride % 2 

122 

123 bbox = (0, 0) + self.size 

124 logger.debug("size: %sx%s", *self.size) 

125 

126 self.tile = [("pcx", bbox, self.fp.tell(), (rawmode, planes * stride))] 

127 

128 

129# -------------------------------------------------------------------- 

130# save PCX files 

131 

132 

133SAVE = { 

134 # mode: (version, bits, planes, raw mode) 

135 "1": (2, 1, 1, "1"), 

136 "L": (5, 8, 1, "L"), 

137 "P": (5, 8, 1, "P"), 

138 "RGB": (5, 8, 3, "RGB;L"), 

139} 

140 

141 

142def _save(im, fp, filename): 

143 

144 try: 

145 version, bits, planes, rawmode = SAVE[im.mode] 

146 except KeyError as e: 

147 raise ValueError(f"Cannot save {im.mode} images as PCX") from e 

148 

149 # bytes per plane 

150 stride = (im.size[0] * bits + 7) // 8 

151 # stride should be even 

152 stride += stride % 2 

153 # Stride needs to be kept in sync with the PcxEncode.c version. 

154 # Ideally it should be passed in in the state, but the bytes value 

155 # gets overwritten. 

156 

157 logger.debug( 

158 "PcxImagePlugin._save: xwidth: %d, bits: %d, stride: %d", 

159 im.size[0], 

160 bits, 

161 stride, 

162 ) 

163 

164 # under windows, we could determine the current screen size with 

165 # "Image.core.display_mode()[1]", but I think that's overkill... 

166 

167 screen = im.size 

168 

169 dpi = 100, 100 

170 

171 # PCX header 

172 fp.write( 

173 o8(10) 

174 + o8(version) 

175 + o8(1) 

176 + o8(bits) 

177 + o16(0) 

178 + o16(0) 

179 + o16(im.size[0] - 1) 

180 + o16(im.size[1] - 1) 

181 + o16(dpi[0]) 

182 + o16(dpi[1]) 

183 + b"\0" * 24 

184 + b"\xFF" * 24 

185 + b"\0" 

186 + o8(planes) 

187 + o16(stride) 

188 + o16(1) 

189 + o16(screen[0]) 

190 + o16(screen[1]) 

191 + b"\0" * 54 

192 ) 

193 

194 assert fp.tell() == 128 

195 

196 ImageFile._save(im, fp, [("pcx", (0, 0) + im.size, 0, (rawmode, bits * planes))]) 

197 

198 if im.mode == "P": 

199 # colour palette 

200 fp.write(o8(12)) 

201 palette = im.im.getpalette("RGB", "RGB") 

202 palette += b"\x00" * (768 - len(palette)) 

203 fp.write(palette) # 768 bytes 

204 elif im.mode == "L": 

205 # greyscale palette 

206 fp.write(o8(12)) 

207 for i in range(256): 

208 fp.write(o8(i) * 3) 

209 

210 

211# -------------------------------------------------------------------- 

212# registry 

213 

214 

215Image.register_open(PcxImageFile.format, PcxImageFile, _accept) 

216Image.register_save(PcxImageFile.format, _save) 

217 

218Image.register_extension(PcxImageFile.format, ".pcx") 

219 

220Image.register_mime(PcxImageFile.format, "image/x-pcx")