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