# HG changeset patch # User Paul Boddie # Date 1444493587 -7200 # Node ID 4e37f510ccfd9e67777435140b713dec87a56ee3 # Parent be170b4cfcde3c59c95db456707050402e816344 Moved image conversion to the extension module. diff -r be170b4cfcde -r 4e37f510ccfd optimiser.py --- a/optimiser.py Sat Oct 10 16:48:46 2015 +0200 +++ b/optimiser.py Sat Oct 10 18:13:07 2015 +0200 @@ -72,53 +72,6 @@ return im.resize((width, height)) -def convert_image(pim): - - "Convert image 'pim' to an appropriate output representation." - - width, height = pim.size - im = SimpleImage(list(pim.getdata()), pim.size) - - for y in range(0, height): - c = get_colours(im, y) - - for l in get_combinations(c, 4): - most = [value for f, value in l] - for x in range(0, width): - rgb = im.getpixel((x, y)) - value = get_value(rgb, most, True) - if value is None: - break # try next combination - else: - break # use this combination - else: - most = [value for f, value in c[:4]] # use the first four - - for x in range(0, width): - rgb = im.getpixel((x, y)) - value = get_value(rgb, most) - im.putpixel((x, y), value) - - if x < width - 1: - rgbn = im.getpixel((x+1, y)) - 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 = ( - 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) - - pim.putdata(im.getdata()) - def get_float(options, flag): try: i = options.index(flag) @@ -208,7 +161,9 @@ # Generate an output image if requested. if make_image: - convert_image(im) + sim = SimpleImage(list(im.getdata()), im.size) + convert_image(sim) + im.putdata(sim.getdata()) im.save(output_filename) # Verify the output image (which may be loaded) if requested. diff -r be170b4cfcde -r 4e37f510ccfd optimiserlib.py --- a/optimiserlib.py Sat Oct 10 16:48:46 2015 +0200 +++ b/optimiserlib.py Sat Oct 10 18:13:07 2015 +0200 @@ -270,6 +270,50 @@ return imp +def convert_image(im): + + "Convert image 'im' to an appropriate output representation." + + width, height = im.size + + for y in range(0, height): + c = get_colours(im, y) + + for l in get_combinations(c, 4): + most = [value for f, value in l] + for x in range(0, width): + rgb = im.getpixel((x, y)) + value = get_value(rgb, most, True) + if value is None: + break # try next combination + else: + break # use this combination + else: + most = [value for f, value in c[:4]] # use the first four + + for x in range(0, width): + rgb = im.getpixel((x, y)) + value = get_value(rgb, most) + im.putpixel((x, y), value) + + if x < width - 1: + rgbn = im.getpixel((x+1, y)) + 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 = ( + 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: "An image behaving like PIL.Image." @@ -316,5 +360,6 @@ process_image(im, 1.0, 0.0, 1.0, 0.0) preview_image(im, False) + convert_image(im) # vim: tabstop=4 expandtab shiftwidth=4