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

74 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 

6## 

7# Image plugin for Palm pixmap images (output only). 

8## 

9 

10from . import Image, ImageFile 

11from ._binary import o8 

12from ._binary import o16be as o16b 

13 

14# fmt: off 

15_Palm8BitColormapValues = ( 

16 (255, 255, 255), (255, 204, 255), (255, 153, 255), (255, 102, 255), 

17 (255, 51, 255), (255, 0, 255), (255, 255, 204), (255, 204, 204), 

18 (255, 153, 204), (255, 102, 204), (255, 51, 204), (255, 0, 204), 

19 (255, 255, 153), (255, 204, 153), (255, 153, 153), (255, 102, 153), 

20 (255, 51, 153), (255, 0, 153), (204, 255, 255), (204, 204, 255), 

21 (204, 153, 255), (204, 102, 255), (204, 51, 255), (204, 0, 255), 

22 (204, 255, 204), (204, 204, 204), (204, 153, 204), (204, 102, 204), 

23 (204, 51, 204), (204, 0, 204), (204, 255, 153), (204, 204, 153), 

24 (204, 153, 153), (204, 102, 153), (204, 51, 153), (204, 0, 153), 

25 (153, 255, 255), (153, 204, 255), (153, 153, 255), (153, 102, 255), 

26 (153, 51, 255), (153, 0, 255), (153, 255, 204), (153, 204, 204), 

27 (153, 153, 204), (153, 102, 204), (153, 51, 204), (153, 0, 204), 

28 (153, 255, 153), (153, 204, 153), (153, 153, 153), (153, 102, 153), 

29 (153, 51, 153), (153, 0, 153), (102, 255, 255), (102, 204, 255), 

30 (102, 153, 255), (102, 102, 255), (102, 51, 255), (102, 0, 255), 

31 (102, 255, 204), (102, 204, 204), (102, 153, 204), (102, 102, 204), 

32 (102, 51, 204), (102, 0, 204), (102, 255, 153), (102, 204, 153), 

33 (102, 153, 153), (102, 102, 153), (102, 51, 153), (102, 0, 153), 

34 (51, 255, 255), (51, 204, 255), (51, 153, 255), (51, 102, 255), 

35 (51, 51, 255), (51, 0, 255), (51, 255, 204), (51, 204, 204), 

36 (51, 153, 204), (51, 102, 204), (51, 51, 204), (51, 0, 204), 

37 (51, 255, 153), (51, 204, 153), (51, 153, 153), (51, 102, 153), 

38 (51, 51, 153), (51, 0, 153), (0, 255, 255), (0, 204, 255), 

39 (0, 153, 255), (0, 102, 255), (0, 51, 255), (0, 0, 255), 

40 (0, 255, 204), (0, 204, 204), (0, 153, 204), (0, 102, 204), 

41 (0, 51, 204), (0, 0, 204), (0, 255, 153), (0, 204, 153), 

42 (0, 153, 153), (0, 102, 153), (0, 51, 153), (0, 0, 153), 

43 (255, 255, 102), (255, 204, 102), (255, 153, 102), (255, 102, 102), 

44 (255, 51, 102), (255, 0, 102), (255, 255, 51), (255, 204, 51), 

45 (255, 153, 51), (255, 102, 51), (255, 51, 51), (255, 0, 51), 

46 (255, 255, 0), (255, 204, 0), (255, 153, 0), (255, 102, 0), 

47 (255, 51, 0), (255, 0, 0), (204, 255, 102), (204, 204, 102), 

48 (204, 153, 102), (204, 102, 102), (204, 51, 102), (204, 0, 102), 

49 (204, 255, 51), (204, 204, 51), (204, 153, 51), (204, 102, 51), 

50 (204, 51, 51), (204, 0, 51), (204, 255, 0), (204, 204, 0), 

51 (204, 153, 0), (204, 102, 0), (204, 51, 0), (204, 0, 0), 

52 (153, 255, 102), (153, 204, 102), (153, 153, 102), (153, 102, 102), 

53 (153, 51, 102), (153, 0, 102), (153, 255, 51), (153, 204, 51), 

54 (153, 153, 51), (153, 102, 51), (153, 51, 51), (153, 0, 51), 

55 (153, 255, 0), (153, 204, 0), (153, 153, 0), (153, 102, 0), 

56 (153, 51, 0), (153, 0, 0), (102, 255, 102), (102, 204, 102), 

57 (102, 153, 102), (102, 102, 102), (102, 51, 102), (102, 0, 102), 

58 (102, 255, 51), (102, 204, 51), (102, 153, 51), (102, 102, 51), 

59 (102, 51, 51), (102, 0, 51), (102, 255, 0), (102, 204, 0), 

60 (102, 153, 0), (102, 102, 0), (102, 51, 0), (102, 0, 0), 

61 (51, 255, 102), (51, 204, 102), (51, 153, 102), (51, 102, 102), 

62 (51, 51, 102), (51, 0, 102), (51, 255, 51), (51, 204, 51), 

63 (51, 153, 51), (51, 102, 51), (51, 51, 51), (51, 0, 51), 

64 (51, 255, 0), (51, 204, 0), (51, 153, 0), (51, 102, 0), 

65 (51, 51, 0), (51, 0, 0), (0, 255, 102), (0, 204, 102), 

66 (0, 153, 102), (0, 102, 102), (0, 51, 102), (0, 0, 102), 

67 (0, 255, 51), (0, 204, 51), (0, 153, 51), (0, 102, 51), 

68 (0, 51, 51), (0, 0, 51), (0, 255, 0), (0, 204, 0), 

69 (0, 153, 0), (0, 102, 0), (0, 51, 0), (17, 17, 17), 

70 (34, 34, 34), (68, 68, 68), (85, 85, 85), (119, 119, 119), 

71 (136, 136, 136), (170, 170, 170), (187, 187, 187), (221, 221, 221), 

72 (238, 238, 238), (192, 192, 192), (128, 0, 0), (128, 0, 128), 

73 (0, 128, 0), (0, 128, 128), (0, 0, 0), (0, 0, 0), 

74 (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), 

75 (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), 

76 (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), 

77 (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), 

78 (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), 

79 (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)) 

