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