From cbe14d53cda6524c1d33b2e22a4539db80d452e7 Mon Sep 17 00:00:00 2001 From: David Ashby Date: Mon, 11 Jan 2021 19:54:03 -0500 Subject: [PATCH] wrap all that repetition in a closure --- quantizer.go | 58 +++++++++++++++------------------------------------- 1 file changed, 16 insertions(+), 42 deletions(-) diff --git a/quantizer.go b/quantizer.go index 4d84e0d..248b999 100644 --- a/quantizer.go +++ b/quantizer.go @@ -134,15 +134,7 @@ func applyError(diffusion diffusion, quantError float64, currentPixel coord, err } } -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, - }, - } +func diffuser(errMap map[coord]float64, d diffusion) quantizerFunction { return func(x int, y int, c color.Color) color.Color { p := coord{x: x, y: y} 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 { errMap := make(map[coord]float64) d := diffusion{ @@ -167,17 +171,7 @@ func floydSteinberg() quantizerFunction { {x: 1, y: 1}: 1.0, }, } - return func(x int, y int, c color.Color) color.Color { - 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 - } + return diffuser(errMap, d) } func jarvisJudiceNinke() quantizerFunction { @@ -201,17 +195,7 @@ func jarvisJudiceNinke() quantizerFunction { {x: 2, y: 2}: 1.0, }, } - return func(x int, y int, c color.Color) color.Color { - 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 - } + return diffuser(errMap, d) } func atkinson() quantizerFunction { @@ -227,17 +211,7 @@ func atkinson() quantizerFunction { {x: 0, y: 2}: 1.0, }, } - return func(x int, y int, c color.Color) color.Color { - 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 - } + return diffuser(errMap, d) } // That is, "relative luminance": https://en.wikipedia.org/wiki/Relative_luminance.