From 088ba19e3caaf45f013ea25ec34183a16ba5c1b9 Mon Sep 17 00:00:00 2001 From: David Ashby Date: Mon, 11 Jan 2021 18:14:22 -0500 Subject: [PATCH] fix basic errormap diffusion to look more-or-less correct --- quantizer.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/quantizer.go b/quantizer.go index 16d8b85..9bb41fd 100644 --- a/quantizer.go +++ b/quantizer.go @@ -40,7 +40,7 @@ func naiveBW(_, _ int, c color.Color) color.Color { // randomNoise injects random noise into the quantization step func randomNoise(_, _ int, c color.Color) color.Color { l := luminence(c) - if (l + rand.Float64() - 0.5) > 0.5 { + if (l + (rand.Float64() - 0.5)) > 0.5 { return color.White } return color.Black @@ -126,15 +126,14 @@ func simpleErrorDiffusion() quantizerFunction { errMap := make(map[coord]float64) return func(x int, y int, c color.Color) color.Color { 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 { - errMap[coord{x: x + 1, y: y}] = (l - 1.0) / 2.0 - errMap[coord{x: x, y: y + 1}] = (l - 1.0) / 2.0 - delete(errMap, coord{x: x, y: y}) + 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}] = errMap[coord{x: x, y: y + 1}] + ((l - 1.0) / 2.0) return color.White } - errMap[coord{x: x + 1, y: y}] = l / 2.0 - errMap[coord{x: x, y: y + 1}] = l / 2.0 - delete(errMap, coord{x: x, y: y}) + errMap[coord{x: x + 1, y: y}] = errMap[coord{x: x + 1, y: y}] + (l / 2.0) + errMap[coord{x: x, y: y + 1}] = errMap[coord{x: x, y: y + 1}] + (l / 2.0) return color.Black } }