80# fmt: on 

81 

82 

83# so build a prototype image to be used for palette resampling 

84def build_prototype_image(): 

85 image = Image.new("L", (1, len(_Palm8BitColormapValues))) 

86 image.putdata(list(range(len(_Palm8BitColormapValues)))) 

87 palettedata = () 

88 for colormapValue in _Palm8BitColormapValues: 

89 palettedata += colormapValue 

90 palettedata += (0, 0, 0) * (256 - len(_Palm8BitColormapValues)) 

91 image.putpalette(palettedata) 

92 return image 

93 

94 

95Palm8BitColormapImage = build_prototype_image() 

96 

97# OK, we now have in Palm8BitColormapImage, 

98# a "P"-mode image with the right palette 

99# 

100# -------------------------------------------------------------------- 

101 

102_FLAGS = {"custom-colormap": 0x4000, "is-compressed": 0x8000, "has-transparent": 0x2000} 

103 

104_COMPRESSION_TYPES = {"none": 0xFF, "rle": 0x01, "scanline": 0x00} 

105 

106 

107# 

108# -------------------------------------------------------------------- 

109 

110## 

111# (Internal) Image save plugin for the Palm format. 

112 

113 

114def _save(im, fp, filename): 

115 

116 if im.mode == "P": 

117 

118 # we assume this is a color Palm image with the standard colormap, 

119 # unless the "info" dict has a "custom-colormap" field 

120 

121 rawmode = "P" 

122 bpp = 8 

123 version = 1 

124 

125 elif im.mode == "L": 

126 if im.encoderinfo.get("bpp") in (1, 2, 4): 

127 # this is 8-bit grayscale, so we shift it to get the high-order bits, 

128 # and invert it because 

129 # Palm does greyscale from white (0) to black (1) 

130 bpp = im.encoderinfo["bpp"] 

131 im = im.point( 

132 lambda x, shift=8 - bpp, maxval=(1 << bpp) - 1: maxval - (x >> shift) 

133 ) 

134 elif im.info.get("bpp") in (1, 2, 4): 

135 # here we assume that even though the inherent mode is 8-bit grayscale, 

136 # only the lower bpp bits are significant. 

137 # We invert them to match the Palm. 

138 bpp = im.info["bpp"] 

139 im = im.point(lambda x, maxval=(1 << bpp) - 1: maxval - (x & maxval)) 

140 else: 

141 raise OSError(f"cannot write mode {im.mode} as Palm") 

142 

143 # we ignore the palette here 

144 im.mode = "P" 

145 rawmode = "P;" + str(bpp) 

146 version = 1 

147 

148 elif im.mode == "1": 

149 

150 # monochrome -- write it inverted, as is the Palm standard 

151 rawmode = "1;I" 

152 bpp = 1 

153 version = 0 

154 

155 else: 

156 

157 raise OSError(f"cannot write mode {im.mode} as Palm") 

158 

159 # 

160 # make sure image data is available 

161 im.load() 

162 

163 # write header 

164 

165 cols = im.size[0] 

166 rows = im.size[1] 

167 

168 rowbytes = int((cols + (16 // bpp - 1)) / (16 // bpp)) * 2 

169 transparent_index = 0 

170 compression_type = _COMPRESSION_TYPES["none"] 

171 

172 flags = 0 

173 if im.mode == "P" and "custom-colormap" in im.info: 

174 flags = flags & _FLAGS["custom-colormap"] 

175 colormapsize = 4 * 256 + 2 

176 colormapmode = im.palette.mode 

177 colormap = im.getdata().getpalette() 

178 else: 

179 colormapsize = 0 

180 

181 if "offset" in im.info: 

182 offset = (rowbytes * rows + 16 + 3 + colormapsize) // 4 

183 else: 

184 offset = 0 

185 

186 fp.write(o16b(cols) + o16b(rows) + o16b(rowbytes) + o16b(flags)) 

187 fp.write(o8(bpp)) 

188 fp.write(o8(version)) 

189 fp.write(o16b(offset)) 

190 fp.write(o8(transparent_index)) 

191 fp.write(o8(compression_type)) 

192 fp.write(o16b(0)) # reserved by Palm 

193 

194 # now write colormap if necessary 

195 

196 if colormapsize > 0: 

197 fp.write(o16b(256)) 

198 for i in range(256): 

199 fp.write(o8(i)) 

200 if colormapmode == "RGB": 

201 fp.write( 

202 o8(colormap[3 * i]) 

203 + o8(colormap[3 * i + 1]) 

204 + o8(colormap[3 * i + 2]) 

205 ) 

206 elif colormapmode == "RGBA": 

207 fp.write( 

208 o8(colormap[4 * i]) 

209 + o8(colormap[4 * i + 1]) 

210 + o8(colormap[4 * i + 2]) 

211 ) 

212 

213 # now convert data to raw form 

214 ImageFile._save(im, fp, [("raw", (0, 0) + im.size, 0, (rawmode, rowbytes, 1))]) 

215 

216 if hasattr(fp, "flush"): 

217 fp.flush() 

218 

219 

220# 

221# -------------------------------------------------------------------- 

222 

223Image.register_save("Palm", _save) 

224 

225Image.register_extension("Palm", ".palm") 

226 

227Image.register_mime("Palm", "image/palm")