Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.10/site-packages/pillow-11.0.0-py3.10-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

98 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) -> None: 

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: int | None = 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 self.palette = ImagePalette.raw( 

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

101 ) 

102 

103 # set things up to decode first frame 

104 self.__frame = -1 

105 self._fp = self.fp 

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

107 self.seek(0) 

108 

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

110 # load palette 

111 

112 i = 0 

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

114 s = self.fp.read(2) 

115 i = i + s[0] 

116 n = s[1] 

117 if n == 0: 

118 n = 256 

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

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

121 r = s[n] << shift 

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

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

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

125 i += 1 

126 

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

128 if not self._seek_check(frame): 

129 return 

130 if frame < self.__frame: 

131 self._seek(0) 

132 

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

134 self._seek(f) 

135 

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

137 if frame == 0: 

138 self.__frame = -1 

139 self._fp.seek(self.__rewind) 

140 self.__offset = 128 

141 else: 

142 # ensure that the previous frame was loaded 

143 self.load() 

144 

145 if frame != self.__frame + 1: 

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

147 raise ValueError(msg) 

148 self.__frame = frame 

149 

150 # move to next frame 

151 self.fp = self._fp 

152 self.fp.seek(self.__offset) 

153 

154 s = self.fp.read(4) 

155 if not s: 

156 msg = "missing frame size" 

157 raise EOFError(msg) 

158 

159 framesize = i32(s) 

160 

161 self.decodermaxblock = framesize 

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

163 

164 self.__offset += framesize 

165 

166 def tell(self) -> int: 

167 return self.__frame 

168 

169 

170# 

171# registry 

172 

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

174 

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