Moved seek frame position check into ImageFile

This commit is contained in:
Andrew Murray 2017-09-30 13:32:43 +10:00
parent 281698796b
commit 78c62727d3
11 changed files with 40 additions and 32 deletions

View File

@ -59,6 +59,7 @@ class DcxImageFile(PcxImageFile):
self._offset.append(offset)
self.__fp = self.fp
self.frame = None
self.seek(0)
@property
@ -70,8 +71,8 @@ class DcxImageFile(PcxImageFile):
return len(self._offset) > 1
def seek(self, frame):
if frame >= len(self._offset):
raise EOFError("attempt to seek outside DCX directory")
if not self._seek_check(frame):
return
self.frame = frame
self.fp = self.__fp
self.fp.seek(self._offset[frame])

View File

@ -118,12 +118,10 @@ class FliImageFile(ImageFile.ImageFile):
return self.__framecount > 1
def seek(self, frame):
if frame == self.__frame:
if not self._seek_check(frame):
return
if frame < self.__frame:
self._seek(0)
if frame >= self.__framecount:
raise EOFError("no more images in FLI file")
for f in range(self.__frame + 1, frame + 1):
self._seek(f)

View File

@ -48,7 +48,7 @@ class GifImageFile(ImageFile.ImageFile):
format = "GIF"
format_description = "Compuserve GIF"
_close_exclusive_fp_after_loading = False
global_palette = None
def data(self):
@ -117,7 +117,7 @@ class GifImageFile(ImageFile.ImageFile):
return self._is_animated
def seek(self, frame):
if frame == self.__frame:
if not self._seek_check(frame):
return
if frame < self.__frame:
self._seek(0)

View File

@ -270,11 +270,7 @@ class ImImageFile(ImageFile.ImageFile):
return self.info[FRAMES] > 1
def seek(self, frame):
if frame < 0 or frame >= self.info[FRAMES]:
raise EOFError("seek outside sequence")
if self.frame == frame:
if not self._seek_check(frame):
return
self.frame = frame
@ -292,7 +288,6 @@ class ImImageFile(ImageFile.ImageFile):
self.tile = [("raw", (0, 0)+self.size, offs, (self.rawmode, 0, -1))]
def tell(self):
return self.frame
#

View File

@ -275,6 +275,16 @@ class ImageFile(Image.Image):
# def load_read(self, bytes):
# pass
def _seek_check(self, frame):
if (frame < 0 or
# Only check upper limit on frames if additional seek operations
# are not required to do so
(not (hasattr(self, "_n_frames") and self._n_frames is None) and
frame >= self.n_frames)):
raise EOFError("attempt to seek outside sequence")
return self.tell() != frame
class StubImageFile(ImageFile):
"""

View File

@ -65,7 +65,7 @@ class MicImageFile(TiffImagePlugin.TiffImageFile):
raise SyntaxError("not an MIC file; no image entries")
self.__fp = self.fp
self.frame = 0
self.frame = None
if len(self.images) > 1:
self.category = Image.CONTAINER
@ -81,7 +81,8 @@ class MicImageFile(TiffImagePlugin.TiffImageFile):
return len(self.images) > 1
def seek(self, frame):
if not self._seek_check(frame):
return
try:
filename = self.images[frame]
except IndexError:

View File

@ -72,14 +72,13 @@ class MpoImageFile(JpegImagePlugin.JpegImageFile):
return self.__framecount > 1
def seek(self, frame):
if frame < 0 or frame >= self.__framecount:
raise EOFError("no more images in MPO file")
else:
self.fp = self.__fp
self.offset = self.__mpoffsets[frame]
self.tile = [
("jpeg", (0, 0) + self.size, self.offset, (self.mode, ""))
]
if not self._seek_check(frame):
return
self.fp = self.__fp
self.offset = self.__mpoffsets[frame]
self.tile = [
("jpeg", (0, 0) + self.size, self.offset, (self.mode, ""))
]
self.__frame = frame
def tell(self):

View File

@ -135,11 +135,12 @@ class PsdImageFile(ImageFile.ImageFile):
return len(self.layers) > 1
def seek(self, layer):
# seek to given layer (1..max)
if layer == self.frame:
if not self._seek_check(layer):
return
# seek to given layer (1..max)
try:
if layer <= 0:
if layer == 0:
raise IndexError
name, mode, bbox, tile = self.layers[layer-1]
self.mode = mode

View File

@ -174,8 +174,8 @@ class SpiderImageFile(ImageFile.ImageFile):
def seek(self, frame):
if self.istack == 0:
raise EOFError("attempt to seek in a non-stack file")
if frame >= self._nimages:
raise EOFError("attempt to seek past end of file")
if not self._seek_check(frame):
return
self.stkoffset = self.hdrlen + frame * (self.hdrlen + self.imgbytes)
self.fp = self.__fp
self.fp.seek(self.stkoffset)

View File

@ -555,8 +555,8 @@ class ImageFileDirectory_v2(collections.MutableMapping):
# Spec'd length == 1, Actual > 1, Warn and truncate. Formerly barfed.
# No Spec, Actual length 1, Formerly (<4.2) returned a 1 element tuple.
# Don't mess with the legacy api, since it's frozen.
if ((info.length == 1) or
(info.length is None and len(values) == 1 and not legacy_api)):
if ((info.length == 1) or
(info.length is None and len(values) == 1 and not legacy_api)):
# Don't mess with the legacy api, since it's frozen.
if legacy_api and self.tagtype[tag] in [5, 10]: # rationals
values = values,
@ -980,7 +980,9 @@ class TiffImageFile(ImageFile.ImageFile):
def seek(self, frame):
"Select a given frame as current image"
self._seek(max(frame, 0)) # Questionable backwards compatibility.
if not self._seek_check(frame):
return
self._seek(frame)
# Create a new core image object on second and
# subsequent frames in the image. Image may be
# different size/mode.

View File

@ -349,13 +349,14 @@ class TestFileTiff(PillowTestCase):
def test_seek(self):
filename = "Tests/images/pil136.tiff"
im = Image.open(filename)
im.seek(-1)
im.seek(0)
self.assertEqual(im.tell(), 0)
def test_seek_eof(self):
filename = "Tests/images/pil136.tiff"
im = Image.open(filename)
self.assertEqual(im.tell(), 0)
self.assertRaises(EOFError, im.seek, -1)
self.assertRaises(EOFError, im.seek, 1)
def test__limit_rational_int(self):