Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/PIL/SgiImagePlugin.py: 53%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

122 statements  

1# 

2# The Python Imaging Library. 

3# $Id$ 

4# 

5# SGI image file handling 

6# 

7# See "The SGI Image File Format (Draft version 0.97)", Paul Haeberli. 

8# <ftp://ftp.sgi.com/graphics/SGIIMAGESPEC> 

9# 

10# 

11# History: 

12# 2017-22-07 mb Add RLE decompression 

13# 2016-16-10 mb Add save method without compression 

14# 1995-09-10 fl Created 

15# 

16# Copyright (c) 2016 by Mickael Bonfill. 

17# Copyright (c) 2008 by Karsten Hiddemann. 

18# Copyright (c) 1997 by Secret Labs AB. 

19# Copyright (c) 1995 by Fredrik Lundh. 

20# 

21# See the README file for information on usage and redistribution. 

22# 

23from __future__ import annotations 

24 

25import os 

26import struct 

27from typing import IO 

28 

29from . import Image, ImageFile 

30from ._binary import i16be as i16 

31from ._binary import o8 

32 

33 

34def _accept(prefix: bytes) -> bool: 

35 return len(prefix) >= 2 and i16(prefix) == 474 

36 

37 

38MODES = { 

39 (1, 1, 1): "L", 

40 (1, 2, 1): "L", 

41 (2, 1, 1): "L;16B", 

42 (2, 2, 1): "L;16B", 

43 (1, 3, 3): "RGB", 

44 (2, 3, 3): "RGB;16B", 

45 (1, 3, 4): "RGBA", 

46 (2, 3, 4): "RGBA;16B", 

47} 

48 

49 

50## 

51# Image plugin for SGI images. 

52class SgiImageFile(ImageFile.ImageFile): 

53 format = "SGI" 

54 format_description = "SGI Image File Format" 

55 

56 def _open(self) -> None: 

57 # HEAD 

58 assert self.fp is not None 

59 

60 headlen = 512 

61 s = self.fp.read(headlen) 

62 

63 if not _accept(s): 

64 msg = "Not an SGI image file" 

65 raise ValueError(msg) 

66 

67 # compression : verbatim or RLE 

68 compression = s[2] 

69 

70 # bpc : 1 or 2 bytes (8bits or 16bits) 

71 bpc = s[3] 

72 

73 # dimension : 1, 2 or 3 (depending on xsize, ysize and zsize) 

74 dimension = i16(s, 4) 

75 

76 # xsize : width 

77 xsize = i16(s, 6) 

78 

79 # ysize : height 

80 ysize = i16(s, 8) 

81 

82 # zsize : channels count 

83 zsize = i16(s, 10) 

84 

85 # layout 

86 layout = bpc, dimension, zsize 

87 

88 # determine mode from bits/zsize 

89 rawmode = "" 

90 try: 

91 rawmode = MODES[layout] 

92 except KeyError: 

93 pass 

94 

95 if rawmode == "": 

96 msg = "Unsupported SGI image mode" 

97 raise ValueError(msg) 

98 

99 self._size = xsize, ysize 

100 self._mode = rawmode.split(";")[0] 

101 if self.mode == "RGB": 

102 self.custom_mimetype = "image/rgb" 

103 

104 # orientation -1 : scanlines begins at the bottom-left corner 

105 orientation = -1 

106 

107 # decoder info 

108 if compression == 0: 

109 pagesize = xsize * ysize * bpc 

110 if bpc == 2: 

111 self.tile = [ 

112 ImageFile._Tile( 

113 "SGI16", 

114 (0, 0) + self.size, 

115 headlen, 

116 (self.mode, 0, orientation), 

117 ) 

118 ] 

119 else: 

120 self.tile = [] 

121 offset = headlen 

122 for layer in self.mode: 

123 self.tile.append( 

124 ImageFile._Tile( 

125 "raw", (0, 0) + self.size, offset, (layer, 0, orientation) 

126 ) 

127 ) 

128 offset += pagesize 

129 elif compression == 1: 

130 self.tile = [ 

131 ImageFile._Tile( 

132 "sgi_rle", (0, 0) + self.size, headlen, (rawmode, orientation, bpc) 

133 ) 

134 ] 

135 

136 

137def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None: 

