-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
160 lines (133 loc) · 3.94 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
158
159
160
package main
import (
"fmt"
"github.com/gammazero/deque"
"golang.org/x/image/draw"
"image"
"image/color"
"image/jpeg"
"image/png"
"os"
"path/filepath"
"strings"
)
func readImage(path string) (image.Image, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
switch {
case strings.HasSuffix(path, ".png"):
return png.Decode(f)
case strings.HasSuffix(path, ".jpg"), strings.HasSuffix(path, ".jpeg"):
return jpeg.Decode(f)
}
return nil, fmt.Errorf("unsupported image type: %s", path)
}
func writeImage(sourcePath, variant string, img image.Image) error {
dirname := filepath.Dir(sourcePath)
basename := filepath.Base(sourcePath)
lastIndex := strings.LastIndex(basename, ".")
root := basename
if lastIndex != -1 {
root = basename[0:lastIndex]
}
destPath := filepath.Join(dirname, fmt.Sprintf("%s_%s.png", root, variant))
f, err := os.Create(destPath)
if err != nil {
return err
}
defer f.Close()
return png.Encode(f, img)
}
func hasTransparency(c color.Color) bool {
_, _, _, a := c.RGBA()
return a < 0xffff
}
func areaOf(inputImage image.Image, isContour func(color.Color) bool, startPoints ...image.Point) []image.Point {
var results []image.Point
maxX := inputImage.Bounds().Dx()
maxY := inputImage.Bounds().Dy()
seen := make([]bool, maxX*maxY)
queue := deque.New()
for _, startPoint := range startPoints {
queue.PushBack(startPoint)
seen[startPoint.X+startPoint.Y*inputImage.Bounds().Dy()] = true
}
for queue.Len() > 0 {
curr := queue.PopFront().(image.Point)
currColor := inputImage.At(curr.X, curr.Y)
if !isContour(currColor) {
continue
}
results = append(results, curr)
above := curr.Add(image.Pt(0, -1))
aboveIdx := above.X + above.Y*inputImage.Bounds().Dy()
if above.X >= 0 && above.X < maxX && above.Y >= 0 && above.Y < maxY && !seen[aboveIdx] {
queue.PushBack(above)
seen[aboveIdx] = true
}
below := curr.Add(image.Pt(0, 1))
belowIdx := below.X + below.Y*inputImage.Bounds().Dy()
if below.X >= 0 && below.X < maxX && below.Y >= 0 && below.Y < maxY && !seen[belowIdx] {
queue.PushBack(below)
seen[belowIdx] = true
}
left := curr.Add(image.Pt(-1, 0))
leftIdx := left.X + left.Y*inputImage.Bounds().Dy()
if left.X >= 0 && left.X < maxX && left.Y >= 0 && left.Y < maxY && !seen[leftIdx] {
queue.PushBack(left)
seen[leftIdx] = true
}
right := curr.Add(image.Pt(1, 0))
rightIdx := right.X + right.Y*inputImage.Bounds().Dy()
if right.X >= 0 && right.X < maxX && right.Y >= 0 && right.Y < maxY && !seen[rightIdx] {
queue.PushBack(right)
seen[rightIdx] = true
}
}
return results
}
func makeIcons(borders map[string]image.Image, inputImagePaths []string) error {
borderAreas := make(map[string][]image.Point)
for name, border := range borders {
borderAreas[name] = areaOf(
border,
hasTransparency,
image.Pt(border.Bounds().Max.X/2, border.Bounds().Max.Y/2),
)
}
for _, inputImagePath := range inputImagePaths {
inputImage, err := readImage(inputImagePath)
if err != nil {
return err
}
for name, border := range borders {
borderBounds := border.Bounds()
scaledImage := image.NewRGBA(image.Rect(0, 0, borderBounds.Dx(), borderBounds.Dy()))
draw.CatmullRom.Scale(scaledImage, scaledImage.Bounds(), inputImage, inputImage.Bounds(), draw.Over, nil)
newImage := image.NewRGBA(image.Rect(0, 0, borderBounds.Dx(), borderBounds.Dy()))
for _, pt := range borderAreas[name] {
newImage.SetRGBA(pt.X, pt.Y, scaledImage.RGBAAt(pt.X, pt.Y))
}
draw.Draw(newImage, newImage.Bounds(), border, border.Bounds().Min, draw.Over)
if err := writeImage(inputImagePath, name, newImage); err != nil {
return err
}
}
}
return nil
}
func main() {
if len(os.Args) < 2 {
fmt.Fprint(os.Stderr, "Must specify input image files to tokenize.")
os.Exit(1)
}
borders := readIconBorders()
if err := makeIcons(borders, os.Args[1:]); err != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
os.Exit(1)
}
os.Exit(0)
}