PaletteOptimiser

Changeset

78:9c39e4cb3ecd
2015-10-10 Paul Boddie raw files shortlog changelog graph Merged channel operation performance improvements. simpleimage
optimiser.py (file)
     1.1 --- a/optimiser.py	Sat Oct 10 14:41:25 2015 +0200
     1.2 +++ b/optimiser.py	Sat Oct 10 14:58:40 2015 +0200
     1.3 @@ -47,13 +47,16 @@
     1.4      return math.sqrt(pow(r1 - r2, 2) + pow(g1 - g2, 2) + pow(b1 - b2, 2))
     1.5  
     1.6  def restore(srgb):
     1.7 -    return tuple(map(lambda x: int(x * 255.0), srgb))
     1.8 +    r, g, b = srgb
     1.9 +    return int(r * 255.0), int(g * 255.0), int(b * 255.0)
    1.10  
    1.11  def scale(rgb):
    1.12 -    return tuple(map(lambda x: x / 255.0, rgb))
    1.13 +    r, g, b = rgb
    1.14 +    return r / 255.0, g / 255.0, b / 255.0
    1.15  
    1.16  def invert(srgb):
    1.17 -    return tuple(map(lambda x: 1.0 - x, srgb))
    1.18 +    r, g, b = srgb
    1.19 +    return 1.0 - r, 1.0 - g, 1.0 - b
    1.20  
    1.21  # Colour distribution functions.
    1.22  
    1.23 @@ -158,13 +161,15 @@
    1.24      return x >= 0 and 1 or -1
    1.25  
    1.26  def saturate_rgb(rgb, exp):
    1.27 -    return tuple([saturate_value(x, exp) for x in rgb])
    1.28 +    r, g, b = rgb
    1.29 +    return saturate_value(r, exp), saturate_value(g, exp), saturate_value(b, exp)
    1.30  
    1.31  def saturate_value(x, exp):
    1.32      return int(127.5 + sign(x - 127.5) * 127.5 * pow(abs(x - 127.5) / 127.5, exp))
    1.33  
    1.34  def amplify_rgb(rgb, exp):
    1.35 -    return tuple([amplify_value(x, exp) for x in rgb])
    1.36 +    r, g, b = rgb
    1.37 +    return amplify_value(r, exp), amplify_value(g, exp), amplify_value(b, exp)
    1.38  
    1.39  def amplify_value(x, exp):
    1.40      return int(pow(x / 255.0, exp) * 255.0)
    1.41 @@ -341,12 +346,20 @@
    1.42  
    1.43              if x < width - 1:
    1.44                  rgbn = im.getpixel((x+1, y))
    1.45 -                rgbn = tuple(map(lambda i: clip(i[0] + (i[1] - i[2]) / 4.0), zip(rgbn, rgb, value)))
    1.46 +                rgbn = (
    1.47 +                    clip(rgbn[0] + (rgb[0] - value[0]) / 4.0),
    1.48 +                    clip(rgbn[1] + (rgb[1] - value[1]) / 4.0),
    1.49 +                    clip(rgbn[2] + (rgb[2] - value[2]) / 4.0)
    1.50 +                    )
    1.51                  im.putpixel((x+1, y), rgbn)
    1.52  
    1.53              if y < height - 1:
    1.54                  rgbn = im.getpixel((x, y+1))
    1.55 -                rgbn = tuple(map(lambda i: clip(i[0] + (i[1] - i[2]) / 2.0), zip(rgbn, rgb, value)))
    1.56 +                rgbn = (
    1.57 +                    clip(rgbn[0] + (rgb[0] - value[0]) / 2.0),
    1.58 +                    clip(rgbn[1] + (rgb[1] - value[1]) / 2.0),
    1.59 +                    clip(rgbn[2] + (rgb[2] - value[2]) / 2.0)
    1.60 +                    )
    1.61                  im.putpixel((x, y+1), rgbn)
    1.62  
    1.63      pim.putdata(im.getdata())