Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/PIL/FliImagePlugin.py: 88%
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
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
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
19__lazy_modules__ = {"PIL._binary", "PIL._util"}
21import os
23from . import Image, ImageFile, ImagePalette
24from ._binary import i16le as i16
25from ._binary import i32le as i32
26from ._util import DeferredError
28#
29# decoder
32def _accept(prefix: bytes) -> bool:
33 return (
34 len(prefix) >= 16
35 and i16(prefix, 4) in [0xAF11, 0xAF12]
36 and i16(prefix, 14) in [0, 3] # flags
37 )
40##
41# Image plugin for the FLI/FLC animation format. Use the <b>seek</b>
42# method to load individual frames.
45class FliImageFile(ImageFile.ImageFile):
46 format = "FLI"
47 format_description = "Autodesk FLI/FLC Animation"
48 _close_exclusive_fp_after_loading = False
50 def _open(self) -> None:
51 # HEAD
52 assert self.fp is not None
53 s = self.fp.read(128)
54 if not (
55 _accept(s)
56 and s[20:22] == b"\x00" * 2
57 and s[42:80] == b"\x00" * 38
58 and s[88:] == b"\x00" * 40
59 ):
60 msg = "not an FLI/FLC file"
61 raise SyntaxError(msg)
63 # frames
64 self.n_frames = i16(s, 6)
65 self.is_animated = self.n_frames > 1
67 # image characteristics
68 self._mode = "P"
69 self._size = i16(s, 8), i16(s, 10)
71 # animation speed
72 duration = i32(s, 16)
73 magic = i16(s, 4)
74 if magic == 0xAF11:
75 duration = (duration * 1000) // 70
76 self.info["duration"] = duration
78 # look for palette
79 palette = [(a, a, a) for a in range(256)]
81 s = self.fp.read(16)
83 self.__offset = 128
85 if i16(s, 4) == 0xF100:
86 # prefix chunk; ignore it
87 self.fp.seek(self.__offset + i32(s))
88 s = self.fp.read(16)
90 if i16(s, 4) == 0xF1FA:
91 # look for palette chunk
92 number_of_subchunks = i16(s, 6)
93 chunk_size: int | None = None
94 for _ in range(number_of_subchunks):
95 if chunk_size is not None:
96 self.fp.seek(chunk_size - 6, os.SEEK_CUR)
97 s = self.fp.read(6)
98 chunk_type = i16(s, 4)
99 if chunk_type in (4, 11):
100 self._palette(palette, 2 if chunk_type == 11 else 0)
101 break
102 chunk_size = i32(s)
103 if not chunk_size:
104 break
106 self.palette = ImagePalette.raw("RGB", tuple(v for rgb in palette for v in rgb))
108 # set things up to decode first frame
109 self.__frame = -1
110 self._fp = self.fp
111 self.__rewind = self.fp.tell()
112 self.seek(0)
114 def _palette(self, palette: list[tuple[int, int, int]], shift: int) -> None:
115 # load palette
116 i = 0
117 assert self.fp is not None
118 for e in range(i16(self.fp.read(2))):
119 s = self.fp.read(2)
120 i += s[0]
121 n = s[1]
122 if n == 0:
123 n = 256
124 if i + n > 256:
125 msg = "too much palette data"
126 raise ValueError(msg)
127 s = self.fp.read(n * 3)
128 for n in range(0, len(s), 3):
129 r = s[n] << shift
130 g = s[n + 1] << shift
131 b = s[n + 2] << shift
132 if r > 255 or g > 255 or b > 255:
133 msg = "palette data out of range"
134 raise ValueError(msg)
135 palette[i] = (r, g, b)
136 i += 1
138 def seek(self, frame: int) -> None:
139 if not self._seek_check(frame):
140 return
141 if frame < self.__frame:
142 self._seek(0)
144 for f in range(self.__frame + 1, frame + 1):
145 self._seek(f)
147 def _seek(self, frame: int) -> None:
148 if isinstance(self._fp, DeferredError):
149 raise self._fp.ex
150 if frame == 0:
151 self.__frame = -1
152 self._fp.seek(self.__rewind)
153 self.__offset = 128
154 else:
155 # ensure that the previous frame was loaded
156 self.load()
158 if frame != self.__frame + 1:
159 msg = f"cannot seek to frame {frame}"
160 raise ValueError(msg)
161 self.__frame = frame
163 # move to next frame
164 self.fp = self._fp
165 self.fp.seek(self.__offset)
167 s = self.fp.read(4)
168 if not s:
169 msg = "missing frame size"
170 raise EOFError(msg)
172 framesize = i32(s)
174 self.decodermaxblock = framesize
175 self.tile = [ImageFile._Tile("fli", None, self.__offset)]
177 self.__offset += framesize
179 def tell(self) -> int:
180 return self.__frame
183#
184# registry
186Image.register_open(FliImageFile.format, FliImageFile, _accept)
188Image.register_extensions(FliImageFile.format, [".fli", ".flc"])