-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
157 lines (136 loc) · 3.71 KB
/
main.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
package main
import (
"flag"
"fmt"
"image"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/unixpickle/essentials"
"github.com/unixpickle/model3d/model2d"
)
func main() {
var invert bool
var smoothIters int
var maxVertices int
var subdivisions int
var upsampleRate float64
var thicken float64
var outline bool
flag.BoolVar(&invert, "invert", false, "invert positive and negative pixels")
flag.IntVar(&smoothIters, "smooth-iters", 100, "number of smoothing iterations")
flag.IntVar(&maxVertices, "max-vertices", 70, "maximum vertices after decimation")
flag.IntVar(&subdivisions, "subdivisions", 2, "number of smoothing sub-divisions")
flag.Float64Var(&upsampleRate, "upsample-rate", 2.0, "extra resolution to add to output")
flag.Float64Var(&thicken, "thicken", 0.0, "extra thickness (in pixels) to give to the output")
flag.BoolVar(&outline, "outline", false, "produce an outline instead of a solid")
flag.Usage = func() {
fmt.Fprintln(os.Stderr, "Usage:", os.Args[0], "[flags] <input> <output>")
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "Flags:")
fmt.Fprintln(os.Stderr)
flag.PrintDefaults()
os.Exit(1)
}
flag.Parse()
if len(flag.Args()) != 2 {
flag.Usage()
}
log.Println("Reading file as mesh...")
mesh, size := ReadImage(flag.Args()[0], invert)
log.Println("Smoothing mesh...")
mesh = mesh.SmoothSq(smoothIters)
log.Println("Decimating mesh...")
mesh = mesh.Decimate(maxVertices)
log.Println("Subdividing mesh...")
if subdivisions > 0 {
mesh = mesh.Subdivide(subdivisions)
}
log.Println("Rendering...")
outPath := flag.Args()[1]
if IsSVGPath(outPath) {
SaveSVG(outPath, mesh, size, outline, upsampleRate)
} else {
SaveRasterImage(outPath, mesh, size, outline, thicken, upsampleRate)
}
}
func SaveSVG(outPath string, mesh *model2d.Mesh, size model2d.Coord, outline bool,
upsampleRate float64) {
mesh = mesh.Scale(upsampleRate)
size = size.Scale(upsampleRate)
bounds := model2d.NewRect(model2d.Origin, size)
var data []byte
if outline {
data = model2d.EncodeCustomSVG(
[]*model2d.Mesh{mesh},
[]string{"black"},
[]float64{1.0},
bounds,
)
} else {
data = model2d.EncodeCustomPathSVG(
[]*model2d.Mesh{mesh},
[]string{"black"},
[]string{"none"},
[]float64{1.0},
bounds,
)
}
essentials.Must(ioutil.WriteFile(outPath, data, 0644))
}
func SaveRasterImage(outPath string, mesh *model2d.Mesh, size model2d.Coord, outline bool,
thicken, upsampleRate float64) {
collider := model2d.MeshToCollider(mesh)
rast := &model2d.Rasterizer{Scale: upsampleRate}
var img image.Image
if outline {
img = rast.RasterizeCollider(&BoundedCollider{
Collider: collider,
MaxVal: size,
})
} else if thicken == 0 {
img = rast.RasterizeColliderSolid(&BoundedCollider{
Collider: collider,
MaxVal: size,
})
} else {
img = rast.RasterizeSolid(&BoundedSolid{
Solid: model2d.NewColliderSolidInset(collider, -thicken),
MaxVal: size,
})
}
essentials.Must(model2d.SaveImage(outPath, img))
}
func ReadImage(path string, invert bool) (mesh *model2d.Mesh, size model2d.Coord) {
bmp := model2d.MustReadBitmap(path, nil)
if invert {
bmp = bmp.Invert()
}
size = model2d.XY(float64(bmp.Width), float64(bmp.Height))
return bmp.Mesh(), size
}
func IsSVGPath(path string) bool {
return filepath.Ext(strings.ToLower(path)) == ".svg"
}
type BoundedSolid struct {
model2d.Solid
MaxVal model2d.Coord
}
func (b BoundedSolid) Min() model2d.Coord {
return model2d.Coord{}
}
func (b BoundedSolid) Max() model2d.Coord {
return b.MaxVal
}
type BoundedCollider struct {
model2d.Collider
MaxVal model2d.Coord
}
func (b BoundedCollider) Min() model2d.Coord {
return model2d.Coord{}
}
func (b BoundedCollider) Max() model2d.Coord {
return b.MaxVal
}