PaletteOptimiser

Changeset

74:18b62cd6bdac
2015-10-10 Paul Boddie raw files shortlog changelog graph Re-expressed various things to try and help Shedskin even further. shedskin
main.py (file) optimiser.py (file)
     1.1 --- a/main.py	Sat Oct 10 00:14:25 2015 +0200
     1.2 +++ b/main.py	Sat Oct 10 01:45:55 2015 +0200
     1.3 @@ -166,9 +166,10 @@
     1.4              pim = PIL.Image.open(output_filename).convert("RGB")
     1.5              im = SimpleImage(list(pim.getdata()), pim.size)
     1.6  
     1.7 -        result = count_colours(im, 4)
     1.8 -        if result is not None:
     1.9 -            y, colours = result
    1.10 +        try:
    1.11 +            count_colours(im, 4)
    1.12 +        except ValueError, exc:
    1.13 +            y, colours = exc.args
    1.14              print "Image %s: row %d has the following colours: %s" % (output_filename, y, "; ".join([repr(c) for c in colours]))
    1.15  
    1.16  # vim: tabstop=4 expandtab shiftwidth=4
     2.1 --- a/optimiser.py	Sat Oct 10 00:14:25 2015 +0200
     2.2 +++ b/optimiser.py	Sat Oct 10 01:45:55 2015 +0200
     2.3 @@ -36,24 +36,16 @@
     2.4      return min(max(v, lower), upper)
     2.5  
     2.6  def clip(v):
     2.7 -    return int(within(v, 0, 255))
     2.8 -
     2.9 -def distance(rgb1, rgb2):
    2.10 -    r1, g1, b1 = rgb1
    2.11 -    r2, g2, b2 = rgb2
    2.12 -    return math.sqrt(pow(r1 - r2, 2) + pow(g1 - g2, 2) + pow(b1 - b2, 2))
    2.13 +    return int(within(v, 0.0, 255.0))
    2.14  
    2.15  def restore(srgb):
    2.16 -    r, g, b = srgb
    2.17 -    return int(r * 255.0), int(g * 255.0), int(b * 255.0)
    2.18 +    return int(srgb[0] * 255.0), int(srgb[1] * 255.0), int(srgb[2] * 255.0)
    2.19  
    2.20  def scale(rgb):
    2.21 -    r, g, b = rgb
    2.22 -    return r / 255.0, g / 255.0, b / 255.0
    2.23 +    return float(rgb[0]) / 255.0, float(rgb[1]) / 255.0, float(rgb[2]) / 255.0
    2.24  
    2.25  def invert(srgb):
    2.26 -    r, g, b = srgb
    2.27 -    return 1.0 - r, 1.0 - g, 1.0 - b
    2.28 +    return 1.0 - srgb[0], 1.0 - srgb[1], 1.0 - srgb[2]
    2.29  
    2.30  # Colour distribution functions.
    2.31  
    2.32 @@ -64,9 +56,11 @@
    2.33      # Get the colour with components scaled from 0 to 1, plus the inverted
    2.34      # component values.
    2.35  
    2.36 -    rgb = scale(rgb)
    2.37 -    rgbi = invert(rgb)
    2.38 -    pairs = zip(rgbi, rgb)
    2.39 +    srgb = scale(rgb)
    2.40 +    rgbi = invert(srgb)
    2.41 +    rc = (rgbi[0], srgb[0])
    2.42 +    gc = (rgbi[1], srgb[1])
    2.43 +    bc = (rgbi[2], srgb[2])
    2.44  
    2.45      # For each corner of the colour cube (primary and secondary colours plus
    2.46      # black and white), calculate the corner value's contribution to the
    2.47 @@ -74,12 +68,15 @@
    2.48  
    2.49      d = []
    2.50      for corner in corners:
    2.51 -        rs, gs, bs = scale(corner)
    2.52 +        crgb = scale(corner)
    2.53 +        rs, gs, bs = crgb
    2.54 +        ri, gi, bi = int(rs), int(gs), int(bs)
    2.55  
    2.56          # Obtain inverted channel values where corner channels are low;
    2.57          # obtain original channel values where corner channels are high.
    2.58  
    2.59 -        d.append((pairs[0][int(rs)] * pairs[1][int(gs)] * pairs[2][int(bs)], corner))
    2.60 +        f = rc[ri] * gc[gi] * bc[bi]
    2.61 +        d.append((f, corner))
    2.62  
    2.63      # Balance the corner contributions.
    2.64  
    2.65 @@ -89,7 +86,6 @@
    2.66  
    2.67      "Return 'rgb' and its complement."
    2.68  
    2.69 -    r, g, b = rgb
    2.70      return rgb, restore(invert(scale(rgb)))
    2.71  
    2.72  def balance(d):
    2.73 @@ -121,7 +117,7 @@
    2.74          out[0] += v * rgb[0]
    2.75          out[1] += v * rgb[1]
    2.76          out[2] += v * rgb[2]
    2.77 -    return out
    2.78 +    return int(out[0]), int(out[1]), int(out[2])
    2.79  
    2.80  def pattern(rgb, chosen=None):
    2.81  
    2.82 @@ -170,15 +166,13 @@
    2.83      return x >= 0 and 1 or -1
    2.84  
    2.85  def saturate_rgb(rgb, exp):
    2.86 -    r, g, b = rgb
    2.87 -    return saturate_value(r, exp), saturate_value(g, exp), saturate_value(b, exp)
    2.88 +    return saturate_value(rgb[0], exp), saturate_value(rgb[1], exp), saturate_value(rgb[2], exp)
    2.89  
    2.90  def saturate_value(x, exp):
    2.91      return int(127.5 + sign(x - 127.5) * 127.5 * pow(abs(x - 127.5) / 127.5, exp))
    2.92  
    2.93  def amplify_rgb(rgb, exp):
    2.94 -    r, g, b = rgb
    2.95 -    return amplify_value(r, exp), amplify_value(g, exp), amplify_value(b, exp)
    2.96 +    return amplify_value(rgb[0], exp), amplify_value(rgb[1], exp), amplify_value(rgb[2], exp)
    2.97  
    2.98  def amplify_value(x, exp):
    2.99      return int(pow(x / 255.0, exp) * 255.0)
   2.100 @@ -251,8 +245,7 @@
   2.101      for y in range(0, height):
   2.102          l = set(im.getdata()[y * width:(y+1) * width])
   2.103          if len(l) > colours:
   2.104 -            return (y, l)
   2.105 -    return None
   2.106 +            raise ValueError(y, l)
   2.107  
   2.108  def process_image(im, saturate, desaturate, darken, brighten):
   2.109  
   2.110 @@ -329,21 +322,19 @@
   2.111  
   2.112              if x < width - 1:
   2.113                  rgbn = im.getpixel((x+1, y))
   2.114 -                rgbn = (
   2.115 +                im.putpixel((x+1, y), (
   2.116                      clip(rgbn[0] + (rgb[0] - value[0]) / 4.0),
   2.117                      clip(rgbn[1] + (rgb[1] - value[1]) / 4.0),
   2.118                      clip(rgbn[2] + (rgb[2] - value[2]) / 4.0)
   2.119 -                    )
   2.120 -                im.putpixel((x+1, y), rgbn)
   2.121 +                    ))
   2.122  
   2.123              if y < height - 1:
   2.124                  rgbn = im.getpixel((x, y+1))
   2.125 -                rgbn = (
   2.126 +                im.putpixel((x, y+1), (
   2.127                      clip(rgbn[0] + (rgb[0] - value[0]) / 2.0),
   2.128                      clip(rgbn[1] + (rgb[1] - value[1]) / 2.0),
   2.129                      clip(rgbn[2] + (rgb[2] - value[2]) / 2.0)
   2.130 -                    )
   2.131 -                im.putpixel((x, y+1), rgbn)
   2.132 +                    ))
   2.133  
   2.134  class SimpleImage:
   2.135  
   2.136 @@ -370,14 +361,16 @@
   2.137  # Test program.
   2.138  
   2.139  if __name__ == "__main__":
   2.140 -    data = [(0, 0, 0)] * 1024
   2.141 -    size = (32, 32)
   2.142 +    data = [(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)]
   2.143 +    size = (2, 2)
   2.144  
   2.145      im = SimpleImage(data, size)
   2.146  
   2.147      process_image(im, 1.0, 0.0, 1.0, 0.0)
   2.148      imp = preview_image(im, False)
   2.149      convert_image(im)
   2.150 +    count_colours(im, 4)
   2.151 +    im.getdata()
   2.152  
   2.153      test_im = SimpleImage(data, size)
   2.154      test_slice(test_im, 32, 0)
   2.155 @@ -385,4 +378,7 @@
   2.156      test_flat_im = SimpleImage(data, size)
   2.157      test_flat_slice(test_flat_im, 32, (200, 100, 50))
   2.158  
   2.159 +    rgb = (200, 150, 100)
   2.160 +    combine(pattern(rgb)) == rgb
   2.161 +
   2.162  # vim: tabstop=4 expandtab shiftwidth=4