| 1 | #!/usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | import math, gd, random |
|---|
| 4 | |
|---|
| 5 | # Tiny image class to make examples short and readable |
|---|
| 6 | class Image(gd.image): |
|---|
| 7 | def __new__(args, truecolor = False): |
|---|
| 8 | gd.gdMaxColors = 256 * 256 * 256 |
|---|
| 9 | if truecolor: |
|---|
| 10 | return gd.image.__new__(args, True) |
|---|
| 11 | else: |
|---|
| 12 | return gd.image.__new__(args) |
|---|
| 13 | def save(self, name): |
|---|
| 14 | print " * saving %s" % (name,) |
|---|
| 15 | self.writePng(name) |
|---|
| 16 | def getGray(self, x, y): |
|---|
| 17 | p = self.getPixel((x, y)) |
|---|
| 18 | c = self.colorComponents(p)[0] / 255.0 |
|---|
| 19 | return c |
|---|
| 20 | def getRgb(self, x, y): |
|---|
| 21 | p = self.getPixel((x, y)) |
|---|
| 22 | rgb = self.colorComponents(p) |
|---|
| 23 | return [rgb[0] / 255.0, rgb[1] / 255.0, rgb[2] / 255.0] |
|---|
| 24 | def setGray(self, x, y, t): |
|---|
| 25 | p = (int)(t * 255.999) |
|---|
| 26 | c = self.colorResolve((p, p, p)) |
|---|
| 27 | self.setPixel((x, y), c) |
|---|
| 28 | def setRgb(self, x, y, r, g, b): |
|---|
| 29 | r = (int)(r * 255.999) |
|---|
| 30 | g = (int)(g * 255.999) |
|---|
| 31 | b = (int)(b * 255.999) |
|---|
| 32 | c = self.colorResolve((r, g, b)) |
|---|
| 33 | self.setPixel((x, y), c) |
|---|
| 34 | |
|---|
| 35 | ############################################################################## |
|---|
| 36 | print "Initialisation" |
|---|
| 37 | |
|---|
| 38 | # Manipulate gamma values |
|---|
| 39 | class Gamma: |
|---|
| 40 | def CtoI(x): |
|---|
| 41 | return math.pow(x, 2.2) |
|---|
| 42 | def ItoC(x): |
|---|
| 43 | if x < 0: |
|---|
| 44 | return -math.pow(-x, 1 / 2.2) |
|---|
| 45 | return math.pow(x, 1 / 2.2) |
|---|
| 46 | CtoI = staticmethod(CtoI) |
|---|
| 47 | ItoC = staticmethod(ItoC) |
|---|
| 48 | |
|---|
| 49 | # Load the 256x256 grayscale Lenna image |
|---|
| 50 | lenna256bw = Image("lenna256bw.png") |
|---|
| 51 | |
|---|
| 52 | # Create a 32x256 grayscale gradient |
|---|
| 53 | gradient256bw = Image((32, 256)) |
|---|
| 54 | for x in range(32): |
|---|
| 55 | for y in range(256): |
|---|
| 56 | gradient256bw.setGray(x, 255 - y, y / 255.) |
|---|
| 57 | gradient256bw.save("gradient256bw.png") |
|---|
| 58 | |
|---|
| 59 | ############################################################################## |
|---|
| 60 | print "Chapter 1. Colour quantisation" |
|---|
| 61 | |
|---|
| 62 | # Output 1.1.1: 50% threshold |
|---|
| 63 | # Output 1.1.2: 40% threshold |
|---|
| 64 | # Output 1.1.3: 60% threshold |
|---|
| 65 | def test11x(src, threshold): |
|---|
| 66 | (w, h) = src.size() |
|---|
| 67 | dest = Image((w, h)) |
|---|
| 68 | for y in range(h): |
|---|
| 69 | for x in range(w): |
|---|
| 70 | c = src.getGray(x, y) > threshold |
|---|
| 71 | dest.setGray(x, y, c) |
|---|
| 72 | return dest |
|---|
| 73 | |
|---|
| 74 | test11x(lenna256bw, 0.5).save("out1-1-1.png") |
|---|
| 75 | test11x(lenna256bw, 0.4).save("out1-1-2.png") |
|---|
| 76 | test11x(lenna256bw, 0.6).save("out1-1-3.png") |
|---|
| 77 | test11x(gradient256bw, 0.5).save("grad1-1-1.png") |
|---|
| 78 | test11x(gradient256bw, 0.4).save("grad1-1-2.png") |
|---|
| 79 | test11x(gradient256bw, 0.6).save("grad1-1-3.png") |
|---|
| 80 | |
|---|
| 81 | # Output 1.2.1: 3-colour threshold |
|---|
| 82 | # Output 1.2.2: 5-colour threshold |
|---|
| 83 | def test12x(src, colors): |
|---|
| 84 | (w, h) = src.size() |
|---|
| 85 | dest = Image((w, h)) |
|---|
| 86 | p = -0.0001 + colors |
|---|
| 87 | q = colors - 1 |
|---|
| 88 | for y in range(h): |
|---|
| 89 | for x in range(w): |
|---|
| 90 | c = src.getGray(x, y) |
|---|
| 91 | c = math.floor(c * p) / q |
|---|
| 92 | dest.setGray(x, y, c) |
|---|
| 93 | return dest |
|---|
| 94 | |
|---|
| 95 | test12x(lenna256bw, 3).save("out1-2-1.png") |
|---|
| 96 | test12x(lenna256bw, 5).save("out1-2-2.png") |
|---|
| 97 | test12x(gradient256bw, 3).save("grad1-2-1.png") |
|---|
| 98 | test12x(gradient256bw, 5).save("grad1-2-2.png") |
|---|
| 99 | |
|---|
| 100 | ############################################################################## |
|---|
| 101 | print "Chapter 2. Halftoning patterns" |
|---|
| 102 | |
|---|
| 103 | # Pattern 2.1.1: a 50% halftone pattern with various block sizes |
|---|
| 104 | dest = Image((320, 80)) |
|---|
| 105 | for x in range(320): |
|---|
| 106 | d = 8 >> (x / 80) |
|---|
| 107 | for y in range(80): |
|---|
| 108 | c = (x / d + y / d) & 1 |
|---|
| 109 | dest.setGray(x, y, c) |
|---|
| 110 | dest.save("pat2-1-1.png") |
|---|
| 111 | |
|---|
| 112 | # Pattern 2.1.2: 25% and 75% halftone patterns with various block sizes |
|---|
| 113 | dest = Image((320, 80)) |
|---|
| 114 | for x in range(320): |
|---|
| 115 | d = 8 >> (x / 80) |
|---|
| 116 | for y in range(40): |
|---|
| 117 | c = ((x / d + y / d) & 1) or (y / d & 1) |
|---|
| 118 | dest.setGray(x, y, c) |
|---|
| 119 | for y in range(40, 80): |
|---|
| 120 | c = ((x / d + y / d) & 1) and (y / d & 1) |
|---|
| 121 | dest.setGray(x, y, c) |
|---|
| 122 | dest.save("pat2-1-2.png") |
|---|
| 123 | |
|---|
| 124 | # Output 2.1.1: 20/40/60/80% threshold with 25/50/75% patterns inbetween: |
|---|
| 125 | def test211(src): |
|---|
| 126 | (w, h) = src.size() |
|---|
| 127 | dest = Image((w, h)) |
|---|
| 128 | for y in range(h): |
|---|
| 129 | for x in range(w): |
|---|
| 130 | c = src.getGray(x, y) |
|---|
| 131 | if c < 0.2: |
|---|
| 132 | c = 0. |
|---|
| 133 | elif c < 0.4: |
|---|
| 134 | c = ((x + y) & 1) and (y & 1) |
|---|
| 135 | elif c < 0.6: |
|---|
| 136 | c = (x + y) & 1 |
|---|
| 137 | elif c < 0.8: |
|---|
| 138 | c = ((x + y) & 1) or (y & 1) |
|---|
| 139 | else: |
|---|
| 140 | c = 1. |
|---|
| 141 | dest.setGray(x, y, c) |
|---|
| 142 | return dest |
|---|
| 143 | |
|---|
| 144 | test211(lenna256bw).save("out2-1-1.png") |
|---|
| 145 | test211(gradient256bw).save("grad2-1-1.png") |
|---|
| 146 | |
|---|
| 147 | # Pattern 2.2.1: vertical, mixed and horizontal black-white halftones |
|---|
| 148 | dest = Image((240, 80)) |
|---|
| 149 | for y in range(80): |
|---|
| 150 | for x in range(80): |
|---|
| 151 | c = x & 1 |
|---|
| 152 | dest.setGray(x, y, c) |
|---|
| 153 | for x in range(80, 160): |
|---|
| 154 | c = (x / d + y / d) & 1 |
|---|
| 155 | dest.setGray(x, y, c) |
|---|
| 156 | for x in range(160, 240): |
|---|
| 157 | c = y & 1 |
|---|
| 158 | dest.setGray(x, y, c) |
|---|
| 159 | dest.save("pat2-2-1.png") |
|---|
| 160 | |
|---|
| 161 | # Pattern 2.2.2: two different 25% patterns |
|---|
| 162 | dest = Image((320, 80)) |
|---|
| 163 | for y in range(80): |
|---|
| 164 | for x in range(80): |
|---|
| 165 | c = (x / 2 & 1) and (y / 2 & 1) |
|---|
| 166 | dest.setGray(x, y, c) |
|---|
| 167 | for x in range(80, 160): |
|---|
| 168 | c = (x & 1) and (y & 1) |
|---|
| 169 | dest.setGray(x, y, c) |
|---|
| 170 | for x in range(160, 240): |
|---|
| 171 | c = (x & 1) and ((y + x / 2) & 1) |
|---|
| 172 | dest.setGray(x, y, c) |
|---|
| 173 | for x in range(240, 320): |
|---|
| 174 | c = (x / 2 & 1) and ((y / 2 + x / 4) & 1) |
|---|
| 175 | dest.setGray(x, y, c) |
|---|
| 176 | dest.save("pat2-2-2.png") |
|---|
| 177 | |
|---|
| 178 | # Output 2.3.1: 4x4 Bayer dithering |
|---|
| 179 | # Output 2.3.2: 4x4 cluster dot |
|---|
| 180 | # Output 2.3.3: 5x3 line dithering |
|---|
| 181 | DITHER_BAYER44 = \ |
|---|
| 182 | [[ 0, 8, 3, 11], |
|---|
| 183 | [ 15, 4, 12, 7], |
|---|
| 184 | [ 2, 10, 1, 9], |
|---|
| 185 | [ 13, 6, 14, 5]] |
|---|
| 186 | DITHER_CLUSTER44 = \ |
|---|
| 187 | [[ 12, 5, 6, 13], |
|---|
| 188 | [ 4, 0, 1, 7], |
|---|
| 189 | [ 11, 3, 2, 8], |
|---|
| 190 | [ 15, 10, 9, 14]] |
|---|
| 191 | DITHER_LINE53 = \ |
|---|
| 192 | [[ 13, 7, 0, 4, 10], |
|---|
| 193 | [ 9, 3, 1, 8, 14], |
|---|
| 194 | [ 11, 5, 2, 6, 12],] |
|---|
| 195 | |
|---|
| 196 | def test23x(src, mat): |
|---|
| 197 | (w, h) = src.size() |
|---|
| 198 | dest = Image((w, h)) |
|---|
| 199 | dx = len(mat[0]) |
|---|
| 200 | dy = len(mat) |
|---|
| 201 | for y in range(h): |
|---|
| 202 | for x in range(w): |
|---|
| 203 | c = src.getGray(x, y) |
|---|
| 204 | threshold = (1. + mat[y % dy][x % dx]) / (dx * dy + 1) |
|---|
| 205 | c = c > threshold |
|---|
| 206 | dest.setGray(x, y, c) |
|---|
| 207 | return dest |
|---|
| 208 | |
|---|
| 209 | test23x(lenna256bw, DITHER_BAYER44).save("out2-3-1.png") |
|---|
| 210 | test23x(gradient256bw, DITHER_BAYER44).save("grad2-3-1.png") |
|---|
| 211 | |
|---|
| 212 | test23x(lenna256bw, DITHER_CLUSTER44).save("out2-3-2.png") |
|---|
| 213 | test23x(gradient256bw, DITHER_CLUSTER44).save("grad2-3-2.png") |
|---|
| 214 | |
|---|
| 215 | test23x(lenna256bw, DITHER_LINE53).save("out2-3-3.png") |
|---|
| 216 | test23x(gradient256bw, DITHER_LINE53).save("grad2-3-3.png") |
|---|
| 217 | |
|---|
| 218 | # Output 2.4.1: uniform random dithering |
|---|
| 219 | def test241(src): |
|---|
| 220 | random.seed(0) |
|---|
| 221 | (w, h) = src.size() |
|---|
| 222 | dest = Image((w, h)) |
|---|
| 223 | for y in range(h): |
|---|
| 224 | for x in range(w): |
|---|
| 225 | c = src.getGray(x, y) |
|---|
| 226 | d = c > random.random() |
|---|
| 227 | dest.setGray(x, y, d) |
|---|
| 228 | return dest |
|---|
| 229 | |
|---|
| 230 | test241(lenna256bw).save("out2-4-1.png") |
|---|
| 231 | test241(gradient256bw).save("grad2-4-1.png") |
|---|
| 232 | |
|---|
| 233 | # Output 2.4.2: gaussian random dithering |
|---|
| 234 | def test242(src): |
|---|
| 235 | random.seed(0) |
|---|
| 236 | (w, h) = src.size() |
|---|
| 237 | dest = Image((w, h)) |
|---|
| 238 | for y in range(h): |
|---|
| 239 | for x in range(w): |
|---|
| 240 | c = src.getGray(x, y) |
|---|
| 241 | d = c > random.gauss(0.5, 0.15) |
|---|
| 242 | dest.setGray(x, y, d) |
|---|
| 243 | return dest |
|---|
| 244 | |
|---|
| 245 | test242(lenna256bw).save("out2-4-2.png") |
|---|
| 246 | test242(gradient256bw).save("grad2-4-2.png") |
|---|
| 247 | |
|---|
| 248 | # Output 2.4.3: random dither matrices |
|---|
| 249 | def test243(src, mat): |
|---|
| 250 | random.seed(0) |
|---|
| 251 | (w, h) = src.size() |
|---|
| 252 | dest = Image((w, h)) |
|---|
| 253 | dx = len(mat[0]) |
|---|
| 254 | dy = len(mat) |
|---|
| 255 | randmat = [[(int)(random.random() * 16) for x in range(w / dx)] for y in range(h / dy)] |
|---|
| 256 | for y in range(h): |
|---|
| 257 | for x in range(w): |
|---|
| 258 | c = src.getGray(x, y) |
|---|
| 259 | val = (mat[y % dy][x % dx] + randmat[y / dy][x / dx]) % (dx * dy) |
|---|
| 260 | threshold = (1. + val) / (dx * dy + 1) |
|---|
| 261 | c = c > threshold |
|---|
| 262 | dest.setGray(x, y, c) |
|---|
| 263 | return dest |
|---|
| 264 | |
|---|
| 265 | test243(lenna256bw, DITHER_BAYER44).save("out2-4-3.png") |
|---|
| 266 | test243(gradient256bw, DITHER_BAYER44).save("grad2-4-3.png") |
|---|
| 267 | |
|---|
| 268 | ############################################################################## |
|---|
| 269 | print "Chapter 3. Error diffusion" |
|---|
| 270 | |
|---|
| 271 | # Output 3.0.1: naive error diffusion |
|---|
| 272 | # Output 3.1.1: standard Floyd-Steinberg |
|---|
| 273 | # Output 3.1.2: serpentine Floyd-Steinberg |
|---|
| 274 | # FIXME: serpentine only works if rows == offset * 2 + 1 |
|---|
| 275 | # Output 3.2.1: Fan (modified Floyd-Steinberg) |
|---|
| 276 | # Output 3.2.2: Jarvis, Judice and Ninke |
|---|
| 277 | # Output 3-2-3: Stucki |
|---|
| 278 | # Output 3-2-4: Burkes |
|---|
| 279 | # Output 3-2-5: Sierra |
|---|
| 280 | # Output 3.2.6: Two-line Sierra |
|---|
| 281 | # Output 3.2.7: Sierra's Filter Lite |
|---|
| 282 | # Output 3-2-8: Atkinson |
|---|
| 283 | ERROR_NAIVE = \ |
|---|
| 284 | [[ -1, 1]] |
|---|
| 285 | ERROR_FSTEIN = \ |
|---|
| 286 | [[ 0., -1, 7./16], |
|---|
| 287 | [ 3./16, 5./16, 1./16]] |
|---|
| 288 | ERROR_FAN = \ |
|---|
| 289 | [[ 0., 0., -1, 7./16], |
|---|
| 290 | [ 1./16, 3./16, 5./16, 0.]] |
|---|
| 291 | ERROR_JAJUNI = \ |
|---|
| 292 | [[ 0., 0., -1, 7./48, 5./48], |
|---|
| 293 | [ 3./48, 5./48, 7./48, 5./48, 3./48], |
|---|
| 294 | [ 1./48, 3./48, 5./48, 3./48, 1./48]] |
|---|
| 295 | ERROR_STUCKI = \ |
|---|
| 296 | [[ 0., 0., -1, 8./42, 4./42], |
|---|
| 297 | [ 2./42, 4./42, 8./42, 4./42, 2./42], |
|---|
| 298 | [ 1./42, 2./42, 4./42, 2./42, 1./42]] |
|---|
| 299 | ERROR_BURKES = \ |
|---|
| 300 | [[ 0., 0., -1, 8./32, 4./32], |
|---|
| 301 | [ 2./32, 4./32, 8./32, 4./32, 2./32]] |
|---|
| 302 | ERROR_SIERRA = \ |
|---|
| 303 | [[ 0., 0., -1, 5./32, 3./32], |
|---|
| 304 | [ 2./32, 4./32, 5./32, 4./32, 2./32], |
|---|
| 305 | [ 0., 2./32, 3./32, 2./32, 0.]] |
|---|
| 306 | ERROR_SIERRA2 = \ |
|---|
| 307 | [[ 0., 0., -1, 4./16, 3./16], |
|---|
| 308 | [ 1./16, 2./16, 3./16, 2./16, 1./16]] |
|---|
| 309 | ERROR_FILTERLITE = \ |
|---|
| 310 | [[ 0., -1, 2./4], |
|---|
| 311 | [ 1./4, 1./4, 0.]] |
|---|
| 312 | ERROR_ATKINSON = \ |
|---|
| 313 | [[ 0., -1, 1./8, 1./8], |
|---|
| 314 | [ 1./8, 1./8, 1./8, 0.], |
|---|
| 315 | [ 0., 1./8, 0., 0.]] |
|---|
| 316 | ## This is Stevenson-Arce in hex lattice |
|---|
| 317 | #ERROR_STAR = \ |
|---|
| 318 | # [[ 0., 0., 0., -1, 0., 32./200, 0.], |
|---|
| 319 | # [ 12./200, 0., 26./200, 0., 30./200, 0., 16./200], |
|---|
| 320 | # [ 0., 12./200, 0., 26./200, 0., 12./200, 0.], |
|---|
| 321 | # [ 5./200, 0., 12./200, 0., 12./200, 0., 5./200]] |
|---|
| 322 | ## This is an attempt at implementing Stevenson-Arce in square lattice |
|---|
| 323 | #ERROR_STAR = \ |
|---|
| 324 | # [[ 0., 0., -1, 32./200, 0.], |
|---|
| 325 | # [ 6./200, 19./200, 28./200, 23./200, 8./200], |
|---|
| 326 | # [ 0., 12./200, 26./200, 12./200, 0.], |
|---|
| 327 | # [ 2./200, 9./200, 12./200, 9./200, 2./200]] |
|---|
| 328 | |
|---|
| 329 | def test3xx(src, mat, serpentine): |
|---|
| 330 | (w, h) = src.size() |
|---|
| 331 | dest = Image((w, h)) |
|---|
| 332 | lines = len(mat) |
|---|
| 333 | rows = len(mat[0]) |
|---|
| 334 | offset = mat[0].index(-1) |
|---|
| 335 | ey = [[0.] * (w + rows - 1) for x in range(lines)] |
|---|
| 336 | for y in range(h): |
|---|
| 337 | ex = [0.] * (rows - offset) |
|---|
| 338 | if serpentine and y & 1: |
|---|
| 339 | xrange = range(w - 1, -1, -1) |
|---|
| 340 | else: |
|---|
| 341 | xrange = range(w) |
|---|
| 342 | for x in xrange: |
|---|
| 343 | # Set pixel |
|---|
| 344 | c = src.getGray(x, y) + ex[0] + ey[0][x + offset] |
|---|
| 345 | d = c > 0.5 |
|---|
| 346 | dest.setGray(x, y, d) |
|---|
| 347 | error = c - d |
|---|
| 348 | # Propagate first line of error |
|---|
| 349 | for dx in range(rows - offset - 2): |
|---|
| 350 | ex[dx] = ex[dx + 1] + error * mat[0][offset + 1 + dx] |
|---|
| 351 | ex[rows - offset - 2] = error * mat[0][rows - 1] |
|---|
| 352 | # Propagate next lines |
|---|
| 353 | if serpentine and y & 1: |
|---|
| 354 | for dy in range(1, lines): |
|---|
| 355 | for dx in range(rows): |
|---|
| 356 | ey[dy][x + dx] += error * mat[dy][rows - 1 - dx] |
|---|
| 357 | else: |
|---|
| 358 | for dy in range(1, lines): |
|---|
| 359 | for dx in range(rows): |
|---|
| 360 | ey[dy][x + dx] += error * mat[dy][dx] |
|---|
| 361 | for dy in range(lines - 1): |
|---|
| 362 | ey[dy] = ey[dy + 1] |
|---|
| 363 | ey[lines - 1] = [0.] * (w + rows - 1) |
|---|
| 364 | return dest |
|---|
| 365 | |
|---|
| 366 | test3xx(lenna256bw, ERROR_NAIVE, False).save("out3-0-1.png") |
|---|
| 367 | test3xx(gradient256bw, ERROR_NAIVE, False).save("grad3-0-1.png") |
|---|
| 368 | |
|---|
| 369 | test3xx(lenna256bw, ERROR_FSTEIN, False).save("out3-1-1.png") |
|---|
| 370 | test3xx(gradient256bw, ERROR_FSTEIN, False).save("grad3-1-1.png") |
|---|
| 371 | test3xx(lenna256bw, ERROR_FSTEIN, True).save("out3-1-2.png") |
|---|
| 372 | test3xx(gradient256bw, ERROR_FSTEIN, True).save("grad3-1-2.png") |
|---|
| 373 | |
|---|
| 374 | test3xx(lenna256bw, ERROR_FAN, False).save("out3-2-1.png") |
|---|
| 375 | test3xx(gradient256bw, ERROR_FAN, False).save("grad3-2-1.png") |
|---|
| 376 | |
|---|
| 377 | test3xx(lenna256bw, ERROR_JAJUNI, False).save("out3-2-2.png") |
|---|
| 378 | test3xx(gradient256bw, ERROR_JAJUNI, False).save("grad3-2-2.png") |
|---|
| 379 | |
|---|
| 380 | test3xx(lenna256bw, ERROR_STUCKI, False).save("out3-2-3.png") |
|---|
| 381 | test3xx(gradient256bw, ERROR_STUCKI, False).save("grad3-2-3.png") |
|---|
| 382 | |
|---|
| 383 | test3xx(lenna256bw, ERROR_BURKES, False).save("out3-2-4.png") |
|---|
| 384 | test3xx(gradient256bw, ERROR_BURKES, False).save("grad3-2-4.png") |
|---|
| 385 | |
|---|
| 386 | test3xx(lenna256bw, ERROR_SIERRA, False).save("out3-2-5.png") |
|---|
| 387 | test3xx(gradient256bw, ERROR_SIERRA, False).save("grad3-2-5.png") |
|---|
| 388 | |
|---|
| 389 | test3xx(lenna256bw, ERROR_SIERRA2, False).save("out3-2-6.png") |
|---|
| 390 | test3xx(gradient256bw, ERROR_SIERRA2, False).save("grad3-2-6.png") |
|---|
| 391 | |
|---|
| 392 | test3xx(lenna256bw, ERROR_FILTERLITE, False).save("out3-2-7.png") |
|---|
| 393 | test3xx(gradient256bw, ERROR_FILTERLITE, False).save("grad3-2-7.png") |
|---|
| 394 | |
|---|
| 395 | test3xx(lenna256bw, ERROR_ATKINSON, False).save("out3-2-8.png") |
|---|
| 396 | test3xx(gradient256bw, ERROR_ATKINSON, False).save("grad3-2-8.png") |
|---|
| 397 | |
|---|
| 398 | #test3xx(lenna256bw, ERROR_STAR, False).save("out3-2-9.png") |
|---|
| 399 | #test3xx(gradient256bw, ERROR_STAR, False).save("grad3-2-9.png") |
|---|
| 400 | |
|---|
| 401 | #test3xx(lenna256bw, ERROR_STAR, False).save("out3-2-9.png") |
|---|
| 402 | #test3xx(gradient256bw, ERROR_STAR, False).save("grad3-2-9.png") |
|---|
| 403 | |
|---|
| 404 | ############################################################################## |
|---|
| 405 | print "Chapter 4. Grayscale dithering" |
|---|
| 406 | |
|---|
| 407 | # Output 4.0.1: 4x4 Bayer dithering, 3 colours |
|---|
| 408 | def test401(src, mat): |
|---|
| 409 | (w, h) = src.size() |
|---|
| 410 | dest = Image((w, h)) |
|---|
| 411 | dx = len(mat[0]) |
|---|
| 412 | dy = len(mat) |
|---|
| 413 | for y in range(h): |
|---|
| 414 | for x in range(w): |
|---|
| 415 | c = src.getGray(x, y) |
|---|
| 416 | threshold = (1. + mat[y % dy][x % dx]) / (dx * dy + 1) |
|---|
| 417 | if c < 0.5: |
|---|
| 418 | c = 0.5 * (c > threshold / 2) |
|---|
| 419 | else: |
|---|
| 420 | c = 0.5 + 0.5 * (c > 0.5 + threshold / 2) |
|---|
| 421 | dest.setGray(x, y, c) |
|---|
| 422 | return dest |
|---|
| 423 | |
|---|
| 424 | test401(lenna256bw, DITHER_BAYER44).save("out4-0-1.png") |
|---|
| 425 | test401(gradient256bw, DITHER_BAYER44).save("grad4-0-1.png") |
|---|
| 426 | |
|---|
| 427 | # Output 4.0.2: standard Floyd-Steinberg, 3 colours |
|---|
| 428 | def test402(src, mat, serpentine): |
|---|
| 429 | (w, h) = src.size() |
|---|
| 430 | dest = Image((w, h)) |
|---|
| 431 | lines = len(mat) |
|---|
| 432 | rows = len(mat[0]) |
|---|
| 433 | offset = mat[0].index(-1) |
|---|
| 434 | ey = [[0.] * (w + rows - 1) for x in range(lines)] |
|---|
| 435 | for y in range(h): |
|---|
| 436 | ex = [0.] * (rows - offset) |
|---|
| 437 | if serpentine and y & 1: |
|---|
| 438 | xrange = range(w - 1, -1, -1) |
|---|
| 439 | else: |
|---|
| 440 | xrange = range(w) |
|---|
| 441 | for x in xrange: |
|---|
| 442 | # Set pixel |
|---|
| 443 | c = src.getGray(x, y) + ex[0] + ey[0][x + offset] |
|---|
| 444 | d = 0.5 * (c > 0.25) + 0.5 * (c > 0.75) |
|---|
| 445 | dest.setGray(x, y, d) |
|---|
| 446 | error = c - d |
|---|
| 447 | # Propagate first line of error |
|---|
| 448 | for dx in range(rows - offset - 2): |
|---|
| 449 | ex[dx] = ex[dx + 1] + error * mat[0][offset + 1 + dx] |
|---|
| 450 | ex[rows - offset - 2] = error * mat[0][rows - 1] |
|---|
| 451 | # Propagate next lines |
|---|
| 452 | if serpentine and y & 1: |
|---|
| 453 | for dy in range(1, lines): |
|---|
| 454 | for dx in range(rows): |
|---|
| 455 | ey[dy][x + dx] += error * mat[dy][rows - 1 - dx] |
|---|
| 456 | else: |
|---|
| 457 | for dy in range(1, lines): |
|---|
| 458 | for dx in range(rows): |
|---|
| 459 | ey[dy][x + dx] += error * mat[dy][dx] |
|---|
| 460 | for dy in range(lines - 1): |
|---|
| 461 | ey[dy] = ey[dy + 1] |
|---|
| 462 | ey[lines - 1] = [0.] * (w + rows - 1) |
|---|
| 463 | return dest |
|---|
| 464 | |
|---|
| 465 | test402(lenna256bw, ERROR_FSTEIN, False).save("out4-0-2.png") |
|---|
| 466 | test402(gradient256bw, ERROR_FSTEIN, False).save("grad4-0-2.png") |
|---|
| 467 | |
|---|
| 468 | # Pattern 4.1.1: gamma-corrected 50% gray, black-white halftone, 50% gray |
|---|
| 469 | dest = Image((240, 80)) |
|---|
| 470 | for y in range(80): |
|---|
| 471 | for x in range(80): |
|---|
| 472 | dest.setGray(x, y, Gamma.ItoC(0.5)) |
|---|
| 473 | for x in range(80, 160): |
|---|
| 474 | c = (x + y) & 1 |
|---|
| 475 | dest.setGray(x, y, c) |
|---|
| 476 | for x in range(160, 240): |
|---|
| 477 | dest.setGray(x, y, 0.5) |
|---|
| 478 | dest.save("pat4-1-1.png") |
|---|
| 479 | |
|---|
| 480 | # Output 4.2.1: gamma-corrected 2-colour Floyd-Steinberg |
|---|
| 481 | # Output 4.2.2: gamma-corrected 3-colour Floyd-Steinberg |
|---|
| 482 | # Output 4.2.3: gamma-corrected 4-colour Floyd-Steinberg |
|---|
| 483 | def test42x(src, mat, threshold): |
|---|
| 484 | (w, h) = src.size() |
|---|
| 485 | dest = Image((w, h)) |
|---|
| 486 | lines = len(mat) |
|---|
| 487 | rows = len(mat[0]) |
|---|
| 488 | offset = mat[0].index(-1) |
|---|
| 489 | ey = [[0.] * (w + rows - 1) for x in range(lines)] |
|---|
| 490 | for y in range(h): |
|---|
| 491 | ex = [0.] * (rows - offset) |
|---|
| 492 | for x in range(w): |
|---|
| 493 | # Set pixel |
|---|
| 494 | c = Gamma.CtoI(src.getGray(x, y)) + ex[0] + ey[0][x + offset] |
|---|
| 495 | d = threshold(c) |
|---|
| 496 | dest.setGray(x, y, Gamma.ItoC(d)) |
|---|
| 497 | error = c - d |
|---|
| 498 | # Propagate first line of error |
|---|
| 499 | for dx in range(rows - offset - 2): |
|---|
| 500 | ex[dx] = ex[dx + 1] + error * mat[0][offset + 1 + dx] |
|---|
| 501 | ex[rows - offset - 2] = error * mat[0][rows - 1] |
|---|
| 502 | # Propagate next lines |
|---|
| 503 | for dy in range(1, lines): |
|---|
| 504 | for dx in range(rows): |
|---|
| 505 | ey[dy][x + dx] += error * mat[dy][dx] |
|---|
| 506 | for dy in range(lines - 1): |
|---|
| 507 | ey[dy] = ey[dy + 1] |
|---|
| 508 | ey[lines - 1] = [0.] * (w + rows - 1) |
|---|
| 509 | return dest |
|---|
| 510 | |
|---|
| 511 | def threshold_2(x): |
|---|
| 512 | if x < Gamma.CtoI(0.50): |
|---|
| 513 | return 0. |
|---|
| 514 | return 1. |
|---|
| 515 | |
|---|
| 516 | def threshold_3(x): |
|---|
| 517 | if x < Gamma.CtoI(0.25): |
|---|
| 518 | return 0. |
|---|
| 519 | elif x < Gamma.CtoI(0.75): |
|---|
| 520 | return Gamma.CtoI(0.5) |
|---|
| 521 | return 1. |
|---|
| 522 | |
|---|
| 523 | def threshold_4(x): |
|---|
| 524 | if x < Gamma.CtoI(0.17): |
|---|
| 525 | return 0. |
|---|
| 526 | elif x < Gamma.CtoI(0.50): |
|---|
| 527 | return Gamma.CtoI(0.3333) |
|---|
| 528 | elif x < Gamma.CtoI(0.83): |
|---|
| 529 | return Gamma.CtoI(0.6666) |
|---|
| 530 | return 1. |
|---|
| 531 | |
|---|
| 532 | test42x(lenna256bw, ERROR_FSTEIN, threshold_2).save("out4-2-1.png") |
|---|
| 533 | test42x(gradient256bw, ERROR_FSTEIN, threshold_2).save("grad4-2-1.png") |
|---|
| 534 | test42x(lenna256bw, ERROR_FSTEIN, threshold_3).save("out4-2-2.png") |
|---|
| 535 | test42x(gradient256bw, ERROR_FSTEIN, threshold_3).save("grad4-2-2.png") |
|---|
| 536 | test42x(lenna256bw, ERROR_FSTEIN, threshold_4).save("out4-2-3.png") |
|---|
| 537 | test42x(gradient256bw, ERROR_FSTEIN, threshold_4).save("grad4-2-3.png") |
|---|
| 538 | |
|---|
| 539 | ############################################################################## |
|---|
| 540 | print "Chapter 5. Colour dithering" |
|---|
| 541 | |
|---|
| 542 | # Pattern 5.1.1: 8-colour palette |
|---|
| 543 | dest = Image((512, 64)) |
|---|
| 544 | for x in range(512): |
|---|
| 545 | d = x / 64 |
|---|
| 546 | r = (d & 2) >> 1 |
|---|
| 547 | g = (d & 4) >> 2 |
|---|
| 548 | b = d & 1 |
|---|
| 549 | for y in range(64): |
|---|
| 550 | dest.setRgb(x, y, r, g, b) |
|---|
| 551 | dest.save("pat5-1-1.png") |
|---|
| 552 | |
|---|
| 553 | # Load the 256x256 colour Lenna image |
|---|
| 554 | lenna256 = Image("lenna256.png") |
|---|
| 555 | |
|---|
| 556 | # Output 5.1.1: 8-colour Floyd-Steinberg RGB dithering |
|---|
| 557 | # Output 5.1.2: 8-colour gamma-corrected Floyd-Steinberg RGB dithering |
|---|
| 558 | def test51x(src, mat, func): |
|---|
| 559 | (w, h) = src.size() |
|---|
| 560 | dest = Image((w, h)) |
|---|
| 561 | tmp = [Image((w, h)), Image((w, h)), Image((w, h))] |
|---|
| 562 | for y in range(h): |
|---|
| 563 | for x in range(w): |
|---|
| 564 | rgb = src.getRgb(x, y) |
|---|
| 565 | for i in range(3): |
|---|
| 566 | tmp[i].setGray(x, y, rgb[i]) |
|---|
| 567 | for i in range(3): |
|---|
| 568 | tmp[i] = func(tmp[i], mat, threshold_2) |
|---|
| 569 | for y in range(h): |
|---|
| 570 | for x in range(w): |
|---|
| 571 | (r, g, b) = [tmp[i].getGray(x, y) for i in range(3)] |
|---|
| 572 | dest.setRgb(x, y, r, g, b) |
|---|
| 573 | return dest |
|---|
| 574 | |
|---|
| 575 | test51x(lenna256, ERROR_FSTEIN, test3xx).save("out5-1-1.png") |
|---|
| 576 | out512 = test51x(lenna256, ERROR_FSTEIN, test42x) |
|---|
| 577 | out512.save("out5-1-2.png") |
|---|
| 578 | |
|---|
| 579 | # Pattern 5.2.1: different colours give the same result |
|---|
| 580 | dest = Image((320, 160)) |
|---|
| 581 | for x in range(80): |
|---|
| 582 | for y in range(80): |
|---|
| 583 | r = DITHER_BAYER44[(y / 8) % 4][(x / 8) % 4] > 7 |
|---|
| 584 | g = DITHER_BAYER44[(y / 8) % 4][(x / 8) % 4] > 13 |
|---|
| 585 | b = DITHER_BAYER44[(y / 8) % 4][(x / 8) % 4] > 13 |
|---|
| 586 | dest.setRgb(x, y, b, g, r) |
|---|
| 587 | for y in range(80, 160): |
|---|
| 588 | r = DITHER_BAYER44[y % 4][x % 4] > 7 |
|---|
| 589 | g = DITHER_BAYER44[y % 4][x % 4] > 13 |
|---|
| 590 | b = DITHER_BAYER44[y % 4][x % 4] > 13 |
|---|
| 591 | dest.setRgb(x, y, b, g, r) |
|---|
| 592 | for x in range(80, 160): |
|---|
| 593 | for y in range(80): |
|---|
| 594 | r = DITHER_BAYER44[(y / 8) % 4][(x / 8) % 4] > 7 |
|---|
| 595 | g = DITHER_BAYER44[(y / 8) % 4][(x / 8 + 1) % 4] > 13 |
|---|
| 596 | b = DITHER_BAYER44[(y / 8) % 4][(x / 8 + 1) % 4] > 13 |
|---|
| 597 | dest.setRgb(x, y, b, g, r) |
|---|
| 598 | for y in range(80, 160): |
|---|
| 599 | r = DITHER_BAYER44[y % 4][x % 4] > 7 |
|---|
| 600 | g = DITHER_BAYER44[y % 4][(x + 1) % 4] > 13 |
|---|
| 601 | b = DITHER_BAYER44[y % 4][(x + 1) % 4] > 13 |
|---|
| 602 | dest.setRgb(x, y, b, g, r) |
|---|
| 603 | for x in range(160, 240): |
|---|
| 604 | for y in range(80): |
|---|
| 605 | r = DITHER_BAYER44[(y / 8 + 1) % 4][(x / 8 + 1) % 4] > 7 |
|---|
| 606 | g = DITHER_BAYER44[(y / 8) % 4][(x / 8) % 4] > 13 |
|---|
| 607 | b = DITHER_BAYER44[(y / 8 + 1) % 4][(x / 8) % 4] > 13 |
|---|
| 608 | dest.setRgb(x, y, b, g, r) |
|---|
| 609 | for y in range(80, 160): |
|---|
| 610 | r = DITHER_BAYER44[(y + 1) % 4][(x + 1) % 4] > 7 |
|---|
| 611 | g = DITHER_BAYER44[y % 4][x % 4] > 13 |
|---|
| 612 | b = DITHER_BAYER44[(y + 1) % 4][x % 4] > 13 |
|---|
| 613 | dest.setRgb(x, y, b, g, r) |
|---|
| 614 | for x in range(240, 320): |
|---|
| 615 | for y in range(80): |
|---|
| 616 | r = DITHER_BAYER44[(y / 8 + 1) % 4][(x / 8) % 4] > 7 |
|---|
| 617 | g = DITHER_BAYER44[(y / 8) % 4][(x / 8) % 4] > 13 |
|---|
| 618 | b = DITHER_BAYER44[(y / 8) % 4][(x / 8 + 2) % 4] > 13 |
|---|
| 619 | dest.setRgb(x, y, b, g, r) |
|---|
| 620 | for y in range(80, 160): |
|---|
| 621 | r = DITHER_BAYER44[(y + 1) % 4][x % 4] > 7 |
|---|
| 622 | g = DITHER_BAYER44[y % 4][x % 4] > 13 |
|---|
| 623 | b = DITHER_BAYER44[y % 4][(x + 2) % 4] > 13 |
|---|
| 624 | dest.setRgb(x, y, b, g, r) |
|---|
| 625 | dest.save("pat5-2-1.png") |
|---|
| 626 | |
|---|
| 627 | # Output 5.2.1: cropped 5.1.2 |
|---|
| 628 | # Output 5.2.2: close-up of cropped 5.1.2 |
|---|
| 629 | def test52x(src, x, y, w, h, z): |
|---|
| 630 | dest = Image((w * z, h * z)) |
|---|
| 631 | for j in range(h): |
|---|
| 632 | for i in range(w): |
|---|
| 633 | (r, g, b) = src.getRgb(x + i, y + j) |
|---|
| 634 | for v in range(z): |
|---|
| 635 | for u in range(z): |
|---|
| 636 | dest.setRgb(i * z + u, j * z + v, r, g, b) |
|---|
| 637 | return dest |
|---|
| 638 | |
|---|
| 639 | test52x(out512, 20, 70, 32, 32, 1).save("out5-2-1.png") |
|---|
| 640 | test52x(out512, 20, 70, 32, 32, 6).save("out5-2-2.png") |
|---|
| 641 | |
|---|
| 642 | out523 = test51x(lenna256, ERROR_STUCKI, test42x) |
|---|
| 643 | out523.save("out5-2-3.png") |
|---|
| 644 | test52x(out523, 20, 70, 32, 32, 1).save("out5-2-4.png") |
|---|
| 645 | test52x(out523, 20, 70, 32, 32, 6).save("out5-2-5.png") |
|---|
| 646 | |
|---|
| 647 | ############################################################################## |
|---|
| 648 | print "Finished" |
|---|
| 649 | |
|---|
| 650 | # Place temporary cruft below this |
|---|
| 651 | import sys; sys.exit(0) |
|---|
| 652 | |
|---|