Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pillow-10.4.0-py3.8-linux-x86_64.egg/PIL/FliImagePlugin.py: 19%

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

99 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 

25 

26# 

27# decoder 

28 

29 

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

31 return ( 

32 len(prefix) >= 6 

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

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

35 ) 

36 

37 

38## 

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

40# method to load individual frames. 

41 

42 

43class FliImageFile(ImageFile.ImageFile): 

44 format = "FLI" 

45 format_description = "Autodesk FLI/FLC Animation" 

46 _close_exclusive_fp_after_loading = False 

47 

48 def _open(self): 

49 # HEAD 

50 s = self.fp.read(128) 

51 if not (_accept(s) and s[20:22] == b"\x00\x00"): 

52 msg = "not an FLI/FLC file" 

53 raise SyntaxError(msg) 

54 

55 # frames 

56 self.n_frames = i16(s, 6) 

57 self.is_animated = self.n_frames > 1 

58 

59 # image characteristics 

60 self._mode = "P" 

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

62 

63 # animation speed 

64 duration = i32(s, 16) 

65 magic = i16(s, 4) 

66 if magic == 0xAF11: 

67 duration = (duration * 1000) // 70 

68 self.info["duration"] = duration 

69 

70 # look for palette 

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

72 

73 s = self.fp.read(16) 

74 

75 self.__offset = 128 

76 

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

78 # prefix chunk; ignore it 

79 self.__offset = self.__offset + i32(s) 

80 self.fp.seek(self.__offset) 

81 s = self.fp.read(16) 

82 

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

84 # look for palette chunk 

85 number_of_subchunks = i16(s, 6) 

86 chunk_size = None 

87 for _ in range(number_of_subchunks): 

88 if chunk_size is not None: 

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

90 s = self.fp.read(6) 

91 chunk_type = i16(s, 4) 

92 if chunk_type in (4, 11): 

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

94 break 

95 chunk_size = i32(s) 

96 if not chunk_size: 

97 break 

98 

99 palette = [o8(r) + o8(g) + o8(b) for (r, g, b) in palette] 

100 self.palette = ImagePalette.raw("RGB", b"".join(palette)) 

101 

102 # set things up to decode first frame 

103 self.__frame = -1 

104 self._fp = self.fp 

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

106 self.seek(0) 

107 

108 def _palette(self, palette, shift): 

109 # load palette 

110 

111 i = 0 

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

113 s = self.fp.read(2) 

114 i = i + s[0] 

115 n = s[1] 

116 if n == 0: 

117 n = 256 

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

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

120 r = s[n] << shift 

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

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

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

124 i += 1 

125 

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

127 if not self._seek_check(frame): 

128 return 

129 if frame < self.__frame: 

130 self._seek(0) 

131 

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

133 self._seek(f) 

134 

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

136 if frame == 0: 

137 self.__frame = -1 

138 self._fp.seek(self.__rewind) 

139 self.__offset = 128 

140 else: 

141 # ensure that the previous frame was loaded 

142 self.load() 

143 

144 if frame != self.__frame + 1: 

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

146 raise ValueError(msg) 

147 self.__frame = frame 

148 

149 # move to next frame 

150 self.fp = self._fp 

151 self.fp.seek(self.__offset) 

152 

153 s = self.fp.read(4) 

154 if not s: 

155 msg = "missing frame size" 

156 raise EOFError(msg) 

157 

158 framesize = i32(s) 

159 

160 self.decodermaxblock = framesize 

161 self.tile = [("fli", (0, 0) + self.size, self.__offset, None)] 

162 

163 self.__offset += framesize 

164 

165 def tell(self) -> int: 

166 return self.__frame 

167 

168 

169# 

170# registry 

171 

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

173 

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