138 if im.mode not in {"RGB", "RGBA", "L"}: 

139 msg = "Unsupported SGI image mode" 

140 raise ValueError(msg) 

141 

142 # Get the keyword arguments 

143 info = im.encoderinfo 

144 

145 # Byte-per-pixel precision, 1 = 8bits per pixel 

146 bpc = info.get("bpc", 1) 

147 

148 if bpc not in (1, 2): 

149 msg = "Unsupported number of bytes per pixel" 

150 raise ValueError(msg) 

151 

152 # Flip the image, since the origin of SGI file is the bottom-left corner 

153 orientation = -1 

154 # Define the file as SGI File Format 

155 magic_number = 474 

156 # Run-Length Encoding Compression - Unsupported at this time 

157 rle = 0 

158 

159 # Number of dimensions (x,y,z) 

160 dim = 3 

161 # X Dimension = width / Y Dimension = height 

162 x, y = im.size 

163 if im.mode == "L" and y == 1: 

164 dim = 1 

165 elif im.mode == "L": 

166 dim = 2 

167 # Z Dimension: Number of channels 

168 z = len(im.mode) 

169 

170 if dim in {1, 2}: 

171 z = 1 

172 

173 # assert we've got the right number of bands. 

174 if len(im.getbands()) != z: 

175 msg = f"incorrect number of bands in SGI write: {z} vs {len(im.getbands())}" 

176 raise ValueError(msg) 

177 

178 # Minimum Byte value 

179 pinmin = 0 

180 # Maximum Byte value (255 = 8bits per pixel) 

181 pinmax = 255 

182 # Image name (79 characters max, truncated below in write) 

183 img_name = os.path.splitext(os.path.basename(filename))[0] 

184 if isinstance(img_name, str): 

185 img_name = img_name.encode("ascii", "ignore") 

186 # Standard representation of pixel in the file 

187 colormap = 0 

188 fp.write(struct.pack(">h", magic_number)) 

189 fp.write(o8(rle)) 

190 fp.write(o8(bpc)) 

191 fp.write(struct.pack(">H", dim)) 

192 fp.write(struct.pack(">H", x)) 

193 fp.write(struct.pack(">H", y)) 

194 fp.write(struct.pack(">H", z)) 

195 fp.write(struct.pack(">l", pinmin)) 

196 fp.write(struct.pack(">l", pinmax)) 

197 fp.write(struct.pack("4s", b"")) # dummy 

198 fp.write(struct.pack("79s", img_name)) # truncates to 79 chars 

199 fp.write(struct.pack("s", b"")) # force null byte after img_name 

200 fp.write(struct.pack(">l", colormap)) 

201 fp.write(struct.pack("404s", b"")) # dummy 

202 

203 rawmode = "L" 

204 if bpc == 2: 

205 rawmode = "L;16B" 

206 

207 for channel in im.split(): 

208 fp.write(channel.tobytes("raw", rawmode, 0, orientation)) 

209 

210 if hasattr(fp, "flush"): 

211 fp.flush() 

212 

213 

214class SGI16Decoder(ImageFile.PyDecoder): 

215 _pulls_fd = True 

216 

217 def decode(self, buffer: bytes | Image.SupportsArrayInterface) -> tuple[int, int]: 

218 assert self.fd is not None 

219 assert self.im is not None 

220 

221 rawmode, stride, orientation = self.args 

222 pagesize = self.state.xsize * self.state.ysize 

223 zsize = len(self.mode) 

224 self.fd.seek(512) 

225 

226 for band in range(zsize): 

227 channel = Image.new("L", (self.state.xsize, self.state.ysize)) 

228 channel.frombytes( 

229 self.fd.read(2 * pagesize), "raw", "L;16B", stride, orientation 

230 ) 

231 self.im.putband(channel.im, band) 

232 

233 return -1, 0 

234 

235 

236# 

237# registry 

238 

239 

240Image.register_decoder("SGI16", SGI16Decoder) 

241Image.register_open(SgiImageFile.format, SgiImageFile, _accept) 

242Image.register_save(SgiImageFile.format, _save) 

243Image.register_mime(SgiImageFile.format, "image/sgi") 

244 

245Image.register_extensions(SgiImageFile.format, [".bw", ".rgb", ".rgba", ".sgi"]) 

246 

247# End of file