wrap all that repetition in a closure

This commit is contained in:
David 2021-01-11 19:54:03 -05:00
parent b9c6cf1a07
commit cbe14d53cd

View File

@ -134,15 +134,7 @@ func applyError(diffusion diffusion, quantError float64, currentPixel coord, err
} }
} }
func simpleErrorDiffusion() quantizerFunction { func diffuser(errMap map[coord]float64, d diffusion) quantizerFunction {
errMap := make(map[coord]float64)
d := diffusion{
divisor: 2.0,
matrix: 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 { return func(x int, y int, c color.Color) color.Color {
p := coord{x: x, y: y} p := coord{x: x, y: y}
l := luminence(c) + errMap[p] l := luminence(c) + errMap[p]
@ -156,6 +148,18 @@ func simpleErrorDiffusion() quantizerFunction {
} }
} }
func simpleErrorDiffusion() quantizerFunction {
errMap := make(map[coord]float64)
d := diffusion{
divisor: 2.0,
matrix: map[coord]float64{
{x: 1, y: 0}: 1.0,
{x: 0, y: 1}: 1.0,
},
}
return diffuser(errMap, d)
}
func floydSteinberg() quantizerFunction { func floydSteinberg() quantizerFunction {
errMap := make(map[coord]float64) errMap := make(map[coord]float64)
d := diffusion{ d := diffusion{
@ -167,17 +171,7 @@ func floydSteinberg() quantizerFunction {
{x: 1, y: 1}: 1.0, {x: 1, y: 1}: 1.0,
}, },
} }
return func(x int, y int, c color.Color) color.Color { return diffuser(errMap, d)
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 {
applyError(d, l-1.0, p, errMap)
return color.White
}
applyError(d, l, p, errMap)
return color.Black
}
} }
func jarvisJudiceNinke() quantizerFunction { func jarvisJudiceNinke() quantizerFunction {
@ -201,17 +195,7 @@ func jarvisJudiceNinke() quantizerFunction {
{x: 2, y: 2}: 1.0, {x: 2, y: 2}: 1.0,
}, },
} }
return func(x int, y int, c color.Color) color.Color { return diffuser(errMap, d)
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 {
applyError(d, l-1.0, p, errMap)
return color.White
}
applyError(d, l, p, errMap)
return color.Black
}
} }
func atkinson() quantizerFunction { func atkinson() quantizerFunction {
@ -227,17 +211,7 @@ func atkinson() quantizerFunction {
{x: 0, y: 2}: 1.0, {x: 0, y: 2}: 1.0,
}, },
} }
return func(x int, y int, c color.Color) color.Color { return diffuser(errMap, d)
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 {
applyError(d, l-1.0, p, errMap)
return color.White
}
applyError(d, l, p, errMap)
return color.Black
}
} }
// That is, "relative luminance": https://en.wikipedia.org/wiki/Relative_luminance. // That is, "relative luminance": https://en.wikipedia.org/wiki/Relative_luminance.