fix basic errormap diffusion to look more-or-less correct

This commit is contained in:
David 2021-01-11 18:14:22 -05:00
parent 254167de4e
commit 088ba19e3c

View File

@ -40,7 +40,7 @@ func naiveBW(_, _ int, c color.Color) color.Color {
// randomNoise injects random noise into the quantization step // randomNoise injects random noise into the quantization step
func randomNoise(_, _ int, c color.Color) color.Color { func randomNoise(_, _ int, c color.Color) color.Color {
l := luminence(c) l := luminence(c)
if (l + rand.Float64() - 0.5) > 0.5 { if (l + (rand.Float64() - 0.5)) > 0.5 {
return color.White return color.White
} }
return color.Black return color.Black
@ -126,15 +126,14 @@ func simpleErrorDiffusion() quantizerFunction {
errMap := make(map[coord]float64) errMap := make(map[coord]float64)
return func(x int, y int, c color.Color) color.Color { return func(x int, y int, c color.Color) color.Color {
l := luminence(c) + errMap[coord{x: x, y: y}] l := luminence(c) + errMap[coord{x: x, y: y}]
delete(errMap, coord{x: x, y: y}) // don't let the error map grow too big
if l > 0.5 { if l > 0.5 {
errMap[coord{x: x + 1, y: y}] = (l - 1.0) / 2.0 errMap[coord{x: x + 1, y: y}] = errMap[coord{x: x + 1, y: y}] + ((l - 1.0) / 2.0)
errMap[coord{x: x, y: y + 1}] = (l - 1.0) / 2.0 errMap[coord{x: x, y: y + 1}] = errMap[coord{x: x, y: y + 1}] + ((l - 1.0) / 2.0)
delete(errMap, coord{x: x, y: y})
return color.White return color.White
} }
errMap[coord{x: x + 1, y: y}] = l / 2.0 errMap[coord{x: x + 1, y: y}] = errMap[coord{x: x + 1, y: y}] + (l / 2.0)
errMap[coord{x: x, y: y + 1}] = l / 2.0 errMap[coord{x: x, y: y + 1}] = errMap[coord{x: x, y: y + 1}] + (l / 2.0)
delete(errMap, coord{x: x, y: y})
return color.Black return color.Black
} }
} }