-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path01_blending.go
198 lines (169 loc) · 4.49 KB
/
01_blending.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Seriál "Programovací jazyk Go"
// https://www.root.cz/serialy/programovaci-jazyk-go/
//
// Patnáctá část
// Programovací jazyk Go a grafika: tvorba animovaných GIFů, grafická knihovna GG
// https://www.root.cz/clanky/programovaci-jazyk-go-a-grafika-tvorba-animovanych-gifu-graficka-knihovna-gg/
//
// Repositář:
// https://github.com/tisnik/go-root/
//
// Seznam demonstračních příkladů z patnácté části:
// https://github.com/tisnik/go-root/blob/master/article_15/README.md
//
// Demonstrační příklad číslo 1:
// Ukázka blendingu
//
// Dokumentace ve stylu "literate programming":
// https://tisnik.github.io/go-root/article_15/01_blending.html
package main
import (
"image"
"image/color"
"image/draw"
"image/png"
"os"
)
const width = 256
const height = 256
// DrawHorizontalLine draws horizontal line from [x1,y] to [x2,y] by specified color
func DrawHorizontalLine(img *image.NRGBA, color color.Color, x1 int, x2 int, y int) {
if x1 > x2 {
x1, x2 = x2, x1
}
for x := x1; x < x2; x++ {
img.Set(x, y, color)
}
}
// DrawVerticalLine draws vertical line from [x,y1] to [x,y2] by specified color
func DrawVerticalLine(img *image.NRGBA, color color.Color, x int, y1 int, y2 int) {
if y1 > y2 {
y1, y2 = y2, y1
}
for y := y1; y < y2; y++ {
img.Set(x, y, color)
}
}
// Abs computes absolute value for given signed integer
func Abs(x int) int {
if x < 0 {
return -x
}
return x
}
// Step computes whether the horizontal/vertical step should be positive or negative
func Step(v1 int, v2 int) int {
if v1 < v2 {
return 1
}
return -1
}
// DrawLine draws line from [x1,y1] to [x2,y2] by specified color
func DrawLine(img *image.NRGBA, color color.Color, x1 int, y1 int, x2 int, y2 int) {
// specialni pripad - svisla usecka
if x1 == x2 {
DrawVerticalLine(img, color, x1, y1, y2)
return
}
// specialni pripad - vodorovna usecka
if y1 == y2 {
DrawHorizontalLine(img, color, x1, x2, y1)
return
}
// takze mame smulu a musime pouzit plnou verzi algoritmu
// zrcadleni algoritmu pro dalsi oktanty
x := x1
y := y1
// konstanty pouzite pri vykreslovani
dx := Abs(x2 - x1)
dy := Abs(y2 - y1)
sx := Step(x1, x2)
sy := Step(y1, y2)
// pocatecni hodnota akumulatoru chyby
err := dx >> 1
if dx <= dy {
err = -dy >> 1
}
// vse je pripraveno k vlastnimu vykresleni usecky
for {
img.Set(x, y, color)
// test, zda se jiz doslo k poslednimu bodu
if x == x2 && y == y2 {
break
}
e2 := err
if e2 > -dx {
// prepocet kumulovane chyby
err -= dy
// posun na predchozi ci dalsi pixel na radku
x += sx
}
if e2 < dy {
// prepocet kumulovane chyby
err += dx
// posun na predchozi ci nasledujici radek
y += sy
}
}
}
// CreateStringArt functions draws a so-called string art into the test image
// https://en.wikipedia.org/wiki/String_art
func CreateStringArt(width int, height int) draw.Image {
img := image.NewNRGBA(image.Rect(0, 0, width, height))
c := color.RGBA{255, 255, 255, 255}
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
img.Set(x, y, c)
}
}
c = color.RGBA{0, 0, 255, 255}
DrawLine(img, c, 20, 10, 245, 10)
c = color.RGBA{255, 0, 0, 255}
DrawLine(img, c, 10, 20, 10, 245)
c = color.RGBA{0, 255, 0, 255}
DrawLine(img, c, 20, 20, width>>1, height>>1)
c = color.RGBA{0, 0, 0, 255}
for x := 10; x < width; x += 10 {
DrawLine(img, c, width-5, x, width-x, height-5)
}
return img
}
// CreateChessboard function draws chessboard onto the test image
func CreateChessboard(width int, height int) draw.Image {
img := image.NewNRGBA(image.Rect(0, 0, width, height))
palette := make(map[int]color.RGBA, 2)
palette[0] = color.RGBA{150, 205, 50, 255}
palette[1] = color.RGBA{0, 100, 0, 0}
indexColor := 0
boardSize := 8
horizontalBlock := int(width / boardSize)
verticalBlock := int(height / boardSize)
xFrom := 0
xTo := horizontalBlock
for x := 0; x < boardSize; x++ {
yFrom := 0
yTo := verticalBlock
for y := 0; y < boardSize; y++ {
r := image.Rect(xFrom, yFrom, xTo, yTo)
draw.Draw(img, r, &image.Uniform{palette[indexColor]}, image.ZP, draw.Src)
yFrom = yTo
yTo += verticalBlock
indexColor = 1 - indexColor
}
xFrom = xTo
xTo += horizontalBlock
indexColor = 1 - indexColor
}
return img
}
func main() {
img1 := CreateChessboard(width, height)
img2 := CreateStringArt(width, height)
draw.Draw(img2, img2.Bounds(), img1, image.ZP, draw.Over)
outfile, err := os.Create("01.png")
if err != nil {
panic(err)
}
defer outfile.Close()
png.Encode(outfile, img2)
}