def _seek(self, index, maxpages=None):
        """Seek file to offset of page specified by index."""
        pages = self.pages
        lenpages = len(pages)
        if lenpages == 0:
            raise IndexError('index out of range')

        fh = self.parent.filehandle
        if fh.closed:
            raise ValueError('seek of closed file')

        if self._indexed or 0 <= index < lenpages:
            page = pages[index]
            offset = page if isinstance(page, inttypes) else page.offset
            fh.seek(offset)
            return

        tiff = self.parent.tiff
        offsetformat = tiff.ifdoffsetformat
        offsetsize = tiff.ifdoffsetsize
        tagnoformat = tiff.tagnoformat
        tagnosize = tiff.tagnosize
        tagsize = tiff.tagsize
        unpack = struct.unpack

        page = pages[-1]
        offset = page if isinstance(page, inttypes) else page.offset

        if maxpages is None:
            maxpages = 2**22
        while lenpages < maxpages:
            # read offsets to pages from file until index is reached
            fh.seek(offset)
            # skip tags
            try:
                tagno = unpack(tagnoformat, fh.read(tagnosize))[0]
                if tagno > 4096:
                    raise TiffFileError(
                        'suspicious number of tags: %i' % tagno)
            except Exception:
                log.warning('TiffPages: corrupted tag list of page %i @ %i',
                            lenpages, offset)
                del pages[-1]
                lenpages -= 1
                self._indexed = True
                break
            self._nextpageoffset = offset + tagnosize + tagno * tagsize
            fh.seek(self._nextpageoffset)

            # read offset to next page
            offset = unpack(offsetformat, fh.read(offsetsize))[0]
            if offset == 0:
                self._indexed = True
                break
            if offset >= fh.size:
                log.warning('TiffPages: invalid page offset (%i)', offset)
                self._indexed = True
                break

            pages.append(offset)
            lenpages += 1
            if 0 <= index < lenpages:
                break

            # detect some circular references
            if lenpages == 100:
                for p in pages[:-1]:
                    if offset == (p if isinstance(p, inttypes) else p.offset):
                        raise TiffFileError('invalid circular IFD reference')

        if index >= lenpages:
            raise IndexError('index out of range')

        page = pages[index]
        fh.seek(page if isinstance(page, inttypes) else page.offset)
def _seek ( self , index , maxpages = None ) : pages = self . pages lenpages = len ( pages ) if lenpages == 0 : raise IndexError ( 'index out of range' ) fh = self . parent . filehandle if fh . closed : raise ValueError ( 'seek of closed file' ) if self . _indexed or 0 <= index < lenpages : page = pages [ index ] offset = page if isinstance ( page , inttypes ) else page . offset fh . seek ( offset ) return tiff = self . parent . tiff offsetformat = tiff . ifdoffsetformat offsetsize = tiff . ifdoffsetsize tagnoformat = tiff . tagnoformat tagnosize = tiff . tagnosize tagsize = tiff . tagsize unpack = struct . unpack page = pages [ - 1 ] offset = page if isinstance ( page , inttypes ) else page . offset if maxpages is None : maxpages = 2 ** 22 while lenpages < maxpages : fh . seek ( offset ) try : tagno = unpack ( tagnoformat , fh . read ( tagnosize ) ) [ 0 ] if tagno > 4096 : raise TiffFileError ( 'suspicious number of tags: %i' % tagno ) except Exception : log . warning ( 'TiffPages: corrupted tag list of page %i @ %i' , lenpages , offset ) del pages [ - 1 ] lenpages -= 1 self . _indexed = True break self . _nextpageoffset = offset + tagnosize + tagno * tagsize fh . seek ( self . _nextpageoffset ) offset = unpack ( offsetformat , fh . read ( offsetsize ) ) [ 0 ] if offset == 0 : self . _indexed = True break if offset >= fh . size : log . warning ( 'TiffPages: invalid page offset (%i)' , offset ) self . _indexed = True break pages . append ( offset ) lenpages += 1 if 0 <= index < lenpages : break if lenpages == 100 : for p in pages [ : - 1 ] : if offset == ( p if isinstance ( p , inttypes ) else p . offset ) : raise TiffFileError ( 'invalid circular IFD reference' ) if index >= lenpages : raise IndexError ( 'index out of range' ) page = pages [ index ] fh . seek ( page if isinstance ( page , inttypes ) else page . offset )
Seek file to offset of page specified by index.