paul@83 | 1 | #!/usr/bin/env python |
paul@83 | 2 | |
paul@83 | 3 | """ |
paul@83 | 4 | Convert and optimise images for display in an Acorn Electron MODE 1 variant |
paul@83 | 5 | with four colours per line but eight colours available for selection on each |
paul@83 | 6 | line. |
paul@83 | 7 | |
paul@83 | 8 | Copyright (C) 2015 Paul Boddie <paul@boddie.org.uk> |
paul@83 | 9 | |
paul@83 | 10 | This program is free software; you can redistribute it and/or modify it under |
paul@83 | 11 | the terms of the GNU General Public License as published by the Free Software |
paul@83 | 12 | Foundation; either version 3 of the License, or (at your option) any later |
paul@83 | 13 | version. |
paul@83 | 14 | |
paul@83 | 15 | This program is distributed in the hope that it will be useful, but WITHOUT ANY |
paul@83 | 16 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
paul@83 | 17 | PARTICULAR PURPOSE. See the GNU General Public License for more details. |
paul@83 | 18 | |
paul@83 | 19 | You should have received a copy of the GNU General Public License along |
paul@83 | 20 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@83 | 21 | """ |
paul@83 | 22 | |
paul@83 | 23 | from random import random, randrange |
paul@84 | 24 | import itertools |
paul@83 | 25 | |
paul@83 | 26 | corners = [ |
paul@83 | 27 | (0, 0, 0), (255, 0, 0), (0, 255, 0), (255, 255, 0), |
paul@83 | 28 | (0, 0, 255), (255, 0, 255), (0, 255, 255), (255, 255, 255) |
paul@83 | 29 | ] |
paul@83 | 30 | |
paul@83 | 31 | # Basic colour operations. |
paul@83 | 32 | |
paul@83 | 33 | def within(v, lower, upper): |
paul@83 | 34 | return min(max(v, lower), upper) |
paul@83 | 35 | |
paul@83 | 36 | def clip(v): |
paul@83 | 37 | return int(within(v, 0, 255)) |
paul@83 | 38 | |
paul@83 | 39 | def restore(srgb): |
paul@83 | 40 | r, g, b = srgb |
paul@83 | 41 | return int(r * 255.0), int(g * 255.0), int(b * 255.0) |
paul@83 | 42 | |
paul@83 | 43 | def scale(rgb): |
paul@83 | 44 | r, g, b = rgb |
paul@83 | 45 | return r / 255.0, g / 255.0, b / 255.0 |
paul@83 | 46 | |
paul@83 | 47 | def invert(srgb): |
paul@83 | 48 | r, g, b = srgb |
paul@83 | 49 | return 1.0 - r, 1.0 - g, 1.0 - b |
paul@83 | 50 | |
paul@83 | 51 | scaled_corners = map(scale, corners) |
paul@83 | 52 | zipped_corners = zip(corners, scaled_corners) |
paul@83 | 53 | |
paul@83 | 54 | # Colour distribution functions. |
paul@83 | 55 | |
paul@83 | 56 | def combination(rgb): |
paul@83 | 57 | |
paul@83 | 58 | "Return the colour distribution for 'rgb'." |
paul@83 | 59 | |
paul@83 | 60 | # Get the colour with components scaled from 0 to 1, plus the inverted |
paul@83 | 61 | # component values. |
paul@83 | 62 | |
paul@83 | 63 | srgb = scale(rgb) |
paul@83 | 64 | rgbi = invert(srgb) |
paul@83 | 65 | pairs = zip(rgbi, srgb) |
paul@83 | 66 | |
paul@83 | 67 | # For each corner of the colour cube (primary and secondary colours plus |
paul@83 | 68 | # black and white), calculate the corner value's contribution to the |
paul@83 | 69 | # input colour. |
paul@83 | 70 | |
paul@83 | 71 | d = [] |
paul@83 | 72 | for corner, scaled in zipped_corners: |
paul@83 | 73 | rs, gs, bs = scaled |
paul@83 | 74 | |
paul@83 | 75 | # Obtain inverted channel values where corner channels are low; |
paul@83 | 76 | # obtain original channel values where corner channels are high. |
paul@83 | 77 | |
paul@83 | 78 | d.append((pairs[0][int(rs)] * pairs[1][int(gs)] * pairs[2][int(bs)], corner)) |
paul@83 | 79 | |
paul@83 | 80 | # Balance the corner contributions. |
paul@83 | 81 | |
paul@83 | 82 | return balance(d) |
paul@83 | 83 | |
paul@83 | 84 | def complements(rgb): |
paul@83 | 85 | |
paul@83 | 86 | "Return 'rgb' and its complement." |
paul@83 | 87 | |
paul@83 | 88 | r, g, b = rgb |
paul@83 | 89 | return rgb, restore(invert(scale(rgb))) |
paul@83 | 90 | |
paul@83 | 91 | bases = [(0, 0, 0), (255, 0, 0), (0, 255, 0), (0, 0, 255)] |
paul@83 | 92 | base_complements = map(complements, bases) |
paul@83 | 93 | |
paul@83 | 94 | def balance(d): |
paul@83 | 95 | |
paul@83 | 96 | """ |
paul@83 | 97 | Balance distribution 'd', cancelling opposing values and their complements |
paul@83 | 98 | and replacing their common contributions with black and white contributions. |
paul@83 | 99 | """ |
paul@83 | 100 | |
paul@83 | 101 | dd = dict([(value, f) for f, value in d]) |
paul@83 | 102 | for primary, secondary in base_complements: |
paul@83 | 103 | common = min(dd[primary], dd[secondary]) |
paul@83 | 104 | dd[primary] -= common |
paul@83 | 105 | dd[secondary] -= common |
paul@83 | 106 | return [(f, value) for value, f in dd.items()] |
paul@83 | 107 | |
paul@83 | 108 | def combine(d): |
paul@83 | 109 | |
paul@83 | 110 | "Combine distribution 'd' to get a colour value." |
paul@83 | 111 | |
paul@83 | 112 | out = [0, 0, 0] |
paul@83 | 113 | for v, rgb in d: |
paul@83 | 114 | out[0] += v * rgb[0] |
paul@83 | 115 | out[1] += v * rgb[1] |
paul@83 | 116 | out[2] += v * rgb[2] |
paul@83 | 117 | return tuple(map(int, out)) |
paul@83 | 118 | |
paul@83 | 119 | def pattern(rgb, chosen=None): |
paul@83 | 120 | |
paul@83 | 121 | """ |
paul@83 | 122 | Obtain a sorted colour distribution for 'rgb', optionally limited to any |
paul@83 | 123 | specified 'chosen' colours. |
paul@83 | 124 | """ |
paul@83 | 125 | |
paul@83 | 126 | l = [(f, value) for f, value in combination(rgb) if not chosen or value in chosen] |
paul@83 | 127 | l.sort(reverse=True) |
paul@83 | 128 | return l |
paul@83 | 129 | |
paul@83 | 130 | def get_value(rgb, chosen=None, fail=False): |
paul@83 | 131 | |
paul@83 | 132 | """ |
paul@83 | 133 | Get an output colour for 'rgb', optionally limited to any specified 'chosen' |
paul@83 | 134 | colours. If 'fail' is set to a true value, return None if the colour cannot |
paul@83 | 135 | be expressed using any of the chosen colours. |
paul@83 | 136 | """ |
paul@83 | 137 | |
paul@83 | 138 | l = pattern(rgb, chosen) |
paul@83 | 139 | limit = sum([f for f, c in l]) |
paul@83 | 140 | if not limit: |
paul@83 | 141 | if fail: |
paul@83 | 142 | return None |
paul@83 | 143 | else: |
paul@83 | 144 | return l[randrange(0, len(l))][1] |
paul@83 | 145 | |
paul@83 | 146 | choose = random() * limit |
paul@83 | 147 | threshold = 0 |
paul@83 | 148 | for f, c in l: |
paul@83 | 149 | threshold += f |
paul@83 | 150 | if choose < threshold: |
paul@83 | 151 | return c |
paul@83 | 152 | return c |
paul@83 | 153 | |
paul@83 | 154 | # Colour processing operations. |
paul@83 | 155 | |
paul@83 | 156 | def sign(x): |
paul@83 | 157 | if x >= 0: |
paul@83 | 158 | return 1 |
paul@83 | 159 | else: |
paul@83 | 160 | return -1 |
paul@83 | 161 | |
paul@83 | 162 | def saturate_rgb(rgb, exp): |
paul@83 | 163 | r, g, b = rgb |
paul@83 | 164 | return saturate_value(r, exp), saturate_value(g, exp), saturate_value(b, exp) |
paul@83 | 165 | |
paul@83 | 166 | def saturate_value(x, exp): |
paul@83 | 167 | return int(127.5 + sign(x - 127.5) * 127.5 * pow(abs(x - 127.5) / 127.5, exp)) |
paul@83 | 168 | |
paul@83 | 169 | def amplify_rgb(rgb, exp): |
paul@83 | 170 | r, g, b = rgb |
paul@83 | 171 | return amplify_value(r, exp), amplify_value(g, exp), amplify_value(b, exp) |
paul@83 | 172 | |
paul@83 | 173 | def amplify_value(x, exp): |
paul@83 | 174 | return int(pow(x / 255.0, exp) * 255.0) |
paul@83 | 175 | |
paul@84 | 176 | # Image operations. |
paul@84 | 177 | |
paul@86 | 178 | def get_colours(im, y): |
paul@86 | 179 | |
paul@86 | 180 | "Get a colour distribution from image 'im' for the row 'y'." |
paul@86 | 181 | |
paul@86 | 182 | width, height = im.size |
paul@86 | 183 | c = {} |
paul@86 | 184 | for x in range(0, width): |
paul@86 | 185 | rgb = im.getpixel((x, y)) |
paul@86 | 186 | |
paul@86 | 187 | # Sum the colour probabilities. |
paul@86 | 188 | |
paul@86 | 189 | for f, value in combination(rgb): |
paul@86 | 190 | if not c.has_key(value): |
paul@86 | 191 | c[value] = f |
paul@86 | 192 | else: |
paul@86 | 193 | c[value] += f |
paul@86 | 194 | |
paul@86 | 195 | d = [(n/width, value) for value, n in c.items()] |
paul@86 | 196 | d.sort(reverse=True) |
paul@86 | 197 | return d |
paul@86 | 198 | |
paul@84 | 199 | def get_combinations(c, n): |
paul@84 | 200 | |
paul@84 | 201 | """ |
paul@84 | 202 | Get combinations of colours from 'c' of size 'n' in decreasing order of |
paul@84 | 203 | probability. |
paul@84 | 204 | """ |
paul@84 | 205 | |
paul@84 | 206 | all = [] |
paul@84 | 207 | for l in itertools.combinations(c, n): |
paul@84 | 208 | total = 0 |
paul@84 | 209 | for f, value in l: |
paul@84 | 210 | total += f |
paul@84 | 211 | all.append((total, l)) |
paul@84 | 212 | all.sort(reverse=True) |
paul@84 | 213 | return [l for total, l in all] |
paul@84 | 214 | |
paul@86 | 215 | def count_colours(im, colours): |
paul@86 | 216 | |
paul@86 | 217 | """ |
paul@86 | 218 | Count colours on each row of image 'im', returning a tuple indicating the |
paul@86 | 219 | first row with more than the given number of 'colours' together with the |
paul@86 | 220 | found colours; otherwise returning None. |
paul@86 | 221 | """ |
paul@86 | 222 | |
paul@86 | 223 | width, height = im.size |
paul@86 | 224 | |
paul@86 | 225 | for y in range(0, height): |
paul@86 | 226 | l = set() |
paul@86 | 227 | for x in range(0, width): |
paul@86 | 228 | l.add(im.getpixel((x, y))) |
paul@86 | 229 | if len(l) > colours: |
paul@86 | 230 | return (y, l) |
paul@86 | 231 | return None |
paul@86 | 232 | |
paul@87 | 233 | def process_image(im, saturate, desaturate, darken, brighten): |
paul@87 | 234 | |
paul@87 | 235 | """ |
paul@87 | 236 | Process image 'im' using the given options: 'saturate', 'desaturate', |
paul@87 | 237 | 'darken', 'brighten'. |
paul@87 | 238 | """ |
paul@87 | 239 | |
paul@87 | 240 | width, height = im.size |
paul@87 | 241 | |
paul@87 | 242 | if saturate or desaturate or darken or brighten: |
paul@87 | 243 | for y in range(0, height): |
paul@87 | 244 | for x in range(0, width): |
paul@87 | 245 | rgb = im.getpixel((x, y)) |
paul@87 | 246 | if saturate or desaturate: |
paul@99 | 247 | rgb = saturate_rgb(rgb, saturate and 0.5 / saturate or 2.0 * desaturate) |
paul@87 | 248 | if darken or brighten: |
paul@99 | 249 | rgb = amplify_rgb(rgb, brighten and 0.5 / brighten or 2.0 * darken) |
paul@87 | 250 | im.putpixel((x, y), rgb) |
paul@87 | 251 | |
paul@106 | 252 | def convert_image(im, colours): |
paul@91 | 253 | |
paul@91 | 254 | "Convert image 'im' to an appropriate output representation." |
paul@91 | 255 | |
paul@91 | 256 | width, height = im.size |
paul@91 | 257 | |
paul@91 | 258 | for y in range(0, height): |
paul@91 | 259 | c = get_colours(im, y) |
paul@91 | 260 | |
paul@106 | 261 | for l in get_combinations(c, colours): |
paul@91 | 262 | most = [value for f, value in l] |
paul@91 | 263 | for x in range(0, width): |
paul@91 | 264 | rgb = im.getpixel((x, y)) |
paul@91 | 265 | value = get_value(rgb, most, True) |
paul@91 | 266 | if value is None: |
paul@91 | 267 | break # try next combination |
paul@91 | 268 | else: |
paul@91 | 269 | break # use this combination |
paul@91 | 270 | else: |
paul@106 | 271 | most = [value for f, value in c[:colours]] # use the first colours |
paul@91 | 272 | |
paul@91 | 273 | for x in range(0, width): |
paul@91 | 274 | rgb = im.getpixel((x, y)) |
paul@91 | 275 | value = get_value(rgb, most) |
paul@91 | 276 | im.putpixel((x, y), value) |
paul@91 | 277 | |
paul@91 | 278 | if x < width - 1: |
paul@91 | 279 | rgbn = im.getpixel((x+1, y)) |
paul@91 | 280 | rgbn = ( |
paul@91 | 281 | clip(rgbn[0] + (rgb[0] - value[0]) / 4.0), |
paul@91 | 282 | clip(rgbn[1] + (rgb[1] - value[1]) / 4.0), |
paul@91 | 283 | clip(rgbn[2] + (rgb[2] - value[2]) / 4.0) |
paul@91 | 284 | ) |
paul@91 | 285 | im.putpixel((x+1, y), rgbn) |
paul@91 | 286 | |
paul@91 | 287 | if y < height - 1: |
paul@91 | 288 | rgbn = im.getpixel((x, y+1)) |
paul@91 | 289 | rgbn = ( |
paul@91 | 290 | clip(rgbn[0] + (rgb[0] - value[0]) / 2.0), |
paul@91 | 291 | clip(rgbn[1] + (rgb[1] - value[1]) / 2.0), |
paul@91 | 292 | clip(rgbn[2] + (rgb[2] - value[2]) / 2.0) |
paul@91 | 293 | ) |
paul@91 | 294 | im.putpixel((x, y+1), rgbn) |
paul@91 | 295 | |
paul@85 | 296 | class SimpleImage: |
paul@85 | 297 | |
paul@85 | 298 | "An image behaving like PIL.Image." |
paul@85 | 299 | |
paul@85 | 300 | def __init__(self, data, size): |
paul@85 | 301 | self.data = data |
paul@85 | 302 | self.width, self.height = self.size = size |
paul@85 | 303 | |
paul@85 | 304 | def copy(self): |
paul@85 | 305 | return SimpleImage(self.data[:], self.size) |
paul@85 | 306 | |
paul@85 | 307 | def getpixel(self, xy): |
paul@85 | 308 | x, y = xy |
paul@85 | 309 | return self.data[y * self.width + x] |
paul@85 | 310 | |
paul@85 | 311 | def putpixel(self, xy, value): |
paul@85 | 312 | x, y = xy |
paul@85 | 313 | self.data[y * self.width + x] = value |
paul@85 | 314 | |
paul@85 | 315 | def getdata(self): |
paul@85 | 316 | return self.data |
paul@85 | 317 | |
paul@83 | 318 | # Exercise functions for Shedskin. |
paul@83 | 319 | |
paul@83 | 320 | if __name__ == "__main__": |
paul@83 | 321 | rgb = (200, 100, 50) |
paul@83 | 322 | saturate_rgb(rgb, 1.0) |
paul@83 | 323 | amplify_rgb(rgb, 1.0) |
paul@83 | 324 | get_value(rgb) |
paul@83 | 325 | get_value(rgb, [(255, 255, 255), (255, 0, 0), (255, 255, 0), (0, 0, 0)]) |
paul@83 | 326 | combine([(1.0, (255, 0, 0)), (0.0, (0, 0, 0))]) |
paul@83 | 327 | clip(200.0) |
paul@84 | 328 | get_combinations([(0.5, (255, 0, 0)), (0.25, (255, 255, 0)), (0.25, (0, 0, 0))], 2) |
paul@83 | 329 | |
paul@85 | 330 | data = [(0, 0, 0), (0, 0, 0)] |
paul@85 | 331 | im = SimpleImage(data, (2, 1)) |
paul@85 | 332 | im2 = im.copy() |
paul@85 | 333 | im2.getpixel((0, 0)) == (0, 0, 0) |
paul@85 | 334 | im2.putpixel((0, 0), (255, 255, 255)) |
paul@85 | 335 | im2.getdata() == [(255, 255, 255), (0, 0, 0)] |
paul@85 | 336 | |
paul@86 | 337 | get_colours(im, 0) == [(1.0, (0, 0, 0))] |
paul@86 | 338 | count_colours(im, 4) |
paul@86 | 339 | |
paul@87 | 340 | process_image(im, 1.0, 0.0, 1.0, 0.0) |
paul@106 | 341 | convert_image(im, 4) |
paul@87 | 342 | |
paul@83 | 343 | # vim: tabstop=4 expandtab shiftwidth=4 |