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