forked from dennwc/gotrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgotrace.go
175 lines (162 loc) · 4.77 KB
/
gotrace.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
// Package gotrace provides bindings for potracelib - bitmap to vector graphics converter.
// More info at http://potrace.sourceforge.net/potracelib.pdf
package gotraceb
// #cgo LDFLAGS: -lm
// #include <stdlib.h>
// #include "bitmap.h"
// #include "potracelib.h"
// int wordSize() { return sizeof(potrace_word); }
// potrace_param_t* newParams() { return malloc(sizeof(potrace_param_t)); }
// static inline void bmSet(potrace_bitmap_t *bm, int x, int y) { BM_SET(bm,x,y); }
// static inline void bmClr(potrace_bitmap_t *bm, int x, int y) { BM_CLR(bm,x,y); }
// static inline int bmGet(potrace_bitmap_t *bm, int x, int y) { return BM_GET(bm,x,y); }
// static inline potrace_word bmRaw(potrace_bitmap_t *bm, int i) { return bm->map[i]; }
import "C"
import (
//"bytes"
"fmt"
"image"
//"io"
gtr "github.com/dennwc/gotrace"
"image/color"
"unsafe"
)
var wordSize = int(C.wordSize()) * 8
func Version() string {
return C.GoString(C.potrace_version())
}
/*func (p Path) ToSvgPath() string {
c := p.Curve
if len(c) == 0 {
return ""
}
l := len(c) - 1
buf := bytes.NewBuffer(nil)
for i, p := range c {
if i == 0 {
fmt.Fprintf(buf, "M%f,%f ", c[l].Pt[2][0], c[l].Pt[2][1]) // end point == start point
}
if p.Type == Corner {
fmt.Fprintf(buf, "L%f,%f", p.Pt[1][0], p.Pt[1][1]) // no last point for now - may be closed with Z
} else if p.Type == Bezier {
fmt.Fprintf(buf, "C%f,%f %f,%f %f,%f", p.Pt[0][0], p.Pt[0][1], p.Pt[1][0], p.Pt[1][1], p.Pt[2][0], p.Pt[2][1])
}
if i == l {
fmt.Fprintf(buf, " Z")
} else if p.Type == Corner {
fmt.Fprintf(buf, " %f,%f ", p.Pt[2][0], p.Pt[2][1]) // last point for corner
}
}
return buf.String()
}*/
func imageToBitmap(img image.Image, th func(x, y int, c color.Color) bool) []uint {
w, h := img.Bounds().Dx(), img.Bounds().Dy()
ww := w / wordSize
if w%wordSize != 0 {
ww++
}
words := make([]uint, ww*h)
for y := 0; y < img.Bounds().Dy(); y++ {
for x := 0; x < img.Bounds().Dx(); x++ {
if th(x, y, img.At(x, y)) {
i := y*ww + x/wordSize
words[i] = words[i] | (1 << uint(wordSize-1-x%wordSize))
}
}
}
return words
}
func Trace(bm *gtr.Bitmap, param *gtr.Params) ([]gtr.Path, error) {
if param == nil {
param = >r.Defaults
}
bmc := C.bm_new(C.int(bm.W), C.int(bm.H))
defer C.bm_free(bmc)
for y := 0; y < bm.H; y++ {
for x := 0; x < bm.W; x++ {
if bm.Get(x, y) {
C.bmSet(bmc, C.int(x), C.int(y))
} else {
C.bmClr(bmc, C.int(x), C.int(y))
}
}
}
par := C.newParams()
defer C.free(unsafe.Pointer(par))
par.turdsize = C.int(param.TurdSize)
par.turnpolicy = C.int(param.TurnPolicy)
par.alphamax = C.double(param.AlphaMax)
if param.OptiCurve {
par.opticurve = C.int(1)
} else {
par.opticurve = C.int(0)
}
par.opttolerance = C.double(param.OptTolerance)
st, err := C.potrace_trace(par, bmc)
defer C.potrace_state_free(st)
if err != nil {
return nil, err
} else if int(st.status) != 0 {
return nil, fmt.Errorf("trace failed")
}
return convPaths(st.plist), nil
}
func convPath(cur *C.potrace_path_t) (p gtr.Path) {
p.Area = int(cur.area)
if byte(cur.sign) == byte('+') {
p.Sign = +1
} else if byte(cur.sign) == byte('-') {
p.Sign = -1
}
// process curve
p.Curve = make([]gtr.Segment, int(cur.curve.n))
for i := range p.Curve {
p.Curve[i] = gtr.Segment{
Type: gtr.SegmType(*(*C.int)(unsafe.Pointer(uintptr(unsafe.Pointer(cur.curve.tag)) + unsafe.Sizeof(C.int(0))*uintptr(i)))),
Pnt: [3]gtr.Point{
pointFromArr(cur.curve.c, i, 0),
pointFromArr(cur.curve.c, i, 1),
pointFromArr(cur.curve.c, i, 2),
},
}
}
return
}
func convPaths(cur *C.potrace_path_t) (out []gtr.Path) {
if cur == nil {
return nil
}
for ; cur != nil; cur = cur.sibling {
p := convPath(cur)
p.Childs = convPaths(cur.childlist)
out = append(out, p)
}
return
}
func pointFromArr(c *[3]C.potrace_dpoint_t, i, j int) gtr.Point {
p := *(*[3]C.potrace_dpoint_t)(unsafe.Pointer(uintptr(unsafe.Pointer(c)) + unsafe.Sizeof([3]C.potrace_dpoint_t{})*uintptr(i)))
return gtr.Point{float64(p[j].x), float64(p[j].y)}
}
/*func WriteSvg(w io.Writer, rect image.Rectangle, paths []Path, color string) error {
if color == "" {
color = "#000000"
}
fmt.Fprintf(w, `<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" xmlns="http://www.w3.org/2000/svg" width="%dpt" height="%dpt" viewBox="0 0 %d %d" preserveAspectRatio="xMidYMid meet">
<g fill="%s" stroke="none">%s`,
rect.Dx(), rect.Dy(), rect.Dx(), rect.Dy(), color, "\n")
for i, p := range paths {
if p.Sign > 0 {
fmt.Fprint(w, `<path d="`)
}
fmt.Fprint(w, p.ToSvgPath())
if i+1 == len(paths) || paths[i+1].Sign > 0 {
fmt.Fprintln(w, `"/>`)
} else {
fmt.Fprint(w, " ")
}
}
_, err := fmt.Fprintln(w, `</g></svg>`)
return err
}*/