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