-
Notifications
You must be signed in to change notification settings - Fork 7
/
dither.go
430 lines (390 loc) · 10.7 KB
/
dither.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package halfgone
import (
"image"
"image/color"
"math"
"math/rand"
"sort"
)
// A Ditherer convert a grayscale image with intensities going from 0 (black) to
// 255 (white) into a black and white image.
type Ditherer interface {
Apply(gray *image.Gray) *image.Gray
}
// ThresholdDitherer converts each pixel in a grayscale image to black or white
// depending on the intensity of the pixel. If a pixel's intensity is above the
// given threshold then the pixel becomes white, else it becomes black.
type ThresholdDitherer struct {
Threshold uint8
}
// Apply threshold dithering.
func (td ThresholdDitherer) Apply(gray *image.Gray) *image.Gray {
var (
bounds = gray.Bounds()
dithered = image.NewGray(bounds)
)
for x := bounds.Min.X; x < bounds.Max.X; x++ {
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
if gray.GrayAt(x, y).Y > td.Threshold {
dithered.SetGray(x, y, color.Gray{255}) // White
} else {
dithered.SetGray(x, y, color.Gray{0}) // Black
}
}
}
return dithered
}
// RandomThresholdDitherer works the same way as ThresholdDitherer except that
// the threshold is randomly sampled for each pixel. This way some pixels are
// white when they would have been actually black.
type RandomThresholdDitherer struct {
MaxThreshold int
RNG *rand.Rand
}
// Apply random threshold dithering.
func (rtd RandomThresholdDitherer) Apply(gray *image.Gray) *image.Gray {
var (
bounds = gray.Bounds()
dithered = image.NewGray(bounds)
)
for x := bounds.Min.X; x < bounds.Max.X; x++ {
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
var threshold = uint8(rtd.RNG.Intn(rtd.MaxThreshold + 1))
if gray.GrayAt(x, y).Y > threshold {
dithered.SetGray(x, y, color.Gray{255}) // White
} else {
dithered.SetGray(x, y, color.Gray{0}) // Black
}
}
}
return dithered
}
// ImportanceSampling implements importance sampling.
type ImportanceSampling struct {
N int // Number of points to sample
Threshold uint8 // Threshold after which intensities are ignored (the threshold is not ignored)
RNG *rand.Rand
}
// Apply importance sampling.
func (is ImportanceSampling) Apply(gray *image.Gray) *image.Gray {
var (
bounds = gray.Bounds()
dithered = makeGray(bounds, 255)
histogram = make(map[uint8][]image.Point)
sampled = make(map[image.Point]bool)
)
// Build histogram
for x := bounds.Min.X; x < bounds.Max.X; x++ {
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
var intensity = gray.GrayAt(x, y).Y
if intensity <= is.Threshold {
histogram[intensity] = append(histogram[intensity], image.Point{x, y})
}
}
}
// Build roulette wheel
var roulette = make([]int, is.Threshold+1)
roulette[0] = 256 * len(histogram[0])
for i := 1; i < len(roulette); i++ {
roulette[i] = roulette[i-1] + (256-i)*len(histogram[uint8(i)])
}
// Run the wheel
var i int
for i < is.N {
var (
ball = is.RNG.Intn(roulette[len(roulette)-1])
bin = uint8(sort.SearchInts(roulette, ball))
point = histogram[bin][is.RNG.Intn(len(histogram[bin]))]
)
// Add the point if it hasn't been already sampled
if !sampled[point] {
dithered.SetGray(point.X, point.Y, color.Gray{0})
sampled[point] = true
i++
}
}
return dithered
}
// GridDitherer implements Bosch and Herman's grid-based method.
type GridDitherer struct {
K int // Size in pixels of a side of a cell
Alpha float64 // Minimum desired number of points in a cell
Beta float64 // Maximum desired number of points in a cell
RNG *rand.Rand
}
// Apply random grid dithering.
func (gd GridDitherer) Apply(gray *image.Gray) *image.Gray {
var (
bounds = gray.Bounds()
dithered = makeGray(bounds, 255)
)
for x := bounds.Min.X; x < bounds.Max.X; x += gd.K {
for y := bounds.Min.Y; y < bounds.Max.Y; y += gd.K {
var (
cell = ImageToGray(gray.SubImage(image.Rect(x, y, x+gd.K, y+gd.K)))
mu = avgIntensity(cell)
n = math.Pow((1-mu)*gd.Beta, 2) / 3
)
if n < gd.Alpha {
n = 0
}
for k := 0; k < int(n); k++ {
var (
xx = randInt(x, min(x+gd.K, bounds.Max.X), gd.RNG)
yy = randInt(y, min(y+gd.K, bounds.Max.Y), gd.RNG)
)
dithered.SetGray(xx, yy, color.Gray{0})
}
}
}
return dithered
}
// A Pattern is a matrix of threshold values used for ordered dithering.
type Pattern [][]uint8
func applyOrderedDithering(gray *image.Gray, pattern Pattern) *image.Gray {
var (
order = len(pattern)
bounds = gray.Bounds()
dithered = makeGray(bounds, 255)
)
for x := bounds.Min.X; x < bounds.Max.X; x++ {
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
var threshold = pattern[x%order][y%order]
if gray.GrayAt(x, y).Y > threshold {
dithered.SetGray(x, y, color.Gray{255}) // White
} else {
dithered.SetGray(x, y, color.Gray{0}) // Black
}
}
}
return dithered
}
// Order2OrderedDitherer implements order-2 ordered dithering.
type Order2OrderedDitherer struct{}
// Apply order-2 ordered dithering dithering.
func (o2od Order2OrderedDitherer) Apply(gray *image.Gray) *image.Gray {
var pattern = Pattern{
{0, 170},
{255, 85},
}
return applyOrderedDithering(gray, pattern)
}
// Order3OrderedDitherer implements order-3 ordered dithering.
type Order3OrderedDitherer struct{}
// Apply order-3 ordered dithering dithering.
func (o3od Order3OrderedDitherer) Apply(gray *image.Gray) *image.Gray {
var pattern = Pattern{
{0, 223, 95},
{191, 159, 63},
{127, 31, 255},
}
return applyOrderedDithering(gray, pattern)
}
// Order4OrderedDitherer implements order-4 ordered dithering.
type Order4OrderedDitherer struct{}
// Apply order-4 ordered dithering dithering.
func (o4od Order4OrderedDitherer) Apply(gray *image.Gray) *image.Gray {
var pattern = Pattern{
{0, 136, 34, 170},
{204, 68, 238, 102},
{51, 187, 17, 153},
{255, 119, 221, 85},
}
return applyOrderedDithering(gray, pattern)
}
// Order8OrderedDitherer implements order-8 ordered dithering.
type Order8OrderedDitherer struct{}
// Apply order-8 ordered dithering dithering.
func (o8od Order8OrderedDitherer) Apply(gray *image.Gray) *image.Gray {
var pattern = Pattern{
{0, 194, 48, 242, 12, 206, 60, 255},
{129, 64, 178, 113, 141, 76, 190, 125},
{32, 226, 16, 210, 44, 238, 28, 222},
{161, 97, 145, 80, 174, 109, 157, 93},
{8, 202, 56, 250, 4, 198, 52, 246},
{137, 72, 186, 121, 133, 68, 182, 117},
{40, 234, 24, 218, 36, 230, 20, 214},
{170, 105, 153, 89, 165, 101, 149, 85},
}
return applyOrderedDithering(gray, pattern)
}
// A DiffusionCell indicates a relative position and a diffusion intensity used for error diffusion.
type DiffusionCell struct {
x int
y int
m int16
}
// A DiffusionMask contains a slice of DiffusionCells and a normalization divisor.
type DiffusionMask struct {
divisor int16
cells []DiffusionCell
}
func applyErrorDiffusion(gray *image.Gray, mask DiffusionMask) *image.Gray {
var (
bounds = gray.Bounds()
dithered = copyGray(gray)
)
for y := bounds.Min.Y; y < bounds.Max.Y; y++ { // Top to bottom
for x := bounds.Min.X; x < bounds.Max.X; x++ { // Left to right
var oldPixel = dithered.GrayAt(x, y)
// Set the pixel to black or white
var newPixel = color.Gray{0} // Black
if oldPixel.Y > 127 {
newPixel = color.Gray{255} // White
}
dithered.SetGray(x, y, newPixel)
// Determine the quantization error
var quant = (int16(oldPixel.Y) - int16(newPixel.Y)) / mask.divisor
// Spread the quantization error
for _, c := range mask.cells {
var newIntensity = int16(dithered.GrayAt(x+c.x, y+c.y).Y) + int16(c.m*quant)
dithered.SetGray(x+c.x, y+c.y, color.Gray{i16ToUI8(newIntensity)})
}
}
}
return dithered
}
// FloydSteinbergDitherer implements Floyd-Steingberg dithering.
type FloydSteinbergDitherer struct{}
// Apply Floyd-Steinberg dithering.
func (fsd FloydSteinbergDitherer) Apply(gray *image.Gray) *image.Gray {
var mask = DiffusionMask{
divisor: 16,
cells: []DiffusionCell{
{1, 0, 7},
{-1, 1, 3},
{0, 1, 5},
{1, 1, 1},
},
}
return applyErrorDiffusion(gray, mask)
}
// JarvisJudiceNinkeDitherer implements Jarvis-Judice-Ninke dithering.
type JarvisJudiceNinkeDitherer struct{}
// Apply Jarvis-Judice-Ninke dithering.
func (jjnd JarvisJudiceNinkeDitherer) Apply(gray *image.Gray) *image.Gray {
var mask = DiffusionMask{
divisor: 48,
cells: []DiffusionCell{
{1, 0, 7},
{2, 0, 5},
{-2, 1, 3},
{-1, 1, 5},
{0, 1, 7},
{1, 1, 5},
{2, 1, 3},
{-2, 2, 1},
{-1, 2, 3},
{0, 2, 5},
{1, 2, 3},
{2, 2, 1},
},
}
return applyErrorDiffusion(gray, mask)
}
// StuckiDitherer implements Stucki dithering.
type StuckiDitherer struct{}
// Apply Stucki dithering.
func (sd StuckiDitherer) Apply(gray *image.Gray) *image.Gray {
var mask = DiffusionMask{
divisor: 42,
cells: []DiffusionCell{
{1, 0, 8},
{2, 0, 4},
{-2, 1, 2},
{-1, 1, 4},
{0, 1, 8},
{1, 1, 4},
{2, 1, 2},
},
}
return applyErrorDiffusion(gray, mask)
}
// AtkinsonDitherer implements Atkinson dithering.
type AtkinsonDitherer struct{}
// Apply Atkinson dithering.
func (ad AtkinsonDitherer) Apply(gray *image.Gray) *image.Gray {
var mask = DiffusionMask{
divisor: 8,
cells: []DiffusionCell{
{1, 0, 1},
{2, 0, 1},
{-1, 1, 1},
{0, 1, 1},
{1, 1, 1},
{0, 2, 1},
},
}
return applyErrorDiffusion(gray, mask)
}
// BurkesDitherer implements Burkes dithering.
type BurkesDitherer struct{}
// Apply Burkes dithering.
func (ad BurkesDitherer) Apply(gray *image.Gray) *image.Gray {
var mask = DiffusionMask{
divisor: 32,
cells: []DiffusionCell{
{1, 0, 8},
{2, 0, 4},
{-2, 1, 2},
{-1, 1, 4},
{0, 1, 8},
{1, 1, 4},
{2, 1, 2},
},
}
return applyErrorDiffusion(gray, mask)
}
// SierraDitherer implements Sierra dithering.
type SierraDitherer struct{}
// Apply Sierra dithering.
func (sd SierraDitherer) Apply(gray *image.Gray) *image.Gray {
var mask = DiffusionMask{
divisor: 32,
cells: []DiffusionCell{
{1, 0, 5},
{2, 0, 3},
{-2, 1, 2},
{-1, 1, 4},
{0, 1, 5},
{1, 1, 4},
{2, 1, 2},
{-1, 2, 2},
{0, 2, 3},
{1, 2, 2},
},
}
return applyErrorDiffusion(gray, mask)
}
// TwoRowSierraDitherer implements Two-row Sierra dithering.
type TwoRowSierraDitherer struct{}
// Apply Two-row Sierra dithering.
func (sd TwoRowSierraDitherer) Apply(gray *image.Gray) *image.Gray {
var mask = DiffusionMask{
divisor: 16,
cells: []DiffusionCell{
{1, 0, 4},
{2, 0, 3},
{-2, 1, 2},
{-1, 1, 2},
{0, 1, 3},
{1, 1, 2},
{2, 1, 1},
},
}
return applyErrorDiffusion(gray, mask)
}
// SierraLiteDitherer implements Sierra Lite dithering.
type SierraLiteDitherer struct{}
// Apply Sierra Lite dithering.
func (sd SierraLiteDitherer) Apply(gray *image.Gray) *image.Gray {
var mask = DiffusionMask{
divisor: 4,
cells: []DiffusionCell{
{1, 0, 1},
{-1, 1, 1},
{0, 1, 1},
},
}
return applyErrorDiffusion(gray, mask)
}