source: www/study/study.py @ 2007

Last change on this file since 2007 was 2007, checked in by Sam Hocevar, 13 years ago
  • Explain the problem with dimension separation.
  • Property svn:executable set to *
File size: 20.7 KB
Line 
1#!/usr/bin/env python
2
3import math, gd, random
4
5# Tiny image class to make examples short and readable
6class 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##############################################################################
36print "Initialisation"
37
38# Manipulate gamma values
39class 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
50lenna256bw = Image("lenna256bw.png")
51
52# Create a 32x256 grayscale gradient
53gradient256bw = Image((32, 256))
54for x in range(32):
55    for y in range(256):
56        gradient256bw.setGray(x, 255 - y, y / 255.)
57gradient256bw.save("gradient256bw.png")
58
59##############################################################################
60print "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
65def 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
74test11x(lenna256bw, 0.5).save("out1-1-1.png")
75test11x(lenna256bw, 0.4).save("out1-1-2.png")
76test11x(lenna256bw, 0.6).save("out1-1-3.png")
77test11x(gradient256bw, 0.5).save("grad1-1-1.png")
78test11x(gradient256bw, 0.4).save("grad1-1-2.png")
79test11x(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
83def 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
95test12x(lenna256bw, 3).save("out1-2-1.png")
96test12x(lenna256bw, 5).save("out1-2-2.png")
97test12x(gradient256bw, 3).save("grad1-2-1.png")
98test12x(gradient256bw, 5).save("grad1-2-2.png")
99
100##############################################################################
101print "Chapter 2. Halftoning patterns"
102
103# Pattern 2.1.1: a 50% halftone pattern with various block sizes
104dest = Image((320, 80))
105for 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)
110dest.save("pat2-1-1.png")
111
112# Pattern 2.1.2: 25% and 75% halftone patterns with various block sizes
113dest = Image((320, 80))
114for 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)
122dest.save("pat2-1-2.png")
123
124# Output 2.1.1: 20/40/60/80% threshold with 25/50/75% patterns inbetween:
125def 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
144test211(lenna256bw).save("out2-1-1.png")
145test211(gradient256bw).save("grad2-1-1.png")
146
147# Pattern 2.2.1: vertical, mixed and horizontal black-white halftones
148dest = Image((240, 80))
149for 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)
159dest.save("pat2-2-1.png")
160
161# Pattern 2.2.2: two different 25% patterns
162dest = Image((320, 80))
163for 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)
176dest.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
181DITHER_BAYER44 = \
182    [[  0,  8,  3, 11],
183     [ 15,  4, 12,  7],
184     [  2, 10,  1,  9],
185     [ 13,  6, 14,  5]]
186DITHER_CLUSTER44 = \
187    [[ 12,  5,  6, 13],
188     [  4,  0,  1,  7],
189     [ 11,  3,  2,  8],
190     [ 15, 10,  9, 14]]
191DITHER_LINE53 = \
192    [[ 13,  7,  0,  4, 10],
193     [  9,  3,  1,  8, 14],
194     [ 11,  5,  2,  6, 12],]
195
196def 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
209test23x(lenna256bw, DITHER_BAYER44).save("out2-3-1.png")
210test23x(gradient256bw, DITHER_BAYER44).save("grad2-3-1.png")
211
212test23x(lenna256bw, DITHER_CLUSTER44).save("out2-3-2.png")
213test23x(gradient256bw, DITHER_CLUSTER44).save("grad2-3-2.png")
214
215test23x(lenna256bw, DITHER_LINE53).save("out2-3-3.png")
216test23x(gradient256bw, DITHER_LINE53).save("grad2-3-3.png")
217
218# Output 2.4.1: uniform random dithering
219def 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
230test241(lenna256bw).save("out2-4-1.png")
231test241(gradient256bw).save("grad2-4-1.png")
232
233# Output 2.4.2: random dithering
234def 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
245test242(lenna256bw).save("out2-4-2.png")
246test242(gradient256bw).save("grad2-4-2.png")
247
248##############################################################################
249print "Chapter 3. Error diffusion"
250
251# Output 3.0.1: naive error diffusion
252# Output 3.1.1: standard Floyd-Steinberg
253# Output 3.1.2: serpentine Floyd-Steinberg
254# FIXME: serpentine only works if rows == offset * 2 + 1
255# Output 3.2.1: Fan (modified Floyd-Steinberg)
256# Output 3.2.2: Jarvis, Judice and Ninke
257# Output 3-2-3: Stucki
258# Output 3-2-4: Burkes
259# Output 3-2-5: Sierra
260# Output 3.2.6: Two-line Sierra
261# Output 3.2.7: Sierra's Filter Lite
262# Output 3-2-8: Atkinson
263ERROR_NAIVE = \
264    [[ -1, 1]]
265ERROR_FSTEIN = \
266    [[    0.,    -1, 7./16],
267     [ 3./16, 5./16, 1./16]]
268ERROR_FAN = \
269    [[    0.,    0.,    -1, 7./16],
270     [ 1./16, 3./16, 5./16,    0.]]
271ERROR_JAJUNI = \
272    [[    0.,    0.,    -1, 7./48, 5./48],
273     [ 3./48, 5./48, 7./48, 5./48, 3./48],
274     [ 1./48, 3./48, 5./48, 3./48, 1./48]]
275ERROR_STUCKI = \
276    [[    0.,    0.,    -1, 8./42, 4./42],
277     [ 2./42, 4./42, 8./42, 4./42, 2./42],
278     [ 1./42, 2./42, 4./42, 2./42, 1./42]]
279ERROR_BURKES = \
280    [[    0.,    0.,    -1, 8./32, 4./32],
281     [ 2./32, 4./32, 8./32, 4./32, 2./32]]
282ERROR_SIERRA = \
283    [[    0.,    0.,    -1, 5./32, 3./32],
284     [ 2./32, 4./32, 5./32, 4./32, 2./32],
285     [    0., 2./32, 3./32, 2./32,    0.]]
286ERROR_SIERRA2 = \
287    [[    0.,    0.,    -1, 4./16, 3./16],
288     [ 1./16, 2./16, 3./16, 2./16, 1./16]]
289ERROR_FILTERLITE = \
290    [[   0.,   -1, 2./4],
291     [ 1./4, 1./4,   0.]]
292ERROR_ATKINSON = \
293    [[   0.,   -1, 1./8, 1./8],
294     [ 1./8, 1./8, 1./8,   0.],
295     [   0., 1./8,   0.,   0.]]
296## This is Stevenson-Arce in hex lattice
297#ERROR_STAR = \
298#    [[      0.,      0.,      0.,      -1,      0.,  32./200,      0.],
299#     [ 12./200,      0., 26./200,      0., 30./200,       0., 16./200],
300#     [      0., 12./200,      0., 26./200,      0.,  12./200,      0.],
301#     [  5./200,      0., 12./200,      0., 12./200,       0.,  5./200]]
302## This is an attempt at implementing Stevenson-Arce in square lattice
303#ERROR_STAR = \
304#    [[      0.,      0.,      -1, 32./200,      0.],
305#     [  6./200, 19./200, 28./200, 23./200,  8./200],
306#     [      0., 12./200, 26./200, 12./200,      0.],
307#     [  2./200,  9./200, 12./200,  9./200,  2./200]]
308
309def test3xx(src, mat, serpentine):
310    (w, h) = src.size()
311    dest = Image((w, h))
312    lines = len(mat)
313    rows = len(mat[0])
314    offset = mat[0].index(-1)
315    ey = [[0.] * (w + rows - 1) for x in range(lines)]
316    for y in range(h):
317        ex = [0.] * (rows - offset)
318        if serpentine and y & 1:
319            xrange = range(w - 1, -1, -1)
320        else:
321            xrange = range(w)
322        for x in xrange:
323            # Set pixel
324            c = src.getGray(x, y) + ex[0] + ey[0][x + offset]
325            d = c > 0.5
326            dest.setGray(x, y, d)
327            error = c - d
328            # Propagate first line of error
329            for dx in range(rows - offset - 2):
330                ex[dx] = ex[dx + 1] + error * mat[0][offset + 1 + dx]
331            ex[rows - offset - 2] = error * mat[0][rows - 1]
332            # Propagate next lines
333            if serpentine and y & 1:
334                for dy in range(1, lines):
335                    for dx in range(rows):
336                        ey[dy][x + dx] += error * mat[dy][rows - 1 - dx]
337            else:
338                for dy in range(1, lines):
339                    for dx in range(rows):
340                        ey[dy][x + dx] += error * mat[dy][dx]
341        for dy in range(lines - 1):
342            ey[dy] = ey[dy + 1]
343        ey[lines - 1] = [0.] * (w + rows - 1)
344    return dest
345
346test3xx(lenna256bw, ERROR_NAIVE, False).save("out3-0-1.png")
347test3xx(gradient256bw, ERROR_NAIVE, False).save("grad3-0-1.png")
348
349test3xx(lenna256bw, ERROR_FSTEIN, False).save("out3-1-1.png")
350test3xx(gradient256bw, ERROR_FSTEIN, False).save("grad3-1-1.png")
351test3xx(lenna256bw, ERROR_FSTEIN, True).save("out3-1-2.png")
352test3xx(gradient256bw, ERROR_FSTEIN, True).save("grad3-1-2.png")
353
354test3xx(lenna256bw, ERROR_FAN, False).save("out3-2-1.png")
355test3xx(gradient256bw, ERROR_FAN, False).save("grad3-2-1.png")
356
357test3xx(lenna256bw, ERROR_JAJUNI, False).save("out3-2-2.png")
358test3xx(gradient256bw, ERROR_JAJUNI, False).save("grad3-2-2.png")
359
360test3xx(lenna256bw, ERROR_STUCKI, False).save("out3-2-3.png")
361test3xx(gradient256bw, ERROR_STUCKI, False).save("grad3-2-3.png")
362
363test3xx(lenna256bw, ERROR_BURKES, False).save("out3-2-4.png")
364test3xx(gradient256bw, ERROR_BURKES, False).save("grad3-2-4.png")
365
366test3xx(lenna256bw, ERROR_SIERRA, False).save("out3-2-5.png")
367test3xx(gradient256bw, ERROR_SIERRA, False).save("grad3-2-5.png")
368
369test3xx(lenna256bw, ERROR_SIERRA2, False).save("out3-2-6.png")
370test3xx(gradient256bw, ERROR_SIERRA2, False).save("grad3-2-6.png")
371
372test3xx(lenna256bw, ERROR_FILTERLITE, False).save("out3-2-7.png")
373test3xx(gradient256bw, ERROR_FILTERLITE, False).save("grad3-2-7.png")
374
375test3xx(lenna256bw, ERROR_ATKINSON, False).save("out3-2-8.png")
376test3xx(gradient256bw, ERROR_ATKINSON, False).save("grad3-2-8.png")
377
378#test3xx(lenna256bw, ERROR_STAR, False).save("out3-2-9.png")
379#test3xx(gradient256bw, ERROR_STAR, False).save("grad3-2-9.png")
380
381#test3xx(lenna256bw, ERROR_STAR, False).save("out3-2-9.png")
382#test3xx(gradient256bw, ERROR_STAR, False).save("grad3-2-9.png")
383
384##############################################################################
385print "Chapter 4. Grayscale dithering"
386
387# Output 4.0.1: 4x4 Bayer dithering, 3 colours
388def test401(src, mat):
389    (w, h) = src.size()
390    dest = Image((w, h))
391    dx = len(mat[0])
392    dy = len(mat)
393    for y in range(h):
394        for x in range(w):
395            c = src.getGray(x, y)
396            threshold = (1. + mat[y % dy][x % dx]) / (dx * dy + 1)
397            if c < 0.5:
398                c = 0.5 * (c > threshold / 2)
399            else:
400                c = 0.5 + 0.5 * (c > 0.5 + threshold / 2)
401            dest.setGray(x, y, c)
402    return dest
403
404test401(lenna256bw, DITHER_BAYER44).save("out4-0-1.png")
405test401(gradient256bw, DITHER_BAYER44).save("grad4-0-1.png")
406
407# Output 4.0.2: standard Floyd-Steinberg, 3 colours
408def test402(src, mat, serpentine):
409    (w, h) = src.size()
410    dest = Image((w, h))
411    lines = len(mat)
412    rows = len(mat[0])
413    offset = mat[0].index(-1)
414    ey = [[0.] * (w + rows - 1) for x in range(lines)]
415    for y in range(h):
416        ex = [0.] * (rows - offset)
417        if serpentine and y & 1:
418            xrange = range(w - 1, -1, -1)
419        else:
420            xrange = range(w)
421        for x in xrange:
422            # Set pixel
423            c = src.getGray(x, y) + ex[0] + ey[0][x + offset]
424            d = 0.5 * (c > 0.25) + 0.5 * (c > 0.75)
425            dest.setGray(x, y, d)
426            error = c - d
427            # Propagate first line of error
428            for dx in range(rows - offset - 2):
429                ex[dx] = ex[dx + 1] + error * mat[0][offset + 1 + dx]
430            ex[rows - offset - 2] = error * mat[0][rows - 1]
431            # Propagate next lines
432            if serpentine and y & 1:
433                for dy in range(1, lines):
434                    for dx in range(rows):
435                        ey[dy][x + dx] += error * mat[dy][rows - 1 - dx]
436            else:
437                for dy in range(1, lines):
438                    for dx in range(rows):
439                        ey[dy][x + dx] += error * mat[dy][dx]
440        for dy in range(lines - 1):
441            ey[dy] = ey[dy + 1]
442        ey[lines - 1] = [0.] * (w + rows - 1)
443    return dest
444
445test402(lenna256bw, ERROR_FSTEIN, False).save("out4-0-2.png")
446test402(gradient256bw, ERROR_FSTEIN, False).save("grad4-0-2.png")
447
448# Pattern 4.1.1: gamma-corrected 50% gray, black-white halftone, 50% gray
449dest = Image((240, 80))
450for y in range(80):
451    for x in range(80):
452        dest.setGray(x, y, Gamma.ItoC(0.5))
453    for x in range(80, 160):
454        c = (x + y) & 1
455        dest.setGray(x, y, c)
456    for x in range(160, 240):
457        dest.setGray(x, y, 0.5)
458dest.save("pat4-1-1.png")
459
460# Output 4.2.1: gamma-corrected 2-colour Floyd-Steinberg
461# Output 4.2.2: gamma-corrected 3-colour Floyd-Steinberg
462# Output 4.2.3: gamma-corrected 4-colour Floyd-Steinberg
463def test42x(src, mat, threshold):
464    (w, h) = src.size()
465    dest = Image((w, h))
466    lines = len(mat)
467    rows = len(mat[0])
468    offset = mat[0].index(-1)
469    ey = [[0.] * (w + rows - 1) for x in range(lines)]
470    for y in range(h):
471        ex = [0.] * (rows - offset)
472        for x in range(w):
473            # Set pixel
474            c = Gamma.CtoI(src.getGray(x, y)) + ex[0] + ey[0][x + offset]
475            d = threshold(c)
476            dest.setGray(x, y, Gamma.ItoC(d))
477            error = c - d
478            # Propagate first line of error
479            for dx in range(rows - offset - 2):
480                ex[dx] = ex[dx + 1] + error * mat[0][offset + 1 + dx]
481            ex[rows - offset - 2] = error * mat[0][rows - 1]
482            # Propagate next lines
483            for dy in range(1, lines):
484                for dx in range(rows):
485                    ey[dy][x + dx] += error * mat[dy][dx]
486        for dy in range(lines - 1):
487            ey[dy] = ey[dy + 1]
488        ey[lines - 1] = [0.] * (w + rows - 1)
489    return dest
490
491def threshold_2(x):
492    if x < Gamma.CtoI(0.50):
493        return 0.
494    return 1.
495
496def threshold_3(x):
497    if x < Gamma.CtoI(0.25):
498        return 0.
499    elif x < Gamma.CtoI(0.75):
500        return Gamma.CtoI(0.5)
501    return 1.
502
503def threshold_4(x):
504    if x < Gamma.CtoI(0.17):
505        return 0.
506    elif x < Gamma.CtoI(0.50):
507        return Gamma.CtoI(0.3333)
508    elif x < Gamma.CtoI(0.83):
509        return Gamma.CtoI(0.6666)
510    return 1.
511
512test42x(lenna256bw, ERROR_FSTEIN, threshold_2).save("out4-2-1.png")
513test42x(gradient256bw, ERROR_FSTEIN, threshold_2).save("grad4-2-1.png")
514test42x(lenna256bw, ERROR_FSTEIN, threshold_3).save("out4-2-2.png")
515test42x(gradient256bw, ERROR_FSTEIN, threshold_3).save("grad4-2-2.png")
516test42x(lenna256bw, ERROR_FSTEIN, threshold_4).save("out4-2-3.png")
517test42x(gradient256bw, ERROR_FSTEIN, threshold_4).save("grad4-2-3.png")
518
519##############################################################################
520print "Chapter 5. Colour dithering"
521
522# Pattern 5.1.1: 8-colour palette
523dest = Image((512, 64))
524for x in range(512):
525    d = x / 64
526    r = (d & 2) >> 1
527    g = (d & 4) >> 2
528    b = d & 1
529    for y in range(64):
530        dest.setRgb(x, y, r, g, b)
531dest.save("pat5-1-1.png")
532
533# Load the 256x256 colour Lenna image
534lenna256 = Image("lenna256.png")
535
536# Output 5.1.1: 8-colour Floyd-Steinberg RGB dithering
537# Output 5.1.2: 8-colour gamma-corrected Floyd-Steinberg RGB dithering
538def test51x(src, mat, func):
539    (w, h) = src.size()
540    dest = Image((w, h))
541    tmp = [Image((w, h)), Image((w, h)), Image((w, h))]
542    for y in range(h):
543        for x in range(w):
544            rgb = src.getRgb(x, y)
545            for i in range(3):
546                tmp[i].setGray(x, y, rgb[i])
547    for i in range(3):
548        tmp[i] = func(tmp[i], mat, threshold_2)
549    for y in range(h):
550        for x in range(w):
551            (r, g, b) = [tmp[i].getGray(x, y) for i in range(3)]
552            dest.setRgb(x, y, r, g, b)
553    return dest
554
555test51x(lenna256, ERROR_FSTEIN, test3xx).save("out5-1-1.png")
556out512 = test51x(lenna256, ERROR_FSTEIN, test42x)
557out512.save("out5-1-2.png")
558
559# Pattern 5.2.1: different colours give the same result
560dest = Image((320, 160))
561for x in range(80):
562    for y in range(80):
563        r = DITHER_BAYER44[(y / 8) % 4][(x / 8) % 4] > 7
564        g = DITHER_BAYER44[(y / 8) % 4][(x / 8) % 4] > 13
565        b = DITHER_BAYER44[(y / 8) % 4][(x / 8) % 4] > 13
566        dest.setRgb(x, y, b, g, r)
567    for y in range(80, 160):
568        r = DITHER_BAYER44[y % 4][x % 4] > 7
569        g = DITHER_BAYER44[y % 4][x % 4] > 13
570        b = DITHER_BAYER44[y % 4][x % 4] > 13
571        dest.setRgb(x, y, b, g, r)
572for x in range(80, 160):
573    for y in range(80):
574        r = DITHER_BAYER44[(y / 8) % 4][(x / 8) % 4] > 7
575        g = DITHER_BAYER44[(y / 8) % 4][(x / 8 + 1) % 4] > 13
576        b = DITHER_BAYER44[(y / 8) % 4][(x / 8 + 1) % 4] > 13
577        dest.setRgb(x, y, b, g, r)
578    for y in range(80, 160):
579        r = DITHER_BAYER44[y % 4][x % 4] > 7
580        g = DITHER_BAYER44[y % 4][(x + 1) % 4] > 13
581        b = DITHER_BAYER44[y % 4][(x + 1) % 4] > 13
582        dest.setRgb(x, y, b, g, r)
583for x in range(160, 240):
584    for y in range(80):
585        r = DITHER_BAYER44[(y / 8 + 1) % 4][(x / 8 + 1) % 4] > 7
586        g = DITHER_BAYER44[(y / 8) % 4][(x / 8) % 4] > 13
587        b = DITHER_BAYER44[(y / 8 + 1) % 4][(x / 8) % 4] > 13
588        dest.setRgb(x, y, b, g, r)
589    for y in range(80, 160):
590        r = DITHER_BAYER44[(y + 1) % 4][(x + 1) % 4] > 7
591        g = DITHER_BAYER44[y % 4][x % 4] > 13
592        b = DITHER_BAYER44[(y + 1) % 4][x % 4] > 13
593        dest.setRgb(x, y, b, g, r)
594for x in range(240, 320):
595    for y in range(80):
596        r = DITHER_BAYER44[(y / 8 + 1) % 4][(x / 8) % 4] > 7
597        g = DITHER_BAYER44[(y / 8) % 4][(x / 8) % 4] > 13
598        b = DITHER_BAYER44[(y / 8) % 4][(x / 8 + 2) % 4] > 13
599        dest.setRgb(x, y, b, g, r)
600    for y in range(80, 160):
601        r = DITHER_BAYER44[(y + 1) % 4][x % 4] > 7
602        g = DITHER_BAYER44[y % 4][x % 4] > 13
603        b = DITHER_BAYER44[y % 4][(x + 2) % 4] > 13
604        dest.setRgb(x, y, b, g, r)
605dest.save("pat5-2-1.png")
606
607# Output 5.2.1: cropped 5.1.2
608# Output 5.2.2: close-up of cropped 5.1.2
609def test52x(src, x, y, w, h, z):
610    dest = Image((w * z, h * z))
611    for j in range(h):
612        for i in range(w):
613            (r, g, b) = src.getRgb(x + i, y + j)
614            for v in range(z):
615                for u in range(z):
616                    dest.setRgb(i * z + u, j * z + v, r, g, b)
617    return dest
618
619test52x(out512, 20, 70, 32, 32, 1).save("out5-2-1.png")
620test52x(out512, 20, 70, 32, 32, 6).save("out5-2-2.png")
621
622out523 = test51x(lenna256, ERROR_STUCKI, test42x)
623out523.save("out5-2-3.png")
624test52x(out523, 20, 70, 32, 32, 1).save("out5-2-4.png")
625test52x(out523, 20, 70, 32, 32, 6).save("out5-2-5.png")
626
627##############################################################################
628print "Finished"
629
630# Place temporary cruft below this
631import sys; sys.exit(0)
632
Note: See TracBrowser for help on using the repository browser.