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