diff --git a/ctf/__init__.py b/ctf/__init__.py deleted file mode 100755 index e69de29..0000000 diff --git a/ctf/flagger.py b/ctf/flagger.py deleted file mode 100755 index 04a9fa8..0000000 --- a/ctf/flagger.py +++ /dev/null @@ -1,33 +0,0 @@ -#! /usr/bin/python - -import asynchat -import asyncore -import socket - -class Flagger(asynchat.async_chat): - """Use to connect to flagd and submit the current flag holder.""" - - def __init__(self, addr, auth): - asynchat.async_chat.__init__(self) - self.create_socket(socket.AF_INET, socket.SOCK_STREAM) - self.connect((addr, 1)) - self.push(auth + '\n') - self.flag = None - - def handle_read(self): - # We don't care. - msg = self.recv(4096) - - def handle_error(self): - # If we lose the connection to flagd, nobody can score any - # points. Terminate everything. - asyncore.close_all() - asynchat.async_chat.handle_error(self) - - def set_flag(self, team): - if team: - eteam = team.encode('utf-8') - else: - eteam = '' - self.push(eteam + '\n') - self.flag = team diff --git a/ctf/html.py b/ctf/html.py deleted file mode 100755 index 101c4d7..0000000 --- a/ctf/html.py +++ /dev/null @@ -1,35 +0,0 @@ -#! /usr/bin/python - -import os -import string -import sys -from codecs import open - -from paths import * - -template_fn = os.path.join(LIB, 'template.html') -template = string.Template(open(template_fn, encoding='utf-8').read()) - -base = BASE_URL -css = base + 'ctf.css' - -def substitute(title, body, base=base, hdr='', body_class='', onload='', links=''): - return template.substitute(title=title, - hdr=hdr, - body_class=body_class, - base=base, - links=links, - onload=onload, - body=body) - -def serve(title, body, **kwargs): - out = substitute(title, body, **kwargs) - print 'Content-type: text/html' - print 'Content-length: %d' % len(out) - print - sys.stdout.write(out) - sys.stdout.flush() - -def write(filename, title, body, **kwargs): - f = open(filename, 'w', encoding='utf-8') - f.write(substitute(title, body, **kwargs)) diff --git a/ctf/pointscli.py b/ctf/pointscli.py deleted file mode 100755 index 5671ebc..0000000 --- a/ctf/pointscli.py +++ /dev/null @@ -1,40 +0,0 @@ -#! /usr/bin/python - -from urllib import quote -import teams -import time -import os -import paths - -pointsdir = os.path.join(paths.VAR, 'points') - -def award(cat, team, points): - if not team: - team = teams.house - now = time.strftime('%Y-%m-%dT%H:%M:%S') - pid = os.getpid() - qcat = quote(cat, '') - qteam = quote(team, '') - basename = '%s.%d.%s.%s' % (now, pid, qcat, qteam) - # FAT can't handle : - basename = basename.replace(':', '.') - tmpfn = os.path.join(pointsdir, 'tmp', basename) - curfn = os.path.join(pointsdir, 'cur', basename) - f = open(tmpfn, 'w') - f.write('%s\t%s\t%s\t%d\n' % (now, cat, team, points)) - f.close() - os.rename(tmpfn, curfn) - -def main(): - import optparse - - p = optparse.OptionParser('%prog CATEGORY TEAM POINTS') - opts, args = p.parse_args() - if len(args) != 3: - p.error('Wrong number of arguments') - cat, team, points = args - points = int(points) - award(cat, team, points) - -if __name__ == '__main__': - main() diff --git a/ctf/teams.py b/ctf/teams.py deleted file mode 100755 index 3e9407c..0000000 --- a/ctf/teams.py +++ /dev/null @@ -1,72 +0,0 @@ -#! /usr/bin/python - -import fcntl -import time -import os -from urllib import quote, unquote -import paths - -house = 'dirtbags' -passwdfn = os.path.join(paths.VAR, 'passwd') -team_colors = ['F0888A', '88BDF0', '00782B', '999900', 'EF9C00', - 'F4B5B7', 'E2EFFB', '89CA9D', 'FAF519', 'FFE7BB', - 'BA88F0', '8DCFF4', 'BEDFC4', 'FFFAB2', 'D7D7D7', - 'C5B9D7', '006189', '8DCB41', 'FFCC00', '898989'] - -teams = {} -built = 0 -def build_teams(): - global teams, built - if not os.path.exists(passwdfn): - return - if os.path.getmtime(passwdfn) <= built: - return - - teams = {} - try: - f = open(passwdfn) - for line in f: - line = line.strip() - if not line: - continue - team, passwd, color = map(unquote, line.strip().split('\t')) - teams[team] = (passwd, color) - except IOError: - pass - built = time.time() - -def validate(team): - build_teams() - -def chkpasswd(team, passwd): - validate(team) - if teams.get(team, [None, None])[0] == passwd: - return True - else: - return False - -def exists(team): - validate(team) - if team == house: - return True - return team in teams - -def add(team, passwd): - build_teams() - color = team_colors[len(teams)%len(team_colors)] - - assert team not in teams, "Team already exists." - - f = open(passwdfn, 'a') - fcntl.lockf(f, fcntl.LOCK_EX) - f.seek(0, 2) - f.write('%s\t%s\t%s\n' % (quote(team, ''), - quote(passwd, ''), - quote(color, ''))) - -def color(team): - validate(team) - t = teams.get(team) - if not t: - return '888888' - return t[1] diff --git a/lib/tanks/easy/berzerker b/lib/tanks/easy/berzerker deleted file mode 100644 index b8189a9..0000000 --- a/lib/tanks/easy/berzerker +++ /dev/null @@ -1,18 +0,0 @@ -Name: berzerker -Author: Neale - -2 random -0 = -{ 50 100 move } -{ 100 50 move } -ifelse - -4 random -0 = -{ 360 random setturret } -if - -30 random -0 = -{ fire } -if diff --git a/lib/tanks/easy/rabbitwithgun b/lib/tanks/easy/rabbitwithgun deleted file mode 100644 index bd6e02d..0000000 --- a/lib/tanks/easy/rabbitwithgun +++ /dev/null @@ -1,31 +0,0 @@ -Name: Rabbit With Gun -Author: Neale -Sensor: 50 0 0 1 -Sensor: 70 0 50 0 - -100 100 move - -( Always set turret to 180 degrees ) -180 setturret - -( Vary walk 1/8 of the time ) -8 random -0 = -{ - 2 random - 0 = - { 70 100 move } - { 100 70 move } - ifelse -} -if - -( If you see something, shoot it ) -0 sensoractive -{ fire } -if - -( Turn, if trouble lies ahead ) -1 sensoractive -{ 0 100 move } -if diff --git a/lib/tanks/hard/chashtank b/lib/tanks/hard/chashtank deleted file mode 100755 index 05d7600..0000000 --- a/lib/tanks/hard/chashtank +++ /dev/null @@ -1,52 +0,0 @@ - ->addsensor(50, 0, 10, 1); ->addsensor(35, 0, 90, 0); ->addsensor(100, 30, 59, 0); ->addsensor(100, 330, 59, 0); ->addsensor(70, 180, 180); ->addsensor(100, 90, 59, 0); ->addsensor(100, 270, 59, 0); ->addsensor(100, 0, 5, 1); - ->addsensor(55, 50, 89, 0); ->addsensor(55, 310, 89, 0); - -# move back and forth - : move(90,90).turretset(0); -random(2,6) : move(95,75).turretset(0); -#random(1,6) : move(75,95).turretset(0); - -# rear sensor -sense(4) : move(90, 90); - -# far right front sensor -sense(5) : move(100,-100); - -# far left front sensor -sense(6) : move(-100,100); - -# right front sensor -sense(2) : move(80,-80); - -# left front sensor -sense(3) : move(-80,80); - -# immediate front sensor in firing range -sense(0) & firenotready() : move(-50, -50); - : turretset(0); - -# near far right front sensor -sense(8) : move(60,-60); -sense(9) : move(-60, 60); - -fireready() : led(); - -# front far sensor -sense(7) & fireready() : move(100,100); - -# collison sensor -sense(1) : move(-100, -100); - : turretset(0); - -sense(0) & fireready() : fire(); - diff --git a/lib/tanks/hard/crashmaster b/lib/tanks/hard/crashmaster deleted file mode 100644 index 344193e..0000000 --- a/lib/tanks/hard/crashmaster +++ /dev/null @@ -1,55 +0,0 @@ -Name: crashmaster -Author: Neale -Sensor: 50 0 8 1 -Sensor: 30 0 50 0 -Sensor: 50 0 10 0 -Sensor: 100 315 100 1 -Sensor: 100 45 100 1 -Sensor: 60 180 180 0 - -( Mem 0: Turn number ) -0 fetch -1 + -0 store - -( Mem 1: Move turret (procedure) ) -{ - getturret - - - setturret -} 1 store - -0 fetch 30 % 10 / ( [0..2], changes every 10 turns ) -dup 0 = { 80 80 move } if -dup 1 = { 60 80 move } if -dup 2 = { 80 60 move } if -pop - -0 setturret - -fireready -{ - ( Behavior for when we can shoot ) - - 0 sensoractive { fire } if - 1 sensoractive { 10 10 move 0 setturret } if - 2 sensoractive { 10 10 move 0 setturret } if - 3 sensoractive { 0 60 move -50 1 call } if - 4 sensoractive { 60 0 move 50 1 call } if - 3 sensoractive 4 sensoractive & { 100 100 move getturret setturret } if - 5 sensoractive { 100 40 move } if -} -{ - ( Behavior for when we can't shoot ) - - setled - - 0 sensoractive { 10 20 move } if - 1 sensoractive { 10 10 move } if - 2 sensoractive { 10 20 move } if - 3 sensoractive { 70 50 move } if - 4 sensoractive { 50 70 move } if - 3 sensoractive 4 sensoractive & { -100 20 move } if - 5 sensoractive { 100 50 move } if -} -ifelse diff --git a/lib/tanks/hard/foobar b/lib/tanks/hard/foobar deleted file mode 100644 index bcdf807..0000000 --- a/lib/tanks/hard/foobar +++ /dev/null @@ -1,22 +0,0 @@ ->addsensor(55, 0, 5, 1); ->addsensor(40, 0, 30); ->addsensor(80, 30, 59, 0); ->addsensor(80, 330, 59, 0); ->addsensor(70, 180, 180); ->addsensor(80, 90, 59, 0); ->addsensor(80, 270, 59, 0); - -# : move(70,80); -# random(3,6) : move(80,70); - : move(65,85); -random(2,6) : move(90,65); -sense(2) : move(80,10).turretcw(100); -sense(3) : move(10,80).turretccw(100); -sense(4) : move(90, 90); -sense(5) : move(90,10).turretcw(100); -sense(6) : move(10,90).turretccw(100); -sense(0) & fireready() : turretset().move(90,90).fire(); -sense(1) : move(-100, -100); - : turretset(0); -fireready() : led(); - diff --git a/lib/tanks/hard/pflarr b/lib/tanks/hard/pflarr deleted file mode 100644 index b4df468..0000000 --- a/lib/tanks/hard/pflarr +++ /dev/null @@ -1,31 +0,0 @@ ->addsensor(50, 0, 45, 1); # 0-Fire Sensor ->addsensor(30, 0, 180); # 1-Anti-collision sensor ->addsensor(100, 40, 60, 1); # 2 turret clockwise ->addsensor(100, 320, 60, 1); # 3 turret ccw ->addsensor(80, 180, 160); # 4 Coward ->addsensor(100, 0, 0, 1); # 5-Fire Sensor2 ->addsensor(100, 0, 0); # 6-Chase Sensor ->addsensor(75, 75, 30); # 7-quick turn right ->addsensor(75, 285, 30); # 8-quick turn left - -# Commands - : move(70, 75). - turretset(0); -random(1, 10): move(75, 75). - turretset(0); -sense(2) : turretcw(50). - move(85, 70); -sense(2) & sense(0): turretcw(25). - move(85, 70); -sense(3) : turretccw(50). - move(70, 85); -sense(3) & sense(0) : turretccw(25). - move(70, 85); -sense(5) & sense(7) : move(70, 30); -sense(5) & sense(8) : move(30, 70); -#sense(5) : turretset(); -sense(0) & sense(5) : fire(); -sense(6) & sense(5) & fireready(): move(100,100); -sense(4) : move(100,100); -sense(1) : move(-50, 25); -fireready() : led(); diff --git a/lib/tanks/medium/simpleton b/lib/tanks/medium/simpleton deleted file mode 100644 index 64fc607..0000000 --- a/lib/tanks/medium/simpleton +++ /dev/null @@ -1,8 +0,0 @@ ->addsensor(50, 0, 5, 1); # 0-Fire Sensor ->addsensor(30, 0, 50); # 1-Anti-collision sensor - -# Commands - : move(90, 100). - turretset(0); -sense(0) : fire(); -sense(1) : move(-100, 100) diff --git a/lib/tanks/medium/sittingduckwithteeth b/lib/tanks/medium/sittingduckwithteeth deleted file mode 100644 index 4ea551f..0000000 --- a/lib/tanks/medium/sittingduckwithteeth +++ /dev/null @@ -1,9 +0,0 @@ ->addsensor(50, 0, 10, 1); # 0-Fire Sensor ->addsensor(100, 90, 150, 1); ->addsensor(100, 270, 150, 1); - -: turretcw(75); -sense(0): fire(); -sense(1): turretcw(); -sense(2): turretccw(); - diff --git a/lib/tanks/medium/sweeper b/lib/tanks/medium/sweeper deleted file mode 100644 index e02e950..0000000 --- a/lib/tanks/medium/sweeper +++ /dev/null @@ -1,18 +0,0 @@ -# Just sit there and sweep the field until it finds something to shoot. -# Uses a long-range sensor on the left and right to hone in. - ->addsensor(50, 0, 5, 1); # 0 ->addsensor(100, 90, 150, 1); # 1 ->addsensor(100, 270, 150, 1); # 2 ->addsensor(100, 0, 359, 0); # 3 - -# Default movement if nothing is detected - : move(70, 70) . turretccw(); -random(2, 3): move(40, 70) . turretccw(); -random(1, 3): move(70, 40) . turretccw(); - -# We found something!! -sense(3): move(0, 0); -sense(1): turretcw(); -sense(2): turretccw(); -sense(0): fire(); diff --git a/site-packages/badmath.py b/site-packages/badmath.py deleted file mode 100644 index e8df9b0..0000000 --- a/site-packages/badmath.py +++ /dev/null @@ -1,125 +0,0 @@ -import random -import math - -# Not defined in older Python's math libs -def multiply(a, b): - return a * b - -def factorial(n): - return reduce(multiply, range(1, n+1)) - -OPS = [lambda a, b: a + b, - lambda a, b: a - b, - lambda a, b: a * b, - lambda a, b: a // b, - lambda a, b: a % b, - lambda a, b: a ^ b, - lambda a, b: a | b, - lambda a, b: a & b, - lambda a, b: max(a,b), - lambda a, b: min(a,b), - lambda a, b: a+b//2, - lambda a, b: ~b, - lambda a, b: a + b + 3, - lambda a, b: max(a,b)//2, - lambda a, b: min(a,b)*3, - lambda a, b: a % 2, - lambda a, b: int(math.degrees(b + a)), - lambda a, b: ~(a & b), - lambda a, b: ~(a ^ b), - lambda a, b: a + b - a%b, - lambda a, b: (a > 0) and (factorial(a)//factorial(a-b)) or 0, - lambda a, b: (b%a) * (a%b), - lambda a, b: factorial(a)%b, - lambda a, b: int(math.sin(a)*b), - lambda a, b: b + a%2, - lambda a, b: a - 1 + b%3, - lambda a, b: a & 0xaaaa, - lambda a, b: a == b and 5 or 6, - lambda a, b: b % 17, - lambda a, b: int( cos( math.radians(b) ) * a )] - -SYMBOLS = '.,<>?/!@#$%^&*()_+="~|;:' -MAX = 100 - -PLAYER_DIR = '' - -def mkPuzzle(lvl): - """Make a puzzle. The puzzle is a simple integer math equation. The trick - is that the math operators don't do what you might expect, and what they do - is randomized each time (from a set list of functions). The equation is - evaluated left to right, with no other order of operations. - - The level determins both the length of the puzzle, and what functions are - enabled. The number of operators is half the level+2, and the number of - functions enabled is equal to the level. - - returns the key, puzzle, and the set of numbers used. - """ - - ops = OPS[:lvl + 1] - length = (lvl + 2)//2 - - key = {} - - bannedNums = set() - - puzzle = [] - for i in range(length): - num = random.randint(1,MAX) - bannedNums.add(num) - puzzle.append( num ) - symbol = random.choice(SYMBOLS) - if symbol not in key: - key[symbol] = random.randint(0, len(ops) - 1) - puzzle.append( symbol ) - - num = random.randint(1,MAX) - bannedNums.add(num) - puzzle.append( num ) - - return key, puzzle, bannedNums - -def parse(puzzle): - """Parse a puzzle string. If the string contains symbols not in - SYMBOLS, a ValueError is raised.""" - - parts = [puzzle] - for symbol in SYMBOLS: - newParts = [] - for part in parts: - if symbol in part: - terms = part.split(symbol) - newParts.append( terms.pop(0)) - while terms: - newParts.append(symbol) - newParts.append( terms.pop(0) ) - else: - newParts.append(part) - parts = newParts - - finalParts = [] - for part in parts: - part = part.strip() - if part in SYMBOLS: - finalParts.append( part ) - else: - try: - finalParts.append( int(part) ) - except: - raise ValueError("Invalid symbol: %s" % part) - - return finalParts - -def solve(key, puzzle): - - puzzle = list(puzzle) - stack = puzzle.pop(0) - - while puzzle: - symbol = puzzle.pop(0) - nextVal = puzzle.pop(0) - op = OPS[key[symbol]] - stack = op(stack, nextVal) - - return stack diff --git a/site-packages/canvas.py b/site-packages/canvas.py deleted file mode 100644 index 3374304..0000000 --- a/site-packages/canvas.py +++ /dev/null @@ -1,40 +0,0 @@ -#! /usr/bin/python - -import png -from array import array - -class Canvas: - def __init__(self, width, height, bg=(0,0,0)): - self.width = width - self.height = height - - # Build the canvas using arrays, which are way quick - row = array('B') - for i in xrange(self.width): - row.extend(bg) - - self.bytes = array('B') - for i in xrange(self.height): - self.bytes.extend(row) - - def get(self, x, y): - offs = ((y*self.width)+x)*3 - return self.bytes[offs:offs+3] - - def set(self, x, y, pixel): - offs = ((y*self.width)+x)*3 - for i in range(3): - self.bytes[offs+i] = pixel[i] - - def write(self, f): - p = png.Writer(self.width, self.height) - p.write_array(f, self.bytes) - -if __name__ == '__main__': - width = 800 - height = 600 - - c = Canvas(width, height) - for x in range(width): - c.set(x, x % height, (x%256,(x*2)%256,(x*3)%256)) - c.write(open('foo.png', 'wb')) diff --git a/site-packages/irc.py b/site-packages/irc.py deleted file mode 100644 index 432800c..0000000 --- a/site-packages/irc.py +++ /dev/null @@ -1,541 +0,0 @@ -#! /usr/bin/python - -import asynchat -import asyncore -import socket -import sys -import traceback -import time - -channel_prefixes = '+#&' - -class IRCHandler(asynchat.async_chat): - """IRC Server connection. - - This is the one you want to derive your connection classes from. - - """ - - debug = False - heartbeat_interval = 1 # seconds per heartbeat - - def __init__(self, host=None, nick=None, gecos=None): - asynchat.async_chat.__init__(self) - self.line = '' - self.timers = [] - self.last_heartbeat = 0 - self.set_terminator('\r\n') - if host: - self.open_connection(host, nick, gecos) - - def dbg(self, msg): - if self.debug: - print(msg) - - def open_connection(self, host, nick, gecos): - self.nick = nick - self.gecos = gecos - self.host = host - self.create_socket(socket.AF_INET, socket.SOCK_STREAM) - self.connect(host) - - def handle_connect(self): - self.write(['NICK', self.nick]) - self.write(['USER', self.nick, '+iw', self.nick], self.gecos) - - def connect(self, host): - self.waiting = False - asynchat.async_chat.connect(self, host) - - def heartbeat(self): - """Invoke all timers.""" - - if not self.timers: - return - timers, self.timers = self.timers, [] - now = time.time() - for t, cb in timers: - if t > now: - self.timers.append((t, cb)) - else: - cb() - - def add_timer(self, secs, callback): - """After secs seconds, call callback""" - self.timers.append((time.time() + secs, callback)) - - def readable(self): - """Called by asynchat to see if we're readable. - - We hook our heartbeat in here. - """ - - now = time.time() - if now > self.last_heartbeat + self.heartbeat_interval: - self.heartbeat() - self.last_heartbeat = now - - if self.connected: - return asynchat.async_chat.readable(self) - else: - return False - - def collect_incoming_data(self, data): - """Called by asynchat when data arrives""" - self.line += data - - def found_terminator(self): - """Called by asynchat when it finds the terminating character. - """ - line = self.line.decode('utf-8') - self.line = '' - self.parse_line(line) - - def write(self, args, text=None): - """Send out an IRC command - - This function helps to prevent you from shooting yourself in the - foot, by forcing you to send commands that are in a valid format - (although it doesn't check the validity of the actual commands). - - As we all know, IRC commands take the form - - :COMMAND ARG1 ARG2 ARG3 ... :text string - - where 'text string' is optional. Well, that's exactly how this - function works. Args is a list of length at least one, and text - string is a string. - - write(['PRIVMSG', nick], 'Hello 12') - - will send - - PRIVMSG nick :Hello 12 - - """ - - cmdstr = ' '.join(args) - if text: - cmdstr = '%s :%s' % (cmdstr, text) - self.dbg('-> %s' % cmdstr) - try: - line = '%s\n' % cmdstr - self.send(line.encode('utf-8')) - except socket.error: - pass - - - def parse_line(self, line): - """Parse a server-provided line - - This does all the magic of parsing those ill-formatted IRC - messages. It will also decide if a PRIVMSG or NOTICE is using - CTCP (the client-to-client protocol, which by convention is any - of the above messages with ^A on both ends of the text. - - This function goes on to invoke self.eval_triggers on the parsed - data like this: - - self.eval_triggers(operation, arguments, text) - - where operation and text are strings, and arguments is a list. - - It returns the same tuple (op, args, text). - - """ - - if (line[0] == ':'): - with_uname = 1 - line = line [1:] - else: - with_uname = 0 - try: - [args, text] = line.split(' :', 1) - args = args.split() - except ValueError: - args = line.split() - text = '' - if (with_uname != 1): - op = args[0] - elif ((args[1] in ["PRIVMSG", "NOTICE"]) and - (text and (text[0] == '\001') and (text[-1] == '\001'))): - op = "C" + args[1] - text = text[1:-1] - else: - op = args[1] - self.dbg("<- %s %s %s" % (op, args, text)) - self.handle(op, args, text) - return (op, args, text) - - - def handle(self, op, args, text): - """Take action on a server message - - Right now, just invokes - - self.do_[op](args, text) - - where [op] is the operation passed in. - - This is a good method to overload if you want a really advanced - client supporting bindings. - - """ - try: - method = getattr(self, "do_" + lower(op)) - except AttributeError: - self.dbg("Unhandled: %s" % (op, args, text)) - return - method(args, text) - - -class Recipient: - """Abstract recipient object""" - - def __init__(self, interface, name): - self._interface = interface - self._name = name - - def __repr__(self): - return 'Recipient(%s)' % self.name() - - def name(self): - return self._name - - def is_channel(self): - return False - - def write(self, cmd, addl): - """Write a raw IRC command to our interface""" - - self._interface.write(cmd, addl) - - def cmd(self, cmd, text): - """Send a command to ourself""" - - self.write([cmd, self._name], text) - - def msg(self, text): - """Tell the recipient something""" - - self.cmd("PRIVMSG", text) - - def notice(self, text): - """Send a notice to the recipient""" - - self.cmd("NOTICE", text) - - def ctcp(self, command, text): - """Send a CTCP command to the recipient""" - - return self.msg("\001%s %s\001" % (command.upper(), text)) - - def act(self, text): - """Send an action to the recipient""" - - return self.ctcp("ACTION", text) - - def cnotice(self, command, text): - """Send a CTCP notice to the recipient""" - - return self.notice("\001%s %s\001" % (command.upper(), text)) - -class Channel(Recipient): - def __repr__(self): - return 'Channel(%s)' % self.name() - - def is_channel(self): - return True - -class User(Recipient): - def __init__(self, interface, name, user, host, op=False): - Recipient.__init__(self, interface, name) - self.user = user - self.host = host - self.op = op - - def __repr__(self): - return 'User(%s, %s, %s)' % (self.name(), self.user, self.host) - -def recipient(interface, name): - if name[0] in channel_prefixes: - return Channel(interface, name) - else: - return User(interface, name, None, None) - -class SmartIRCHandler(IRCHandler): - """This is like the IRCHandler, except it creates Recipient objects - for IRC messages. The intent is to make it easier to write stuff - without knowledge of the IRC protocol. - - """ - - def recipient(self, name): - return recipient(self, name) - - def err(self, exception): - if self.debug: - traceback.print_exception(*exception) - - def handle(self, op, args, text): - """Parse more, creating objects and stuff - - makes a call to self.handle_op(sender, forum, addl) - - sender is always a Recipient object; if you want to reply - privately, you can send your reply to sender. - - forum is a Recipient object corresponding with the forum over - which the message was carried. For user-to-user PRIVMSG and - NOTICE commands, this is the same as sender. For those same - commands sent to a channel, it is the channel. Thus, you can - always send a reply to forum, and it will be sent back in an - appropriate manner (ie. the way you expect). - - addl is a tuple, containing additional information which might - be relelvant. Here's what it will contain, based on the server - operation: - - op | addl - ---------+---------------- - PRIVMSG | text of the message - NOTICE | text of the notice - CPRIVMSG | CTCP command, text of the command - CNOTICE | CTCP response, text of the response - KICK * | victim of kick, kick text - MODE * | all mode args - JOIN * | empty - PART * | empty - QUIT | quit message - PING | ping text - NICK ! | old nickname - others | all arguments; text is last element - - * The forum in these items is the channel to which the action - pertains. - ! The sender for the NICK command is the *new* nickname. This - is so you can send messages to the sender object and they'll - go to the right place. - """ - - try: - sender = User(self, *unpack_nuhost(args)) - except ValueError: - sender = None - forum = None - addl = () - - if op in ("PRIVMSG", "NOTICE"): - # PRIVMSG ['neale!~user@127.0.0.1', 'PRIVMSG', '#hydra'] firebot, foo - # PRIVMSG ['neale!~user@127.0.0.1', 'PRIVMSG', 'firebot'] firebot, foo - try: - forum = self.recipient(args[2]) - if not forum.is_channel(): - forum = sender - addl = (text,) - except IndexError: - addl = (text, args[1]) - elif op in ("CPRIVMSG", "CNOTICE"): - forum = self.recipient(args[2]) - splits = text.split(" ") - if splits[0] == "DCC": - op = "DC" + op - addl = (splits[1],) + tuple(splits[2:]) - else: - addl = (splits[0],) + tuple(splits[1:]) - elif op in ("KICK",): - forum = self.recipient(args[2]) - addl = (self.recipient(args[3]), text) - elif op in ("MODE",): - forum = self.recipient(args[2]) - addl = args[3:] - elif op in ("JOIN", "PART"): - try: - forum = self.recipient(args[2]) - except IndexError: - forum = self.recipient(text) - elif op in ("QUIT",): - addl = (text,) - elif op in ("PING", "PONG"): - # PING ['PING'] us.boogernet.org. - # PONG ['irc.foonet.com', 'PONG', 'irc.foonet.com'] 1114199424 - addl = (text,) - elif op in ("NICK",): - # NICK ['brad!~brad@10.168.2.33', 'NICK'] bradaway - # - # The sender is the new nickname here, in case you want to - # send something to the sender. - - # Apparently there are two different standards for this - # command. - if text: - sender = self.recipient(text) - else: - sender = self.recipient(args[2]) - addl = (unpack_nuhost(args)[0],) - elif op in ("INVITE",): - # INVITE [u'pflarr!~pflarr@www.clanspum.net', u'INVITE', u'gallium', u'#mysterious'] - # INVITE [u'pflarr!~pflarr@www.clanspum.net', u'INVITE', u'gallium'] #mysterious - if len(args) > 3: - forum = self.recipient(args[3]) - else: - forum = self.recipient(text) - else: - try: - int(op) - except ValueError: - self.dbg("WARNING: unknown server code: %s" % op) - addl = tuple(args[2:]) + (text,) - - try: - self.handle_cooked(op, sender, forum, addl) - except SystemExit: - raise - except: - self.err(sys.exc_info()) - - def handle_cooked(self, op, sender, forum, addl): - try: - func = getattr(self, 'cmd_' + op.upper()) - except AttributeError: - self.unhandled(op, sender, forum, addl) - return - func(sender, forum, addl) - - def cmd_PING(self, sender, forum, addl): - self.write(['PONG'], addl[0]) - - def unhandled(self, op, sender, forum, addl): - """Handle all the stuff that had no handler. - - This is a special handler in that it also gets the server code - as the first argument. - - """ - - self.dbg("unhandled: %s" % ((op, sender, forum, addl),)) - - -class Bot(SmartIRCHandler): - """A simple bot. - - This automatically joins the channels you pass to the constructor, - tries to use one of the nicks provided, and reconnects if it gets - booted. You can use this as a base for more sophisticated bots. - - """ - - def __init__(self, host, nicks, gecos, channels): - self.nicks = nicks - self.channels = channels - self.waiting = True - self._spool = [] - SmartIRCHandler.__init__(self, host, nicks[0], gecos) - - def despool(self, target, lines): - """Slowly despool a bunch of lines to a target - - Since the IRC server will block all output if we send it too - fast, use this to send large multi-line responses. - - """ - - self._spool.append((target, list(lines))) - - def heartbeat(self): - SmartIRCHandler.heartbeat(self) - - # Despool data - if self._spool: - # Take the first one on the queue, and put it on the end - which = self._spool[0] - del self._spool[0] - self._spool.append(which) - - # Despool a line - target, lines = which - if lines: - line = lines[0] - target.msg(line) - del lines[0] - else: - self._spool.remove(which) - - def announce(self, text): - for c in self.channels: - self.write(['PRIVMSG', c], text) - - def err(self, exception): - SmartIRCHandler.err(self, exception) - self.announce('*bzert*') - - def cmd_001(self, sender, forum, addl): - # Welcome to IRC - self.nick = addl[0] - for c in self.channels: - self.write(['JOIN'], c) - - def cmd_433(self, sender, forum, addl): - # Nickname already in use - self.nicks.append(self.nicks.pop(0)) - self.write(['NICK', self.nicks[0]]) - - def cmd_NICK(self, sender, forum, addl): - if addl[0] == self.nick: - self.nick = sender.name() - print(self.nick) - - def writable(self): - if not self.waiting: - return asynchat.async_chat.writable(self) - else: - return False - - def write(self, *args): - SmartIRCHandler.write(self, *args) - - def close(self, final=False): - SmartIRCHandler.close(self) - if not final: - self.dbg("Connection closed, reconnecting...") - self.waiting = True - self.connected = 0 - # Wait a bit and reconnect - self.create_socket(socket.AF_INET, socket.SOCK_STREAM) - self.add_timer(23, lambda : self.connect(self.host)) - - def handle_close(self): - self.close() - - -## -## Miscellaneous IRC functions -## - -def unpack_nuhost(nuhost): - """Unpack nick!user@host - - Frequently, the first argument in a server message is in - nick!user@host format. You can just pass your whole argument list - to this function and get back a tuple containing: - - (nick, user, host) - - """ - - try: - [nick, uhost] = nuhost[0].split('!', 1) - [user, host] = uhost.split('@', 1) - except ValueError: - raise ValueError("not in nick!user@host format") - return (nick, user, host) - -def run_forever(timeout=2.0): - """Run your clients forever. - - Just a handy front-end to asyncore.loop, so you don't have to import - asyncore yourself. - - """ - - asyncore.loop(timeout) diff --git a/site-packages/png.py b/site-packages/png.py deleted file mode 100644 index edf0750..0000000 --- a/site-packages/png.py +++ /dev/null @@ -1,3720 +0,0 @@ -#!/usr/bin/env python - -# $URL$ -# $Rev$ - -# png.py - PNG encoder/decoder in pure Python -# -# Copyright (C) 2006 Johann C. Rocholl -# Portions Copyright (C) 2009 David Jones -# And probably portions Copyright (C) 2006 Nicko van Someren -# -# Original concept by Johann C. Rocholl. -# -# LICENSE (The MIT License) -# -# Permission is hereby granted, free of charge, to any person -# obtaining a copy of this software and associated documentation files -# (the "Software"), to deal in the Software without restriction, -# including without limitation the rights to use, copy, modify, merge, -# publish, distribute, sublicense, and/or sell copies of the Software, -# and to permit persons to whom the Software is furnished to do so, -# subject to the following conditions: -# -# The above copyright notice and this permission notice shall be -# included in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS -# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN -# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN -# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. -# -# Changelog (recent first): -# 2009-03-11 David: interlaced bit depth < 8 (writing). -# 2009-03-10 David: interlaced bit depth < 8 (reading). -# 2009-03-04 David: Flat and Boxed pixel formats. -# 2009-02-26 David: Palette support (writing). -# 2009-02-23 David: Bit-depths < 8; better PNM support. -# 2006-06-17 Nicko: Reworked into a class, faster interlacing. -# 2006-06-17 Johann: Very simple prototype PNG decoder. -# 2006-06-17 Nicko: Test suite with various image generators. -# 2006-06-17 Nicko: Alpha-channel, grey-scale, 16-bit/plane support. -# 2006-06-15 Johann: Scanline iterator interface for large input files. -# 2006-06-09 Johann: Very simple prototype PNG encoder. - -# Incorporated into Bangai-O Development Tools by drj on 2009-02-11 from -# http://trac.browsershots.org/browser/trunk/pypng/lib/png.py?rev=2885 - -# Incorporated into pypng by drj on 2009-03-12 from -# //depot/prj/bangaio/master/code/png.py#67 - - -""" -Pure Python PNG Reader/Writer - -This Python module implements support for PNG images (see PNG -specification at http://www.w3.org/TR/2003/REC-PNG-20031110/ ). It reads -and writes PNG files with all allowable bit depths (1/2/4/8/16/24/32/48/64 -bits per pixel) and colour combinations: greyscale (1/2/4/8/16 bit); RGB, -RGBA, LA (greyscale with alpha) with 8/16 bits per channel; colour mapped -images (1/2/4/8 bit). Adam7 interlacing is supported for reading and -writing. A number of optional chunks can be specified (when writing) -and understood (when reading): ``tRNS``, ``bKGD``, ``gAMA``. - -For help, type ``import png; help(png)`` in your python interpreter. - -A good place to start is the :class:`Reader` and :class:`Writer` classes. - -Requires Python 2.3. Limited support is available for Python 2.2, but -not everything works. Best with Python 2.4 and higher. Installation is -trivial, but see the ``README.txt`` file (with the source distribution) -for details. - -This file can also be used as a command-line utility to convert -`Netpbm `_ PNM files to PNG, and the reverse conversion from PNG to -PNM. The interface is similar to that of the ``pnmtopng`` program from -Netpbm. Type ``python png.py --help`` at the shell prompt -for usage and a list of options. - -A note on spelling and terminology ----------------------------------- - -Generally British English spelling is used in the documentation. So -that's "greyscale" and "colour". This not only matches the author's -native language, it's also used by the PNG specification. - -The major colour models supported by PNG (and hence by PyPNG) are: -greyscale, RGB, greyscale--alpha, RGB--alpha. These are sometimes -referred to using the abbreviations: L, RGB, LA, RGBA. In this case -each letter abbreviates a single channel: *L* is for Luminance or Luma or -Lightness which is the channel used in greyscale images; *R*, *G*, *B* stand -for Red, Green, Blue, the components of a colour image; *A* stands for -Alpha, the opacity channel (used for transparency effects, but higher -values are more opaque, so it makes sense to call it opacity). - -A note on formats ------------------ - -When getting pixel data out of this module (reading) and presenting -data to this module (writing) there are a number of ways the data could -be represented as a Python value. Generally this module uses one of -three formats called "flat row flat pixel", "boxed row flat pixel", and -"boxed row boxed pixel". Basically the concern is whether each pixel -and each row comes in its own little tuple (box), or not. - -Consider an image that is 3 pixels wide by 2 pixels high, and each pixel -has RGB components: - -Boxed row flat pixel:: - - list([R,G,B, R,G,B, R,G,B], - [R,G,B, R,G,B, R,G,B]) - -Each row appears as its own list, but the pixels are flattened so that -three values for one pixel simply follow the three values for the previous -pixel. This is the most common format used, because it provides a good -compromise between space and convenience. PyPNG regards itself as -at liberty to replace any sequence type with any sufficiently compatible -other sequence type; in practice each row is an array (from the array -module), and the outer list is sometimes an iterator rather than an -explicit list (so that streaming is possible). - -Flat row flat pixel:: - - [R,G,B, R,G,B, R,G,B, - R,G,B, R,G,B, R,G,B] - -The entire image is one single giant sequence of colour values. -Generally an array will be used (to save space), not a list. - -Boxed row boxed pixel:: - - list([ (R,G,B), (R,G,B), (R,G,B) ], - [ (R,G,B), (R,G,B), (R,G,B) ]) - -Each row appears in its own list, but each pixel also appears in its own -tuple. A serious memory burn in Python. - -In all cases the top row comes first, and for each row the pixels are -ordered from left-to-right. Within a pixel the values appear in the -order, R-G-B-A (or L-A for greyscale--alpha). - -There is a fourth format, mentioned because it is used internally, -is close to what lies inside a PNG file itself, and has some support -from the public API. This format is called packed. When packed, -each row is a sequence of bytes (integers from 0 to 255), just as -it is before PNG scanline filtering is applied. When the bit depth -is 8 this is essentially the same as boxed row flat pixel; when the -bit depth is less than 8, several pixels are packed into each byte; -when the bit depth is 16 (the only value more than 8 that is supported -by the PNG image format) each pixel value is decomposed into 2 bytes -(and `packed` is a misnomer). This format is used by the -:meth:`Writer.write_packed` method. It isn't usually a convenient -format, but may be just right if the source data for the PNG image -comes from something that uses a similar format (for example, 1-bit -BMPs, or another PNG file). - -And now, my famous members --------------------------- -""" - -# http://www.python.org/doc/2.2.3/whatsnew/node5.html -from __future__ import generators - -__version__ = "$URL$ $Rev$" - -from array import array -try: # See :pyver:old - import itertools -except: - pass -import math -# http://www.python.org/doc/2.4.4/lib/module-operator.html -import operator -import struct -import sys -import zlib -# http://www.python.org/doc/2.4.4/lib/module-warnings.html -import warnings - - -__all__ = ['Image', 'Reader', 'Writer', 'write_chunks', 'from_array'] - - -# The PNG signature. -# http://www.w3.org/TR/PNG/#5PNG-file-signature -_signature = struct.pack('8B', 137, 80, 78, 71, 13, 10, 26, 10) - -_adam7 = ((0, 0, 8, 8), - (4, 0, 8, 8), - (0, 4, 4, 8), - (2, 0, 4, 4), - (0, 2, 2, 4), - (1, 0, 2, 2), - (0, 1, 1, 2)) - -def group(s, n): - # See - # http://www.python.org/doc/2.6/library/functions.html#zip - return zip(*[iter(s)]*n) - -def isarray(x): - """Same as ``isinstance(x, array)`` except on Python 2.2, where it - always returns ``False``. This helps PyPNG work on Python 2.2. - """ - - try: - return isinstance(x, array) - except: - return False - -try: # see :pyver:old - array.tostring -except: - def tostring(row): - l = len(row) - return struct.pack('%dB' % l, *row) -else: - def tostring(row): - """Convert row of bytes to string. Expects `row` to be an - ``array``. - """ - return row.tostring() - - -def interleave_planes(ipixels, apixels, ipsize, apsize): - """ - Interleave (colour) planes, e.g. RGB + A = RGBA. - - Return an array of pixels consisting of the `ipsize` elements of data - from each pixel in `ipixels` followed by the `apsize` elements of data - from each pixel in `apixels`. Conventionally `ipixels` and - `apixels` are byte arrays so the sizes are bytes, but it actually - works with any arrays of the same type. The returned array is the - same type as the input arrays which should be the same type as each other. - """ - - itotal = len(ipixels) - atotal = len(apixels) - newtotal = itotal + atotal - newpsize = ipsize + apsize - # Set up the output buffer - # See http://www.python.org/doc/2.4.4/lib/module-array.html#l2h-1356 - out = array(ipixels.typecode) - # It's annoying that there is no cheap way to set the array size :-( - out.extend(ipixels) - out.extend(apixels) - # Interleave in the pixel data - for i in range(ipsize): - out[i:newtotal:newpsize] = ipixels[i:itotal:ipsize] - for i in range(apsize): - out[i+ipsize:newtotal:newpsize] = apixels[i:atotal:apsize] - return out - -def check_palette(palette): - """Check a palette argument (to the :class:`Writer` class) for validity. - Returns the palette as a list if okay; raises an exception otherwise. - """ - - # None is the default and is allowed. - if palette is None: - return None - - p = list(palette) - if not (0 < len(p) <= 256): - raise ValueError("a palette must have between 1 and 256 entries") - seen_triple = False - for i,t in enumerate(p): - if len(t) not in (3,4): - raise ValueError( - "palette entry %d: entries must be 3- or 4-tuples." % i) - if len(t) == 3: - seen_triple = True - if seen_triple and len(t) == 4: - raise ValueError( - "palette entry %d: all 4-tuples must precede all 3-tuples" % i) - for x in t: - if int(x) != x or not(0 <= x <= 255): - raise ValueError( - "palette entry %d: values must be integer: 0 <= x <= 255" % i) - return p - -class Error(Exception): - prefix = 'Error' - def __str__(self): - return self.prefix + ': ' + ' '.join(self.args) - -class FormatError(Error): - """Problem with input file format. In other words, PNG file does - not conform to the specification in some way and is invalid. - """ - - prefix = 'FormatError' - -class ChunkError(FormatError): - prefix = 'ChunkError' - - -class Writer: - """ - PNG encoder in pure Python. - """ - - def __init__(self, width=None, height=None, - size=None, - greyscale=False, - alpha=False, - bitdepth=8, - palette=None, - transparent=None, - background=None, - gamma=None, - compression=None, - interlace=False, - bytes_per_sample=None, # deprecated - planes=None, - colormap=None, - maxval=None, - chunk_limit=2**20): - """ - Create a PNG encoder object. - - Arguments: - - width, height - Image size in pixels, as two separate arguments. - size - Image size (w,h) in pixels, as single argument. - greyscale - Input data is greyscale, not RGB. - alpha - Input data has alpha channel (RGBA or LA). - bitdepth - Bit depth: from 1 to 16. - palette - Create a palette for a colour mapped image (colour type 3). - transparent - Specify a transparent colour (create a ``tRNS`` chunk). - background - Specify a default background colour (create a ``bKGD`` chunk). - gamma - Specify a gamma value (create a ``gAMA`` chunk). - compression - zlib compression level (1-9). - interlace - Create an interlaced image. - chunk_limit - Write multiple ``IDAT`` chunks to save memory. - - The image size (in pixels) can be specified either by using the - `width` and `height` arguments, or with the single `size` - argument. If `size` is used it should be a pair (*width*, - *height*). - - `greyscale` and `alpha` are booleans that specify whether - an image is greyscale (or colour), and whether it has an - alpha channel (or not). - - `bitdepth` specifies the bit depth of the source pixel values. - Each source pixel value must be an integer between 0 and - ``2**bitdepth-1``. For example, 8-bit images have values - between 0 and 255. PNG only stores images with bit depths of - 1,2,4,8, or 16. When `bitdepth` is not one of these values, - the next highest valid bit depth is selected, and an ``sBIT`` - (significant bits) chunk is generated that specifies the original - precision of the source image. In this case the supplied pixel - values will be rescaled to fit the range of the selected bit depth. - - The details of which bit depth / colour model combinations the - PNG file format supports directly, are somewhat arcane - (refer to the PNG specification for full details). Briefly: - "small" bit depths (1,2,4) are only allowed with greyscale and - colour mapped images; colour mapped images cannot have bit depth - 16. - - For colour mapped images (in other words, when the `palette` - argument is specified) the `bitdepth` argument must match one of - the valid PNG bit depths: 1, 2, 4, or 8. (It is valid to have a - PNG image with a palette and an ``sBIT`` chunk, but the meaning - is slightly different; it would be awkward to press the - `bitdepth` argument into service for this.) - - The `palette` option, when specified, causes a colour mapped image - to be created: the PNG colour type is set to 3; greyscale - must not be set; alpha must not be set; transparent must - not be set; the bit depth must be 1,2,4, or 8. When a colour - mapped image is created, the pixel values are palette indexes - and the `bitdepth` argument specifies the size of these indexes - (not the size of the colour values in the palette). - - The palette argument value should be a sequence of 3- or - 4-tuples. 3-tuples specify RGB palette entries; 4-tuples - specify RGBA palette entries. If both 4-tuples and 3-tuples - appear in the sequence then all the 4-tuples must come - before all the 3-tuples. A ``PLTE`` chunk is created; if there - are 4-tuples then a ``tRNS`` chunk is created as well. The - ``PLTE`` chunk will contain all the RGB triples in the same - sequence; the ``tRNS`` chunk will contain the alpha channel for - all the 4-tuples, in the same sequence. Palette entries - are always 8-bit. - - If specified, the `transparent` and `background` parameters must - be a tuple with three integer values for red, green, blue, or - a simple integer (or singleton tuple) for a greyscale image. - - If specified, the `gamma` parameter must be a positive number - (generally, a float). A ``gAMA`` chunk will be created. Note that - this will not change the values of the pixels as they appear in - the PNG file, they are assumed to have already been converted - appropriately for the gamma specified. - - The `compression` argument specifies the compression level - to be used by the ``zlib`` module. Higher values are likely - to compress better, but will be slower to compress. The - default for this argument is ``None``; this does not mean - no compression, rather it means that the default from the - ``zlib`` module is used (which is generally acceptable). - - If `interlace` is true then an interlaced image is created - (using PNG's so far only interace method, *Adam7*). This does not - affect how the pixels should be presented to the encoder, rather - it changes how they are arranged into the PNG file. On slow - connexions interlaced images can be partially decoded by the - browser to give a rough view of the image that is successively - refined as more image data appears. - - .. note :: - - Enabling the `interlace` option requires the entire image - to be processed in working memory. - - `chunk_limit` is used to limit the amount of memory used whilst - compressing the image. In order to avoid using large amounts of - memory, multiple ``IDAT`` chunks may be created. - """ - - # At the moment the `planes` argument is ignored; - # its purpose is to act as a dummy so that - # ``Writer(x, y, **info)`` works, where `info` is a dictionary - # returned by Reader.read and friends. - # Ditto for `colormap`. - - # A couple of helper functions come first. Best skipped if you - # are reading through. - - def isinteger(x): - try: - return int(x) == x - except: - return False - - def check_color(c, which): - """Checks that a colour argument for transparent or - background options is the right form. Also "corrects" bare - integers to 1-tuples. - """ - - if c is None: - return c - if greyscale: - try: - l = len(c) - except TypeError: - c = (c,) - if len(c) != 1: - raise ValueError("%s for greyscale must be 1-tuple" % - which) - if not isinteger(c[0]): - raise ValueError( - "%s colour for greyscale must be integer" % - which) - else: - if not (len(c) == 3 and - isinteger(c[0]) and - isinteger(c[1]) and - isinteger(c[2])): - raise ValueError( - "%s colour must be a triple of integers" % - which) - return c - - if size: - if len(size) != 2: - raise ValueError( - "size argument should be a pair (width, height)") - if width is not None and width != size[0]: - raise ValueError( - "size[0] (%r) and width (%r) should match when both are used." - % (size[0], width)) - if height is not None and height != size[1]: - raise ValueError( - "size[1] (%r) and height (%r) should match when both are used." - % (size[1], height)) - width,height = size - del size - - if width <= 0 or height <= 0: - raise ValueError("width and height must be greater than zero") - if not isinteger(width) or not isinteger(height): - raise ValueError("width and height must be integers") - # http://www.w3.org/TR/PNG/#7Integers-and-byte-order - if width > 2**32-1 or height > 2**32-1: - raise ValueError("width and height cannot exceed 2**32-1") - - if alpha and transparent is not None: - raise ValueError( - "transparent colour not allowed with alpha channel") - - if bytes_per_sample is not None: - warnings.warn('please use bitdepth instead of bytes_per_sample', - DeprecationWarning) - if bytes_per_sample not in (0.125, 0.25, 0.5, 1, 2): - raise ValueError( - "bytes per sample must be .125, .25, .5, 1, or 2") - bitdepth = int(8*bytes_per_sample) - del bytes_per_sample - if not isinteger(bitdepth) or bitdepth < 1 or 16 < bitdepth: - raise ValueError("bitdepth (%r) must be a postive integer <= 16" % - bitdepth) - - self.rescale = None - if palette: - if bitdepth not in (1,2,4,8): - raise ValueError("with palette, bitdepth must be 1, 2, 4, or 8") - if transparent is not None: - raise ValueError("transparent and palette not compatible") - if alpha: - raise ValueError("alpha and palette not compatible") - if greyscale: - raise ValueError("greyscale and palette not compatible") - else: - # No palette, check for sBIT chunk generation. - if alpha or not greyscale: - if bitdepth not in (8,16): - targetbitdepth = (8,16)[bitdepth > 8] - self.rescale = (bitdepth, targetbitdepth) - bitdepth = targetbitdepth - del targetbitdepth - else: - assert greyscale - assert not alpha - if bitdepth not in (1,2,4,8,16): - if bitdepth > 8: - targetbitdepth = 16 - elif bitdepth == 3: - targetbitdepth = 4 - else: - assert bitdepth in (5,6,7) - targetbitdepth = 8 - self.rescale = (bitdepth, targetbitdepth) - bitdepth = targetbitdepth - del targetbitdepth - - if bitdepth < 8 and (alpha or not greyscale and not palette): - raise ValueError( - "bitdepth < 8 only permitted with greyscale or palette") - if bitdepth > 8 and palette: - raise ValueError( - "bit depth must be 8 or less for images with palette") - - transparent = check_color(transparent, 'transparent') - background = check_color(background, 'background') - - # It's important that the true boolean values (greyscale, alpha, - # colormap, interlace) are converted to bool because Iverson's - # convention is relied upon later on. - self.width = width - self.height = height - self.transparent = transparent - self.background = background - self.gamma = gamma - self.greyscale = bool(greyscale) - self.alpha = bool(alpha) - self.colormap = bool(palette) - self.bitdepth = int(bitdepth) - self.compression = compression - self.chunk_limit = chunk_limit - self.interlace = bool(interlace) - self.palette = check_palette(palette) - - self.color_type = 4*self.alpha + 2*(not greyscale) + 1*self.colormap - assert self.color_type in (0,2,3,4,6) - - self.color_planes = (3,1)[self.greyscale or self.colormap] - self.planes = self.color_planes + self.alpha - # :todo: fix for bitdepth < 8 - self.psize = (self.bitdepth/8) * self.planes - - def make_palette(self): - """Create the byte sequences for a ``PLTE`` and if necessary a - ``tRNS`` chunk. Returned as a pair (*p*, *t*). *t* will be - ``None`` if no ``tRNS`` chunk is necessary. - """ - - p = array('B') - t = array('B') - - for x in self.palette: - p.extend(x[0:3]) - if len(x) > 3: - t.append(x[3]) - p = tostring(p) - t = tostring(t) - if t: - return p,t - return p,None - - def write(self, outfile, rows): - """Write a PNG image to the output file. `rows` should be - an iterable that yields each row in boxed row flat pixel format. - The rows should be the rows of the original image, so there - should be ``self.height`` rows of ``self.width * self.planes`` values. - If `interlace` is specified (when creating the instance), then - an interlaced PNG file will be written. Supply the rows in the - normal image order; the interlacing is carried out internally. - - .. note :: - - Interlacing will require the entire image to be in working memory. - """ - - if self.interlace: - fmt = 'BH'[self.bitdepth > 8] - a = array(fmt, itertools.chain(*rows)) - return self.write_array(outfile, a) - else: - nrows = self.write_passes(outfile, rows) - if nrows != self.height: - raise ValueError( - "rows supplied (%d) does not match height (%d)" % - (nrows, self.height)) - - def write_passes(self, outfile, rows, packed=False): - """ - Write a PNG image to the output file. - - Most users are expected to find the :meth:`write` or - :meth:`write_array` method more convenient. - - The rows should be given to this method in the order that - they appear in the output file. For straightlaced images, - this is the usual top to bottom ordering, but for interlaced - images the rows should have already been interlaced before - passing them to this function. - - `rows` should be an iterable that yields each row. When - `packed` is ``False`` the rows should be in boxed row flat pixel - format; when `packed` is ``True`` each row should be a packed - sequence of bytes. - - """ - - # http://www.w3.org/TR/PNG/#5PNG-file-signature - outfile.write(_signature) - - # http://www.w3.org/TR/PNG/#11IHDR - write_chunk(outfile, 'IHDR', - struct.pack("!2I5B", self.width, self.height, - self.bitdepth, self.color_type, - 0, 0, self.interlace)) - - # See :chunk:order - # http://www.w3.org/TR/PNG/#11gAMA - if self.gamma is not None: - write_chunk(outfile, 'gAMA', - struct.pack("!L", int(round(self.gamma*1e5)))) - - # See :chunk:order - # http://www.w3.org/TR/PNG/#11sBIT - if self.rescale: - write_chunk(outfile, 'sBIT', - struct.pack('%dB' % self.planes, - *[self.rescale[0]]*self.planes)) - - # :chunk:order: Without a palette (PLTE chunk), ordering is - # relatively relaxed. With one, gAMA chunk must precede PLTE - # chunk which must precede tRNS and bKGD. - # See http://www.w3.org/TR/PNG/#5ChunkOrdering - if self.palette: - p,t = self.make_palette() - write_chunk(outfile, 'PLTE', p) - if t: - # tRNS chunk is optional. Only needed if palette entries - # have alpha. - write_chunk(outfile, 'tRNS', t) - - # http://www.w3.org/TR/PNG/#11tRNS - if self.transparent is not None: - if self.greyscale: - write_chunk(outfile, 'tRNS', - struct.pack("!1H", *self.transparent)) - else: - write_chunk(outfile, 'tRNS', - struct.pack("!3H", *self.transparent)) - - # http://www.w3.org/TR/PNG/#11bKGD - if self.background is not None: - if self.greyscale: - write_chunk(outfile, 'bKGD', - struct.pack("!1H", *self.background)) - else: - write_chunk(outfile, 'bKGD', - struct.pack("!3H", *self.background)) - - # http://www.w3.org/TR/PNG/#11IDAT - if self.compression is not None: - compressor = zlib.compressobj(self.compression) - else: - compressor = zlib.compressobj() - - # Choose an extend function based on the bitdepth. The extend - # function packs/decomposes the pixel values into bytes and - # stuffs them onto the data array. - data = array('B') - if self.bitdepth == 8 or packed: - extend = data.extend - elif self.bitdepth == 16: - # Decompose into bytes - def extend(sl): - fmt = '!%dH' % len(sl) - data.extend(array('B', struct.pack(fmt, *sl))) - else: - # Pack into bytes - assert self.bitdepth < 8 - # samples per byte - spb = int(8/self.bitdepth) - def extend(sl): - a = array('B', sl) - # Adding padding bytes so we can group into a whole - # number of spb-tuples. - l = float(len(a)) - extra = math.ceil(l / float(spb))*spb - l - a.extend([0]*int(extra)) - # Pack into bytes - l = group(a, spb) - l = map(lambda e: reduce(lambda x,y: - (x << self.bitdepth) + y, e), l) - data.extend(l) - if self.rescale: - oldextend = extend - factor = \ - float(2**self.rescale[1]-1) / float(2**self.rescale[0]-1) - def extend(sl): - oldextend(map(lambda x: int(round(factor*x)), sl)) - - # Build the first row, testing mostly to see if we need to - # changed the extend function to cope with NumPy integer types - # (they cause our ordinary definition of extend to fail, so we - # wrap it). See - # http://code.google.com/p/pypng/issues/detail?id=44 - enumrows = enumerate(rows) - del rows - - # First row's filter type. - data.append(0) - # :todo: Certain exceptions in the call to ``.next()`` or the - # following try would indicate no row data supplied. - # Should catch. - i,row = enumrows.next() - try: - # If this fails... - extend(row) - except: - # ... try a version that converts the values to int first. - # Not only does this work for the (slightly broken) NumPy - # types, there are probably lots of other, unknown, "nearly" - # int types it works for. - def wrapmapint(f): - return lambda sl: f(map(int, sl)) - extend = wrapmapint(extend) - del wrapmapint - extend(row) - - for i,row in enumrows: - # Add "None" filter type. Currently, it's essential that - # this filter type be used for every scanline as we do not - # mark the first row of a reduced pass image; that means we - # could accidentally compute the wrong filtered scanline if - # we used "up", "average", or "paeth" on such a line. - data.append(0) - extend(row) - if len(data) > self.chunk_limit: - compressed = compressor.compress(tostring(data)) - if len(compressed): - # print >> sys.stderr, len(data), len(compressed) - write_chunk(outfile, 'IDAT', compressed) - # Because of our very witty definition of ``extend``, - # above, we must re-use the same ``data`` object. Hence - # we use ``del`` to empty this one, rather than create a - # fresh one (which would be my natural FP instinct). - del data[:] - if len(data): - compressed = compressor.compress(tostring(data)) - else: - compressed = '' - flushed = compressor.flush() - if len(compressed) or len(flushed): - # print >> sys.stderr, len(data), len(compressed), len(flushed) - write_chunk(outfile, 'IDAT', compressed + flushed) - # http://www.w3.org/TR/PNG/#11IEND - write_chunk(outfile, 'IEND') - return i+1 - - def write_array(self, outfile, pixels): - """ - Write an array in flat row flat pixel format as a PNG file on - the output file. See also :meth:`write` method. - """ - - if self.interlace: - self.write_passes(outfile, self.array_scanlines_interlace(pixels)) - else: - self.write_passes(outfile, self.array_scanlines(pixels)) - - def write_packed(self, outfile, rows): - """ - Write PNG file to `outfile`. The pixel data comes from `rows` - which should be in boxed row packed format. Each row should be - a sequence of packed bytes. - - Technically, this method does work for interlaced images but it - is best avoided. For interlaced images, the rows should be - presented in the order that they appear in the file. - - This method should not be used when the source image bit depth - is not one naturally supported by PNG; the bit depth should be - 1, 2, 4, 8, or 16. - """ - - if self.rescale: - raise Error("write_packed method not suitable for bit depth %d" % - self.rescale[0]) - return self.write_passes(outfile, rows, packed=True) - - def convert_pnm(self, infile, outfile): - """ - Convert a PNM file containing raw pixel data into a PNG file - with the parameters set in the writer object. Works for - (binary) PGM, PPM, and PAM formats. - """ - - if self.interlace: - pixels = array('B') - pixels.fromfile(infile, - (self.bitdepth/8) * self.color_planes * - self.width * self.height) - self.write_passes(outfile, self.array_scanlines_interlace(pixels)) - else: - self.write_passes(outfile, self.file_scanlines(infile)) - - def convert_ppm_and_pgm(self, ppmfile, pgmfile, outfile): - """ - Convert a PPM and PGM file containing raw pixel data into a - PNG outfile with the parameters set in the writer object. - """ - pixels = array('B') - pixels.fromfile(ppmfile, - (self.bitdepth/8) * self.color_planes * - self.width * self.height) - apixels = array('B') - apixels.fromfile(pgmfile, - (self.bitdepth/8) * - self.width * self.height) - pixels = interleave_planes(pixels, apixels, - (self.bitdepth/8) * self.color_planes, - (self.bitdepth/8)) - if self.interlace: - self.write_passes(outfile, self.array_scanlines_interlace(pixels)) - else: - self.write_passes(outfile, self.array_scanlines(pixels)) - - def file_scanlines(self, infile): - """ - Generates boxed rows in flat pixel format, from the input file - `infile`. It assumes that the input file is in a "Netpbm-like" - binary format, and is positioned at the beginning of the first - pixel. The number of pixels to read is taken from the image - dimensions (`width`, `height`, `planes`) and the number of bytes - per value is implied by the image `bitdepth`. - """ - - # Values per row - vpr = self.width * self.planes - row_bytes = vpr - if self.bitdepth > 8: - assert self.bitdepth == 16 - row_bytes *= 2 - fmt = '>%dH' % vpr - def line(): - return array('H', struct.unpack(fmt, infile.read(row_bytes))) - else: - def line(): - scanline = array('B', infile.read(row_bytes)) - return scanline - for y in range(self.height): - yield line() - - def array_scanlines(self, pixels): - """ - Generates boxed rows (flat pixels) from flat rows (flat pixels) - in an array. - """ - - # Values per row - vpr = self.width * self.planes - stop = 0 - for y in range(self.height): - start = stop - stop = start + vpr - yield pixels[start:stop] - - def array_scanlines_interlace(self, pixels): - """ - Generator for interlaced scanlines from an array. `pixels` is - the full source image in flat row flat pixel format. The - generator yields each scanline of the reduced passes in turn, in - boxed row flat pixel format. - """ - - # http://www.w3.org/TR/PNG/#8InterlaceMethods - # Array type. - fmt = 'BH'[self.bitdepth > 8] - # Value per row - vpr = self.width * self.planes - for xstart, ystart, xstep, ystep in _adam7: - if xstart >= self.width: - continue - # Pixels per row (of reduced image) - ppr = int(math.ceil((self.width-xstart)/float(xstep))) - # number of values in reduced image row. - row_len = ppr*self.planes - for y in range(ystart, self.height, ystep): - if xstep == 1: - offset = y * vpr - yield pixels[offset:offset+vpr] - else: - row = array(fmt) - # There's no easier way to set the length of an array - row.extend(pixels[0:row_len]) - offset = y * vpr + xstart * self.planes - end_offset = (y+1) * vpr - skip = self.planes * xstep - for i in range(self.planes): - row[i::self.planes] = \ - pixels[offset+i:end_offset:skip] - yield row - -def write_chunk(outfile, tag, data=''): - """ - Write a PNG chunk to the output file, including length and - checksum. - """ - - # http://www.w3.org/TR/PNG/#5Chunk-layout - outfile.write(struct.pack("!I", len(data))) - outfile.write(tag) - outfile.write(data) - checksum = zlib.crc32(tag) - checksum = zlib.crc32(data, checksum) - outfile.write(struct.pack("!i", checksum)) - -def write_chunks(out, chunks): - """Create a PNG file by writing out the chunks.""" - - out.write(_signature) - for chunk in chunks: - write_chunk(out, *chunk) - -def filter_scanline(type, line, fo, prev=None): - """Apply a scanline filter to a scanline. `type` specifies the - filter type (0 to 4); `line` specifies the current (unfiltered) - scanline as a sequence of bytes; `prev` specifies the previous - (unfiltered) scanline as a sequence of bytes. `fo` specifies the - filter offset; normally this is size of a pixel in bytes (the number - of bytes per sample times the number of channels), but when this is - < 1 (for bit depths < 8) then the filter offset is 1. - """ - - assert 0 <= type < 5 - - # The output array. Which, pathetically, we extend one-byte at a - # time (fortunately this is linear). - out = array('B', [type]) - - def sub(): - ai = -fo - for x in line: - if ai >= 0: - x = (x - line[ai]) & 0xff - out.append(x) - ai += 1 - def up(): - for i,x in enumerate(line): - x = (x - prev[i]) & 0xff - out.append(x) - def average(): - ai = -fo - for i,x in enumerate(line): - if ai >= 0: - x = (x - ((line[ai] + prev[i]) >> 1)) & 0xff - else: - x = (x - (prev[i] >> 1)) & 0xff - out.append(x) - ai += 1 - def paeth(): - # http://www.w3.org/TR/PNG/#9Filter-type-4-Paeth - ai = -fo # also used for ci - for i,x in enumerate(line): - a = 0 - b = prev[i] - c = 0 - - if ai >= 0: - a = line[ai] - c = prev[ai] - p = a + b - c - pa = abs(p - a) - pb = abs(p - b) - pc = abs(p - c) - if pa <= pb and pa <= pc: Pr = a - elif pb <= pc: Pr = b - else: Pr = c - - x = (x - Pr) & 0xff - out.append(x) - ai += 1 - - if not prev: - # We're on the first line. Some of the filters can be reduced - # to simpler cases which makes handling the line "off the top" - # of the image simpler. "up" becomes "none"; "paeth" becomes - # "left" (non-trivial, but true). "average" needs to be handled - # specially. - if type == 2: # "up" - return line # type = 0 - elif type == 3: - prev = [0]*len(line) - elif type == 4: # "paeth" - type = 1 - if type == 0: - out.extend(line) - elif type == 1: - sub() - elif type == 2: - up() - elif type == 3: - average() - else: # type == 4 - paeth() - return out - - -def from_array(a, mode=None, info={}): - """Create a PNG :class:`Image` object from a 2- or 3-dimensional array. - One application of this function is easy PIL-style saving: - ``png.from_array(pixels, 'L').save('foo.png')``. - - .. note : - - The use of the term *3-dimensional* is for marketing purposes - only. It doesn't actually work. Please bear with us. Meanwhile - enjoy the complimentary snacks (on request) and please use a - 2-dimensional array. - - Unless they are specified using the *info* parameter, the PNG's - height and width are taken from the array size. For a 3 dimensional - array the first axis is the height; the second axis is the width; - and the third axis is the channel number. Thus an RGB image that is - 16 pixels high and 8 wide will use an array that is 16x8x3. For 2 - dimensional arrays the first axis is the height, but the second axis - is ``width*channels``, so an RGB image that is 16 pixels high and 8 - wide will use a 2-dimensional array that is 16x24 (each row will be - 8*3==24 sample values). - - *mode* is a string that specifies the image colour format in a - PIL-style mode. It can be: - - ``'L'`` - greyscale (1 channel) - ``'LA'`` - greyscale with alpha (2 channel) - ``'RGB'`` - colour image (3 channel) - ``'RGBA'`` - colour image with alpha (4 channel) - - The mode string can also specify the bit depth (overriding how this - function normally derives the bit depth, see below). Appending - ``';16'`` to the mode will cause the PNG to be 16 bits per channel; - any decimal from 1 to 16 can be used to specify the bit depth. - - When a 2-dimensional array is used *mode* determines how many - channels the image has, and so allows the width to be derived from - the second array dimension. - - The array is expected to be a ``numpy`` array, but it can be any - suitable Python sequence. For example, a list of lists can be used: - ``png.from_array([[0, 255, 0], [255, 0, 255]], 'L')``. The exact - rules are: ``len(a)`` gives the first dimension, height; - ``len(a[0])`` gives the second dimension; ``len(a[0][0])`` gives the - third dimension, unless an exception is raised in which case a - 2-dimensional array is assumed. It's slightly more complicated than - that because an iterator of rows can be used, and it all still - works. Using an iterator allows data to be streamed efficiently. - - The bit depth of the PNG is normally taken from the array element's - datatype (but if *mode* specifies a bitdepth then that is used - instead). The array element's datatype is determined in a way which - is supposed to work both for ``numpy`` arrays and for Python - ``array.array`` objects. A 1 byte datatype will give a bit depth of - 8, a 2 byte datatype will give a bit depth of 16. If the datatype - does not have an implicit size, for example it is a plain Python - list of lists, as above, then a default of 8 is used. - - The *info* parameter is a dictionary that can be used to specify - metadata (in the same style as the arguments to the - :class:``png.Writer`` class). For this function the keys that are - useful are: - - height - overrides the height derived from the array dimensions and allows - *a* to be an iterable. - width - overrides the width derived from the array dimensions. - bitdepth - overrides the bit depth derived from the element datatype (but - must match *mode* if that also specifies a bit depth). - - Generally anything specified in the - *info* dictionary will override any implicit choices that this - function would otherwise make, but must match any explicit ones. - For example, if the *info* dictionary has a ``greyscale`` key then - this must be true when mode is ``'L'`` or ``'LA'`` and false when - mode is ``'RGB'`` or ``'RGBA'``. - """ - - # We abuse the *info* parameter by modifying it. Take a copy here. - # (Also typechecks *info* to some extent). - info = dict(info) - - # Syntax check mode string. - bitdepth = None - try: - mode = mode.split(';') - if len(mode) not in (1,2): - raise Error() - if mode[0] not in ('L', 'LA', 'RGB', 'RGBA'): - raise Error() - if len(mode) == 2: - try: - bitdepth = int(mode[1]) - except: - raise Error() - except Error: - raise Error("mode string should be 'RGB' or 'L;16' or similar.") - mode = mode[0] - - # Get bitdepth from *mode* if possible. - if bitdepth: - if info.get('bitdepth') and bitdepth != info['bitdepth']: - raise Error("mode bitdepth (%d) should match info bitdepth (%d)." % - (bitdepth, info['bitdepth'])) - info['bitdepth'] = bitdepth - - # Fill in and/or check entries in *info*. - # Dimensions. - if 'size' in info: - # Check width, height, size all match where used. - for dimension,axis in [('width', 0), ('height', 1)]: - if dimension in info: - if info[dimension] != info['size'][axis]: - raise Error( - "info[%r] shhould match info['size'][%r]." % - (dimension, axis)) - info['width'],info['height'] = info['size'] - if 'height' not in info: - try: - l = len(a) - except: - raise Error( - "len(a) does not work, supply info['height'] instead.") - info['height'] = l - # Colour format. - if 'greyscale' in info: - if bool(info['greyscale']) != ('L' in mode): - raise Error("info['greyscale'] should match mode.") - info['greyscale'] = 'L' in mode - if 'alpha' in info: - if bool(info['alpha']) != ('A' in mode): - raise Error("info['alpha'] should match mode.") - info['alpha'] = 'A' in mode - - planes = len(mode) - if 'planes' in info: - if info['planes'] != planes: - raise Error("info['planes'] should match mode.") - - # In order to work out whether we the array is 2D or 3D we need its - # first row, which requires that we take a copy of its iterator. - # We may also need the first row to derive width and bitdepth. - a,t = itertools.tee(a) - row = t.next() - del t - try: - row[0][0] - threed = True - testelement = row[0] - except: - threed = False - testelement = row - if 'width' not in info: - if threed: - width = len(row) - else: - width = len(row) // planes - info['width'] = width - - # Not implemented yet - assert not threed - - if 'bitdepth' not in info: - try: - dtype = testelement.dtype - # goto the "else:" clause. Sorry. - except: - try: - # Try a Python array.array. - bitdepth = 8 * testelement.itemsize - except: - # We can't determine it from the array element's - # datatype, use a default of 8. - bitdepth = 8 - else: - # If we got here without exception, we now assume that - # the array is a numpy array. - if dtype.kind == 'b': - bitdepth = 1 - else: - bitdepth = 8 * dtype.itemsize - info['bitdepth'] = bitdepth - - for thing in 'width height bitdepth greyscale alpha'.split(): - assert thing in info - return Image(a, info) - -# So that refugee's from PIL feel more at home. Not documented. -fromarray = from_array - -class Image: - """A PNG image. - You can create an :class:`Image` object from an array of pixels by calling - :meth:`png.from_array`. It can be saved to disk with the - :meth:`save` method.""" - def __init__(self, rows, info): - """ - .. note :: - - The constructor is not public. Please do not call it. - """ - - self.rows = rows - self.info = info - - def save(self, file): - """Save the image to *file*. If *file* looks like an open file - descriptor then it is used, otherwise it is treated as a - filename and a fresh file is opened. - - In general, you can only call this method once; after it has - been called the first time and the PNG image has been saved, the - source data will have been streamed, and cannot be streamed - again. - """ - - w = Writer(**self.info) - - try: - file.write - def close(): pass - except: - file = open(file, 'wb') - def close(): file.close() - - try: - w.write(file, self.rows) - finally: - close() - -class _readable: - """ - A simple file-like interface for strings and arrays. - """ - - def __init__(self, buf): - self.buf = buf - self.offset = 0 - - def read(self, n): - r = self.buf[self.offset:self.offset+n] - if isarray(r): - r = r.tostring() - self.offset += n - return r - - -class Reader: - """ - PNG decoder in pure Python. - """ - - def __init__(self, _guess=None, **kw): - """ - Create a PNG decoder object. - - The constructor expects exactly one keyword argument. If you - supply a positional argument instead, it will guess the input - type. You can choose among the following keyword arguments: - - filename - Name of input file (a PNG file). - file - A file-like object (object with a read() method). - bytes - ``array`` or ``string`` with PNG data. - - """ - if ((_guess is not None and len(kw) != 0) or - (_guess is None and len(kw) != 1)): - raise TypeError("Reader() takes exactly 1 argument") - - # Will be the first 8 bytes, later on. See validate_signature. - self.signature = None - self.transparent = None - # A pair of (len,type) if a chunk has been read but its data and - # checksum have not (in other words the file position is just - # past the 4 bytes that specify the chunk type). See preamble - # method for how this is used. - self.atchunk = None - - if _guess is not None: - if isarray(_guess): - kw["bytes"] = _guess - elif isinstance(_guess, str): - kw["filename"] = _guess - elif isinstance(_guess, file): - kw["file"] = _guess - - if "filename" in kw: - self.file = file(kw["filename"], "rb") - elif "file" in kw: - self.file = kw["file"] - elif "bytes" in kw: - self.file = _readable(kw["bytes"]) - else: - raise TypeError("expecting filename, file or bytes array") - - def chunk(self, seek=None): - """ - Read the next PNG chunk from the input file; returns a - (*type*,*data*) tuple. *type* is the chunk's type as a string - (all PNG chunk types are 4 characters long). *data* is the - chunk's data content, as a string. - - If the optional `seek` argument is - specified then it will keep reading chunks until it either runs - out of file or finds the type specified by the argument. Note - that in general the order of chunks in PNGs is unspecified, so - using `seek` can cause you to miss chunks. - """ - - self.validate_signature() - - while True: - # http://www.w3.org/TR/PNG/#5Chunk-layout - if not self.atchunk: - self.atchunk = self.chunklentype() - length,type = self.atchunk - self.atchunk = None - data = self.file.read(length) - if len(data) != length: - raise ChunkError('Chunk %s too short for required %i octets.' - % (type, length)) - checksum = self.file.read(4) - if len(checksum) != 4: - raise ValueError('Chunk %s too short for checksum.', tag) - if seek and type != seek: - continue - verify = zlib.crc32(type) - verify = zlib.crc32(data, verify) - # Whether the output from zlib.crc32 is signed or not varies - # according to hideous implementation details, see - # http://bugs.python.org/issue1202 . - # We coerce it to be positive here (in a way which works on - # Python 2.3 and older). - verify &= 2**32 - 1 - verify = struct.pack('!I', verify) - if checksum != verify: - # print repr(checksum) - (a, ) = struct.unpack('!I', checksum) - (b, ) = struct.unpack('!I', verify) - raise ChunkError( - "Checksum error in %s chunk: 0x%08X != 0x%08X." % - (type, a, b)) - return type, data - - def chunks(self): - """Return an iterator that will yield each chunk as a - (*chunktype*, *content*) pair. - """ - - while True: - t,v = self.chunk() - yield t,v - if t == 'IEND': - break - - def undo_filter(self, filter_type, scanline, previous): - """Undo the filter for a scanline. `scanline` is a sequence of - bytes that does not include the initial filter type byte. - `previous` is decoded previous scanline (for straightlaced - images this is the previous pixel row, but for interlaced - images, it is the previous scanline in the reduced image, which - in general is not the previous pixel row in the final image). - When there is no previous scanline (the first row of a - straightlaced image, or the first row in one of the passes in an - interlaced image), then this argument should be ``None``. - - The scanline will have the effects of filtering removed, and the - result will be returned as a fresh sequence of bytes. - """ - - # :todo: Would it be better to update scanline in place? - - # Create the result byte array. It seems that the best way to - # create the array to be the right size is to copy from an - # existing sequence. *sigh* - # If we fill the result with scanline, then this allows a - # micro-optimisation in the "null" and "sub" cases. - result = array('B', scanline) - - if filter_type == 0: - # And here, we _rely_ on filling the result with scanline, - # above. - return result - - if filter_type not in (1,2,3,4): - raise FormatError('Invalid PNG Filter Type.' - ' See http://www.w3.org/TR/2003/REC-PNG-20031110/#9Filters .') - - # Filter unit. The stride from one pixel to the corresponding - # byte from the previous previous. Normally this is the pixel - # size in bytes, but when this is smaller than 1, the previous - # byte is used instead. - fu = max(1, self.psize) - - # For the first line of a pass, synthesize a dummy previous - # line. An alternative approach would be to observe that on the - # first line 'up' is the same as 'null', 'paeth' is the same - # as 'sub', with only 'average' requiring any special case. - if not previous: - previous = array('B', [0]*len(scanline)) - - def sub(): - """Undo sub filter.""" - - ai = 0 - # Loops starts at index fu. Observe that the initial part - # of the result is already filled in correctly with - # scanline. - for i in range(fu, len(result)): - x = scanline[i] - a = result[ai] - result[i] = (x + a) & 0xff - ai += 1 - - def up(): - """Undo up filter.""" - - for i in range(len(result)): - x = scanline[i] - b = previous[i] - result[i] = (x + b) & 0xff - - def average(): - """Undo average filter.""" - - ai = -fu - for i in range(len(result)): - x = scanline[i] - if ai < 0: - a = 0 - else: - a = result[ai] - b = previous[i] - result[i] = (x + ((a + b) >> 1)) & 0xff - ai += 1 - - def paeth(): - """Undo Paeth filter.""" - - # Also used for ci. - ai = -fu - for i in range(len(result)): - x = scanline[i] - if ai < 0: - a = c = 0 - else: - a = result[ai] - c = previous[ai] - b = previous[i] - p = a + b - c - pa = abs(p - a) - pb = abs(p - b) - pc = abs(p - c) - if pa <= pb and pa <= pc: - pr = a - elif pb <= pc: - pr = b - else: - pr = c - result[i] = (x + pr) & 0xff - ai += 1 - - # Call appropriate filter algorithm. Note that 0 has already - # been dealt with. - (None, sub, up, average, paeth)[filter_type]() - return result - - def deinterlace(self, raw): - """ - Read raw pixel data, undo filters, deinterlace, and flatten. - Return in flat row flat pixel format. - """ - - # print >> sys.stderr, ("Reading interlaced, w=%s, r=%s, planes=%s," + - # " bpp=%s") % (self.width, self.height, self.planes, self.bps) - # Values per row (of the target image) - vpr = self.width * self.planes - - # Make a result array, and make it big enough. Interleaving - # writes to the output array randomly (well, not quite), so the - # entire output array must be in memory. - fmt = 'BH'[self.bitdepth > 8] - a = array(fmt, [0]*vpr*self.height) - source_offset = 0 - - for xstart, ystart, xstep, ystep in _adam7: - # print >> sys.stderr, "Adam7: start=%s,%s step=%s,%s" % ( - # xstart, ystart, xstep, ystep) - if xstart >= self.width: - continue - # The previous (reconstructed) scanline. None at the - # beginning of a pass to indicate that there is no previous - # line. - recon = None - # Pixels per row (reduced pass image) - ppr = int(math.ceil((self.width-xstart)/float(xstep))) - # Row size in bytes for this pass. - row_size = int(math.ceil(self.psize * ppr)) - for y in range(ystart, self.height, ystep): - filter_type = raw[source_offset] - source_offset += 1 - scanline = raw[source_offset:source_offset+row_size] - source_offset += row_size - recon = self.undo_filter(filter_type, scanline, recon) - # Convert so that there is one element per pixel value - flat = self.serialtoflat(recon, ppr) - if xstep == 1: - assert xstart == 0 - offset = y * vpr - a[offset:offset+vpr] = flat - else: - offset = y * vpr + xstart * self.planes - end_offset = (y+1) * vpr - skip = self.planes * xstep - for i in range(self.planes): - a[offset+i:end_offset:skip] = \ - flat[i::self.planes] - return a - - def iterboxed(self, rows): - """Iterator that yields each scanline in boxed row flat pixel - format. `rows` should be an iterator that yields the bytes of - each row in turn. - """ - - def asvalues(raw): - """Convert a row of raw bytes into a flat row. Result may - or may not share with argument""" - - if self.bitdepth == 8: - return raw - if self.bitdepth == 16: - raw = tostring(raw) - return array('H', struct.unpack('!%dH' % (len(raw)//2), raw)) - assert self.bitdepth < 8 - width = self.width - # Samples per byte - spb = 8//self.bitdepth - out = array('B') - mask = 2**self.bitdepth - 1 - shifts = map(self.bitdepth.__mul__, reversed(range(spb))) - for o in raw: - out.extend(map(lambda i: mask&(o>>i), shifts)) - return out[:width] - - return itertools.imap(asvalues, rows) - - def serialtoflat(self, bytes, width=None): - """Convert serial format (byte stream) pixel data to flat row - flat pixel. - """ - - if self.bitdepth == 8: - return bytes - if self.bitdepth == 16: - bytes = tostring(bytes) - return array('H', - struct.unpack('!%dH' % (len(bytes)//2), bytes)) - assert self.bitdepth < 8 - if width is None: - width = self.width - # Samples per byte - spb = 8//self.bitdepth - out = array('B') - mask = 2**self.bitdepth - 1 - shifts = map(self.bitdepth.__mul__, reversed(range(spb))) - l = width - for o in bytes: - out.extend(map(lambda i: mask&(o>>i), shifts)[:l]) - l -= spb - if l <= 0: - l = width - return out - - def iterstraight(self, raw): - """Iterator that undoes the effect of filtering, and yields each - row in serialised format (as a sequence of bytes). Assumes input - is straightlaced. `raw` should be an iterable that yields the - raw bytes in chunks of arbitrary size.""" - - # length of row, in bytes - rb = self.row_bytes - a = array('B') - # The previous (reconstructed) scanline. None indicates first - # line of image. - recon = None - for some in raw: - a.extend(some) - while len(a) >= rb + 1: - filter_type = a[0] - scanline = a[1:rb+1] - del a[:rb+1] - recon = self.undo_filter(filter_type, scanline, recon) - yield recon - if len(a) != 0: - # :file:format We get here with a file format error: when the - # available bytes (after decompressing) do not pack into exact - # rows. - raise FormatError( - 'Wrong size for decompressed IDAT chunk.') - assert len(a) == 0 - - def validate_signature(self): - """If signature (header) has not been read then read and - validate it; otherwise do nothing. - """ - - if self.signature: - return - self.signature = self.file.read(8) - if self.signature != _signature: - raise FormatError("PNG file has invalid signature.") - - def preamble(self): - """ - Extract the image metadata by reading the initial part of the PNG - file up to the start of the ``IDAT`` chunk. All the chunks that - precede the ``IDAT`` chunk are read and either processed for - metadata or discarded. - """ - - self.validate_signature() - - while True: - if not self.atchunk: - self.atchunk = self.chunklentype() - if self.atchunk is None: - raise FormatError( - 'This PNG file has no IDAT chunks.') - if self.atchunk[1] == 'IDAT': - return - self.process_chunk() - - def chunklentype(self): - """Reads just enough of the input to determine the next - chunk's length and type, returned as a (*length*, *type*) pair - where *type* is a string. If there are no more chunks, ``None`` - is returned. - """ - - x = self.file.read(8) - if not x: - return None - if len(x) != 8: - raise FormatError( - 'End of file whilst reading chunk length and type.') - length,type = struct.unpack('!I4s', x) - if length > 2**31-1: - raise FormatError('Chunk %s is too large: %d.' % (type,length)) - return length,type - - def process_chunk(self): - """Process the next chunk and its data. This only processes the - following chunk types, all others are ignored: ``IHDR``, - ``PLTE``, ``bKGD``, ``tRNS``, ``gAMA``, ``sBIT``. - """ - - type, data = self.chunk() - if type == 'IHDR': - # http://www.w3.org/TR/PNG/#11IHDR - if len(data) != 13: - raise FormatError('IHDR chunk has incorrect length.') - (self.width, self.height, self.bitdepth, self.color_type, - self.compression, self.filter, - self.interlace) = struct.unpack("!2I5B", data) - - # Check that the header specifies only valid combinations. - if self.bitdepth not in (1,2,4,8,16): - raise Error("invalid bit depth %d" % self.bitdepth) - if self.color_type not in (0,2,3,4,6): - raise Error("invalid colour type %d" % self.color_type) - # Check indexed (palettized) images have 8 or fewer bits - # per pixel; check only indexed or greyscale images have - # fewer than 8 bits per pixel. - if ((self.color_type & 1 and self.bitdepth > 8) or - (self.bitdepth < 8 and self.color_type not in (0,3))): - raise FormatError("Illegal combination of bit depth (%d)" - " and colour type (%d)." - " See http://www.w3.org/TR/2003/REC-PNG-20031110/#table111 ." - % (self.bitdepth, self.color_type)) - if self.compression != 0: - raise Error("unknown compression method %d" % self.compression) - if self.filter != 0: - raise FormatError("Unknown filter method %d," - " see http://www.w3.org/TR/2003/REC-PNG-20031110/#9Filters ." - % self.filter) - if self.interlace not in (0,1): - raise FormatError("Unknown interlace method %d," - " see http://www.w3.org/TR/2003/REC-PNG-20031110/#8InterlaceMethods ." - % self.interlace) - - # Derived values - # http://www.w3.org/TR/PNG/#6Colour-values - colormap = bool(self.color_type & 1) - greyscale = not (self.color_type & 2) - alpha = bool(self.color_type & 4) - color_planes = (3,1)[greyscale or colormap] - planes = color_planes + alpha - - self.colormap = colormap - self.greyscale = greyscale - self.alpha = alpha - self.color_planes = color_planes - self.planes = planes - self.psize = float(self.bitdepth)/float(8) * planes - if int(self.psize) == self.psize: - self.psize = int(self.psize) - self.row_bytes = int(math.ceil(self.width * self.psize)) - # Stores PLTE chunk if present, and is used to check - # chunk ordering constraints. - self.plte = None - # Stores tRNS chunk if present, and is used to check chunk - # ordering constraints. - self.trns = None - # Stores sbit chunk if present. - self.sbit = None - elif type == 'PLTE': - # http://www.w3.org/TR/PNG/#11PLTE - if self.plte: - warnings.warn("Multiple PLTE chunks present.") - self.plte = data - if len(data) % 3 != 0: - raise FormatError( - "PLTE chunk's length should be a multiple of 3.") - if len(data) > (2**self.bitdepth)*3: - raise FormatError("PLTE chunk is too long.") - if len(data) == 0: - raise FormatError("Empty PLTE is not allowed.") - elif type == 'bKGD': - try: - if self.colormap: - if not self.plte: - warnings.warn( - "PLTE chunk is required before bKGD chunk.") - self.background = struct.unpack('B', data) - else: - self.background = struct.unpack("!%dH" % self.color_planes, - data) - except struct.error: - raise FormatError("bKGD chunk has incorrect length.") - elif type == 'tRNS': - # http://www.w3.org/TR/PNG/#11tRNS - self.trns = data - if self.colormap: - if not self.plte: - warnings.warn("PLTE chunk is required before tRNS chunk.") - else: - if len(data) > len(self.plte)/3: - # Was warning, but promoted to Error as it - # would otherwise cause pain later on. - raise FormatError("tRNS chunk is too long.") - else: - if self.alpha: - raise FormatError( - "tRNS chunk is not valid with colour type %d." % - self.color_type) - try: - self.transparent = \ - struct.unpack("!%dH" % self.color_planes, data) - except struct.error: - raise FormatError("tRNS chunk has incorrect length.") - elif type == 'gAMA': - try: - self.gamma = struct.unpack("!L", data)[0] / 100000.0 - except struct.error: - raise FormatError("gAMA chunk has incorrect length.") - elif type == 'sBIT': - self.sbit = data - if (self.colormap and len(data) != 3 or - not self.colormap and len(data) != self.planes): - raise FormatError("sBIT chunk has incorrect length.") - - def read(self): - """ - Read the PNG file and decode it. Returns (`width`, `height`, - `pixels`, `metadata`). - - May use excessive memory. - - `pixels` are returned in boxed row flat pixel format. - """ - - def iteridat(): - """Iterator that yields all the ``IDAT`` chunks as strings.""" - while True: - try: - type, data = self.chunk() - except ValueError, e: - raise ChunkError(e.args[0]) - if type == 'IEND': - # http://www.w3.org/TR/PNG/#11IEND - break - if type != 'IDAT': - continue - # type == 'IDAT' - # http://www.w3.org/TR/PNG/#11IDAT - if self.colormap and not self.plte: - warnings.warn("PLTE chunk is required before IDAT chunk") - yield data - - def iterdecomp(idat): - """Iterator that yields decompressed strings. `idat` should - be an iterator that yields the ``IDAT`` chunk data. - """ - - # Currently, with no max_length paramter to decompress, this - # routine will do one yield per IDAT chunk. So not very - # incremental. - d = zlib.decompressobj() - # The decompression loop: - # Decompress an IDAT chunk, then decompress any remaining - # unused data until the unused data does not get any - # smaller. Add the unused data to the front of the input - # and loop to process the next IDAT chunk. - cdata = '' - for data in idat: - # :todo: add a max_length argument here to limit output - # size. - yield array('B', d.decompress(cdata + data)) - yield array('B', d.flush()) - - self.preamble() - raw = iterdecomp(iteridat()) - - if self.interlace: - raw = array('B', itertools.chain(*raw)) - arraycode = 'BH'[self.bitdepth>8] - # Like :meth:`group` but producing an array.array object for - # each row. - pixels = itertools.imap(lambda *row: array(arraycode, row), - *[iter(self.deinterlace(raw))]*self.width*self.planes) - else: - pixels = self.iterboxed(self.iterstraight(raw)) - meta = dict() - for attr in 'greyscale alpha planes bitdepth interlace'.split(): - meta[attr] = getattr(self, attr) - meta['size'] = (self.width, self.height) - for attr in 'gamma transparent background'.split(): - a = getattr(self, attr, None) - if a is not None: - meta[attr] = a - return self.width, self.height, pixels, meta - - - def read_flat(self): - """ - Read a PNG file and decode it into flat row flat pixel format. - Returns (*width*, *height*, *pixels*, *metadata*). - - May use excessive memory. - - `pixels` are returned in flat row flat pixel format. - - See also the :meth:`read` method which returns pixels in the - more stream-friendly boxed row flat pixel format. - """ - - x, y, pixel, meta = self.read() - arraycode = 'BH'[meta['bitdepth']>8] - pixel = array(arraycode, itertools.chain(*pixel)) - return x, y, pixel, meta - - def palette(self, alpha='natural'): - """Returns a palette that is a sequence of 3-tuples or 4-tuples, - synthesizing it from the ``PLTE`` and ``tRNS`` chunks. These - chunks should have already been processed (for example, by - calling the :meth:`preamble` method). All the tuples are the - same size: 3-tuples if there is no ``tRNS`` chunk, 4-tuples when - there is a ``tRNS`` chunk. Assumes that the image is colour type - 3 and therefore a ``PLTE`` chunk is required. - - If the `alpha` argument is ``'force'`` then an alpha channel is - always added, forcing the result to be a sequence of 4-tuples. - """ - - if not self.plte: - raise FormatError( - "Required PLTE chunk is missing in colour type 3 image.") - plte = group(array('B', self.plte), 3) - if self.trns or alpha == 'force': - trns = array('B', self.trns or '') - trns.extend([255]*(len(plte)-len(trns))) - plte = map(operator.add, plte, group(trns, 1)) - return plte - - def asDirect(self): - """Returns the image data as a direct representation of an - ``x * y * planes`` array. This method is intended to remove the - need for callers to deal with palettes and transparency - themselves. Images with a palette (colour type 3) - are converted to RGB or RGBA; images with transparency (a - ``tRNS`` chunk) are converted to LA or RGBA as appropriate. - When returned in this format the pixel values represent the - colour value directly without needing to refer to palettes or - transparency information. - - Like the :meth:`read` method this method returns a 4-tuple: - - (*width*, *height*, *pixels*, *meta*) - - This method normally returns pixel values with the bit depth - they have in the source image, but when the source PNG has an - ``sBIT`` chunk it is inspected and can reduce the bit depth of - the result pixels; pixel values will be reduced according to - the bit depth specified in the ``sBIT`` chunk (PNG nerds should - note a single result bit depth is used for all channels; the - maximum of the ones specified in the ``sBIT`` chunk. An RGB565 - image will be rescaled to 6-bit RGB666). - - The *meta* dictionary that is returned reflects the `direct` - format and not the original source image. For example, an RGB - source image with a ``tRNS`` chunk to represent a transparent - colour, will have ``planes=3`` and ``alpha=False`` for the - source image, but the *meta* dictionary returned by this method - will have ``planes=4`` and ``alpha=True`` because an alpha - channel is synthesized and added. - - *pixels* is the pixel data in boxed row flat pixel format (just - like the :meth:`read` method). - - All the other aspects of the image data are not changed. - """ - - self.preamble() - - # Simple case, no conversion necessary. - if not self.colormap and not self.trns and not self.sbit: - return self.read() - - x,y,pixels,meta = self.read() - - if self.colormap: - meta['colormap'] = False - meta['alpha'] = bool(self.trns) - meta['bitdepth'] = 8 - meta['planes'] = 3 + bool(self.trns) - plte = self.palette() - def iterpal(pixels): - for row in pixels: - row = map(plte.__getitem__, row) - yield array('B', itertools.chain(*row)) - pixels = iterpal(pixels) - elif self.trns: - # It would be nice if there was some reasonable way of doing - # this without generating a whole load of intermediate tuples. - # But tuples does seem like the easiest way, with no other way - # clearly much simpler or much faster. (Actually, the L to LA - # conversion could perhaps go faster (all those 1-tuples!), but - # I still wonder whether the code proliferation is worth it) - it = self.transparent - maxval = 2**meta['bitdepth']-1 - planes = meta['planes'] - meta['alpha'] = True - meta['planes'] += 1 - typecode = 'BH'[meta['bitdepth']>8] - def itertrns(pixels): - for row in pixels: - # For each row we group it into pixels, then form a - # characterisation vector that says whether each pixel - # is opaque or not. Then we convert True/False to - # 0/maxval (by multiplication), and add it as the extra - # channel. - row = group(row, planes) - opa = map(it.__ne__, row) - opa = map(maxval.__mul__, opa) - opa = zip(opa) # convert to 1-tuples - yield array(typecode, - itertools.chain(*map(operator.add, row, opa))) - pixels = itertrns(pixels) - targetbitdepth = None - if self.sbit: - sbit = struct.unpack('%dB' % len(self.sbit), self.sbit) - targetbitdepth = max(sbit) - if targetbitdepth > meta['bitdepth']: - raise Error('sBIT chunk %r exceeds bitdepth %d' % - (sbit,self.bitdepth)) - if min(sbit) <= 0: - raise Error('sBIT chunk %r has a 0-entry' % sbit) - if targetbitdepth == meta['bitdepth']: - targetbitdepth = None - if targetbitdepth: - shift = meta['bitdepth'] - targetbitdepth - meta['bitdepth'] = targetbitdepth - def itershift(pixels): - for row in pixels: - yield map(shift.__rrshift__, row) - pixels = itershift(pixels) - return x,y,pixels,meta - - def asFloat(self, maxval=1.0): - """Return image pixels as per :meth:`asDirect` method, but scale - all pixel values to be floating point values between 0.0 and - *maxval*. - """ - - x,y,pixels,info = self.asDirect() - sourcemaxval = 2**info['bitdepth']-1 - del info['bitdepth'] - info['maxval'] = float(maxval) - factor = float(maxval)/float(sourcemaxval) - def iterfloat(): - for row in pixels: - yield map(factor.__mul__, row) - return x,y,iterfloat(),info - - def _as_rescale(self, get, targetbitdepth): - """Helper used by :meth:`asRGB8` and :meth:`asRGBA8`.""" - - width,height,pixels,meta = get() - maxval = 2**meta['bitdepth'] - 1 - targetmaxval = 2**targetbitdepth - 1 - factor = float(targetmaxval) / float(maxval) - meta['bitdepth'] = targetbitdepth - def iterscale(): - for row in pixels: - yield map(lambda x: int(round(x*factor)), row) - return width, height, iterscale(), meta - - def asRGB8(self): - """Return the image data as an RGB pixels with 8-bits per - sample. This is like the :meth:`asRGB` method except that - this method additionally rescales the values so that they - are all between 0 and 255 (8-bit). In the case where the - source image has a bit depth < 8 the transformation preserves - all the information; where the source image has bit depth - > 8, then rescaling to 8-bit values loses precision. No - dithering is performed. Like :meth:`asRGB`, an alpha channel - in the source image will raise an exception. - - This function returns a 4-tuple: - (*width*, *height*, *pixels*, *metadata*). - *width*, *height*, *metadata* are as per the :meth:`read` method. - - *pixels* is the pixel data in boxed row flat pixel format. - """ - - return self._as_rescale(self.asRGB, 8) - - def asRGBA8(self): - """Return the image data as RGBA pixels with 8-bits per - sample. This method is similar to :meth:`asRGB8` and - :meth:`asRGBA`: The result pixels have an alpha channel, *and* - values are rescaled to the range 0 to 255. The alpha channel is - synthesized if necessary (with a small speed penalty). - """ - - return self._as_rescale(self.asRGBA, 8) - - def asRGB(self): - """Return image as RGB pixels. RGB colour images are passed - through unchanged; greyscales are expanded into RGB - triplets (there is a small speed overhead for doing this). - - An alpha channel in the source image will raise an - exception. - - The return values are as for the :meth:`read` method - except that the *metadata* reflect the returned pixels, not the - source image. In particular, for this method - ``metadata['greyscale']`` will be ``False``. - """ - - width,height,pixels,meta = self.asDirect() - if meta['alpha']: - raise Error("will not convert image with alpha channel to RGB") - if not meta['greyscale']: - return width,height,pixels,meta - meta['greyscale'] = False - typecode = 'BH'[meta['bitdepth'] > 8] - def iterrgb(): - for row in pixels: - a = array(typecode, [0]) * 3 * width - for i in range(3): - a[i::3] = row - yield a - return width,height,iterrgb(),meta - - def asRGBA(self): - """Return image as RGBA pixels. Greyscales are expanded into - RGB triplets; an alpha channel is synthesized if necessary. - The return values are as for the :meth:`read` method - except that the *metadata* reflect the returned pixels, not the - source image. In particular, for this method - ``metadata['greyscale']`` will be ``False``, and - ``metadata['alpha']`` will be ``True``. - """ - - width,height,pixels,meta = self.asDirect() - if meta['alpha'] and not meta['greyscale']: - return width,height,pixels,meta - typecode = 'BH'[meta['bitdepth'] > 8] - maxval = 2**meta['bitdepth'] - 1 - def newarray(): - return array(typecode, [0]) * 4 * width - if meta['alpha'] and meta['greyscale']: - # LA to RGBA - def convert(): - for row in pixels: - # Create a fresh target row, then copy L channel - # into first three target channels, and A channel - # into fourth channel. - a = newarray() - for i in range(3): - a[i::4] = row[0::2] - a[3::4] = row[1::2] - yield a - elif meta['greyscale']: - # L to RGBA - def convert(): - for row in pixels: - a = newarray() - for i in range(3): - a[i::4] = row - a[3::4] = array(typecode, maxval) * width - yield a - else: - assert not meta['alpha'] and not meta['greyscale'] - # RGB to RGBA - def convert(): - for row in pixels: - a = newarray() - for i in range(3): - a[i::4] = row[i::3] - a[3::4] = array(typecode, [maxval]) * width - yield a - meta['alpha'] = True - meta['greyscale'] = False - return width,height,convert(),meta - - -# === Legacy Version Support === - -# :pyver:old: PyPNG works on Python versions 2.3 and 2.2, but not -# without some awkward problems. Really PyPNG works on Python 2.4 (and -# above); it works on Pythons 2.3 and 2.2 by virtue of fixing up -# problems here. It's a bit ugly (which is why it's hidden down here). -# -# Generally the strategy is one of pretending that we're running on -# Python 2.4 (or above), and patching up the library support on earlier -# versions so that it looks enough like Python 2.4. When it comes to -# Python 2.2 there is one thing we cannot patch: extended slices -# http://www.python.org/doc/2.3/whatsnew/section-slices.html. -# Instead we simply declare that features that are implemented using -# extended slices will not work on Python 2.2. -# -# In order to work on Python 2.3 we fix up a recurring annoyance involving -# the array type. In Python 2.3 an array cannot be initialised with an -# array, and it cannot be extended with a list (or other sequence). -# Both of those are repeated issues in the code. Whilst I would not -# normally tolerate this sort of behaviour, here we "shim" a replacement -# for array into place (and hope no-ones notices). You never read this. -# -# In an amusing case of warty hacks on top of warty hacks... the array -# shimming we try and do only works on Python 2.3 and above (you can't -# subclass array.array in Python 2.2). So to get it working on Python -# 2.2 we go for something much simpler and (probably) way slower. -try: - array('B').extend([]) - array('B', array('B')) -except: - # Expect to get here on Python 2.3 - try: - class _array_shim(array): - true_array = array - def __new__(cls, typecode, init=None): - super_new = super(_array_shim, cls).__new__ - it = super_new(cls, typecode) - if init is None: - return it - it.extend(init) - return it - def extend(self, extension): - super_extend = super(_array_shim, self).extend - if isinstance(extension, self.true_array): - return super_extend(extension) - if not isinstance(extension, (list, str)): - # Convert to list. Allows iterators to work. - extension = list(extension) - return super_extend(self.true_array(self.typecode, extension)) - array = _array_shim - except: - # Expect to get here on Python 2.2 - def array(typecode, init=()): - if type(init) == str: - return map(ord, init) - return list(init) - -# Further hacks to get it limping along on Python 2.2 -try: - enumerate -except: - def enumerate(seq): - i=0 - for x in seq: - yield i,x - i += 1 - -try: - reversed -except: - def reversed(l): - l = list(l) - l.reverse() - for x in l: - yield x - -try: - itertools -except: - class _dummy_itertools: - pass - itertools = _dummy_itertools() - def _itertools_imap(f, seq): - for x in seq: - yield f(x) - itertools.imap = _itertools_imap - def _itertools_chain(*iterables): - for it in iterables: - for element in it: - yield element - itertools.chain = _itertools_chain - - - -# === Internal Test Support === - -# This section comprises the tests that are internally validated (as -# opposed to tests which produce output files that are externally -# validated). Primarily they are unittests. - -# Note that it is difficult to internally validate the results of -# writing a PNG file. The only thing we can do is read it back in -# again, which merely checks consistency, not that the PNG file we -# produce is valid. - -# Run the tests from the command line: -# python -c 'import png;png.test()' - -from StringIO import StringIO -import tempfile -# http://www.python.org/doc/2.4.4/lib/module-unittest.html -import unittest - - -def test(): - unittest.main(__name__) - -def topngbytes(name, rows, x, y, **k): - """Convenience function for creating a PNG file "in memory" as a - string. Creates a :class:`Writer` instance using the keyword arguments, - then passes `rows` to its :meth:`Writer.write` method. The resulting - PNG file is returned as a string. `name` is used to identify the file for - debugging. - """ - - import os - - print name - f = StringIO() - w = Writer(x, y, **k) - w.write(f, rows) - if os.environ.get('PYPNG_TEST_TMP'): - w = open(name, 'wb') - w.write(f.getvalue()) - w.close() - return f.getvalue() - -def testWithIO(inp, out, f): - """Calls the function `f` with ``sys.stdin`` changed to `inp` - and ``sys.stdout`` changed to `out`. They are restored when `f` - returns. This function returns whatever `f` returns. - """ - try: - oldin,sys.stdin = sys.stdin,inp - oldout,sys.stdout = sys.stdout,out - x = f() - finally: - sys.stdin = oldin - sys.stdout = oldout - return x - -class Test(unittest.TestCase): - # This member is used by the superclass. If we don't define a new - # class here then when we use self.assertRaises() and the PyPNG code - # raises an assertion then we get no proper traceback. I can't work - # out why, but defining a new class here means we get a proper - # traceback. - class failureException(Exception): - pass - - def helperLN(self, n): - mask = (1 << n) - 1 - # Use small chunk_limit so that multiple chunk writing is - # tested. Making it a test for Issue 20. - w = Writer(15, 17, greyscale=True, bitdepth=n, chunk_limit=99) - f = StringIO() - w.write_array(f, array('B', map(mask.__and__, range(1, 256)))) - r = Reader(bytes=f.getvalue()) - x,y,pixels,meta = r.read() - self.assertEqual(x, 15) - self.assertEqual(y, 17) - self.assertEqual(list(itertools.chain(*pixels)), - map(mask.__and__, range(1,256))) - def testL8(self): - return self.helperLN(8) - def testL4(self): - return self.helperLN(4) - def testL2(self): - "Also tests asRGB8." - w = Writer(1, 4, greyscale=True, bitdepth=2) - f = StringIO() - w.write_array(f, array('B', range(4))) - r = Reader(bytes=f.getvalue()) - x,y,pixels,meta = r.asRGB8() - self.assertEqual(x, 1) - self.assertEqual(y, 4) - for i,row in enumerate(pixels): - self.assertEqual(len(row), 3) - self.assertEqual(list(row), [0x55*i]*3) - def testP2(self): - "2-bit palette." - a = (255,255,255) - b = (200,120,120) - c = (50,99,50) - w = Writer(1, 4, bitdepth=2, palette=[a,b,c]) - f = StringIO() - w.write_array(f, array('B', (0,1,1,2))) - r = Reader(bytes=f.getvalue()) - x,y,pixels,meta = r.asRGB8() - self.assertEqual(x, 1) - self.assertEqual(y, 4) - self.assertEqual(list(pixels), map(list, [a, b, b, c])) - def testPtrns(self): - "Test colour type 3 and tRNS chunk (and 4-bit palette)." - a = (50,99,50,50) - b = (200,120,120,80) - c = (255,255,255) - d = (200,120,120) - e = (50,99,50) - w = Writer(3, 3, bitdepth=4, palette=[a,b,c,d,e]) - f = StringIO() - w.write_array(f, array('B', (4, 3, 2, 3, 2, 0, 2, 0, 1))) - r = Reader(bytes=f.getvalue()) - x,y,pixels,meta = r.asRGBA8() - self.assertEquals(x, 3) - self.assertEquals(y, 3) - c = c+(255,) - d = d+(255,) - e = e+(255,) - boxed = [(e,d,c),(d,c,a),(c,a,b)] - flat = map(lambda row: itertools.chain(*row), boxed) - self.assertEqual(map(list, pixels), map(list, flat)) - def testRGBtoRGBA(self): - "asRGBA8() on colour type 2 source.""" - # Test for Issue 26 - r = Reader(bytes=_pngsuite['basn2c08']) - x,y,pixels,meta = r.asRGBA8() - # Test the pixels at row 9 columns 0 and 1. - row9 = list(pixels)[9] - self.assertEqual(row9[0:8], - [0xff, 0xdf, 0xff, 0xff, 0xff, 0xde, 0xff, 0xff]) - def testCtrns(self): - "Test colour type 2 and tRNS chunk." - # Test for Issue 25 - r = Reader(bytes=_pngsuite['tbrn2c08']) - x,y,pixels,meta = r.asRGBA8() - # I just happen to know that the first pixel is transparent. - # In particular it should be #7f7f7f00 - row0 = list(pixels)[0] - self.assertEqual(tuple(row0[0:4]), (0x7f, 0x7f, 0x7f, 0x00)) - def testAdam7read(self): - """Adam7 interlace reading. - Specifically, test that for images in the PngSuite that - have both an interlaced and straightlaced pair that both - images from the pair produce the same array of pixels.""" - for candidate in _pngsuite: - if not candidate.startswith('basn'): - continue - candi = candidate.replace('n', 'i') - if candi not in _pngsuite: - continue - print 'adam7 read', candidate - straight = Reader(bytes=_pngsuite[candidate]) - adam7 = Reader(bytes=_pngsuite[candi]) - # Just compare the pixels. Ignore x,y (because they're - # likely to be correct?); metadata is ignored because the - # "interlace" member differs. Lame. - straight = straight.read()[2] - adam7 = adam7.read()[2] - self.assertEqual(map(list, straight), map(list, adam7)) - def testAdam7write(self): - """Adam7 interlace writing. - For each test image in the PngSuite, write an interlaced - and a straightlaced version. Decode both, and compare results. - """ - # Not such a great test, because the only way we can check what - # we have written is to read it back again. - - for name,bytes in _pngsuite.items(): - # Only certain colour types supported for this test. - if name[3:5] not in ['n0', 'n2', 'n4', 'n6']: - continue - it = Reader(bytes=bytes) - x,y,pixels,meta = it.read() - pngi = topngbytes('adam7wn'+name+'.png', pixels, - x=x, y=y, bitdepth=it.bitdepth, - greyscale=it.greyscale, alpha=it.alpha, - transparent=it.transparent, - interlace=False) - x,y,ps,meta = Reader(bytes=pngi).read() - it = Reader(bytes=bytes) - x,y,pixels,meta = it.read() - pngs = topngbytes('adam7wi'+name+'.png', pixels, - x=x, y=y, bitdepth=it.bitdepth, - greyscale=it.greyscale, alpha=it.alpha, - transparent=it.transparent, - interlace=True) - x,y,pi,meta = Reader(bytes=pngs).read() - self.assertEqual(map(list, ps), map(list, pi)) - def testPGMin(self): - """Test that the command line tool can read PGM files.""" - def do(): - return _main(['testPGMin']) - s = StringIO() - s.write('P5 2 2 3\n') - s.write('\x00\x01\x02\x03') - s.flush() - s.seek(0) - o = StringIO() - testWithIO(s, o, do) - r = Reader(bytes=o.getvalue()) - x,y,pixels,meta = r.read() - self.assert_(r.greyscale) - self.assertEqual(r.bitdepth, 2) - def testPAMin(self): - """Test that the command line tool can read PAM file.""" - def do(): - return _main(['testPAMin']) - s = StringIO() - s.write('P7\nWIDTH 3\nHEIGHT 1\nDEPTH 4\nMAXVAL 255\n' - 'TUPLTYPE RGB_ALPHA\nENDHDR\n') - # The pixels in flat row flat pixel format - flat = [255,0,0,255, 0,255,0,120, 0,0,255,30] - s.write(''.join(map(chr, flat))) - s.flush() - s.seek(0) - o = StringIO() - testWithIO(s, o, do) - r = Reader(bytes=o.getvalue()) - x,y,pixels,meta = r.read() - self.assert_(r.alpha) - self.assert_(not r.greyscale) - self.assertEqual(list(itertools.chain(*pixels)), flat) - def testLA4(self): - """Create an LA image with bitdepth 4.""" - bytes = topngbytes('la4.png', [[5, 12]], 1, 1, - greyscale=True, alpha=True, bitdepth=4) - sbit = Reader(bytes=bytes).chunk('sBIT')[1] - self.assertEqual(sbit, '\x04\x04') - def testPNMsbit(self): - """Test that PNM files can generates sBIT chunk.""" - def do(): - return _main(['testPNMsbit']) - s = StringIO() - s.write('P6 8 1 1\n') - for pixel in range(8): - s.write(struct.pack('>sys.stderr, "skipping numpy test" - return - - rows = [map(numpy.uint16, range(0,0x10000,0x5555))] - b = topngbytes('numpyuint16.png', rows, 4, 1, - greyscale=True, alpha=False, bitdepth=16) - def testNumpyuint8(self): - """numpy uint8.""" - - try: - import numpy - except ImportError: - print >>sys.stderr, "skipping numpy test" - return - - rows = [map(numpy.uint8, range(0,0x100,0x55))] - b = topngbytes('numpyuint8.png', rows, 4, 1, - greyscale=True, alpha=False, bitdepth=8) - def testNumpybool(self): - """numpy bool.""" - - try: - import numpy - except ImportError: - print >>sys.stderr, "skipping numpy test" - return - - rows = [map(numpy.bool, [0,1])] - b = topngbytes('numpybool.png', rows, 2, 1, - greyscale=True, alpha=False, bitdepth=1) - def testNumpyarray(self): - """numpy array.""" - try: - import numpy - except ImportError: - print >>sys.stderr, "skipping numpy test" - return - - pixels = numpy.array([[0,0x5555],[0x5555,0xaaaa]], numpy.uint16) - img = from_array(pixels, 'L') - img.save('testnumpyL16.png') - -# === Command Line Support === - -def _dehex(s): - """Liberally convert from hex string to binary string.""" - import re - - # Remove all non-hexadecimal digits - s = re.sub(r'[^a-fA-F\d]', '', s) - return s.decode('hex') - -# Copies of PngSuite test files taken -# from http://www.schaik.com/pngsuite/pngsuite_bas_png.html -# on 2009-02-19 by drj and converted to hex. -# Some of these are not actually in PngSuite (but maybe they should -# be?), they use the same naming scheme, but start with a capital -# letter. -_pngsuite = { - 'basi0g01': _dehex(""" -89504e470d0a1a0a0000000d49484452000000200000002001000000012c0677 -cf0000000467414d41000186a031e8965f0000009049444154789c2d8d310ec2 -300c45dfc682c415187a00a42e197ab81e83b127e00c5639001363a580d8582c -65c910357c4b78b0bfbfdf4f70168c19e7acb970a3f2d1ded9695ce5bf5963df -d92aaf4c9fd927ea449e6487df5b9c36e799b91bdf082b4d4bd4014fe4014b01 -ab7a17aee694d28d328a2d63837a70451e1648702d9a9ff4a11d2f7a51aa21e5 -a18c7ffd0094e3511d661822f20000000049454e44ae426082 -"""), - 'basi0g02': _dehex(""" -89504e470d0a1a0a0000000d49484452000000200000002002000000016ba60d -1f0000000467414d41000186a031e8965f0000005149444154789c635062e860 -00e17286bb609c93c370ec189494960631366e4467b3ae675dcf10f521ea0303 -90c1ca006444e11643482064114a4852c710baea3f18c31918020c30410403a6 -0ac1a09239009c52804d85b6d97d0000000049454e44ae426082 -"""), - 'basi0g04': _dehex(""" -89504e470d0a1a0a0000000d4948445200000020000000200400000001e4e6f8 -bf0000000467414d41000186a031e8965f000000ae49444154789c658e5111c2 -301044171c141c141c041c843a287510ea20d441c041c141c141c04191102454 -03994998cecd7edcecedbb9bdbc3b2c2b6457545fbc4bac1be437347f7c66a77 -3c23d60db15e88f5c5627338a5416c2e691a9b475a89cd27eda12895ae8dfdab -43d61e590764f5c83a226b40d669bec307f93247701687723abf31ff83a2284b -a5b4ae6b63ac6520ad730ca4ed7b06d20e030369bd6720ed383290360406d24e -13811f2781eba9d34d07160000000049454e44ae426082 -"""), - 'basi0g08': _dehex(""" -89504e470d0a1a0a0000000d4948445200000020000000200800000001211615 -be0000000467414d41000186a031e8965f000000b549444154789cb5905d0ac2 -3010849dbac81c42c47bf843cf253e8878b0aa17110f214bdca6be240f5d21a5 -94ced3e49bcd322c1624115515154998aa424822a82a5624a1aa8a8b24c58f99 -999908130989a04a00d76c2c09e76cf21adcb209393a6553577da17140a2c59e -70ecbfa388dff1f03b82fb82bd07f05f7cb13f80bb07ad2fd60c011c3c588eef -f1f4e03bbec7ce832dca927aea005e431b625796345307b019c845e6bfc3bb98 -769d84f9efb02ea6c00f9bb9ff45e81f9f280000000049454e44ae426082 -"""), - 'basi0g16': _dehex(""" -89504e470d0a1a0a0000000d49484452000000200000002010000000017186c9 -fd0000000467414d41000186a031e8965f000000e249444154789cb5913b0ec2 -301044c7490aa8f85d81c3e4301c8f53a4ca0da8902c8144b3920b4043111282 -23bc4956681a6bf5fc3c5a3ba0448912d91a4de2c38dd8e380231eede4c4f7a1 -4677700bec7bd9b1d344689315a3418d1a6efbe5b8305ba01f8ff4808c063e26 -c60d5c81edcf6c58c535e252839e93801b15c0a70d810ae0d306b205dc32b187 -272b64057e4720ff0502154034831520154034c3df81400510cdf0015c86e5cc -5c79c639fddba9dcb5456b51d7980eb52d8e7d7fa620a75120d6064641a05120 -b606771a05626b401a05f1f589827cf0fe44c1f0bae0055698ee8914fffffe00 -00000049454e44ae426082 -"""), - 'basi2c08': _dehex(""" -89504e470d0a1a0a0000000d49484452000000200000002008020000018b1fdd -350000000467414d41000186a031e8965f000000f249444154789cd59341aa04 -210c44abc07b78133d59d37333bd89d76868b566d10cf4675af8596431a11662 -7c5688919280e312257dd6a0a4cf1a01008ee312a5f3c69c37e6fcc3f47e6776 -a07f8bdaf5b40feed2d33e025e2ff4fe2d4a63e1a16d91180b736d8bc45854c5 -6d951863f4a7e0b66dcf09a900f3ffa2948d4091e53ca86c048a64390f662b50 -4a999660ced906182b9a01a8be00a56404a6ede182b1223b4025e32c4de34304 -63457680c93aada6c99b73865aab2fc094920d901a203f5ddfe1970d28456783 -26cffbafeffcd30654f46d119be4793f827387fc0d189d5bc4d69a3c23d45a7f -db803146578337df4d0a3121fc3d330000000049454e44ae426082 -"""), - 'basi2c16': _dehex(""" -89504e470d0a1a0a0000000d4948445200000020000000201002000001db8f01 -760000000467414d41000186a031e8965f0000020a49444154789cd5962173e3 -3010853fcf1838cc61a1818185a53e56787fa13fa130852e3b5878b4b0b03081 -b97f7030070b53e6b057a0a8912bbb9163b9f109ececbc59bd7dcf2b45492409 -d66f00eb1dd83cb5497d65456aeb8e1040913b3b2c04504c936dd5a9c7e2c6eb -b1b8f17a58e8d043da56f06f0f9f62e5217b6ba3a1b76f6c9e99e8696a2a72e2 -c4fb1e4d452e92ec9652b807486d12b6669be00db38d9114b0c1961e375461a5 -5f76682a85c367ad6f682ff53a9c2a353191764b78bb07d8ddc3c97c1950f391 -6745c7b9852c73c2f212605a466a502705c8338069c8b9e84efab941eb393a97 -d4c9fd63148314209f1c1d3434e847ead6380de291d6f26a25c1ebb5047f5f24 -d85c49f0f22cc1d34282c72709cab90477bf25b89d49f0f351822297e0ea9704 -f34c82bc94002448ede51866e5656aef5d7c6a385cb4d80e6a538ceba04e6df2 -480e9aa84ddedb413bb5c97b3838456df2d4fec2c7a706983e7474d085fae820 -a841776a83073838973ac0413fea2f1dc4a06e71108fda73109bdae48954ad60 -bf867aac3ce44c7c1589a711cf8a81df9b219679d96d1cec3d8bbbeaa2012626 -df8c7802eda201b2d2e0239b409868171fc104ba8b76f10b4da09f6817ffc609 -c413ede267fd1fbab46880c90f80eccf0013185eb48b47ba03df2bdaadef3181 -cb8976f18e13188768170f98c0f844bb78cb04c62ddac59d09fc3fa25dfc1da4 -14deb3df1344f70000000049454e44ae426082 -"""), - 'basi3p08': _dehex(""" -89504e470d0a1a0a0000000d494844520000002000000020080300000133a3ba -500000000467414d41000186a031e8965f00000300504c5445224400f5ffed77 -ff77cbffff110a003a77002222ffff11ff110000222200ffac5566ff66ff6666 -ff01ff221200dcffffccff994444ff005555220000cbcbff44440055ff55cbcb -00331a00ffecdcedffffe4ffcbffdcdc44ff446666ff330000442200ededff66 -6600ffa444ffffaaeded0000cbcbfefffffdfffeffff0133ff33552a000101ff -8888ff00aaaa010100440000888800ffe4cbba5b0022ff22663200ffff99aaaa -ff550000aaaa00cb630011ff11d4ffaa773a00ff4444dc6b0066000001ff0188 -4200ecffdc6bdc00ffdcba00333300ed00ed7300ffff88994a0011ffff770000 -ff8301ffbabafe7b00fffeff00cb00ff999922ffff880000ffff77008888ffdc -ff1a33000000aa33ffff009900990000000001326600ffbaff44ffffffaaff00 -770000fefeaa00004a9900ffff66ff22220000998bff1155ffffff0101ff88ff -005500001111fffffefffdfea4ff4466ffffff66ff003300ffff55ff77770000 -88ff44ff00110077ffff006666ffffed000100fff5ed1111ffffff44ff22ffff -eded11110088ffff00007793ff2200dcdc3333fffe00febabaff99ffff333300 -63cb00baba00acff55ffffdcffff337bfe00ed00ed5555ffaaffffdcdcff5555 -00000066dcdc00dc00dc83ff017777fffefeffffffcbff5555777700fefe00cb -00cb0000fe010200010000122200ffff220044449bff33ffd4aa0000559999ff -999900ba00ba2a5500ffcbcbb4ff66ff9b33ffffbaaa00aa42880053aa00ffaa -aa0000ed00babaffff1100fe00000044009999990099ffcc99ba000088008800 -dc00ff93220000dcfefffeaa5300770077020100cb0000000033ffedff00ba00 -ff3333edffedffc488bcff7700aa00660066002222dc0000ffcbffdcffdcff8b -110000cb00010155005500880000002201ffffcbffcbed0000ff88884400445b -ba00ffbc77ff99ff006600baffba00777773ed00fe00003300330000baff77ff -004400aaffaafffefe000011220022c4ff8800eded99ff99ff55ff002200ffb4 -661100110a1100ff1111dcffbabaffff88ff88010001ff33ffb98ed362000002 -a249444154789c65d0695c0b001806f03711a9904a94d24dac63292949e5a810 -d244588a14ca5161d1a1323973252242d62157d12ae498c8124d25ca3a11398a -16e55a3cdffab0ffe7f77d7fcff3528645349b584c3187824d9d19d4ec2e3523 -9eb0ae975cf8de02f2486d502191841b42967a1ad49e5ddc4265f69a899e26b5 -e9e468181baae3a71a41b95669da8df2ea3594c1b31046d7b17bfb86592e4cbe -d89b23e8db0af6304d756e60a8f4ad378bdc2552ae5948df1d35b52143141533 -33bbbbababebeb3b3bc9c9c9c6c6c0c0d7b7b535323225a5aa8a02024a4bedec -0a0a2a2bcdcd7d7cf2f3a9a9c9cdcdd8b8adcdd5b5ababa828298982824a4ab2 -b21212acadbdbc1414e2e24859b9a72730302f4f49292c4c57373c9c0a0b7372 -8c8c1c1c3a3a92936d6dfdfd293e3e26262a4a4eaea2424b4b5fbfbc9c323278 -3c0b0ba1303abaae8ecdeeed950d6669a9a7a7a141d4de9e9d5d5cdcd2229b94 -c572716132f97cb1d8db9bc3110864a39795d9db6b6a26267a7a9a98d4d6a6a7 -cb76090ef6f030354d4d75766e686030545464cb393a1a1ac6c68686eae8f8f9 -a9aa4644c8b66d6e1689dcdd2512a994cb35330b0991ad9f9b6b659596a6addd -d8282fafae5e5323fb8f41d01f76c22fd8061be01bfc041a0323e1002c81cd30 -0b9ec027a0c930014ec035580fc3e112bc069a0b53e11c0c8095f00176c163a0 -e5301baec06a580677600ddc05ba0f13e120bc81a770133ec355a017300d4ec2 -0c7800bbe1219c02fa08f3e13c1c85dbb00a2ec05ea0dff00a6ec15a98027360 -070c047a06d7e1085c84f1b014f6c03fa0b33018b6c0211801ebe018fc00da0a -6f61113c877eb01d4ec317a085700f26c130f80efbe132bc039a0733e106fc81 -f7f017f6c10aa0d1300a0ec374780943e1382c06fa0a9b60238c83473016cec0 -02f80f73fefe1072afc1e50000000049454e44ae426082 -"""), - 'basi6a08': _dehex(""" -89504e470d0a1a0a0000000d4948445200000020000000200806000001047d4a -620000000467414d41000186a031e8965f0000012049444154789cc595414ec3 -3010459fa541b8bbb26641b8069b861e8b4d12c1c112c1452a710a2a65d840d5 -949041fc481ec98ae27c7f3f8d27e3e4648047600fec0d1f390fbbe2633a31e2 -9389e4e4ea7bfdbf3d9a6b800ab89f1bd6b553cfcbb0679e960563d72e0a9293 -b7337b9f988cc67f5f0e186d20e808042f1c97054e1309da40d02d7e27f92e03 -6cbfc64df0fc3117a6210a1b6ad1a00df21c1abcf2a01944c7101b0cb568a001 -909c9cf9e399cf3d8d9d4660a875405d9a60d000b05e2de55e25780b7a5268e0 -622118e2399aab063a815808462f1ab86890fc2e03e48bb109ded7d26ce4bf59 -0db91bac0050747fec5015ce80da0e5700281be533f0ce6d5900b59bcb00ea6d -200314cf801faab200ea752803a8d7a90c503a039f824a53f4694e7342000000 -0049454e44ae426082 -"""), - 'basn0g01': _dehex(""" -89504e470d0a1a0a0000000d49484452000000200000002001000000005b0147 -590000000467414d41000186a031e8965f0000005b49444154789c2dccb10903 -300c05d1ebd204b24a200b7a346f90153c82c18d0a61450751f1e08a2faaead2 -a4846ccea9255306e753345712e211b221bf4b263d1b427325255e8bdab29e6f -6aca30692e9d29616ee96f3065f0bf1f1087492fd02f14c90000000049454e44 -ae426082 -"""), - 'basn0g02': _dehex(""" -89504e470d0a1a0a0000000d49484452000000200000002002000000001ca13d -890000000467414d41000186a031e8965f0000001f49444154789c6360085df5 -1f8cf1308850c20053868f0133091f6390b90700bd497f818b0989a900000000 -49454e44ae426082 -"""), - # A version of basn0g04 dithered down to 3 bits. - 'Basn0g03': _dehex(""" -89504e470d0a1a0a0000000d494844520000002000000020040000000093e1c8 -2900000001734249540371d88211000000fd49444154789c6d90d18906210c84 -c356f22356b2889588604301b112112b11d94a96bb495cf7fe87f32d996f2689 -44741cc658e39c0b118f883e1f63cc89dafbc04c0f619d7d898396c54b875517 -83f3a2e7ac09a2074430e7f497f00f1138a5444f82839c5206b1f51053cca968 -63258821e7f2b5438aac16fbecc052b646e709de45cf18996b29648508728612 -952ca606a73566d44612b876845e9a347084ea4868d2907ff06be4436c4b41a3 -a3e1774285614c5affb40dbd931a526619d9fa18e4c2be420858de1df0e69893 -a0e3e5523461be448561001042b7d4a15309ce2c57aef2ba89d1c13794a109d7 -b5880aa27744fc5c4aecb5e7bcef5fe528ec6293a930690000000049454e44ae -426082 -"""), - 'basn0g04': _dehex(""" -89504e470d0a1a0a0000000d494844520000002000000020040000000093e1c8 -290000000467414d41000186a031e8965f0000004849444154789c6360601014 -545232367671090d4d4b2b2f6720430095dbd1418e002a77e64c720450b9ab56 -912380caddbd9b1c0154ee9933e408a072efde25470095fbee1d1902001f14ee -01eaff41fa0000000049454e44ae426082 -"""), - 'basn0g08': _dehex(""" -89504e470d0a1a0a0000000d4948445200000020000000200800000000561125 -280000000467414d41000186a031e8965f0000004149444154789c6364602400 -1408c8b30c05058c0f0829f8f71f3f6079301c1430ca11906764a2795c0c0605 -8c8ff0cafeffcff887e67131181430cae0956564040050e5fe7135e2d8590000 -000049454e44ae426082 -"""), - 'basn0g16': _dehex(""" -89504e470d0a1a0a0000000d49484452000000200000002010000000000681f9 -6b0000000467414d41000186a031e8965f0000005e49444154789cd5d2310ac0 -300c4351395bef7fc6dca093c0287b32d52a04a3d98f3f3880a7b857131363a0 -3a82601d089900dd82f640ca04e816dc06422640b7a03d903201ba05b7819009 -d02d680fa44c603f6f07ec4ff41938cf7f0016d84bd85fae2b9fd70000000049 -454e44ae426082 -"""), - 'basn2c08': _dehex(""" -89504e470d0a1a0a0000000d4948445200000020000000200802000000fc18ed -a30000000467414d41000186a031e8965f0000004849444154789cedd5c10900 -300c024085ec91fdb772133b442bf4a1f8cee12bb40d043b800a14f81ca0ede4 -7d4c784081020f4a871fc284071428f0a0743823a94081bb7077a3c00182b1f9 -5e0f40cf4b0000000049454e44ae426082 -"""), - 'basn2c16': _dehex(""" -89504e470d0a1a0a0000000d4948445200000020000000201002000000ac8831 -e00000000467414d41000186a031e8965f000000e549444154789cd596c10a83 -301044a7e0417fcb7eb7fdadf6961e06039286266693cc7a188645e43dd6a08f -1042003e2fe09aef6472737e183d27335fcee2f35a77b702ebce742870a23397 -f3edf2705dd10160f3b2815fe8ecf2027974a6b0c03f74a6e4192843e75c6c03 -35e8ec3202f5e84c0181bbe8cca967a00d9df3491bb040671f2e6087ce1c2860 -8d1e05f8c7ee0f1d00b667e70df44467ef26d01fbd9bc028f42860f71d188bce -fb8d3630039dbd59601e7ab3c06cf428507f0634d039afdc80123a7bb1801e7a -b1802a7a14c89f016d74ce331bf080ce9e08f8414f04bca133bfe642fe5e07bb -c4ec0000000049454e44ae426082 -"""), - 'basn6a08': _dehex(""" -89504e470d0a1a0a0000000d4948445200000020000000200806000000737a7a -f40000000467414d41000186a031e8965f0000006f49444154789cedd6310a80 -300c46e12764684fa1f73f55048f21c4ddc545781d52e85028fc1f4d28d98a01 -305e7b7e9cffba33831d75054703ca06a8f90d58a0074e351e227d805c8254e3 -1bb0420f5cdc2e0079208892ffe2a00136a07b4007943c1004d900195036407f -011bf00052201a9c160fb84c0000000049454e44ae426082 -"""), - 'cs3n3p08': _dehex(""" -89504e470d0a1a0a0000000d494844520000002000000020080300000044a48a -c60000000467414d41000186a031e8965f0000000373424954030303a392a042 -00000054504c544592ff0000ff9200ffff00ff0000dbff00ff6dffb600006dff -b6ff00ff9200dbff000049ffff2400ff000024ff0049ff0000ffdb00ff4900ff -b6ffff0000ff2400b6ffffdb000092ffff6d000024ffff49006dff00df702b17 -0000004b49444154789c85cac70182000000b1b3625754b0edbfa72324ef7486 -184ed0177a437b680bcdd0031c0ed00ea21f74852ed00a1c9ed0086da0057487 -6ed0121cd6d004bda0013a421ff803224033e177f4ae260000000049454e44ae -426082 -"""), - 's09n3p02': _dehex(""" -89504e470d0a1a0a0000000d49484452000000090000000902030000009dffee -830000000467414d41000186a031e8965f000000037342495404040477f8b5a3 -0000000c504c544500ff000077ffff00ffff7700ff5600640000001f49444154 -789c63600002fbff0c0c56ab19182ca381581a4283f82071200000696505c36a -437f230000000049454e44ae426082 -"""), - 'tbgn3p08': _dehex(""" -89504e470d0a1a0a0000000d494844520000002000000020080300000044a48a -c60000000467414d41000186a031e8965f00000207504c54457f7f7fafafafab -abab110000222200737300999999510d00444400959500959595e6e600919191 -8d8d8d620d00898989666600b7b700911600000000730d007373736f6f6faaaa -006b6b6b676767c41a00cccc0000f30000ef00d51e0055555567670000dd0051 -515100d1004d4d4de61e0038380000b700160d0d00ab00560d00090900009500 -009100008d003333332f2f2f2f2b2f2b2b000077007c7c001a05002b27000073 -002b2b2b006f00bb1600272727780d002323230055004d4d00cc1e00004d00cc -1a000d00003c09006f6f00002f003811271111110d0d0d55554d090909001100 -4d0900050505000d00e2e200000900000500626200a6a6a6a2a2a29e9e9e8484 -00fb00fbd5d500801100800d00ea00ea555500a6a600e600e6f7f700e200e233 -0500888888d900d9848484c01a007777003c3c05c8c8008080804409007c7c7c -bb00bbaa00aaa600a61e09056262629e009e9a009af322005e5e5e05050000ee -005a5a5adddd00a616008d008d00e20016050027270088110078780000c40078 -00787300736f006f44444400aa00c81e004040406600663c3c3c090000550055 -1a1a00343434d91e000084004d004d007c004500453c3c00ea1e00222222113c -113300331e1e1efb22001a1a1a004400afaf00270027003c001616161e001e0d -160d2f2f00808000001e00d1d1001100110d000db7b7b7090009050005b3b3b3 -6d34c4230000000174524e530040e6d86600000001624b474402660b7c640000 -01f249444154789c6360c0048c8c58049100575f215ee92e6161ef109cd2a15e -4b9645ce5d2c8f433aa4c24f3cbd4c98833b2314ab74a186f094b9c2c27571d2 -6a2a58e4253c5cda8559057a392363854db4d9d0641973660b0b0bb76bb16656 -06970997256877a07a95c75a1804b2fbcd128c80b482a0b0300f8a824276a9a8 -ec6e61612b3e57ee06fbf0009619d5fac846ac5c60ed20e754921625a2daadc6 -1967e29e97d2239c8aec7e61fdeca9cecebef54eb36c848517164514af16169e -866444b2b0b7b55534c815cc2ec22d89cd1353800a8473100a4485852d924a6a -412adc74e7ad1016ceed043267238c901716f633a812022998a4072267c4af02 -92127005c0f811b62830054935ce017b38bf0948cc5c09955f030a24617d9d46 -63371fd940b0827931cbfdf4956076ac018b592f72d45594a9b1f307f3261b1a -084bc2ad50018b1900719ba6ba4ca325d0427d3f6161449486f981144cf3100e -2a5f2a1ce8683e4ddf1b64275240c8438d98af0c729bbe07982b8a1c94201dc2 -b3174c9820bcc06201585ad81b25b64a2146384e3798290c05ad280a18c0a62e -e898260c07fca80a24c076cc864b777131a00190cdfa3069035eccbc038c30e1 -3e88b46d16b6acc5380d6ac202511c392f4b789aa7b0b08718765990111606c2 -9e854c38e5191878fbe471e749b0112bb18902008dc473b2b2e8e72700000000 -49454e44ae426082 -"""), - 'Tp2n3p08': _dehex(""" -89504e470d0a1a0a0000000d494844520000002000000020080300000044a48a -c60000000467414d41000186a031e8965f00000300504c544502ffff80ff05ff -7f0703ff7f0180ff04ff00ffff06ff000880ff05ff7f07ffff06ff000804ff00 -0180ff02ffff03ff7f02ffff80ff0503ff7f0180ffff0008ff7f0704ff00ffff -06ff000802ffffff7f0704ff0003ff7fffff0680ff050180ff04ff000180ffff -0008ffff0603ff7f80ff05ff7f0702ffffff000880ff05ffff0603ff7f02ffff -ff7f070180ff04ff00ffff06ff000880ff050180ffff7f0702ffff04ff0003ff -7fff7f0704ff0003ff7f0180ffffff06ff000880ff0502ffffffff0603ff7fff -7f0702ffff04ff000180ff80ff05ff0008ff7f07ffff0680ff0504ff00ff0008 -0180ff03ff7f02ffff02ffffffff0604ff0003ff7f0180ffff000880ff05ff7f -0780ff05ff00080180ff02ffffff7f0703ff7fffff0604ff00ff7f07ff0008ff -ff0680ff0504ff0002ffff0180ff03ff7fff0008ffff0680ff0504ff000180ff -02ffff03ff7fff7f070180ff02ffff04ff00ffff06ff0008ff7f0780ff0503ff -7fffff06ff0008ff7f0780ff0502ffff03ff7f0180ff04ff0002ffffff7f07ff -ff0604ff0003ff7fff00080180ff80ff05ffff0603ff7f0180ffff000804ff00 -80ff0502ffffff7f0780ff05ffff0604ff000180ffff000802ffffff7f0703ff -7fff0008ff7f070180ff03ff7f02ffff80ff05ffff0604ff00ff0008ffff0602 -ffff0180ff04ff0003ff7f80ff05ff7f070180ff04ff00ff7f0780ff0502ffff -ff000803ff7fffff0602ffffff7f07ffff0680ff05ff000804ff0003ff7f0180 -ff02ffff0180ffff7f0703ff7fff000804ff0080ff05ffff0602ffff04ff00ff -ff0603ff7fff7f070180ff80ff05ff000803ff7f0180ffff7f0702ffffff0008 -04ff00ffff0680ff0503ff7f0180ff04ff0080ff05ffff06ff000802ffffff7f -0780ff05ff0008ff7f070180ff03ff7f04ff0002ffffffff0604ff00ff7f07ff -000880ff05ffff060180ff02ffff03ff7f80ff05ffff0602ffff0180ff03ff7f -04ff00ff7f07ff00080180ffff000880ff0502ffff04ff00ff7f0703ff7fffff -06ff0008ffff0604ff00ff7f0780ff0502ffff03ff7f0180ffdeb83387000000 -f874524e53000000000000000008080808080808081010101010101010181818 -1818181818202020202020202029292929292929293131313131313131393939 -393939393941414141414141414a4a4a4a4a4a4a4a52525252525252525a5a5a -5a5a5a5a5a62626262626262626a6a6a6a6a6a6a6a73737373737373737b7b7b -7b7b7b7b7b83838383838383838b8b8b8b8b8b8b8b94949494949494949c9c9c -9c9c9c9c9ca4a4a4a4a4a4a4a4acacacacacacacacb4b4b4b4b4b4b4b4bdbdbd -bdbdbdbdbdc5c5c5c5c5c5c5c5cdcdcdcdcdcdcdcdd5d5d5d5d5d5d5d5dedede -dededededee6e6e6e6e6e6e6e6eeeeeeeeeeeeeeeef6f6f6f6f6f6f6f6b98ac5 -ca0000012c49444154789c6360e7169150d230b475f7098d4ccc28a96ced9e32 -63c1da2d7b8e9fb97af3d1fb8f3f18e8a0808953544a4dd7c4c2c9233c2621bf -b4aab17fdacce5ab36ee3a72eafaad87efbefea68702362e7159652d031b07cf -c0b8a4cce28aa68e89f316aedfb4ffd0b92bf79fbcfcfe931e0a183904e55435 -8decdcbcc22292b3caaadb7b27cc5db67af3be63e72fdf78fce2d31f7a2860e5 -119356d037b374f10e8a4fc92eaa6fee99347fc9caad7b0f9ebd74f7c1db2fbf -e8a180995f484645dbdccad12f38363dafbcb6a573faeca5ebb6ed3e7ce2c29d -e76fbefda38702063e0149751d537b67ff80e8d4dcc29a86bea97316add9b0e3 -c0e96bf79ebdfafc971e0a587885e515f58cad5d7d43a2d2720aeadaba26cf5a -bc62fbcea3272fde7efafac37f3a28000087c0fe101bc2f85f0000000049454e -44ae426082 -"""), - 'tbbn1g04': _dehex(""" -89504e470d0a1a0a0000000d494844520000002000000020040000000093e1c8 -290000000467414d41000186a031e8965f0000000274524e530007e8f7589b00 -000002624b47440000aa8d23320000013e49444154789c55d1cd4b024118c7f1 -efbe6419045b6a48a72d352808b435284f9187ae9b098627a1573a19945beba5 -e8129e8222af11d81e3a4545742de8ef6af6d5762e0fbf0fc33c33f36085cb76 -bc4204778771b867260683ee57e13f0c922df5c719c2b3b6c6c25b2382cea4b9 -9f7d4f244370746ac71f4ca88e0f173a6496749af47de8e44ba8f3bf9bdfa98a -0faf857a7dd95c7dc8d7c67c782c99727997f41eb2e3c1e554152465bb00fe8e -b692d190b718d159f4c0a45c4435915a243c58a7a4312a7a57913f05747594c6 -46169866c57101e4d4ce4d511423119c419183a3530cc63db88559ae28e7342a -1e9c8122b71139b8872d6e913153224bc1f35b60e4445bd4004e20ed6682c759 -1d9873b3da0fbf50137dc5c9bde84fdb2ec8bde1189e0448b63584735993c209 -7a601bd2710caceba6158797285b7f2084a2f82c57c01a0000000049454e44ae -426082 -"""), - 'tbrn2c08': _dehex(""" -89504e470d0a1a0a0000000d4948445200000020000000200802000000fc18ed -a30000000467414d41000186a031e8965f0000000674524e53007f007f007f8a -33334f00000006624b474400ff0000000033277cf3000004d649444154789cad -965f68537714c73fd912d640235e692f34d0406fa0c1663481045ab060065514 -56660a295831607df0a1488715167060840a1614e6431e9cb34fd2c00a762c85 -f6a10f816650c13b0cf40612e1822ddc4863bd628a8924d23d6464f9d3665dd9 -f7e977ce3dbff3cd3939bfdfef6bb87dfb364782dbed065ebe7cd93acc78b4ec -a228debd7bb7bfbfbfbbbbfb7f261045311a8d261209405194274f9ea4d3e916 -f15f1c3eb5dd6e4fa5fecce526239184a2b0b8486f6f617171b1f5ae4311381c -8e57af5e5dbd7a351088150a78bd389d44222c2f93cdfe66b7db8f4ee07038b6 -b6b6bebf766d7e7e7e60a06432313b4ba984c3c1c4049a46b95c5a58583822c1 -dbb76f27272733d1b9df853c3030c0f232562b9108cf9eb1b888d7cbf030abab -31abd5fa1f08dc6ef7e7cf9f1f3f7e1c8944745d4f1400c62c001313acad21cb -b8dd2c2c603271eb1640341aad4c6d331aa7e8c48913a150a861307ecc11e964 -74899919bc5e14e56fffc404f1388502f178dceff7ef4bf0a5cfe7abb533998c -e5f9ea2f1dd88c180d64cb94412df3dd57e83a6b3b3c7a84c98420100c72fd3a -636348bae726379fe69e8e8d8dbd79f3a6558b0607079796965256479b918085 -7b02db12712b6181950233023f3f647494ee6e2e5ea45864cce5b8a7fe3acffc -3aebb22c2bd5d20e22d0757d7b7bbbbdbd3d94a313bed1b0aa3cd069838b163a -8d4c59585f677292d0b84d9a995bd337def3fe6bbe5e6001989b9b6bfe27ea08 -36373781542ab56573248b4c5bc843ac4048c7ab21aa24ca00534c25482828a3 -8c9ee67475bbaaaab22cb722c8e57240a150301a8d219de94e44534d7d90e885 -87acb0e2c4f9800731629b6c5ee14a35a6b9887d2a0032994cb9cf15dbe59650 -ff7b46a04c9a749e7cc5112214266cc65c31354d5b5d5d3d90209bcd5616a552 -a95c2e87f2a659bd9ee01c2cd73964e438f129a6aa9e582c363838b80f81d7eb -5555b56a2a8ad2d9d7affd0409f8015c208013fea00177b873831b0282c964f2 -783c1e8fa7582cee5f81a669b5e6eeeeaee58e8559b0c233d8843c7c0b963a82 -34e94b5cb2396d7d7d7db22c8ba258fb0afd43f0e2c58b919191ba9de9b4d425 -118329b0c3323c8709d02041b52b4ea7f39de75d2a934a2693c0a953a76a93d4 -5d157ebf7f6565a5542a553df97c5e10045dd731c130b86113cc300cbd489224 -08422a952a140a95788fc763b1d41558d7a2d7af5f5fb870a1d6a3aaaacd6603 -18802da84c59015bd2e6897b745d9765b99a1df0f97c0daf74e36deaf7fbcd66 -73ad2797cb89a2c839880188a2e8743a8bc5a22ccbba5e376466b3b9bdbdbd21 -6123413a9d0e0402b51e4dd3bababa788eb022b85caeb6b6364551b6b7b76942 -43f7f727007a7a7a04a1ee8065b3595fde2768423299ac1ec6669c3973e65004 -c0f8f878ad69341a33994ced2969c0d0d0502412f9f8f163f3a7fd654b474787 -288ad53e74757535df6215b85cae60302849d2410aecc037f9f2e5cbd5b5c160 -680eb0dbede170381c0e7ff8f0a185be3b906068684892a4ca7a6f6faff69328 -8ad3d3d3f7efdfdfdbdbfb57e96868a14d0d0643381c96242997cbe5f3794010 -84603078fcf8f1d6496bd14a3aba5c2ea7d369341a5555b5582c8140e0fcf9f3 -1b1b1b87cf4eeb0a8063c78e45a3d19e9e1ebfdfdf5a831e844655d18093274f -9e3d7bf6d3a74f3b3b3b47c80efc05ff7af28fefb70d9b0000000049454e44ae -426082 -"""), - 'basn6a16': _dehex(""" -89504e470d0a1a0a0000000d494844520000002000000020100600000023eaa6 -b70000000467414d41000186a031e8965f00000d2249444154789cdd995f6c1c -d775c67ff38fb34b724d2ee55a8e4b04a0ac87049100cab4dbd8c6528902cb4d -10881620592e52d4325ac0905bc98a94025e71fd622cb5065ac98a0c283050c0 -728a00b6e542a1d126885cd3298928891d9a0444037e904434951d4b90b84b2f -c9dde1fcebc33977a95555348f411e16dfce9d3b77ee77eebde77ce78c95a669 -0ad07c17009a13edd898b87dfb1fcb7d2b4d1bff217f33df80deb1e6267df0ff -c1e6e6dfafdf1f5a7fd30f9aef66b6d546dd355bf02c40662e3307f9725a96c6 -744c3031f83782f171c148dbc3bf1774f5dad1e79d6f095a3f54d4fbec5234ef -d9a2f8d73afe4f14f57ef4f42def7b44f19060f06b45bddf1c5534d77fd922be -2973a15a82e648661c6e3240aa3612ead952b604bde57458894f29deaf133bac -13d2766f5227a4a3b8cf08da7adfd6fbd6bd8a4fe9dbb43d35e3dfa3f844fbf8 -9119bf4f7144094fb56333abf8a86063ca106f94b3a3b512343765e60082097f -1bb86ba72439a653519b09f5cee1ce61c897d37eedf5553580ae60f4af8af33a -b14fd400b6a0f34535c0434afc0b3a9f07147527a5fa7ca218ff56c74d74dc3f -155cfd3325fc278acf2ae1cb4a539f5f9937c457263b0bd51234c732a300cdd1 -cc1840f0aaff54db0e4874ed5a9b5d6d27d4bb36746d80de72baa877ff4b275a -d7895ed1897ea4139b5143fcbb1a62560da1ed9662aaed895ec78a91c18795b8 -5e07ab4af8ba128e95e682e0728bf8f2e5ae815a091a53d902ac1920d8e05f06 -589de8d8d66680789f4e454fb9d9ec66cd857af796ee2d902fa73fd5bba775a2 -153580ae44705ed0d37647d15697cb8f14bfa3e3e8fdf8031d47af571503357c -f30d25acedcbbf135c9a35c49766ba07ab255859e8ec03684e66860182dff8f7 -0304bff6ff1c20fc81b7afdd00a71475539a536e36bb5973a19e3b923b02bde5 -e4efd4003ac170eb2d13fe274157afedbd82d6fb3a9a1e85e4551d47cf7078f8 -9671fe4289ebf5f2bf08d63f37c4eb4773c55a0996efeefa0ca011671d8060ca -2f0004c7fcc300e166ef0240f825efe3361f106d57d423d0723f7acacd66376b -2ed47b7a7a7a205f4ef4ac4691e0aad9aa0d41cf13741c3580a506487574ddca -61a8c403c1863ebfbcac3475168b2de28b8b3d77544bb05ce92a02aceced3c0d -d0cc65ea371b201cf1c601c24dde1c4078cedbdeb60322f50126a019bf6edc9b -39e566b39b3517eaf97c3e0fbde5e4491d45bd74537145d155b476aa0176e868 -c6abebf30dbd5e525c54ac8e18e2d56abeb756827a3d970358a97416019a6f64 -f60004fdfe1580d5c98e618070cc1b05887eee7e0d209a70db7d8063029889b4 -c620ead78d7b33a7dc6c76b3e6427ddddbebde867c393aa7845e5403e8ca794a -d0d6fb897af5f03525fe5782f5e7046bdaef468bf88d1debc6ab25583cd17310 -6079b9ab0ba059c914018245bf076075b5a303200c3c1f209a733701444fbbaf -00c4134ebb016c5d0b23614c243701cdf875e3decce9349bddacb9505fbf7dfd -76e82d87736a00f5d2b5ffd4b7dce2719a4d25ae717ee153c1abef18e257cfad -7fa45682da48ef38c052b53b0fd06864b300c151ff08c0ea431de701a287dd5f -004497dc7b01a253ee3e80b8c7f91c20f967fb6fdb7c80ada7d8683723614c24 -3701cdf875e3decc29379bddacb950ef3fd47f08f2e5a61ea4aa2a3eb757cd55 -13345efcfa59c12b2f19e2578ef77fb75a82854ffbee01a83f977b11a031931d -040802df07082b5e11207cc17b1e209a770700e2df0a83e409fb7580f827c230 -99b06fd901fb058d6835dacd481813c94d40337eddb83773cacd66376b2ed437 -bebcf165e82d2f4e4beb7f3fa6e652c2d7ee10bc78c010bfb87fe3c95a09ae9f -bd732740bd2fb700d0f865f64180e059ff044018ca0ca28a5b04883f701e0088 -bfec7c0c909cb71f0448c6ec518074b375012079d9dedf66004bcfbc51eb2dd1 -aadacd481813c94d40337eddb83773cacd66376b2ed487868686205fbe7c49ef -5605a73f34c4a7a787eeab96e0da81bb4e022c15ba27019a5b339300e16bf286 -a8eae601e25866907cdf3e0890acb36f00245fb57f05904e59c300e92561946e -b2e600d209ab7d07f04d458dfb46ad1bd16ab49b913026929b8066fcba716fe6 -949bcd6ed65ca8ef7e7cf7e3d05b7e7c8f217ee6cdddbb6a25a856f37980e0c7 -fe4e80a82623c48193014846ec7180f4acf518409aca0cd28a5504e03b32c374 -de1a00608a0240faaa327a4b19fe946fb6f90054dbb5f2333d022db56eb4966a -3723614c243701cdf8f556bea8a7dc6c76b3e66bd46584ddbbcebc0990cf4b0f -ff4070520c282338a7e26700ec725202b01e4bcf0258963c6f1d4d8f0030cb20 -805549c520930c03584fa522b676f11600ffc03fde3e1b3489a9c9054c9aa23b -c08856a3dd8c843191dc0434e3d78d7b33a75c36fb993761f7ae5a69f72ef97f -e6ad336fed7e1c60e8bee96980bbdebbb60da07b7069062033d9dc0ae03d296f -70ab511ec071640676252902d833c916007b3e1900b0a6d2028035968e025861 -ea01581369fb11488c34d18cbc95989afccca42baad65ba2d5683723614c24d7 -8066fcbab8b7e96918baaf5aaa56219f975fb50a43f7c9bde90fa73f1c1a02d8 -78f2e27e803b77ca08b90519315b6fe400fc1392097a9eccc0ad444500e70199 -a1331f0f00d8934901c07e5d526ceb87c2d07e2579badd005a2b31a5089391b7 -1253358049535a6add8856dd0146c298482e01ede27ed878b256ba7600ee3a09 -c18fc1df09fe01084ec25defc1b56db0f1a4f4bd78e0e2818d2f0334e7330300 -7df7c888b917e50dd9c1c60c80efcb0cbc63e1f700bce7c31700dccbd1060027 -8add9b0de06c8e2f00d84962b7d7030e2a61538331b98051f92631bd253f336a -dd8856a3dd44c25c390efddfad96ae9f853b77c25201ba27c533b8bdf28b6ad0 -3d084b33d2e7fa59099e9901b8f2d29597fa0f01848f78e70082117f1ca07b76 -6910209b9519f895a008d031bbba05c09d8f06005c5b18b8fba25300cea6780e -c03e911c6ccf06d507b48a4fa606634a114609de929f9934c5a87511ad57cfc1 -fa476aa5854fa1ef1e3910b905686e85cc24c40138198915f133d2d6dc2a7dea -7df2ccc2a752faf2cec1d577aebeb37e3b4034eeee0008dff3be0e6b923773b4 -7904c0ef9119767cb4fa1500ef1361e08e452500f71561e84cc4ed3e20fab6a2 -c905f40cb76a3026bf3319b91ac2e46792a6dcd801ebc6aba5da08f48ecb81c8 -bd088d5f42f6417191de93908c803d0e76199292b485af41b60e8d9c3c537f0e -8211f0c7211a077707dc18b931b2ee6d80a4d7ae024491ebc24d4a708ff70680 -7f25e807e8785f1878e322d6ddaf453f0770ff2dfa769b01423dbbad72a391b6 -5a7c3235985629423372494cab55c8f7d64a8b27a0e7202c55a13b0f8d19c80e -4ae9ca3f015115dc3ca467c17a4c7ee95970ab10e5a54ff0ac3cd39881ee5958 -1a84f03df0be0e492fd855a8d6aa35d10b4962dbb0a604a3d3ee5e80a8eee600 -a24977f8660378bf0bbf00e01d0a8fb7f980f04b8aa6ce6aca8d5a7533c52753 -839152c4e222f4dc512dd5eb90cbc981e8ea12cf90cd8a8bf47d89159e2741d3 -7124f65b96fcd254dae258fa84a13c13043246a32129574787e49eae2b49b86d -c3e2e78b9ff7f4002415bb08907c66df0d103b4e0c104db90500ff70700c203a -ee1e82dba4c3e16e256c0acca6ceaae9afd1f612d7eb472157ac95962bd05594 -7dd1598466053245088e827f44628657942a825b84e4fb601f84b4025611aca3 -901e01bb024911dc0a4445f08e41f83df02b10142173149ab71baf027611ea95 -7a257704201d14cd9af4d90b00f194530088cb4e09c0df1c5c0088f7393f6833 -c0aa3ac156655de3bca9b34ab9716906ba07aba5e5bba1eb3358d90b9da7c533 -64f6888bf47b60f521e8380fe10be03d2feac17900927560df40f4e48f805960 -50328d648bf4893f9067c217a0631656b7c898c122847bc07b03a2d3e0ee85e4 -33b0ef867450c4fad2ecd26cf7168074c0ba0c904cdac300c9cfec4701924df6 -1cdca61e10685c6f7d52d0caba1498972f43d740adb4b2009d7d7220b20e3473 -90a943d00ffe959bb6eac3e0fe42ea49ee00c45f06e76329b1dabf127d690d80 -5581b408f63c2403e0cc433c00ee658836803b0fd100747c04ab5f917704fd10 -d5c1cd41ec801343d207f602a403605d86e5f9e5f9ae0d00e994556833806685 -c931fb709b0f08b4e869bea5c827859549e82c544b8d29c816a0390999613920 -7e610d5727a16318c2003c1fa24be0de2b32caf92224e7c17e5004b6350c4c01 -05601218066b0ad28224e149019c086257ca315102de2712903bde97b8144d82 -3b2c6ac52d403c054e019249b087f53d0558995a99ea946c70cc927458b3c1ff -550f30050df988d4284376b4566a8e416654cc921985e037e0df0fc131f00f4b -acf0c6211c036f14a239703741740adc7da227edd7e56b833d0ae92549b4d357 -25dfb49ed2ff63908e6adf27d6d0dda7638d4154d2778daca17f58e61297c129 -41f233b01f5dc3740cac51688c35c6b22580f48224fee9b83502569a66b629f1 -09f3713473413e2666e7fe6f6c6efefdfafda1f56f6e06f93496d9d67cb7366a -9964b6f92e64b689196ec6c604646fd3fe4771ff1bf03f65d8ecc3addbb5f300 -00000049454e44ae426082 -"""), -} - -def test_suite(options, args): - """ - Create a PNG test image and write the file to stdout. - """ - - # Below is a big stack of test image generators. - # They're all really tiny, so PEP 8 rules are suspended. - - def test_gradient_horizontal_lr(x, y): return x - def test_gradient_horizontal_rl(x, y): return 1-x - def test_gradient_vertical_tb(x, y): return y - def test_gradient_vertical_bt(x, y): return 1-y - def test_radial_tl(x, y): return max(1-math.sqrt(x*x+y*y), 0.0) - def test_radial_center(x, y): return test_radial_tl(x-0.5, y-0.5) - def test_radial_tr(x, y): return test_radial_tl(1-x, y) - def test_radial_bl(x, y): return test_radial_tl(x, 1-y) - def test_radial_br(x, y): return test_radial_tl(1-x, 1-y) - def test_stripe(x, n): return float(int(x*n) & 1) - def test_stripe_h_2(x, y): return test_stripe(x, 2) - def test_stripe_h_4(x, y): return test_stripe(x, 4) - def test_stripe_h_10(x, y): return test_stripe(x, 10) - def test_stripe_v_2(x, y): return test_stripe(y, 2) - def test_stripe_v_4(x, y): return test_stripe(y, 4) - def test_stripe_v_10(x, y): return test_stripe(y, 10) - def test_stripe_lr_10(x, y): return test_stripe(x+y, 10) - def test_stripe_rl_10(x, y): return test_stripe(1+x-y, 10) - def test_checker(x, y, n): return float((int(x*n) & 1) ^ (int(y*n) & 1)) - def test_checker_8(x, y): return test_checker(x, y, 8) - def test_checker_15(x, y): return test_checker(x, y, 15) - def test_zero(x, y): return 0 - def test_one(x, y): return 1 - - test_patterns = { - 'GLR': test_gradient_horizontal_lr, - 'GRL': test_gradient_horizontal_rl, - 'GTB': test_gradient_vertical_tb, - 'GBT': test_gradient_vertical_bt, - 'RTL': test_radial_tl, - 'RTR': test_radial_tr, - 'RBL': test_radial_bl, - 'RBR': test_radial_br, - 'RCTR': test_radial_center, - 'HS2': test_stripe_h_2, - 'HS4': test_stripe_h_4, - 'HS10': test_stripe_h_10, - 'VS2': test_stripe_v_2, - 'VS4': test_stripe_v_4, - 'VS10': test_stripe_v_10, - 'LRS': test_stripe_lr_10, - 'RLS': test_stripe_rl_10, - 'CK8': test_checker_8, - 'CK15': test_checker_15, - 'ZERO': test_zero, - 'ONE': test_one, - } - - def test_pattern(width, height, bitdepth, pattern): - """Create a single plane (monochrome) test pattern. Returns a - flat row flat pixel array. - """ - - maxval = 2**bitdepth-1 - if maxval > 255: - a = array('H') - else: - a = array('B') - fw = float(width) - fh = float(height) - pfun = test_patterns[pattern] - for y in range(height): - fy = float(y)/fh - for x in range(width): - a.append(int(round(pfun(float(x)/fw, fy) * maxval))) - return a - - def test_rgba(size=256, bitdepth=8, - red="GTB", green="GLR", blue="RTL", alpha=None): - """ - Create a test image. Each channel is generated from the - specified pattern; any channel apart from red can be set to - None, which will cause it not to be in the image. It - is possible to create all PNG channel types (L, RGB, LA, RGBA), - as well as non PNG channel types (RGA, and so on). - """ - - i = test_pattern(size, size, bitdepth, red) - psize = 1 - for channel in (green, blue, alpha): - if channel: - c = test_pattern(size, size, bitdepth, channel) - i = interleave_planes(i, c, psize, 1) - psize += 1 - return i - - def pngsuite_image(name): - """ - Create a test image by reading an internal copy of the files - from the PngSuite. Returned in flat row flat pixel format. - """ - - if name not in _pngsuite: - raise NotImplementedError("cannot find PngSuite file %s (use -L for a list)" % name) - r = Reader(bytes=_pngsuite[name]) - w,h,pixels,meta = r.asDirect() - assert w == h - # LAn for n < 8 is a special case for which we need to rescale - # the data. - if meta['greyscale'] and meta['alpha'] and meta['bitdepth'] < 8: - factor = 255 // (2**meta['bitdepth']-1) - def rescale(data): - for row in data: - yield map(factor.__mul__, row) - pixels = rescale(pixels) - meta['bitdepth'] = 8 - arraycode = 'BH'[meta['bitdepth']>8] - return w, array(arraycode, itertools.chain(*pixels)), meta - - # The body of test_suite() - size = 256 - if options.test_size: - size = options.test_size - options.bitdepth = options.test_depth - options.greyscale=bool(options.test_black) - - kwargs = {} - if options.test_red: - kwargs["red"] = options.test_red - if options.test_green: - kwargs["green"] = options.test_green - if options.test_blue: - kwargs["blue"] = options.test_blue - if options.test_alpha: - kwargs["alpha"] = options.test_alpha - if options.greyscale: - if options.test_red or options.test_green or options.test_blue: - raise ValueError("cannot specify colours (R, G, B) when greyscale image (black channel, K) is specified") - kwargs["red"] = options.test_black - kwargs["green"] = None - kwargs["blue"] = None - options.alpha = bool(options.test_alpha) - if not args: - pixels = test_rgba(size, options.bitdepth, **kwargs) - else: - size,pixels,meta = pngsuite_image(args[0]) - for k in ['bitdepth', 'alpha', 'greyscale']: - setattr(options, k, meta[k]) - - writer = Writer(size, size, - bitdepth=options.bitdepth, - transparent=options.transparent, - background=options.background, - gamma=options.gamma, - greyscale=options.greyscale, - alpha=options.alpha, - compression=options.compression, - interlace=options.interlace) - writer.write_array(sys.stdout, pixels) - -def read_pam_header(infile): - """ - Read (the rest of a) PAM header. `infile` should be positioned - immediately after the initial 'P7' line (at the beginning of the - second line). Returns are as for `read_pnm_header`. - """ - - # Unlike PBM, PGM, and PPM, we can read the header a line at a time. - header = dict() - while True: - l = infile.readline().strip() - if l == 'ENDHDR': - break - if l == '': - raise EOFError('PAM ended prematurely') - if l[0] == '#': - continue - l = l.split(None, 1) - if l[0] not in header: - header[l[0]] = l[1] - else: - header[l[0]] += ' ' + l[1] - - if ('WIDTH' not in header or - 'HEIGHT' not in header or - 'DEPTH' not in header or - 'MAXVAL' not in header): - raise Error('PAM file must specify WIDTH, HEIGHT, DEPTH, and MAXVAL') - width = int(header['WIDTH']) - height = int(header['HEIGHT']) - depth = int(header['DEPTH']) - maxval = int(header['MAXVAL']) - if (width <= 0 or - height <= 0 or - depth <= 0 or - maxval <= 0): - raise Error( - 'WIDTH, HEIGHT, DEPTH, MAXVAL must all be positive integers') - return 'P7', width, height, depth, maxval - -def read_pnm_header(infile, supported=('P5','P6')): - """ - Read a PNM header, returning (format,width,height,depth,maxval). - `width` and `height` are in pixels. `depth` is the number of - channels in the image; for PBM and PGM it is synthesized as 1, for - PPM as 3; for PAM images it is read from the header. `maxval` is - synthesized (as 1) for PBM images. - """ - - # Generally, see http://netpbm.sourceforge.net/doc/ppm.html - # and http://netpbm.sourceforge.net/doc/pam.html - - # Technically 'P7' must be followed by a newline, so by using - # rstrip() we are being liberal in what we accept. I think this - # is acceptable. - type = infile.read(3).rstrip() - if type not in supported: - raise NotImplementedError('file format %s not supported' % type) - if type == 'P7': - # PAM header parsing is completely different. - return read_pam_header(infile) - # Expected number of tokens in header (3 for P4, 4 for P6) - expected = 4 - pbm = ('P1', 'P4') - if type in pbm: - expected = 3 - header = [type] - - # We have to read the rest of the header byte by byte because the - # final whitespace character (immediately following the MAXVAL in - # the case of P6) may not be a newline. Of course all PNM files in - # the wild use a newline at this point, so it's tempting to use - # readline; but it would be wrong. - def getc(): - c = infile.read(1) - if c == '': - raise Error('premature EOF reading PNM header') - return c - - c = getc() - while True: - # Skip whitespace that precedes a token. - while c.isspace(): - c = getc() - # Skip comments. - while c == '#': - while c not in '\n\r': - c = getc() - if not c.isdigit(): - raise Error('unexpected character %s found in header' % c) - # According to the specification it is legal to have comments - # that appear in the middle of a token. - # This is bonkers; I've never seen it; and it's a bit awkward to - # code good lexers in Python (no goto). So we break on such - # cases. - token = '' - while c.isdigit(): - token += c - c = getc() - # Slight hack. All "tokens" are decimal integers, so convert - # them here. - header.append(int(token)) - if len(header) == expected: - break - # Skip comments (again) - while c == '#': - while c not in '\n\r': - c = getc() - if not c.isspace(): - raise Error('expected header to end with whitespace, not %s' % c) - - if type in pbm: - # synthesize a MAXVAL - header.append(1) - depth = (1,3)[type == 'P6'] - return header[0], header[1], header[2], depth, header[3] - -def write_pnm(file, width, height, pixels, meta): - """Write a Netpbm PNM/PAM file.""" - - bitdepth = meta['bitdepth'] - maxval = 2**bitdepth - 1 - # Rudely, the number of image planes can be used to determine - # whether we are L (PGM), LA (PAM), RGB (PPM), or RGBA (PAM). - planes = meta['planes'] - # Can be an assert as long as we assume that pixels and meta came - # from a PNG file. - assert planes in (1,2,3,4) - if planes in (1,3): - if 1 == planes: - # PGM - # Could generate PBM if maxval is 1, but we don't (for one - # thing, we'd have to convert the data, not just blat it - # out). - fmt = 'P5' - else: - # PPM - fmt = 'P6' - file.write('%s %d %d %d\n' % (fmt, width, height, maxval)) - if planes in (2,4): - # PAM - # See http://netpbm.sourceforge.net/doc/pam.html - if 2 == planes: - tupltype = 'GRAYSCALE_ALPHA' - else: - tupltype = 'RGB_ALPHA' - file.write('P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\n' - 'TUPLTYPE %s\nENDHDR\n' % - (width, height, planes, maxval, tupltype)) - # Values per row - vpr = planes * width - # struct format - fmt = '>%d' % vpr - if maxval > 0xff: - fmt = fmt + 'H' - else: - fmt = fmt + 'B' - for row in pixels: - file.write(struct.pack(fmt, *row)) - file.flush() - -def color_triple(color): - """ - Convert a command line colour value to a RGB triple of integers. - FIXME: Somewhere we need support for greyscale backgrounds etc. - """ - if color.startswith('#') and len(color) == 4: - return (int(color[1], 16), - int(color[2], 16), - int(color[3], 16)) - if color.startswith('#') and len(color) == 7: - return (int(color[1:3], 16), - int(color[3:5], 16), - int(color[5:7], 16)) - elif color.startswith('#') and len(color) == 13: - return (int(color[1:5], 16), - int(color[5:9], 16), - int(color[9:13], 16)) - - -def _main(argv): - """ - Run the PNG encoder with options from the command line. - """ - - # Parse command line arguments - from optparse import OptionParser - import re - version = '%prog ' + re.sub(r'( ?\$|URL: |Rev:)', '', __version__) - parser = OptionParser(version=version) - parser.set_usage("%prog [options] [imagefile]") - parser.add_option('-r', '--read-png', default=False, - action='store_true', - help='Read PNG, write PNM') - parser.add_option("-i", "--interlace", - default=False, action="store_true", - help="create an interlaced PNG file (Adam7)") - parser.add_option("-t", "--transparent", - action="store", type="string", metavar="color", - help="mark the specified colour (#RRGGBB) as transparent") - parser.add_option("-b", "--background", - action="store", type="string", metavar="color", - help="save the specified background colour") - parser.add_option("-a", "--alpha", - action="store", type="string", metavar="pgmfile", - help="alpha channel transparency (RGBA)") - parser.add_option("-g", "--gamma", - action="store", type="float", metavar="value", - help="save the specified gamma value") - parser.add_option("-c", "--compression", - action="store", type="int", metavar="level", - help="zlib compression level (0-9)") - parser.add_option("-T", "--test", - default=False, action="store_true", - help="create a test image (a named PngSuite image if an argument is supplied)") - parser.add_option('-L', '--list', - default=False, action='store_true', - help="print list of named test images") - parser.add_option("-R", "--test-red", - action="store", type="string", metavar="pattern", - help="test pattern for the red image layer") - parser.add_option("-G", "--test-green", - action="store", type="string", metavar="pattern", - help="test pattern for the green image layer") - parser.add_option("-B", "--test-blue", - action="store", type="string", metavar="pattern", - help="test pattern for the blue image layer") - parser.add_option("-A", "--test-alpha", - action="store", type="string", metavar="pattern", - help="test pattern for the alpha image layer") - parser.add_option("-K", "--test-black", - action="store", type="string", metavar="pattern", - help="test pattern for greyscale image") - parser.add_option("-d", "--test-depth", - default=8, action="store", type="int", - metavar='NBITS', - help="create test PNGs that are NBITS bits per channel") - parser.add_option("-S", "--test-size", - action="store", type="int", metavar="size", - help="width and height of the test image") - (options, args) = parser.parse_args(args=argv[1:]) - - # Convert options - if options.transparent is not None: - options.transparent = color_triple(options.transparent) - if options.background is not None: - options.background = color_triple(options.background) - - if options.list: - names = list(_pngsuite) - names.sort() - for name in names: - print name - return - - # Run regression tests - if options.test: - return test_suite(options, args) - - # Prepare input and output files - if len(args) == 0: - infilename = '-' - infile = sys.stdin - elif len(args) == 1: - infilename = args[0] - infile = open(infilename, 'rb') - else: - parser.error("more than one input file") - outfile = sys.stdout - - if options.read_png: - # Encode PNG to PPM - png = Reader(file=infile) - width,height,pixels,meta = png.asDirect() - write_pnm(outfile, width, height, pixels, meta) - else: - # Encode PNM to PNG - format, width, height, depth, maxval = \ - read_pnm_header(infile, ('P5','P6','P7')) - # When it comes to the variety of input formats, we do something - # rather rude. Observe that L, LA, RGB, RGBA are the 4 colour - # types supported by PNG and that they correspond to 1, 2, 3, 4 - # channels respectively. So we use the number of channels in - # the source image to determine which one we have. We do not - # care about TUPLTYPE. - greyscale = depth <= 2 - pamalpha = depth in (2,4) - supported = map(lambda x: 2**x-1, range(1,17)) - try: - mi = supported.index(maxval) - except ValueError: - raise NotImplementedError( - 'your maxval (%s) not in supported list %s' % - (maxval, str(supported))) - bitdepth = mi+1 - writer = Writer(width, height, - greyscale=greyscale, - bitdepth=bitdepth, - interlace=options.interlace, - transparent=options.transparent, - background=options.background, - alpha=bool(pamalpha or options.alpha), - gamma=options.gamma, - compression=options.compression) - if options.alpha: - pgmfile = open(options.alpha, 'rb') - format, awidth, aheight, adepth, amaxval = \ - read_pnm_header(pgmfile, 'P5') - if amaxval != '255': - raise NotImplementedError( - 'maxval %s not supported for alpha channel' % amaxval) - if (awidth, aheight) != (width, height): - raise ValueError("alpha channel image size mismatch" - " (%s has %sx%s but %s has %sx%s)" - % (infilename, width, height, - options.alpha, awidth, aheight)) - writer.convert_ppm_and_pgm(infile, pgmfile, outfile) - else: - writer.convert_pnm(infile, outfile) - - -if __name__ == '__main__': - try: - _main(sys.argv) - except Error, e: - print >>sys.stderr, e diff --git a/tanks/GameMath.py b/tanks/GameMath.py deleted file mode 100644 index 4af140c..0000000 --- a/tanks/GameMath.py +++ /dev/null @@ -1,196 +0,0 @@ -import math - -pi2 = math.pi * 2 - -def rotatePoint(point, angle): - """Assuming 0,0 is the center, rotate the given point around it.""" - - x,y = point - r = math.sqrt(x**2 + y**2) - if r == 0: - return 0, 0 - - theta = math.acos(x/r) - if y < 0: - theta = -theta - theta = theta + angle - return int(round(r*math.cos(theta))), int(round(r*math.sin(theta))) - -def rotatePoly(points, angle): - """Rotate the given list of points around 0,0 by angle.""" - return [ rotatePoint(point, angle) for point in points ] - -def displace(point, disp, limits): - """Displace point by disp, wrapping around limits.""" - x = (point[0] + disp[0]) - while x >= limits[0]: - x = x - limits[0] - while x < 0: - x = x + limits[0] - - y = (point[1] + disp[1]) - while y >= limits[1]: - y = y - limits[1] - while y < 0: - y = y + limits[1] - - return x,y - -def displacePoly(points, disp, limits, coordSequence=False): - """Displace each point (x,y) in 'points' by 'disp' (x,y). The limits of - the drawing space are assumed to be at x=0, y=0 and x=limits[0], - y=limits[1]. If the poly overlaps the edge of the drawing space, the - poly is duplicated on each side. -@param coordSequence: If true, the coordinates are returned as a sequence - - x1, y1, x2, y2, ... This is need by some PIL drawing - commands. -@returns: A list of polys, displaced by disp - """ - xDup = 0; yDup = 0 - maxX, maxY = limits - basePoints = [] - for point in points: - x,y = int(point[0] + disp[0]), int(point[1] + disp[1]) - - # Check if duplication is needed on each axis - if x > maxX: - # If this is negative, then we need to duplicate in the negative - # direction. - xDup = -1 - elif x < 0: - xDup = 1 - - if y > maxY: - yDup = -1 - elif y < 0: - yDup = 1 - - basePoints.append( (x,y) ) - - polys = [basePoints] - if xDup: - polys.append([(x + maxX*xDup, y) for x,y in basePoints] ) - if yDup: - polys.append([(x, maxY*yDup + y) for x,y in basePoints] ) - if xDup and yDup: - polys.append([(x+maxX*xDup, maxY*yDup+y) for x,y in basePoints]) - - # Switch coordinates to sequence mode. - # (x1, y1, x2, y2) instead of ((x1, y1), (x2, y2)) - if coordSequence: - seqPolys = [] - for poly in polys: - points = [] - for point in poly: - points.extend(point) - seqPolys.append(points) - polys = seqPolys - - return polys - -def polar2cart(r, theta): - """Return the cartesian coordinates for r, theta.""" - x = r*math.cos(theta) - y = r*math.sin(theta) - return x,y - -def minShift(center, point, limits): - """Get the minimum distances between the two points, given that the board - wraps at the givin limits.""" - dx = point[0] - center[0] - if dx < -limits[0]/2.0: - dx = point[0] + limits[0] - center[0] - elif dx > limits[0]/2.0: - dx = point[0] - (center[0] + limits[0]) - - dy = point[1] - center[1] - if dy < - limits[1]/2.0: - dy = point[1] + limits[1] - center[1] - elif dy > limits[1]/2.0: - dy = point[1] - (limits[1] + center[1]) - - return dx, dy - -def relativePolar(center, point, limits): - """Returns the angle, from zero, to the given point assuming this -center is the origin. Take into account wrapping round the limits of the board. -@returns: r, theta - """ - - dx, dy = minShift(center, point, limits) - - r = math.sqrt(dx**2 + dy**2) - theta = math.acos(dx/r) - if dy < 0: - theta = pi2 - theta - - return r, theta - -def reduceAngle(angle): - """Reduce the angle such that it is in 0 <= angle < 2pi""" - - return angle % pi2 - -def angleDiff(angle1, angle2): - """Returns the difference between the two angles. They are assumed -to be in radians, and must be in the range 0 <= angle < 2*pi. -@raises AssertionError: The angles given must be in the range 0 <= angle < 2pi -@returns: The minimum distance between the two angles; The distance - is negative if angle2 leads angle1 (clockwise).. - """ - - ret = (angle2 - angle1) % pi2 - if ret > math.pi: - ret -= pi2 - return ret - -def getDist(point1, point2): - """Returns the distance between point1 and point2.""" - dx = point2[0] - point1[0] - dy = point2[1] - point1[1] - - return math.sqrt(dx**2 + dy**2) - -def segmentCircleCollision(segment, center, radius): - """Return True if the given circle touches the given line segment. -@param segment: A list of two points [(x1,y1), (x2, y2)] that define - the line segment. -@param center: The center point of the circle. -@param radius: The radius of the circle. -@returns: True if the the circle touches the line segment, False otherwise. - """ - - a = getDist(segment[0], center) - c = getDist(segment[1], center) - base = getDist(segment[0], segment[1]) - - # If we're close enough to the end points, then we're close - # enough to the segment. - if a < radius or c < radius: - return True - - # First we find the are of the triangle formed by the line segment - # and point. I use Heron's formula for the area. Using this, we'll - # find the distance d from the point to the line. We'll later make - # sure that the collision is with the line segment, and not just the - # line. - s = (a + c + base)/2 - A = math.sqrt(s*(s - a)*(s - c)*(s - base)) - d = 2*A/base - -# print s, a, c, A, d, radius - - # If the distance from the point to the line is more than the - # target radius, this isn't a hit. - if d > radius: - return False - - # If the distance from an endpoint to the intersection between - # our line segment and the line perpendicular to it that passes through - # the point is longer than the line segment, then this isn't a hit. - elif math.sqrt(a**2 - d**2) > base or \ - math.sqrt(c**2 - d**2) > base: - return False - else: - # The triangle is acute, that means we're close enough. - return True diff --git a/tanks/Pflanzarr.py b/tanks/Pflanzarr.py deleted file mode 100644 index 58fdf4b..0000000 --- a/tanks/Pflanzarr.py +++ /dev/null @@ -1,392 +0,0 @@ -import fcntl -import math -import os -import random -import cgi -from sets import Set as set -from ctf import teams, html, paths -from cStringIO import StringIO - -from urllib import unquote, quote - -import Tank - -class NotEnoughPlayers(Exception): - pass - -class Pflanzarr: - SPACING = 150 - - def __init__(self, dir): - """Initialize a new game of Pflanzarr. -@param dir: The data directory.""" - - # Setup the game environment - self._setupDirectories(dir) - - # Figure out what game number this is. - self.gameNum = self._getGameNum() - self.gameFilename = os.path.join(self._resultsDir, '%04d.html' % self.gameNum) - - tmpPlayers = os.listdir(self._playerDir) - players = [] - AIs = {} - for fn in tmpPlayers: - p = unquote(fn) - if (not (p.startswith('.') - or p.endswith('#') - or p.endswith('~')) - and teams.exists(p)): - players.append(p) - AIs[p] = open(os.path.join(self._playerDir, fn)).read() - defaultAIs = self._getDefaultAIs(dir) - - if len(players) < 1: - raise NotEnoughPlayers() - - # The one is added to ensure that there is at least one house - # bot. - cols = int(math.ceil(math.sqrt(len(players) + 1))) - cols = max(cols, 2) - - rows = int(math.ceil(len(players)/float(cols))) - rows = max(rows, 2) - - self._board = (cols*self.SPACING, rows*self.SPACING) - - while len(players) < cols*rows: - players.append(None) - - self._tanks = [] - for i in range(cols): - for j in range(rows): - startX = i*self.SPACING + self.SPACING/2 - startY = j*self.SPACING + self.SPACING/2 - player = random.choice(players) - players.remove(player) - color = '#' + teams.color(player) - tank = Tank.Tank( player, (startX, startY), color, - self._board) - if player == None: - tank.set_program(random.choice(defaultAIs)) - else: - tank.set_program(AIs[player]) - self._tanks.append(tank) - - # We only want to make these once, so we do it here. - self._tanksByX = list(self._tanks) - self._tanksByY = list(self._tanks) - - self._deadTanks = set() - - def run(self, maxTurns=None): - kills = {} - for tank in self._tanks: - kills[tank] = set() - - # Open HTML output - hdr = StringIO() - hdr.write('\n' - '\n') - - # Decide on the winner - winner = self._chooseWinner(kills) - self.winner = winner.name - - # Now generate HTML body - body = StringIO() - body.write(' \n' % self._board) - body.write(' Sorry, you need an HTML5-capable browser to see this.\n' - ' \n' - '

