def feed(self, data): # collect data if self.finished: return if self.data is None: self.data = data else: self.data = self.data + data # parse what we have if self.decoder: if self.offset > 0: # skip header skip = min(len(self.data), self.offset) self.data = self.data[skip:] self.offset = self.offset - skip if self.offset > 0 or not self.data: return n, e = self.decoder.decode(self.data) if n < 0: # end of stream self.data = None self.finished = 1 if e < 0: # decoding error self.image = None raise_ioerror(e) else: # end of image return self.data = self.data[n:] elif self.image: # if we end up here with no decoder, this file cannot # be incrementally parsed. wait until we've gotten all # available data pass else: # attempt to open this file try: try: fp = _ParserFile(self.data) im = Image.open(fp) finally: fp.close() # explicitly close the virtual file except IOError: pass # not enough data else: flag = hasattr(im, "load_seek") or hasattr(im, "load_read") if flag or len(im.tile) != 1: # custom load code, or multiple tiles self.decode = None else: # initialize decoder im.load_prepare() d, e, o, a = im.tile[0] im.tile = [] self.decoder = Image._getdecoder(im.mode, d, a, im.decoderconfig) self.decoder.setimage(im.im, e) # calculate decoder offset self.offset = o if self.offset <= len(self.data): self.data = self.data[self.offset :] self.offset = 0 self.image = im
def feed(self, data): # collect data if self.finished: return if self.data is None: self.data = data else: self.data = self.data + data # parse what we have if self.decoder: if self.offset > 0: # skip header skip = min(len(self.data), self.offset) self.data = self.data[skip:] self.offset = self.offset - skip if self.offset > 0 or not self.data: return n, e = self.decoder.decode(self.data) if n < 0: # end of stream self.data = None self.finished = 1 if e < 0: # decoding error self.image = None raise_ioerror(e) else: # end of image return self.data = self.data[n:] elif self.image: # if we end up here with no decoder, this file cannot # be incrementally parsed. wait until we've gotten all # available data pass else: # attempt to open this file try: try: fp = _ParserFile(self.data) im = Image.open(fp) finally: fp.close() # explicitly close the virtual file except IOError: pass # not enough data else: flag = hasattr(im, "load_seek") or hasattr(im, "load_read") if flag or len(im.tile) != 1: # custom load code, or multiple tiles self.decode = None else: # initialize decoder im.load_prepare() d, e, o, a = im.tile[0] im.tile = [] self.decoder = Image._getdecoder(im.mode, d, a, im.decoderconfig) self.decoder.setimage(im.im, e) # calculate decoder offset self.offset = o if self.offset <= len(self.data): self.data = self.data[self.offset:] self.offset = 0 self.image = im
def load(self): "Load image data based on tile list" pixel = Image.Image.load(self) if self.tile is None: raise IOError("cannot load this image") if not self.tile: return pixel self.map = None readonly = 0 if self.filename and len(self.tile) == 1: # try memory mapping d, e, o, a = self.tile[0] if d == "raw" and a[0] == self.mode and a[0] in Image._MAPMODES: try: if hasattr(Image.core, "map"): # use built-in mapper self.map = Image.core.map(self.filename) self.map.seek(o) self.im = self.map.readimage(self.mode, self.size, a[1], a[2]) else: # use mmap, if possible import mmap file = open(self.filename, "r+") size = os.path.getsize(self.filename) # FIXME: on Unix, use PROT_READ etc self.map = mmap.mmap(file.fileno(), size) self.im = Image.core.map_buffer(self.map, self.size, d, e, o, a) readonly = 1 except (AttributeError, EnvironmentError, ImportError): self.map = None self.load_prepare() # look for read/seek overrides try: read = self.load_read except AttributeError: read = self.fp.read try: seek = self.load_seek except AttributeError: seek = self.fp.seek if not self.map: # sort tiles in file order self.tile.sort(_tilesort) try: # FIXME: This is a hack to handle TIFF's JpegTables tag. prefix = self.tile_prefix except AttributeError: prefix = "" for d, e, o, a in self.tile: d = Image._getdecoder(self.mode, d, a, self.decoderconfig) seek(o) try: d.setimage(self.im, e) except ValueError: continue b = prefix t = len(b) while 1: s = read(self.decodermaxblock) if not s: self.tile = [] raise IOError("image file is truncated (%d bytes not processed)" % len(b)) b = b + s n, e = d.decode(b) if n < 0: break b = b[n:] t = t + n self.tile = [] self.readonly = readonly self.fp = None # might be shared if not self.map and e < 0: raise_ioerror(e) # post processing if hasattr(self, "tile_post_rotate"): # FIXME: This is a hack to handle rotated PCD's self.im = self.im.rotate(self.tile_post_rotate) self.size = self.im.size self.load_end() return Image.Image.load(self)
def load(self): "Load image data based on tile list" pixel = Image.Image.load(self) if self.tile is None: raise IOError("cannot load this image") if not self.tile: return pixel self.map = None readonly = 0 if self.filename and len(self.tile) == 1: # try memory mapping d, e, o, a = self.tile[0] if d == "raw" and a[0] == self.mode and a[0] in Image._MAPMODES: try: if hasattr(Image.core, "map"): # use built-in mapper self.map = Image.core.map(self.filename) self.map.seek(o) self.im = self.map.readimage(self.mode, self.size, a[1], a[2]) else: # use mmap, if possible import mmap file = open(self.filename, "r+") size = os.path.getsize(self.filename) # FIXME: on Unix, use PROT_READ etc self.map = mmap.mmap(file.fileno(), size) self.im = Image.core.map_buffer( self.map, self.size, d, e, o, a) readonly = 1 except (AttributeError, EnvironmentError, ImportError): self.map = None self.load_prepare() # look for read/seek overrides try: read = self.load_read except AttributeError: read = self.fp.read try: seek = self.load_seek except AttributeError: seek = self.fp.seek if not self.map: # sort tiles in file order self.tile.sort(_tilesort) try: # FIXME: This is a hack to handle TIFF's JpegTables tag. prefix = self.tile_prefix except AttributeError: prefix = "" for d, e, o, a in self.tile: d = Image._getdecoder(self.mode, d, a, self.decoderconfig) seek(o) try: d.setimage(self.im, e) except ValueError: continue b = prefix t = len(b) while 1: s = read(self.decodermaxblock) if not s: self.tile = [] raise IOError( "image file is truncated (%d bytes not processed)" % len(b)) b = b + s n, e = d.decode(b) if n < 0: break b = b[n:] t = t + n self.tile = [] self.readonly = readonly self.fp = None # might be shared if not self.map and e < 0: raise_ioerror(e) # post processing if hasattr(self, "tile_post_rotate"): # FIXME: This is a hack to handle rotated PCD's self.im = self.im.rotate(self.tile_post_rotate) self.size = self.im.size self.load_end() return Image.Image.load(self)