-
Notifications
You must be signed in to change notification settings - Fork 1
/
marchingsquare.go
192 lines (173 loc) · 3.37 KB
/
marchingsquare.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
package marchingsquare
import (
"image"
"image/jpeg"
"image/png"
"os"
)
var _ = png.Decode
var _ = jpeg.Decode
func panicIfErr(err error) {
if err != nil {
panic(err)
}
}
const (
directionNone = iota
diretionUp
directionLeft
directionDown
directionRight
)
type TestFunc func(r, g, b, a uint32) bool
type marchingSquare struct {
img image.Image
previousStep int
nextStep int
result []image.Point
test TestFunc
}
func (m *marchingSquare) findStartPoint() (int, int) {
b := m.img.Bounds()
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
r, g, b, a := m.img.At(x, y).RGBA()
if m.test(r, g, b, a) {
return x, y
}
}
}
return 0, 0
}
func (m *marchingSquare) isPixelSolid(x, y int) bool {
rect := m.img.Bounds()
if x < rect.Min.X || x > rect.Max.X || y < rect.Min.Y || y > rect.Max.Y {
return false
}
r, g, b, a := m.img.At(x, y).RGBA()
if m.test(r, g, b, a) {
return true
}
return false
}
func (m *marchingSquare) step(x, y int) {
upLeft := m.isPixelSolid(x-1, y-1)
upRight := m.isPixelSolid(x, y-1)
downLeft := m.isPixelSolid(x-1, y)
downRight := m.isPixelSolid(x, y)
m.previousStep = m.nextStep
state := 0
if upLeft {
state |= 1
}
if upRight {
state |= 2
}
if downLeft {
state |= 4
}
if downRight {
state |= 8
}
switch state {
case 1:
m.nextStep = diretionUp
case 2:
m.nextStep = directionRight
case 3:
m.nextStep = directionRight
case 4:
m.nextStep = directionLeft
case 5:
m.nextStep = diretionUp
case 6:
if m.previousStep == diretionUp {
m.nextStep = directionLeft
} else {
m.nextStep = directionRight
}
case 7:
m.nextStep = directionRight
case 8:
m.nextStep = directionDown
case 9:
if m.previousStep == directionRight {
m.nextStep = diretionUp
} else {
m.nextStep = directionDown
}
case 10:
m.nextStep = directionDown
case 11:
m.nextStep = directionDown
case 12:
m.nextStep = directionLeft
case 13:
m.nextStep = diretionUp
case 14:
m.nextStep = directionLeft
default:
m.nextStep = directionNone
}
}
func (m *marchingSquare) walk(startX, startY int) {
b := m.img.Bounds()
if startX < b.Min.X {
startX = b.Min.X
}
if startX > b.Max.X {
startX = b.Max.X
}
if startY < b.Min.Y {
startY = b.Min.Y
}
if startY > b.Max.Y {
startY = b.Max.Y
}
x, y := startX, startY
for {
m.step(x, y)
if x >= b.Min.X && x < b.Max.X && y >= b.Min.Y && y < b.Max.Y {
m.result = append(m.result, image.Point{x, y})
}
switch m.nextStep {
case diretionUp:
y--
case directionLeft:
x--
case directionDown:
y++
case directionRight:
x++
}
if x == startX && y == startY {
break
}
}
}
func (m *marchingSquare) doMarch(img image.Image, f TestFunc) []image.Point {
m.img = img
m.test = f
m.result = make([]image.Point, 0)
m.walk(m.findStartPoint())
return m.result
}
func (m *marchingSquare) doMarchWithFileName(filename string, f TestFunc) []image.Point {
file, err := os.Open(filename)
defer file.Close()
panicIfErr(err)
img, _, err := image.Decode(file)
panicIfErr(err)
return m.doMarch(img, f)
}
func Process(img image.Image, f TestFunc) []image.Point {
m := marchingSquare{}
return m.doMarch(img, f)
}
func ProcessWithFile(filename string, f TestFunc) []image.Point {
m := marchingSquare{}
return m.doMarchWithFileName(filename, f)
}
func TransparentTest(r, g, b, a uint32) bool {
return a > 0
}