\n') - if self.gameNum > 0: - body.write(' ← Prev |' % - (self.gameNum - 1)) - body.write(' Next → |' % - (self.gameNum + 1)) - body.write(' 0 fps\n' - '

\n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n') - - tanks = self._tanks[:] - tanks.remove(winner) - tanks[0:0] = [winner] - for tank in tanks: - if tank is winner: - rowStyle = ('style="font-weight: bold; ' - 'color: #000; ' - 'background-color: %s;"' % tank.color) - else: - rowStyle = 'style="background-color:%s; color: #000;"' % tank.color - if tank.name: - name = cgi.escape(tank.name) - else: - name = teams.house - body.write('' % - (rowStyle, - name, - len(kills[tank]), - cgi.escape(tank.deathReason))) - body.write('
TeamKillsCause of Death
%s%d%s
\n') - - links='''

Tanks

- -''' - - # Write everything out - html.write(self.gameFilename, - 'Tanks round %d' % self.gameNum, - body.getvalue(), - hdr=hdr.getvalue(), - links=links, - onload='start(turns);') - - - - def _killTanks(self, tanks, reason): - for tank in tanks: - if tank in self._tanksByX: - self._tanksByX.remove(tank) - if tank in self._tanksByY: - self._tanksByY.remove(tank) - - tank.die(reason) - - self._deadTanks = self._deadTanks.union(tanks) - - def _chooseWinner(self, kills): - """Choose a winner. In case of a tie, live tanks prevail, in case - of further ties, a winner is chosen at random. This outputs the winner - to the winners file and outputs a results table html file.""" - tanks = list(self._tanks) - def winSort(t1, t2): - """Sort by # of kill first, then by life status.""" - result = cmp(len(kills[t1]), len(kills[t2])) - if result != 0: - return result - - if t1.isDead and not t2.isDead: - return -1 - elif not t1.isDead and t2.isDead: - return 1 - else: - return 0 - tanks.sort(winSort) - tanks.reverse() - - # Get the list of potential winners - winners = [] - for i in range(len(tanks)): - if len( kills[tanks[0]] ) == len( kills[tanks[i]] ) and \ - tanks[0].isDead == tanks[i].isDead: - winners.append(tanks[i]) - else: - break - winner = random.choice(winners) - return winner - - - def _outputErrors(self, tank): - """Output errors for each team.""" - - out = tank.program.get_output() - print 'Errors %r: %r' % (tank, out) - - if tank.name == None: - return - - fileName = os.path.join(self._errorDir, quote(tank.name, '')) - open(fileName, 'w').write(tank.program.get_output()) - - def _getNear(self): - """A dictionary of the set of tanks nearby each tank. Nearby is - defined as within the square centered the tank with side length equal - twice the sensor range. Only a few tanks within the set (those in the - corners of the square) should be outside the sensor range.""" - - self._tanksByX.sort(lambda t1, t2: cmp(t1.pos[0], t2.pos[0])) - self._tanksByY.sort(lambda t1, t2: cmp(t1.pos[1], t2.pos[1])) - - nearX = {} - nearY = {} - for tank in self._tanksByX: - nearX[tank] = set() - nearY[tank] = set() - - numTanks = len(self._tanksByX) - offset = 1 - for index in range(numTanks): - cTank = self._tanksByX[index] - maxRange = cTank.SENSOR_RANGE + cTank.RADIUS + 1 - near = set([cTank]) - for i in [(j + index) % numTanks for j in range(1, offset)]: - near.add(self._tanksByX[i]) - while offset < numTanks: - nTank = self._tanksByX[(index + offset) % numTanks] - if (index + offset >= numTanks and - self._board[0] + nTank.pos[0] - cTank.pos[0] < maxRange): - near.add(nTank) - offset = offset + 1 - elif (index + offset < numTanks and - nTank.pos[0] - cTank.pos[0] < maxRange ): - near.add(nTank) - offset = offset + 1 - else: - break - - if offset > 1: - offset = offset - 1 - - for tank in near: - nearX[tank] = nearX[tank].union(near) - - offset = 1 - for index in range(numTanks): - cTank = self._tanksByY[index] - maxRange = cTank.SENSOR_RANGE + cTank.RADIUS + 1 - near = set([cTank]) - for i in [(j + index) % numTanks for j in range(1, offset)]: - near.add(self._tanksByY[i]) - while offset < numTanks: - nTank = self._tanksByY[(index + offset) % numTanks] - if (index + offset < numTanks and - nTank.pos[1] - cTank.pos[1] < maxRange): - near.add(nTank) - offset = offset + 1 - elif (index + offset >= numTanks and - self._board[1] + nTank.pos[1] - cTank.pos[1] < maxRange): - near.add(nTank) - offset = offset + 1 - else: - break - - if offset > 1: - offset = offset - 1 - - for tank in near: - nearY[tank] = nearY[tank].union(near) - - near = {} - for tank in self._tanksByX: - near[tank] = nearX[tank].intersection(nearY[tank]) - near[tank].remove(tank) - - return near - - def _setupDirectories(self, dir): - """Setup all the directories needed by the game.""" - - if not os.path.exists(dir): - os.mkdir(dir) - - self._dir = dir - - # Don't run more than one game at the same time. - self._lockFile = open(os.path.join(dir, '.lock'), 'a') - try: - fcntl.flock(self._lockFile, fcntl.LOCK_EX|fcntl.LOCK_NB) - except: - sys.exit(1) - - # Setup all the directories we'll need. - self._resultsDir = os.path.join(dir, 'results') - self._errorDir = os.path.join(dir, 'errors') - self._playerDir = os.path.join(dir, 'players') - - def _getDefaultAIs(self, basedir): - """Load all the house bot AIs.""" - defaultAIs = [] - - path = os.path.join(basedir, 'house') - files = os.listdir(path) - for fn in files: - if fn.startswith('.'): - continue - - fn = os.path.join(path, fn) - f = open(fn) - defaultAIs.append(f.read()) - - return defaultAIs - - def _getGameNum(self): - """Figure out what game number this is from the past games played.""" - - games = os.listdir(self._resultsDir) - games.sort() - if games: - fn = games[-1] - s, _ = os.path.splitext(fn) - return int(s) + 1 - else: - return 0 - -if __name__ == '__main__': - import sys, traceback - try: - p = Pflanzarr(sys.argv[1]) - p.run(int(sys.argv[3])) - except: - traceback.print_exc() - print "Usage: Pflanzarr.py dataDirectory #turns" - - diff --git a/tanks/Program.py b/tanks/Program.py deleted file mode 100755 index cb2a824..0000000 --- a/tanks/Program.py +++ /dev/null @@ -1,100 +0,0 @@ -#! /usr/bin/python - -import forf -import random -import rfc822 -from cStringIO import StringIO -from math import pi - -def deg2rad(deg): - return float(deg) * pi / 180 - -def rad2deg(rad): - return int(rad * 180 / pi) - -class Environment(forf.Environment): - def __init__(self, tank, stdout): - forf.Environment.__init__(self) - self.tank = tank - self.stdout = stdout - - def err(self, msg): - self.stdout.write('Error: %s\n' % msg) - - def msg(self, msg): - self.stdout.write('%s\n' % msg) - - def cmd_random(self, data): - high = data.pop() - ret = random.randrange(high) - data.push(ret) - - def cmd_fireready(self, data): - ret = self.tank.fireReady() - data.push(ret) - - def cmd_sensoractive(self, data): - sensor = data.pop() - try: - ret = int(self.tank.getSensorState(sensor)) - except KeyError: - ret = 0 - data.push(ret) - - def cmd_getturret(self, data): - rad = self.tank.getTurretAngle() - deg = rad2deg(rad) - data.push(deg) - - def cmd_setled(self, data): - self.tank.setLED() - - def cmd_fire(self, data): - self.tank.setFire() - - def cmd_move(self, data): - right = data.pop() - left = data.pop() - self.tank.setMove(left, right) - - def cmd_setturret(self, data): - deg = data.pop() - rad = deg2rad(deg) - self.tank.setTurretAngle(rad) - - -class Program: - def __init__(self, tank, source): - self.tank = tank - self.stdout = StringIO() - self.env = Environment(self.tank, self.stdout) - - code_str = self.read_source(StringIO(source)) - self.env.parse_str(code_str) - - def get_output(self): - return self.stdout.getvalue() - - def read_source(self, f): - """Read in a tank program, establish sensors, and return code. - - Tank programs are stored as rfc822 messages. The header - block includes fields for sensors (Sensor:) - and other crap which may be used later. - """ - - message = rfc822.Message(f) - print 'reading tank %s' % message['Name'] - sensors = message.getallmatchingheaders('Sensor') - for s in sensors: - k, v = s.strip().split(':') - r, angle, width, turret = [int(p) for p in v.split()] - r = float(r) / 100 - angle = deg2rad(angle) - width = deg2rad(width) - self.tank.addSensor(r, angle, width, turret) - return message.fp.read() - - def run(self): - self.env.eval() - diff --git a/tanks/Tank.py b/tanks/Tank.py deleted file mode 100644 index 906f183..0000000 --- a/tanks/Tank.py +++ /dev/null @@ -1,426 +0,0 @@ -import math -import random -from sets import Set as set - -import GameMath as gm -import Program -from cStringIO import StringIO - -class Tank(object): - - # How often, in turns, that we can fire. - FIRE_RATE = 20 - # How far the laser shoots from the center of the tank - FIRE_RANGE = 45.0 - # The radius of the tank, from the center of the turret. - # This is what is used for collision and hit detection. - RADIUS = 7.5 - # Max speed, in pixels - MAXSPEED = 7.0 - # Max acceleration, as a fraction of speed. - MAXACCEL = 35 - # Sensor range, in pixels - SENSOR_RANGE = 90.0 - # Max turret turn rate, in radians - TURRET_TURN_RATE = math.pi/10 - - # The max number of sensors/timers/toggles - SENSOR_LIMIT = 10 - - def __init__(self, name, pos, color, boardSize, angle=None, tAngle=None): - """Create a new tank. -@param name: The name name of the tank. Stored in self.name. -@param pos: The starting position of the tank (x,y) -@param color: The color of the tank. -@param boardSize: The size of the board. (maxX, maxY) -@param angle: The starting angle of the tank, defaults to random. -@param tAngle: The starting turretAngle of the tank, defaults to random. - """ - - self.name = name - - assert len(pos) == 2 and pos[0] > 0 and pos[1] > 0, \ - 'Bad starting position: %s' % str(pos) - self.pos = pos - - # The last speed of each tread (left, right) - self._lastSpeed = 0.0, 0.0 - # The next speed that the tank should try to attain. - self._nextMove = 0,0 - - # When set, the led is drawn on the tank. - self.led = False - - assert len(boardSize) == 2 and boardSize[0] > 0 and boardSize[1] > 0 - # The limits of the playfield (maxX, maxY) - self._limits = boardSize - - # The current angle of the tank. - if angle is None: - self._angle = random.random()*2*math.pi - else: - self._angle = angle - - # The current angle of the turret - if tAngle is None: - self._tAngle = random.random()*2*math.pi - else: - self._tAngle = tAngle - - self.color = color - - # You can't fire until fireReady is 0. - self._fireReady = self.FIRE_RATE - # Means the tank will fire at it's next opportunity. - self._fireNow = False - # True when the tank has fired this turn (for drawing purposes) - self._fired = False - - # What the desired turret angle should be (from the front of the tank). - # None means the turret should stay stationary. - self._tGoal = None - - # Holds the properties of each sensor - self._sensors = [] - # Holds the state of each sensor - self._sensorState = [] - - # The tanks toggle memory - self.toggles = [] - - # The tanks timers - self._timers = [] - - # Is this tank dead? - self.isDead = False - - # Death reason - self.deathReason = 'survived' - - # Something to log to - self.stdout = StringIO() - - - def __repr__(self): - return '' % (self.name, self.pos[0], self.pos[1]) - - def fire(self, near): - """Shoots, if ordered to and able. Returns the set of tanks - destroyed.""" - - killed = set() - if self._fireReady > 0: - # Ignore the shoot order - self._fireNow = False - - if self._fireNow: - self._fireNow = False - self._fireReady = self.FIRE_RATE - self._fired = True - - - firePoint = gm.polar2cart(self.FIRE_RANGE, - self._angle + self._tAngle) - for tank in near: - enemyPos = gm.minShift(self.pos, tank.pos, self._limits) - if gm.segmentCircleCollision(((0,0), firePoint), enemyPos, - self.RADIUS): - killed.add(tank) - else: - self._fired = False - - return killed - - def addSensor(self, range, angle, width, attachedTurret=False): - """Add a sensor to this tank. -@param angle: The angle, from the tanks front and going clockwise, - of the center of the sensor. (radians) -@param width: The width of the sensor (radians). -@param range: The range of the sensor (percent) -@param attachedTurret: If set, the sensor moves with the turret. - """ - assert range >=0 and range <= 1, "Invalid range value." - - if len(self._sensors) >= self.SENSOR_LIMIT: - raise ValueError("You can only have 10 sensors.") - - range = range * self.SENSOR_RANGE - - if attachedTurret: - attachedTurret = True - else: - attachedTurret = False - - self._sensors.append((range, angle, width, attachedTurret)) - self._sensorState.append(False) - - def getSensorState(self, key): - return self._sensorState[key] - - def setMove(self, left, right): - """Parse the speed values given, and set them for the next move.""" - - self._nextMove = left, right - - def getTurretAngle(self): - return self._tAngle - - def setTurretAngle(self, angle=None): - """Set the desired angle of the turret. No angle means the turret - should remain stationary.""" - - if angle is None: - self._tGoal = None - else: - self._tGoal = gm.reduceAngle(angle) - - def setFire(self): - """Set the tank to fire, next turn.""" - self._fireNow = True - - def fireReady(self): - """Returns True if the tank can fire now.""" - return self._fireReady == 0 - - def setLED(self): - self.led = True - - def set_program(self, text): - """Set the program for this tank.""" - - self.program = Program.Program(self, text) - - def execute(self): - """Execute this tanks program.""" - - self.led = False - - self.program.run() - - self._move(self._nextMove[0], self._nextMove[1]) - self._moveTurret() - if self._fireReady > 0: - self._fireReady = self._fireReady - 1 - - def sense(self, near): - """Detect collisions and trigger sensors. Returns the set of - tanks collided with, plus this one. We do both these steps at once - mainly because all the data is available.""" - - near = list(near) - polar = [] - for tank in near: - polar.append(gm.relativePolar(self.pos, tank.pos, self._limits)) - - for sensorId in range(len(self._sensors)): - # Reset the sensor - self._sensorState[sensorId] = False - - dist, sensorAngle, width, tSens = self._sensors[sensorId] - - # Adjust the sensor angles according to the tanks angles. - sensorAngle = sensorAngle + self._angle - # If the angle is tied to the turret, add that too. - if tSens: - sensorAngle = sensorAngle + self._tAngle - - while sensorAngle >= 2*math.pi: - sensorAngle = sensorAngle - 2*math.pi - - for i in range(len(near)): - r, theta = polar[i] - # Find the difference between the object angle and the sensor. - # The max this can be is pi, so adjust for that. - dAngle = gm.angleDiff(theta, sensorAngle) - - rCoord = gm.polar2cart(dist, sensorAngle - width/2) - lCoord = gm.polar2cart(dist, sensorAngle + width/2) - rightLine = ((0,0), rCoord) - leftLine = ((0,0), lCoord) - tankRelPos = gm.minShift(self.pos, near[i].pos, self._limits) - if r < (dist + self.RADIUS): - if abs(dAngle) <= (width/2) or \ - gm.segmentCircleCollision(rightLine, tankRelPos, - self.RADIUS) or \ - gm.segmentCircleCollision(leftLine, tankRelPos, - self.RADIUS): - - self._sensorState[sensorId] = True - break - - # Check for collisions here, since we already have all the data. - collided = set() - for i in range(len(near)): - r, theta = polar[i] - if r < (self.RADIUS + near[i].RADIUS): - collided.add(near[i]) - - # Add this tank (a collision kills both, after all - if collided: - collided.add(self) - - return collided - - def die(self, reason): - self.isDead = True - self.deathReason = reason - - def _moveTurret(self): - if self._tGoal is None or self._tAngle == self._tGoal: - return - - diff = gm.angleDiff(self._tGoal, self._tAngle) - - if abs(diff) < self.TURRET_TURN_RATE: - self._tAngle = self._tGoal - elif diff > 0: - self._tAngle = gm.reduceAngle(self._tAngle - self.TURRET_TURN_RATE) - else: - self._tAngle = gm.reduceAngle(self._tAngle + self.TURRET_TURN_RATE) - - def _move(self, lSpeed, rSpeed): - - assert abs(lSpeed) <= 100, "Bad speed value: %s" % lSpeed - assert abs(rSpeed) <= 100, "Bad speed value: %s" % rSpeed - - # Handle acceleration - if self._lastSpeed[0] < lSpeed and \ - self._lastSpeed[0] + self.MAXACCEL < lSpeed: - lSpeed = self._lastSpeed[0] + self.MAXACCEL - elif self._lastSpeed[0] > lSpeed and \ - self._lastSpeed[0] - self.MAXACCEL > lSpeed: - lSpeed = self._lastSpeed[0] - self.MAXACCEL - - if self._lastSpeed[1] < rSpeed and \ - self._lastSpeed[1] + self.MAXACCEL < rSpeed: - rSpeed = self._lastSpeed[1] + self.MAXACCEL - elif self._lastSpeed[1] > rSpeed and \ - self._lastSpeed[1] - self.MAXACCEL > rSpeed: - rSpeed = self._lastSpeed[1] - self.MAXACCEL - - self._lastSpeed = lSpeed, rSpeed - - # The simple case - if lSpeed == rSpeed: - fSpeed = self.MAXSPEED*lSpeed/100 - x = fSpeed*math.cos(self._angle) - y = fSpeed*math.sin(self._angle) - # Adjust our position - self._reposition((x,y), 0) - return - - # The works as follows: - # The tank drives around in a circle of radius r, which is some - # offset on a line perpendicular to the tank. The distance it travels - # around the circle varies with the speed of each tread, and is - # such that each side of the tank moves an equal angle around the - # circle. - L = self.MAXSPEED * lSpeed/100.0 - R = self.MAXSPEED * rSpeed/100.0 - friction = .75 * abs(L-R)/(2.0*self.MAXSPEED) - L = L * (1 - friction) - R = R * (1 - friction) - - # Si is the speed of the tread on the inside of the turn, - # So is the speed on the outside of the turn. - # dir is to note the direction of rotation. - if abs(L) > abs(R): - Si = R; So = L - dir = 1 - else: - Si = L; So = R - dir = -1 - - # The width of the tank... - w = self.RADIUS * 2 - - # This is the angle that will determine the circle the tank travels - # around. -# theta = math.atan((So - Sl)/w) - # This is the distance from the outer tread to the center of the - # circle formed by it's movement. - r = w*So/(So - Si) - - # The fraction of the circle traveled is equal to the speed of - # the outer tread over the circumference of the circle. - # Ft = So/(2*pi*r) - # The angle traveled is equal to the Fraction traveled * 2 * pi - # This reduces to a simple: So/r - # We multiply it by dir to adjust for the direction of rotation - theta = So/r * dir - - # These are the offsets from the center of the circle, given that - # the tank is turned in some direction. The tank is facing - # perpendicular to the circle - # So far everything has been relative to the outer tread. At this - # point, however, we need to move relative to the center of the - # tank. Hence the adjustment in r. - x = -math.cos( self._angle + math.pi/2*dir ) * (r - w/2.0) - y = -math.sin( self._angle + math.pi/2*dir ) * (r - w/2.0) - - # Now we just rotate the tank's position around the center of the - # circle to get the change in coordinates. - mx, my = gm.rotatePoint((x,y), theta) - mx = mx - x - my = my - y - - # Finally, we shift the tank relative to the playing field, and - # rotate it by theta. - self._reposition((mx, my), theta) - - def _reposition(self, move, angleChange): - """Move the tank by x,y = move, and change it's angle by angle. - I assume the tanks move slower than the boardSize.""" - - x = self.pos[0] + move[0] - y = self.pos[1] + move[1] - self._angle = self._angle + angleChange - - if x < 0: - x = self._limits[0] + x - elif x > self._limits[0]: - x = x - self._limits[0] - - if y < 0: - y = self._limits[1] + y - elif y > self._limits[1]: - y = y - self._limits[1] - - self.pos = round(x), round(y) - - while self._angle < 0: - self._angle = self._angle + math.pi * 2 - - while self._angle > math.pi * 2: - self._angle = self._angle - math.pi * 2 - - def describe(self, f): - """Output a description of this tank""" - - f.write(' ["%s",[' % self.color) - for i in range(len(self._sensors)): - dist, sensorAngle, width, tSens = self._sensors[i] - - f.write('[%d,%.2f,%.2f,%d],' % (dist, sensorAngle, width, tSens)) - f.write(']],\n') - - def draw(self, f): - """Output this tank's state as JSON. - - [color, x, y, angle, turret_angle, led, fired] - - """ - - if self.isDead: - f.write(' 0,\n') - else: - flags = (self._fired << 0) | (self.led << 1) - sensors = 0 - for i in range(len(self._sensorState)): - sensors |= self._sensorState[i] << i - f.write(' [%d,%d,%.2f,%.2f,%d,%d],\n' % (self.pos[0], - self.pos[1], - self._angle, - self._tAngle, - flags, - sensors)) diff --git a/tanks/__init__.py b/tanks/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tanks/docs.py b/tanks/docs.py deleted file mode 100644 index 0318250..0000000 --- a/tanks/docs.py +++ /dev/null @@ -1,26 +0,0 @@ -import xml.sax.saxutils - -def mkDocTable(objects): - objects.sort(lambda o1, o2: cmp(o1.__doc__, o2.__doc__)) - - for object in objects: - if object.__doc__ is None: - print '
%s
Bad object
' % \ - xml.sax.saxutils.escape(str(object)) - continue - text = object.__doc__ - lines = text.split('\n') - head = lines[0].strip() - head = xml.sax.saxutils.escape(head) - - body = [] - for line in lines[1:]: - line = line.strip() #xml.sax.saxutils.escape( line.strip() ) - line = line.replace('.', '.
') - body.append(line) - - body = '\n'.join(body) - print '
%s
%s
' % (head, body) - #print '%sIntentionally blank%s' % (head, body) - - diff --git a/tanks/forf.py b/tanks/forf.py deleted file mode 100755 index e2c8b29..0000000 --- a/tanks/forf.py +++ /dev/null @@ -1,290 +0,0 @@ -#! /usr/bin/python - -"""A shitty FORTH interpreter - -15:58 WELCOME TO FORF! -15:58 *PUNCH* -""" - -import operator - -class ParseError(Exception): - pass - -class Overflow(Exception): - pass - -class Underflow(Exception): - pass - -class Stack: - def __init__(self, init=None, size=50): - self.size = size - self.stack = init or [] - - def __str__(self): - if not self.stack: - return '{}' - guts = ' '.join(repr(i) for i in self.stack) - return '{ %s }' % guts - __repr__ = __str__ - - def push(self, *values): - for val in values: - if len(self.stack) == self.size: - raise Overflow() - self.stack.append(val) - - def extend(self, other): - self.stack.extend(other.stack) - - def dup(self): - return Stack(init=self.stack[:], size=self.size) - - def pop(self): - if not self.stack: - raise Underflow() - return self.stack.pop() - - def mpop(self, n): - return [self.pop() for i in range(n)] - - def __nonzero__(self): - return bool(self.stack) - - -class Environment: - def __init__(self, ticks=2000, codelen=500): - self.ticks = ticks - self.codelen = codelen - self.registers = [0] * 10 - self.unfuncs = {'~' : operator.inv, - '!' : operator.not_, - 'abs': operator.abs, - } - self.binfuncs = {'+' : operator.add, - '-' : operator.sub, - '*' : operator.mul, - '/' : operator.div, - '%' : operator.mod, - '**': operator.pow, - '&' : operator.and_, - '|' : operator.or_, - '^' : operator.xor, - '<<': operator.lshift, - '>>': operator.rshift, - '>' : operator.gt, - '>=': operator.ge, - '<' : operator.lt, - '<=': operator.le, - '=' : operator.eq, - '<>': operator.ne, - '!=': operator.ne, - } - self.data = Stack() - - def get(self, s): - unfunc = self.unfuncs.get(s) - if unfunc: - return self.apply_unfunc(unfunc) - - binfunc = self.binfuncs.get(s) - if binfunc: - return self.apply_binfunc(binfunc) - - try: - return getattr(self, 'cmd_' + s) - except AttributeError: - return None - - def apply_unfunc(self, func): - """Apply a unary function""" - - def f(data): - a = data.pop() - data.push(int(func(a))) - return f - - def apply_binfunc(self, func): - """Apply a binary function""" - - def f(data): - a = data.pop() - b = data.pop() - data.push(int(func(b, a))) - return f - - def run(self, s): - self.parse_str(s) - self.eval() - - def parse_str(self, s): - tokens = s.strip().split() - tokens.reverse() # so .parse can tokens.pop() - self.code = self.parse(tokens) - - def parse(self, tokens, token=0, depth=0): - if depth > 4: - raise ParseError('Maximum recursion depth exceeded at token %d' % token) - code = [] - while tokens: - val = tokens.pop() - token += 1 - f = self.get(val) - if f: - code.append(f) - elif val == '(': - # Comment - while val != ')': - val = tokens.pop() - token += 1 - elif val == '{}': - # Empty block - code.append(Stack()) - elif val == '{': - block = self.parse(tokens, token, depth+1) - code.append(block) - elif val == '}': - break - else: - # The only literals we support are ints - try: - code.append(int(val)) - except ValueError: - raise ParseError('Invalid literal at token %d (%s)' % (token, val)) - if len(code) > self.codelen: - raise ParseError('Code stack overflow') - # Reverse so we can .pop() - code.reverse() - return Stack(code, size=self.codelen) - - def eval(self): - ticks = self.ticks - code_orig = self.code.dup() - while self.code and ticks: - ticks -= 1 - val = self.code.pop() - try: - if callable(val): - val(self.data) - else: - self.data.push(val) - except Underflow: - self.err('Stack underflow at proc %r' % (val)) - except Overflow: - self.err('Stack overflow at proc %r' % (val)) - if self.code: - self.err('Ran out of ticks!') - self.code = code_orig - - def err(self, msg): - print 'Error: %s' % msg - - def msg(self, msg): - print msg - - ## - ## Commands - ## - def cmd_print(self, data): - a = data.pop() - self.msg(a) - - def cmd_dumpstack(self, data): - a = data.pop() - self.msg('(dumpstack %d) %r' % (a, data.stack)) - - def cmd_dumpmem(self, data): - a = data.pop() - self.msg('(dumpmem %d) %r' % (a, self.registers)) - - def cmd_exch(self, data): - a, b = data.mpop(2) - data.push(a, b) - - def cmd_dup(self, data): - a = data.pop() - data.push(a, a) - - def cmd_pop(self, data): - data.pop() - - def cmd_store(self, data): - a, b = data.mpop(2) - self.registers[a % 10] = b - - def cmd_fetch(self, data): - a = data.pop() - data.push(self.registers[a % 10]) - - ## - ## Evaluation commands - ## - def eval_block(self, block): - try: - self.code.extend(block) - except TypeError: - # If it's not a block, just append it - self.code.push(block) - - def cmd_if(self, data): - block = data.pop() - cond = data.pop() - if cond: - self.eval_block(block) - - def cmd_ifelse(self, data): - elseblock = data.pop() - ifblock = data.pop() - cond = data.pop() - if cond: - self.eval_block(ifblock) - else: - self.eval_block(elseblock) - - def cmd_eval(self, data): - # Interestingly, this is the same as "1 exch if" - block = data.pop() - self.eval_block(block) - - def cmd_call(self, data): - # Shortcut for "fetch eval" - self.cmd_fetch(data) - self.cmd_eval(data) - - -def repl(): - env = Environment() - while True: - try: - s = raw_input('>8[= =] ') - except (KeyboardInterrupt, EOFError): - print - break - try: - env.run(s) - print env.data - except ParseError, err: - print r' \ nom nom nom, %s!' % err - print r' \ bye bye!' - -if __name__ == '__main__': - import sys - import time - try: - import readline - except ImportError: - pass - - if len(sys.argv) > 1: - s = open(sys.argv[1]).read() - env = Environment() - begin = time.time() - env.run(s) - end = time.time() - elapsed = end - begin - print 'Evaluated in %.2f seconds' % elapsed - else: - print 'WELCOME TO FORF!' - print '*PUNCH*' - repl() diff --git a/www/grunge.png b/www/grunge.png index 2b98730..23e55fb 100644 Binary files a/www/grunge.png and b/www/grunge.png differ diff --git a/www/register.cgi b/www/register.cgi index 6318ae9..bb0021c 100755 --- a/www/register.cgi +++ b/www/register.cgi @@ -1,63 +1,106 @@ -#! /usr/bin/python +#! /usr/bin/lua -import cgitb; cgitb.enable() -import cgi -import os -import fcntl -import string +function decode(str) + local hexdec = function(h) + return string.char(tonumber(h, 16)) + end + str = string.gsub(str, "+", " ") + return string.gsub(str, "%%(%x%x)", hexdec) +end -from ctf import teams, html +function decode_query(query) + local ret = {} + + for key, val in string.gfind(query, "([^&=]+)=([^&=]+)") do + ret[string.lower(decode(key))] = decode(val) + end + + return ret +end -def main(): - f = cgi.FieldStorage() +function escape(str) + str = string.gsub(str, "&", "&") + str = string.gsub(str, "<", "<") + str = string.gsub(str, ">", ">") + return str +end - team = f.getfirst('team', '') - pw = f.getfirst('pw') - confirm_pw = f.getfirst('confirm_pw') +function djbhash(s) + local hash = 5380 + for i=0,string.len(s) do + local c = string.byte(string.sub(s, i, i+1)) + hash = math.mod(((hash * 32) + hash + c), 2147483647) + end + return string.format("%08x", hash) +end - tmpl = string.Template(''' -

