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

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

114 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 

25__lazy_modules__ = {"PIL._binary", "struct"} 

26 

27import os 

28import struct 

29 

30from . import Image, ImageFile 

31from ._binary import i16be as i16 

32from ._binary import o8 

33 

34TYPE_CHECKING = False 

35if TYPE_CHECKING: 

36 from typing import IO 

37 

38 

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

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

41 

42 

43MODES = { 

44 (1, 1, 1): "L", 

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

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

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

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

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

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

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

52} 

53 

54 

55## 

56# Image plugin for SGI images. 

57class SgiImageFile(ImageFile.ImageFile): 

58 format = "SGI" 

59 format_description = "SGI Image File Format" 

60 

61 def _open(self) -> None: 

62 # HEAD 

63 assert self.fp is not None 

64 

65 headlen = 512 

66 s = self.fp.read(headlen) 

67 

68 if not _accept(s): 

69 msg = "Not an SGI image file" 

70 raise ValueError(msg) 

71 

72 # compression : verbatim or RLE 

73 compression = s[2] 

74 

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

76 bpc = s[3] 

77 

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

79 dimension = i16(s, 4) 

80 

81 # xsize : width 

82 xsize = i16(s, 6) 

83 

84 # ysize : height 

85 ysize = i16(s, 8) 

86 

87 # zsize : channels count 

88 zsize = i16(s, 10) 

89 

90 # determine mode from bits/zsize 

91 try: 

92 rawmode = MODES[(bpc, dimension, zsize)] 

93 except KeyError: 

94 msg = "Unsupported SGI image mode" 

95 raise ValueError(msg) 

96 

97 self._size = xsize, ysize 

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

99 if self.mode == "RGB": 

100 self.custom_mimetype = "image/rgb" 

101 

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

103 orientation = -1 

104 

105 # decoder info 

106 if compression == 0: 

107 pagesize = xsize * ysize * bpc 

108 if bpc == 2: 

109 self.tile = [ 

110 ImageFile._Tile( 

111 "SGI16", 

112 None, 

113 headlen, 

114 (self.mode, 0, orientation), 

115 ) 

116 ] 

117 else: 

118 self.tile = [] 

119 offset = headlen 

120 for layer in self.mode: 

121 self.tile.append( 

122 ImageFile._Tile("raw", None, offset, (layer, 0, orientation)) 

123 ) 

124 offset += pagesize 

125 elif compression == 1: 

126 self.tile = [ 

127 ImageFile._Tile("sgi_rle", None, headlen, (rawmode, orientation, bpc)) 

128 ] 

129 

130 

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

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

133 msg = "Unsupported SGI image mode" 

134 raise ValueError(msg) 

135 

136 # Get the keyword arguments 

137 info = im.encoderinfo 

138 

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

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

141 

142 if bpc not in (1, 2): 

143 msg = "Unsupported number of bytes per pixel" 

144 raise ValueError(msg) 

145 

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

147 orientation = -1 

148 # Define the file as SGI File Format 

149 magic_number = 474 

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

151 rle = 0 

152 

153 # X Dimension = width / Y Dimension = height 

154 x, y = im.size 

155 # Z Dimension: Number of channels 

156 z = len(im.mode) 

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

158 if im.mode == "L": 

159 dimension = 1 if y == 1 else 2 

160 else: 

161 dimension = 3 

162 

163 # Minimum Byte value 

164 pinmin = 0 

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

166 pinmax = 255 

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

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

169 if isinstance(img_name, str): 

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

171 # Standard representation of pixel in the file 

172 colormap = 0 

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

174 fp.write(o8(rle)) 

175 fp.write(o8(bpc)) 

176 fp.write(struct.pack(">H", dimension)) 

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

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

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

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

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

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

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

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

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

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

187 

188 rawmode = "L" 

189 if bpc == 2: 

190 rawmode = "L;16B" 

191 

192 for channel in im.split(): 

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

194 

195 if hasattr(fp, "flush"): 

196 fp.flush() 

197 

198 

199class SGI16Decoder(ImageFile.PyDecoder): 

200 _pulls_fd = True 

201 

202 def decode(self, buffer: Image.DecoderInput) -> tuple[int, int]: 

203 assert self.fd is not None 

204 assert self.im is not None 

205 

206 rawmode, stride, orientation = self.args 

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

208 zsize = len(self.mode) 

209 self.fd.seek(512) 

210 

211 for band in range(zsize): 

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

213 channel.frombytes( 

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

215 ) 

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

217 

218 return -1, 0 

219 

220 

221# 

222# registry 

223 

224 

225Image.register_decoder("SGI16", SGI16Decoder) 

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

227Image.register_save(SgiImageFile.format, _save) 

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

229 

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

231 

232# End of file