def err(i): "error function" f = 1 / (1. + quality * 254) t = rgba(i) c2 = (int(int(t[0] * f) / f), int(int(t[1] * f) / f), int(int(t[2] * f) / f), t[3]) return reduce(c2)
def image_decode(f): record = None palette = None while record != modes['end']: record, size = struct.unpack("<2I", f.read(8)) raw_data = f.read(size) f.read(-size % 4) # align if record == modes['header']: w, h = struct.unpack("<2I", raw_data) print "(header) w: %d, h:%d " % (w, h) elif record == modes['end']: pass elif record == modes['line16']: pass elif record == modes['rle']: print '(data)', len(raw_data), 'bytes' # read all blits / lines y = 0 src = 0 tuples = [] line = [] while y < h: header, color = struct.unpack('<HH', raw_data[src:src + 4]) src += 4 nb = (header >> 3) # pixels eol = header & 1 if DEBUG: print 'header(nb=%d,color=%d,eol=%d)' % (nb, color, eol), if (eol): print for i in range(nb): line.append(rgba(color)) if eol: for i in range(w - len(line)): line.append((0, 0, 0, 0)) y += 1 tuples += line line = [] # finish line by skipping bytes src += (-src % 4) else: print "unknown record type:", record img = Image.new("RGBA", (w, h)) img.putdata(tuples) return img
def image_decode(f) : record=None;palette=None while record != modes['end'] : record,size=struct.unpack("<2I",f.read(8)) raw_data = f.read(size) f.read(-size%4) # align if record==modes['header'] : w,h=struct.unpack("<2I",raw_data) print "(header) w: %d, h:%d "%(w,h) elif record==modes['end'] : pass elif record==modes['line16'] : pass elif record == modes['rle'] : print '(data)',len(raw_data),'bytes' # read all blits / lines y=0; src=0 tuples=[] line=[] while y<h : header, color=struct.unpack('<HH',raw_data[src:src+4]) src +=4 nb = (header>>3) # pixels eol = header&1 if DEBUG : print 'header(nb=%d,color=%d,eol=%d)'%(nb,color,eol), if (eol) : print for i in range(nb) : line.append(rgba(color)) if eol : for i in range(w-len(line)) : line.append((0,0,0,0)) y += 1 tuples +=line line=[] # finish line by skipping bytes src += (-src%4) else : print "unknown record type:",record img = Image.new("RGBA",(w,h)) img.putdata(tuples) return img
def err(i) : "error function" f = 1/(1.+quality*254) t=rgba(i) c2=(int(int(t[0]*f)/f),int(int(t[1]*f)/f),int(int(t[2]*f)/f),t[3]) return reduce(c2)