- Pick a short team name: you'll be typing it a lot. -

+function head(title) + print("Content-type: text/html") + print("") + print("") + print("") + print(" ") + print(" ") + print(title) + print(" </title") + print(' <link rel="stylesheet" href="ctf.css" type="text/css">') + print(" </head>") + print(" <body>") + print(" <h1>") + print(title) + print(" </h1>") +end - <form method="post" action="register.cgi"> - <fieldset> - <legend>Registration information:</legend> +function foot() + print(" </body>") + print("</html>") + os.exit() +end - <label>Team Name:</label> - <input type="text" name="team" /> - <span class="error">$team_error</span><br /> +if (os.getenv("REQUEST_METHOD") ~= "POST") then + print("405 Method not allowed") + print("Allow: POST") + print("Content-type: text/html") + print() + print("<h1>Method not allowed</h1>") + print("<p>I only speak POST. Sorry.</p>") +end - <label>Password:</label> - <input type="password" name="pw" /> - <br /> - <label>Confirm Password:</label> - <input type="password" name="confirm_pw" /> - <span class="error">$pw_match_error</span><br /> +inlen = tonumber(os.getenv("CONTENT_LENGTH")) +if (inlen > 200) then + head("Bad team name") + print("<p>That's a bit on the long side, don't you think?</p>") + foot() +end +formdata = io.read(inlen) +f = decode_query(formdata) - <input type="submit" value="Register" /> - </fieldset> - </form>''') +team = f["t"] +if (not team) or (team == "dirtbags") then + head("Bad team name") + print("<p>Go back and try again.</p>") + foot() +end +hash = djbhash(team) - if not (team and pw and confirm_pw): # If we're starting from the beginning? - body = tmpl.substitute(team_error='', - pw_match_error='') - elif teams.exists(team): - body = tmpl.substitute(team_error='Team team already taken', - pw_match_error='') - elif pw != confirm_pw: - body = tmpl.substitute(team_error='', - pw_match_error='Passwords do not match') - else: - teams.add(team, pw) - body = ('<p>Congratulations, <samp>%s</samp> is now registered. Go <a href="/">back to the front page</a> and start playing!</p>' % cgi.escape(team)) +if io.open(hash) then + head("Team name taken") + print("<p>Either someone's already using that team name,") + print("or you found a hash collision. Either way, you're") + print("going to have to pick something else.</p>") + foot() +end - html.serve('Team Registration', body) +f = io.open(hash, "w"):write(team) -if __name__ == '__main__': - import sys, codecs - - sys.stdout = codecs.getwriter('utf-8')(sys.stdout) - - main() +head("Team registered") +print("<p>Team name: <samp>") +print(escape(team)) +print("</samp></p>") +print("<p>Team token: <samp>") +print(hash) +print("</samp></p>") +print("<p><b>Save your team token somewhere</b>!") +print("You will need it to claim points.</p>") +foot() \ No newline at end of file diff --git a/www/register.html b/www/register.html new file mode 100644 index 0000000..b02848d --- /dev/null +++ b/www/register.html @@ -0,0 +1,16 @@ +<!DOCTYPE html> +<html> + <head> + <title>Team Registration + + + +

Team Registration

+
+ + + + +
+ +