# HG changeset patch # User Paul Boddie # Date 1444434355 -7200 # Node ID 18b62cd6bdac847ffff67f26a0c6d4ed7fd05c61 # Parent f789f95ee65637b7660a60a98832a258ffe77af6 Re-expressed various things to try and help Shedskin even further. diff -r f789f95ee656 -r 18b62cd6bdac main.py --- a/main.py Sat Oct 10 00:14:25 2015 +0200 +++ b/main.py Sat Oct 10 01:45:55 2015 +0200 @@ -166,9 +166,10 @@ pim = PIL.Image.open(output_filename).convert("RGB") im = SimpleImage(list(pim.getdata()), pim.size) - result = count_colours(im, 4) - if result is not None: - y, colours = result + try: + count_colours(im, 4) + except ValueError, exc: + y, colours = exc.args print "Image %s: row %d has the following colours: %s" % (output_filename, y, "; ".join([repr(c) for c in colours])) # vim: tabstop=4 expandtab shiftwidth=4 diff -r f789f95ee656 -r 18b62cd6bdac optimiser.py --- a/optimiser.py Sat Oct 10 00:14:25 2015 +0200 +++ b/optimiser.py Sat Oct 10 01:45:55 2015 +0200 @@ -36,24 +36,16 @@ return min(max(v, lower), upper) def clip(v): - return int(within(v, 0, 255)) - -def distance(rgb1, rgb2): - r1, g1, b1 = rgb1 - r2, g2, b2 = rgb2 - return math.sqrt(pow(r1 - r2, 2) + pow(g1 - g2, 2) + pow(b1 - b2, 2)) + return int(within(v, 0.0, 255.0)) def restore(srgb): - r, g, b = srgb - return int(r * 255.0), int(g * 255.0), int(b * 255.0) + return int(srgb[0] * 255.0), int(srgb[1] * 255.0), int(srgb[2] * 255.0) def scale(rgb): - r, g, b = rgb - return r / 255.0, g / 255.0, b / 255.0 + return float(rgb[0]) / 255.0, float(rgb[1]) / 255.0, float(rgb[2]) / 255.0 def invert(srgb): - r, g, b = srgb - return 1.0 - r, 1.0 - g, 1.0 - b + return 1.0 - srgb[0], 1.0 - srgb[1], 1.0 - srgb[2] # Colour distribution functions. @@ -64,9 +56,11 @@ # Get the colour with components scaled from 0 to 1, plus the inverted # component values. - rgb = scale(rgb) - rgbi = invert(rgb) - pairs = zip(rgbi, rgb) + srgb = scale(rgb) + rgbi = invert(srgb) + rc = (rgbi[0], srgb[0]) + gc = (rgbi[1], srgb[1]) + bc = (rgbi[2], srgb[2]) # For each corner of the colour cube (primary and secondary colours plus # black and white), calculate the corner value's contribution to the @@ -74,12 +68,15 @@ d = [] for corner in corners: - rs, gs, bs = scale(corner) + crgb = scale(corner) + rs, gs, bs = crgb + ri, gi, bi = int(rs), int(gs), int(bs) # Obtain inverted channel values where corner channels are low; # obtain original channel values where corner channels are high. - d.append((pairs[0][int(rs)] * pairs[1][int(gs)] * pairs[2][int(bs)], corner)) + f = rc[ri] * gc[gi] * bc[bi] + d.append((f, corner)) # Balance the corner contributions. @@ -89,7 +86,6 @@ "Return 'rgb' and its complement." - r, g, b = rgb return rgb, restore(invert(scale(rgb))) def balance(d): @@ -121,7 +117,7 @@ out[0] += v * rgb[0] out[1] += v * rgb[1] out[2] += v * rgb[2] - return out + return int(out[0]), int(out[1]), int(out[2]) def pattern(rgb, chosen=None): @@ -170,15 +166,13 @@ return x >= 0 and 1 or -1 def saturate_rgb(rgb, exp): - r, g, b = rgb - return saturate_value(r, exp), saturate_value(g, exp), saturate_value(b, exp) + return saturate_value(rgb[0], exp), saturate_value(rgb[1], exp), saturate_value(rgb[2], 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): - r, g, b = rgb - return amplify_value(r, exp), amplify_value(g, exp), amplify_value(b, exp) + return amplify_value(rgb[0], exp), amplify_value(rgb[1], exp), amplify_value(rgb[2], exp) def amplify_value(x, exp): return int(pow(x / 255.0, exp) * 255.0) @@ -251,8 +245,7 @@ for y in range(0, height): l = set(im.getdata()[y * width:(y+1) * width]) if len(l) > colours: - return (y, l) - return None + raise ValueError(y, l) def process_image(im, saturate, desaturate, darken, brighten): @@ -329,21 +322,19 @@ if x < width - 1: rgbn = im.getpixel((x+1, y)) - rgbn = ( + im.putpixel((x+1, y), ( 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 = ( + im.putpixel((x, y+1), ( 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: @@ -370,14 +361,16 @@ # Test program. if __name__ == "__main__": - data = [(0, 0, 0)] * 1024 - size = (32, 32) + data = [(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)] + size = (2, 2) im = SimpleImage(data, size) process_image(im, 1.0, 0.0, 1.0, 0.0) imp = preview_image(im, False) convert_image(im) + count_colours(im, 4) + im.getdata() test_im = SimpleImage(data, size) test_slice(test_im, 32, 0) @@ -385,4 +378,7 @@ test_flat_im = SimpleImage(data, size) test_flat_slice(test_flat_im, 32, (200, 100, 50)) + rgb = (200, 150, 100) + combine(pattern(rgb)) == rgb + # vim: tabstop=4 expandtab shiftwidth=4