From b8b7ff71671e0412004f2398851f5a861b0f7d8d Mon Sep 17 00:00:00 2001 From: David Ashby Date: Mon, 11 Jan 2021 19:42:42 -0500 Subject: [PATCH] make the error-propagation handling generic --- quantizer.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/quantizer.go b/quantizer.go index 9bb41fd..0c0340e 100644 --- a/quantizer.go +++ b/quantizer.go @@ -122,18 +122,28 @@ func bayerDithering(level int, invert bool) quantizerFunction { } } +func applyError(diffusionMatrix map[coord]float64, divisor float64, quantError float64, currentPixel coord, errMap map[coord]float64) { + for c, i := range diffusionMatrix { + target := coord{x: currentPixel.x + c.x, y: currentPixel.y + c.y} + errMap[target] = errMap[target] + (quantError * (i / divisor)) + } +} + func simpleErrorDiffusion() quantizerFunction { errMap := make(map[coord]float64) + diffusionMatrix := map[coord]float64{ + {x: 1, y: 0}: 1.0, + {x: 0, y: 1}: 1.0, + } 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 + p := coord{x: x, y: y} + l := luminence(c) + errMap[p] + delete(errMap, p) // don't let the error map grow too big if l > 0.5 { - 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) + applyError(diffusionMatrix, 2.0, l-1.0, p, errMap) return color.White } - 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) + applyError(diffusionMatrix, 2.0, l, p, errMap) return color.Black } }