-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
146 lines (132 loc) · 2.88 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
package main
import (
"encoding/xml"
"fmt"
"image"
"image/color"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
_ "image/png"
)
type Imageinfo struct {
XMLName xml.Name `xml:"imageinfo"`
CellsX int `xml:"cells_x"`
CellsY int `xml:"cells_y"`
Segment []Segment `xml:"segment"`
}
func (i *Imageinfo) String() string {
lines := []string{}
lines = append(lines, strings.Repeat("-", i.CellsX*2+2))
for _, v := range i.Segment {
lines = append(lines, v.String())
}
lines = append(lines, strings.Repeat("-", i.CellsX*2+2))
return strings.Join(lines, "\n")
}
type Segment struct {
X1 int `xml:"x1,attr"`
X2 int `xml:"x2,attr"`
Y1 int `xml:"y1,attr"`
Y2 int `xml:"y2,attr"`
maxwd int
}
func (s *Segment) String() string {
lines := []string{}
line := []rune{}
line = append(line, '|')
for y := s.Y1 - 1; y < s.Y2; y++ {
for x := 1; x < s.X1; x++ {
line = append(line, ' ')
line = append(line, ' ')
}
for x := s.X1; x <= s.X2; x++ {
line = append(line, '■')
line = append(line, '■')
}
for x := s.X2; x < s.maxwd; x++ {
line = append(line, ' ')
line = append(line, ' ')
}
line = append(line, '|')
lines = append(lines, string(line))
}
return strings.Join(lines, "\n")
}
func isBlack(col color.Color) bool {
switch x := col.(type) {
case color.Gray:
return x.Y < 0xfa
default:
log.Fatalf("Unknown type for color %#v", col)
}
return true
}
func main() {
if len(os.Args) < 2 {
fmt.Println(os.Args[0], "<inputfile>")
os.Exit(0)
}
imagepath := os.Args[1]
name := strings.TrimSuffix(imagepath, filepath.Ext(imagepath))
resolution := 40
// Run imagemagick to get a max(40x40) pixel image
cmd := exec.Command("convert", imagepath, "-background", "white", "-alpha", "remove", "-type", "Grayscale", "-resize", fmt.Sprintf("%dx%d", resolution, resolution), "-threshold", "97%", "png:-")
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
err = cmd.Start()
if err != nil {
log.Fatal(err)
}
img, _, err := image.Decode(stdout)
if err != nil {
log.Fatal(err)
}
err = cmd.Wait()
if err != nil {
log.Fatal(err)
}
// Now construct the XML
bounds := img.Bounds()
ii := Imageinfo{}
ii.CellsX = bounds.Dx()
ii.CellsY = bounds.Dy()
for y := 0; y < bounds.Dy(); y++ {
s := Segment{maxwd: ii.CellsX}
hasBlackPixel := false
for x := 0; x < bounds.Dx(); x++ {
s.Y1 = y + 1
s.Y2 = y + 1
if isBlack(img.At(x, y)) {
hasBlackPixel = true
if s.X1 == 0 {
s.X1 = x + 1
}
s.X2 = x + 1
}
}
if hasBlackPixel {
ii.Segment = append(ii.Segment, s)
}
}
b, err := xml.MarshalIndent(ii, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Println(ii.String())
f, err := os.OpenFile(name+".xml", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
_, err = f.Write(b)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
f.Close()
}