Skip to content

Commit

Permalink
Merge 7088e1a into 5f5ac09
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Aug 12, 2017
2 parents 5f5ac09 + 7088e1a commit 50be6bd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
38 changes: 9 additions & 29 deletions PIL/FliImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class FliImageFile(ImageFile.ImageFile):
format = "FLI"
format_description = "Autodesk FLI/FLC Animation"
_close_exclusive_fp_after_loading = False

def _open(self):

# HEAD
Expand All @@ -49,6 +49,9 @@ def _open(self):
s[20:22] == b"\x00\x00"): # reserved
raise SyntaxError("not an FLI/FLC file")

# frames
self.__framecount = i16(s[6:8])

# image characteristics
self.mode = "P"
self.size = i16(s[8:10]), i16(s[10:12])
Expand Down Expand Up @@ -86,8 +89,6 @@ def _open(self):
self.__frame = -1
self.__fp = self.fp
self.__rewind = self.fp.tell()
self._n_frames = None
self._is_animated = None
self.seek(0)

def _palette(self, palette, shift):
Expand All @@ -110,43 +111,22 @@ def _palette(self, palette, shift):

@property
def n_frames(self):
if self._n_frames is None:
current = self.tell()
try:
while True:
self.seek(self.tell() + 1)
except EOFError:
self._n_frames = self.tell() + 1
self.seek(current)
return self._n_frames
return self.__framecount

@property
def is_animated(self):
if self._is_animated is None:
current = self.tell()

try:
self.seek(1)
self._is_animated = True
except EOFError:
self._is_animated = False

self.seek(current)
return self._is_animated
return self.__framecount > 1

def seek(self, frame):
if frame == self.__frame:
return
if frame < self.__frame:
self._seek(0)
if frame >= self.__framecount:
raise EOFError("no more images in FLI file")

last_frame = self.__frame
for f in range(self.__frame + 1, frame + 1):
try:
self._seek(f)
except EOFError:
self.seek(last_frame)
raise EOFError("no more images in FLI file")
self._seek(f)

def _seek(self, frame):
if frame == 0:
Expand Down
19 changes: 19 additions & 0 deletions Tests/test_file_fli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ def test_sanity(self):
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.format, "FLI")

def test_tell(self):
# Arrange
im = Image.open(test_file)

# Act
frame = im.tell()

# Assert
self.assertEqual(frame, 0)

def test_invalid_file(self):
invalid_file = "Tests/images/flower.jpg"

Expand All @@ -39,6 +49,15 @@ def test_eoferror(self):
except EOFError:
self.assertLess(im.tell(), n_frames)

def test_seek_outside(self):
# Test negative seek
im = Image.open(test_file)
im.seek(-1)
self.assertEqual(im.tell(), 0)

# Test seek past end of file
self.assertRaises(EOFError, lambda: im.seek(2))


if __name__ == '__main__':
unittest.main()

0 comments on commit 50be6bd

Please sign in to comment.