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

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

103 statements  

1# 

2# The Python Imaging Library. 

3# $Id$ 

4# 

5# FLI/FLC file handling. 

6# 

7# History: 

8# 95-09-01 fl Created 

9# 97-01-03 fl Fixed parser, setup decoder tile 

10# 98-07-15 fl Renamed offset attribute to avoid name clash 

11# 

12# Copyright (c) Secret Labs AB 1997-98. 

13# Copyright (c) Fredrik Lundh 1995-97. 

14# 

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

16# 

17from __future__ import annotations 

18 

19import os 

20 

21from . import Image, ImageFile, ImagePalette 

22from ._binary import i16le as i16 

23from ._binary import i32le as i32 

24from ._binary import o8 

25from ._util import DeferredError 

26 

27# 

28# decoder 

29 

30 

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

32 return ( 

33 len(prefix) >= 16 

34 and i16(prefix, 4) in [0xAF11, 0xAF12] 

35 and i16(prefix, 14) in [0, 3] # flags 

36 ) 

37 

38 

39## 

40# Image plugin for the FLI/FLC animation format. Use the <b>seek</b> 

41# method to load individual frames. 

42 

43 

44class FliImageFile(ImageFile.ImageFile): 

45 format = "FLI" 

46 format_description = "Autodesk FLI/FLC Animation" 

47 _close_exclusive_fp_after_loading = False 

48 

49 def _open(self) -> None: 

50 # HEAD 

51 assert self.fp is not None 

52 s = self.fp.read(128) 

53 if not ( 

54 _accept(s) 

55 and s[20:22] == b"\x00" * 2 

56 and s[42:80] == b"\x00" * 38 

57 and s[88:] == b"\x00" * 40 

58 ): 

59 msg = "not an FLI/FLC file" 

60 raise SyntaxError(msg) 

61 

62 # frames 

63 self.n_frames = i16(s, 6) 

64 self.is_animated = self.n_frames > 1 

65 

66 # image characteristics 

67 self._mode = "P" 

68 self._size = i16(s, 8), i16(s, 10) 

69 

70 # animation speed 

71 duration = i32(s, 16) 

72 magic = i16(s, 4) 

73 if magic == 0xAF11: 

74 duration = (duration * 1000) // 70 

75 self.info["duration"] = duration 

76 

77 # look for palette 

78 palette = [(a, a, a) for a in range(256)] 

79 

80 s = self.fp.read(16) 

81 

82 self.__offset = 128 

83 

84 if i16(s, 4) == 0xF100: 

85 # prefix chunk; ignore it 

86 self.fp.seek(self.__offset + i32(s)) 

87 s = self.fp.read(16) 

88 

89 if i16(s, 4) == 0xF1FA: 

90 # look for palette chunk 

91 number_of_subchunks = i16(s, 6) 

92 chunk_size: int | None = None 

93 for _ in range(number_of_subchunks): 

94 if chunk_size is not None: 

95 self.fp.seek(chunk_size - 6, os.SEEK_CUR) 

96 s = self.fp.read(6) 

97 chunk_type = i16(s, 4) 

98 if chunk_type in (4, 11): 

99 self._palette(palette, 2 if chunk_type == 11 else 0) 

100 break 

101 chunk_size = i32(s) 

102 if not chunk_size: 

103 break 

104 

105 self.palette = ImagePalette.raw( 

106 "RGB", b"".join(o8(r) + o8(g) + o8(b) for (r, g, b) in palette) 

107 ) 

108 

109 # set things up to decode first frame 

110 self.__frame = -1 

111 self._fp = self.fp 

112 self.__rewind = self.fp.tell() 

113 self.seek(0) 

114 

115 def _palette(self, palette: list[tuple[int, int, int]], shift: int) -> None: 

116 # load palette 

117 

118 i = 0 

119 assert self.fp is not None 

120 for e in range(i16(self.fp.read(2))): 

121 s = self.fp.read(2) 

122 i = i + s[0] 

123 n = s[1] 

124 if n == 0: 

125 n = 256 

126 s = self.fp.read(n * 3) 

127 for n in range(0, len(s), 3): 

128 r = s[n] << shift 

129 g = s[n + 1] << shift 

130 b = s[n + 2] << shift 

131 palette[i] = (r, g, b) 

132 i += 1 

133 

134 def seek(self, frame: int) -> None: 

135 if not self._seek_check(frame): 

136 return 

137 if frame < self.__frame: 

138 self._seek(0) 

139 

140 for f in range(self.__frame + 1, frame + 1): 

141 self._seek(f) 

142 

143 def _seek(self, frame: int) -> None: 

144 if isinstance(self._fp, DeferredError): 

145 raise self._fp.ex 

146 if frame == 0: 

147 self.__frame = -1 

148 self._fp.seek(self.__rewind) 

149 self.__offset = 128 

150 else: 

151 # ensure that the previous frame was loaded 

152 self.load() 

153 

154 if frame != self.__frame + 1: 

155 msg = f"cannot seek to frame {frame}" 

156 raise ValueError(msg) 

157 self.__frame = frame 

158 

159 # move to next frame 

160 self.fp = self._fp 

161 self.fp.seek(self.__offset) 

162 

163 s = self.fp.read(4) 

164 if not s: 

165 msg = "missing frame size" 

166 raise EOFError(msg) 

167 

168 framesize = i32(s) 

169 

170 self.decodermaxblock = framesize 

171 self.tile = [ImageFile._Tile("fli", (0, 0) + self.size, self.__offset)] 

172 

173 self.__offset += framesize 

174 

175 def tell(self) -> int: 

176 return self.__frame 

177 

178 

179# 

180# registry 

181 

182Image.register_open(FliImageFile.format, FliImageFile, _accept) 

183 

184Image.register_extensions(FliImageFile.format, [".fli", ".flc"])