-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
193 lines (155 loc) · 5.2 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
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
193
package main
import (
"bytes"
"encoding/base64"
"flag"
"fmt"
"github.com/ajstarks/svgo"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io"
"log"
"os"
"path"
"strconv"
"strings"
)
var (
output = flag.String("o", "output_1.svg", "output filename")
firstLayer = flag.String("pics_1", "", "pictures")
firstRepeat = flag.Int("repeat_1", 0, "svg repeat. default indefinite")
firstDur = flag.String("dur_1", "0", "svg duration. default 1")
firstBegin = flag.String("begin_1", "0", "svg begin. default 0")
secondLayer = flag.String("pics_2", "", "pictures")
secondRepeat = flag.Int("repeat_2", 0, "svg repeat. default indefinite")
secondDur = flag.String("dur_2", "0", "svg duration. default pics count")
secondBegin = flag.String("begin_2", "0", "svg begin. default 0")
thirdLayer = flag.String("pics_3", "", "pictures")
thirdRepeat = flag.Int("repeat_3", 0, "svg repeat. default indefinite")
thirdDur = flag.String("dur_3", "0", "svg duration. default pics count")
thirdBegin = flag.String("begin_3", "0", "svg begin. default 0")
fourthLayer = flag.String("pics_4", "", "pictures")
fourthRepeat = flag.Int("repeat_4", 0, "svg repeat. default indefinite")
fourthDur = flag.String("dur_4", "0", "svg duration. default pics count")
fourthBegin = flag.String("begin_4", "0", "svg begin. default 0")
fifthLayer = flag.String("pics_5", "", "pictures")
fifthRepeat = flag.Int("repeat_5", 0, "svg repeat. default indefinite")
fifthDur = flag.String("dur_5", "0", "svg duration. default pics count")
fifthBegin = flag.String("begin_5", "0", "svg begin. default 0")
)
type Capturer struct {
saved *os.File
bufferChannel chan string
out *os.File
in *os.File
}
// stdout capture start
func (c *Capturer) StartCapturingStdout() {
c.saved = os.Stdout
var err error
c.in, c.out, err = os.Pipe()
if err != nil {
panic(err)
}
os.Stdout = c.out
c.bufferChannel = make(chan string)
go func() {
var b bytes.Buffer
io.Copy(&b, c.in)
c.bufferChannel <- b.String()
}()
}
// stop capture
func (c *Capturer) StopCapturingStdout() string {
c.out.Close()
os.Stdout = c.saved
return <-c.bufferChannel
}
func base64Encode(filePath string) string {
file, err := os.Open(filePath)
if err != nil {
fmt.Println("A file read error occured: " + filePath)
log.Fatal(err)
}
defer file.Close()
fi, _ := file.Stat()
size := fi.Size()
data := make([]byte, size)
file.Read(data)
return base64.StdEncoding.EncodeToString(data)
}
func getImageSize(filePath string) (int, int) {
file, err := os.Open(filePath)
if err != nil {
fmt.Println("A file read error occured: " + filePath)
log.Fatal(err)
}
defer file.Close()
conf, _, err := image.DecodeConfig(file)
if err != nil {
fmt.Println("image recognition error: " + filePath)
log.Fatal(err)
}
return conf.Width, conf.Height
}
func makeSvgDef(id int, layer string, repeat int, duration string, begin string, s *svg.SVG) {
layerList := strings.Split(layer, ",")
var animateValues []string
s.Def()
for i := 0; i < len(layerList); i++ {
layerId := "layer_" + strconv.Itoa(id+1) + "_" + strconv.Itoa(i+1)
imageFilePath := layerList[i]
ext := strings.ToLower(path.Ext(imageFilePath))
width, height := getImageSize(imageFilePath)
animateValues = append(animateValues, "#"+layerId)
imageMimeType := ""
if ext == ".png" {
imageMimeType = "png"
} else if ext == ".jpeg" {
imageMimeType = "jpeg"
}
fmt.Println("<image id=\"" + layerId + "\" x=\"0\" y=\"0\" width=\"" + strconv.Itoa(width) + "\" height=\"" + strconv.Itoa(height) + "\" xlink:href=\"data:image/" + imageMimeType + ";base64," + base64Encode(imageFilePath) + "\" shape-rendering=\"crispEdges\" image-rendering=\"optimizeQuality\" />")
}
s.DefEnd()
animateValuesStr := strings.Join(animateValues, ";")
repeatCount := "indefinite"
if repeat != 0 {
repeatCount = strconv.Itoa(repeat)
}
durationNum := strconv.Itoa(1)
if duration != "0" {
durationNum = duration
}
beginNum := strconv.Itoa(0)
if begin != "0" {
beginNum = begin
}
fmt.Println("<use x=\"0\" y=\"0\">")
fmt.Println("<animate attributeName=\"xlink:href\" begin=\"" + beginNum + "s\" dur=\"" + durationNum + "s\" repeatCount=\"" + repeatCount + "\" values=\"" + animateValuesStr + "\" />")
fmt.Println("</use>")
}
func main() {
flag.Parse()
firstLayerList := strings.Split(*firstLayer, ",")
svgStartViewWidth, svgStartViewHeight := getImageSize(firstLayerList[0])
allLayer := []string{*firstLayer, *secondLayer, *thirdLayer, *fourthLayer, *fifthLayer}
allLayerRepeat := []int{*firstRepeat, *secondRepeat, *thirdRepeat, *fourthRepeat, *fifthRepeat}
allLayerDur := []string{*firstDur, *secondDur, *thirdDur, *fourthDur, *fifthDur}
allLayerBegin := []string{*firstBegin, *secondBegin, *thirdBegin, *fourthBegin, *fifthBegin}
c := &Capturer{}
c.StartCapturingStdout()
s := svg.New(os.Stdout)
s.Startview(svgStartViewWidth, svgStartViewHeight, 0, 0, svgStartViewWidth, svgStartViewHeight)
for i := 0; i < len(allLayer); i++ {
if allLayer[i] != "" {
makeSvgDef(i, allLayer[i], allLayerRepeat[i], allLayerDur[i], allLayerBegin[i], s)
}
}
s.End()
captured := c.StopCapturingStdout()
output, _ := os.Create(*output)
output.WriteString(captured)
fmt.Println("done!")
}