def _open(self): # Screen s = self.fp.read(13) if s[:6] not in ["GIF87a", "GIF89a"]: raise SyntaxError, "not a GIF file" self.info["version"] = s[:6] self.size = i16(s[6:]), i16(s[8:]) self.tile = [] flags = ord(s[10]) bits = (flags & 7) + 1 if flags & 128: # get global palette self.info["background"] = ord(s[11]) self.global_palette = self.palette =\ ImagePaletteH.raw("RGB", self.fp.read(3<<bits)) self.fp2 = self.fp # FIXME: hack self.offset = 0 self.dispose = None self.frame = -1 self.seek(0) # get ready to read first frame
def putpalette(self, data, rawmode = "RGB"): "Put palette data into an image." if self.mode not in ("L", "P"): raise ValueError, "illegal image mode" if type(data) != StringType: data = string.join(map(chr, data), "") self.mode = "P" self.palette = ImagePaletteH.raw(rawmode, data) self.palette.mode = "RGB"
def _open(self): if self.fp.read(8) != _MAGIC: raise SyntaxError, "not a PNG file" # # Parse headers, up until the first IDAT chunk self.png = PngStream(self.fp) while 1: # # get next chunk cid, pos, len = self.png.read() try: s = self.png.call(cid, pos, len) except EOFError: break except AttributeError: if ImageH.DEBUG: print cid, pos, len, "(unknown)" s = self.fp.read(len) self.png.crc(cid, s) # # Copy relevant attributes from the PngStream. An alternative # would be to let the PngStream class modify these attributes # directly, but that introduces circular references which are # difficult to break no matter what happens in the decoders. # (believe me, I've tried ;-) self.mode = self.png.im_mode self.size = self.png.im_size self.info = self.png.im_info self.tile = self.png.im_tile if self.png.im_palette: rawmode, data = self.png.im_palette self.palette = ImagePaletteH.raw(rawmode, data) self.__idat = len # used by load_read()
def seek(self, frame): # FIXME: can only seek to next frame; should add rewind capability if frame != self.frame + 1: raise ValueError, "cannot seek to frame %d" % frame self.frame = frame self.tile = [] self.fp = self.fp2 # FIXME: hack if self.offset: # backup to last frame self.fp.seek(self.offset) while self.data(): pass self.offset = 0 if self.dispose: self.im = self.dispose self.dispose = None self.palette = self.global_palette while 1: s = self.fp.read(1) if not s or s == ";": break elif s == "!": # # extensions # s = self.fp.read(1) block = self.data() if ord(s) == 249: # # graphic control extension # flags = ord(block[0]) if flags & 1: self.info["transparency"] = ord(block[3]) self.info["duration"] = i16(block[1:3]) * 10 try: # disposal methods if flags & 8: # replace with background colour self.dispose = ImageH.core.fill("P", self.size, self.info["background"]) elif flags & 16: # replace with previous contents self.dispose = self.im.copy() except (AttributeError, KeyError): pass elif ord(s) == 255: # # application extension # self.info["extension"] = block, self.fp.tell() if block[:11] == "NETSCAPE2.0": self.info["loop"] = 1 # FIXME while self.data(): pass elif s == ",": # # local image # s = self.fp.read(9) # extent x0, y0 = i16(s[0:]), i16(s[2:]) x1, y1 = x0 + i16(s[4:]), y0 + i16(s[6:]) flags = ord(s[8]) interlace = (flags & 64) != 0 if flags & 128: bits = (flags & 7) + 1 self.palette =\ ImagePaletteH.raw("RGB", self.fp.read(3<<bits)) # image data bits = ord(self.fp.read(1)) self.offset = self.fp.tell() self.tile = [("gif", (x0, y0, x1, y1), self.offset, (bits, interlace))] break else: pass # raise IOError, "illegal GIF tag `%x`" % ord(s) if not self.tile: self.fp2 = None raise EOFError, "no more images in GIF file" self.mode = "L" if self.palette: self.mode = "P"
def seek(self, frame): # FIXME: can only seek to next frame; should add rewind capability if frame != self.frame + 1: raise ValueError, "cannot seek to frame %d" % frame self.frame = frame self.tile = [] self.fp = self.fp2 # FIXME: hack if self.offset: # backup to last frame self.fp.seek(self.offset) while self.data(): pass self.offset = 0 if self.dispose: self.im = self.dispose self.dispose = None self.palette = self.global_palette while 1: s = self.fp.read(1) if not s or s == ";": break elif s == "!": # # extensions # s = self.fp.read(1) block = self.data() if ord(s) == 249: # # graphic control extension # flags = ord(block[0]) if flags & 1: self.info["transparency"] = ord(block[3]) self.info["duration"] = i16(block[1:3]) * 10 try: # disposal methods if flags & 8: # replace with background colour self.dispose = ImageH.core.fill( "P", self.size, self.info["background"]) elif flags & 16: # replace with previous contents self.dispose = self.im.copy() except (AttributeError, KeyError): pass elif ord(s) == 255: # # application extension # self.info["extension"] = block, self.fp.tell() if block[:11] == "NETSCAPE2.0": self.info["loop"] = 1 # FIXME while self.data(): pass elif s == ",": # # local image # s = self.fp.read(9) # extent x0, y0 = i16(s[0:]), i16(s[2:]) x1, y1 = x0 + i16(s[4:]), y0 + i16(s[6:]) flags = ord(s[8]) interlace = (flags & 64) != 0 if flags & 128: bits = (flags & 7) + 1 self.palette =\ ImagePaletteH.raw("RGB", self.fp.read(3<<bits)) # image data bits = ord(self.fp.read(1)) self.offset = self.fp.tell() self.tile = [("gif", (x0, y0, x1, y1), self.offset, (bits, interlace))] break else: pass # raise IOError, "illegal GIF tag `%x`" % ord(s) if not self.tile: self.fp2 = None raise EOFError, "no more images in GIF file" self.mode = "L" if self.palette: self.mode = "P"