Skip to content

Commit 16f81b6

Browse files
committed
image/draw: add FloydSteinberg Drawer example
Updates #16360 Change-Id: I80b981aa291a8e16d2986d4a2dfd84d3819bf488 Reviewed-on: https://go-review.googlesource.com/29443 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Joe Tsai <[email protected]>
1 parent e719147 commit 16f81b6

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/image/draw/example_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2016 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package draw_test
6+
7+
import (
8+
"fmt"
9+
"image"
10+
"image/color"
11+
"image/draw"
12+
"math"
13+
)
14+
15+
func ExampleDrawer_floydSteinberg() {
16+
const width = 130
17+
const height = 50
18+
19+
im := image.NewGray(image.Rectangle{Max: image.Point{X: width, Y: height}})
20+
for x := 0; x < width; x++ {
21+
for y := 0; y < height; y++ {
22+
dist := math.Sqrt(math.Pow(float64(x-width/2), 2)/3+math.Pow(float64(y-height/2), 2)) / (height / 1.5) * 255
23+
var gray uint8
24+
if dist > 255 {
25+
gray = 255
26+
} else {
27+
gray = uint8(dist)
28+
}
29+
im.SetGray(x, y, color.Gray{Y: 255 - gray})
30+
}
31+
}
32+
pi := image.NewPaletted(im.Bounds(), []color.Color{
33+
color.Gray{Y: 255},
34+
color.Gray{Y: 160},
35+
color.Gray{Y: 70},
36+
color.Gray{Y: 35},
37+
color.Gray{Y: 0},
38+
})
39+
40+
draw.FloydSteinberg.Draw(pi, im.Bounds(), im, image.ZP)
41+
shade := []string{" ", "░", "▒", "▓", "█"}
42+
for i, p := range pi.Pix {
43+
fmt.Print(shade[p])
44+
if (i+1)%width == 0 {
45+
fmt.Print("\n")
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)