# HG changeset patch # User Paul Boddie # Date 1444428354 -7200 # Node ID 92c6d7aa93436ea007001133246f440b2e1dc3e4 # Parent ba632fd74cbb14d16f59105e1cce956998300f1d Used more explicit coding practices to help Shedskin. diff -r ba632fd74cbb -r 92c6d7aa9343 optimiser.py --- a/optimiser.py Fri Oct 09 23:48:21 2015 +0200 +++ b/optimiser.py Sat Oct 10 00:05:54 2015 +0200 @@ -44,13 +44,16 @@ return math.sqrt(pow(r1 - r2, 2) + pow(g1 - g2, 2) + pow(b1 - b2, 2)) def restore(srgb): - return tuple(map(lambda x: int(x * 255.0), srgb)) + r, g, b = srgb + return int(r * 255.0), int(g * 255.0), int(b * 255.0) def scale(rgb): - return tuple(map(lambda x: x / 255.0, rgb)) + r, g, b = rgb + return r / 255.0, g / 255.0, b / 255.0 def invert(srgb): - return tuple(map(lambda x: 1.0 - x, srgb)) + r, g, b = srgb + return 1.0 - r, 1.0 - g, 1.0 - b # Colour distribution functions. @@ -96,12 +99,18 @@ and replacing their common contributions with black and white contributions. """ - d = dict([(value, f) for f, value in d]) - for primary, secondary in map(complements, [(0, 0, 0), (255, 0, 0), (0, 255, 0), (0, 0, 255)]): - common = min(d[primary], d[secondary]) - d[primary] -= common - d[secondary] -= common - return [(f, value) for value, f in d.items()] + dd = {} + for f, value in d: + dd[value] = f + for colour in [(0, 0, 0), (255, 0, 0), (0, 255, 0), (0, 0, 255)]: + primary, secondary = complements(colour) + common = min(dd[primary], dd[secondary]) + dd[primary] -= common + dd[secondary] -= common + d = [] + for value, f in dd.items(): + d.append((f, value)) + return d def combine(d): @@ -121,7 +130,10 @@ specified 'chosen' colours. """ - l = [(f, value) for f, value in combination(rgb) if not chosen or value in chosen] + l = [] + for f, value in combination(rgb): + if not chosen or value in chosen: + l.append((f, value)) l.sort(reverse=True) return l @@ -155,13 +167,15 @@ return x >= 0 and 1 or -1 def saturate_rgb(rgb, exp): - return tuple([saturate_value(x, exp) for x in rgb]) + r, g, b = rgb + return saturate_value(r, exp), saturate_value(g, exp), saturate_value(b, exp) def saturate_value(x, exp): return int(127.5 + sign(x - 127.5) * 127.5 * pow(abs(x - 127.5) / 127.5, exp)) def amplify_rgb(rgb, exp): - return tuple([amplify_value(x, exp) for x in rgb]) + r, g, b = rgb + return amplify_value(r, exp), amplify_value(g, exp), amplify_value(b, exp) def amplify_value(x, exp): return int(pow(x / 255.0, exp) * 255.0) @@ -185,9 +199,11 @@ else: c[value] += f - c = [(n/width, value) for value, n in c.items()] - c.sort(reverse=True) - return c + d = [] + for value, n in c.items(): + d.append((n/width, value)) + d.sort(reverse=True) + return d def get_combinations(c, n): @@ -203,7 +219,10 @@ total += f all.append((total, l)) all.sort(reverse=True) - return [l for total, l in all] + cc = [] + for total, l in all: + cc.append(l) + return cc def test_slice(im, size, r): for g in range(0, size): @@ -298,12 +317,20 @@ if x < width - 1: rgbn = im.getpixel((x+1, y)) - rgbn = tuple(map(lambda i: clip(i[0] + (i[1] - i[2]) / 4.0), zip(rgbn, rgb, value))) + rgbn = ( + clip(rgbn[0] + (rgb[0] - value[0]) / 4.0), + clip(rgbn[1] + (rgb[1] - value[1]) / 4.0), + clip(rgbn[2] + (rgb[2] - value[2]) / 4.0) + ) im.putpixel((x+1, y), rgbn) if y < height - 1: rgbn = im.getpixel((x, y+1)) - rgbn = tuple(map(lambda i: clip(i[0] + (i[1] - i[2]) / 2.0), zip(rgbn, rgb, value))) + rgbn = ( + clip(rgbn[0] + (rgb[0] - value[0]) / 2.0), + clip(rgbn[1] + (rgb[1] - value[1]) / 2.0), + clip(rgbn[2] + (rgb[2] - value[2]) / 2.0) + ) im.putpixel((x, y+1), rgbn) class SimpleImage: