make the error-propagation handling generic

This commit is contained in:
David 2021-01-11 19:42:42 -05:00
parent 088ba19e3c
commit b8b7ff7167

View File

@ -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
}
}