/
main.go
305 lines (281 loc) · 8.11 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
flag "github.com/spf13/pflag"
"github.com/missdeer/yairc/util"
)
var (
compress bool
backgroundImagePath string
foregroundImagePath string
inputPath string
outputPath string
action string
cutEdgePosition string
platform = "ios"
red uint32 = 127
green uint32 = 127
blue uint32 = 127
outputHeight uint
outputWidth uint
cutEdgeStep uint = 1
transparentWhiteDirect bool
// Gitcommit contains the commit where we built from.
GitCommit string
imageFormatMap = map[string]int{
".icns": util.IT_icns,
".png": util.IT_png,
".jpg": util.IT_jpeg,
".jpeg": util.IT_jpeg,
".gif": util.IT_gif,
".webp": util.IT_webp,
".tiff": util.IT_tiff,
".ico": util.IT_ico,
".bmp": util.IT_bmp,
}
)
func main() {
showHelpMessage := false
showVersion := false
flag.Uint32VarP(&red, "red", "", red, "set red threshold")
flag.Uint32VarP(&green, "green", "", green, "set green threshold")
flag.Uint32VarP(&blue, "blue", "", blue, "set blue threshold")
flag.BoolVarP(&compress, "compress", "", true, "compress output PNG files")
flag.StringVarP(&platform, "platform", "p", "common", "candidates: ios, android, common")
flag.StringVarP(&action, "action", "a", "", "candidats: icons, appIcon, launchImage, transparent, invert, resize, convert, cutedge, info")
flag.StringVarP(&backgroundImagePath, "background", "b", "", "path of background image for launch image")
flag.StringVarP(&foregroundImagePath, "foreground", "f", "", "path of foreground image for launch image")
flag.StringVarP(&inputPath, "input", "i", "", "input image file path")
flag.StringVarP(&outputPath, "output", "o", ".", "output directory/file path")
flag.StringVarP(&cutEdgePosition, "cut-edge-position", "e", "", "cut edge position, candidates: (l)eft, (r)ight, (t)op, (b)ottom, (h)orizontal, (v)ertical, (a)ll")
flag.UintVarP(&cutEdgeStep, "cut-edge-step", "", cutEdgeStep, "cut edge step")
flag.UintVarP(&outputHeight, "height", "", 0, "set output image height, 0 for original height")
flag.UintVarP(&outputWidth, "width", "", 0, "set output image width, 0 for original width")
flag.BoolVarP(&transparentWhiteDirect, "transparent-white-direct", "", false, "false - make white color be transparent, true - make black color be transparent")
flag.BoolVarP(&showHelpMessage, "help", "h", false, "show this help message")
flag.BoolVarP(&showVersion, "version", "v", false, "show version number")
flag.Parse()
if showHelpMessage {
flag.PrintDefaults()
return
}
if showVersion {
fmt.Println("yairc version:", GitCommit)
return
}
// icon scale mode
if action == "icons" && inputPath != "" && outputPath != "" {
log.Println("generate /@2x/@3x/@4x & /x18/x36/x48 icons from", inputPath, "to", outputPath)
err := iconScale(inputPath, outputPath)
if err != nil {
log.Fatal(err)
}
return
}
// ios app icon mode
if action == "appIcon" && platform == "ios" {
fmt.Println("output ios app icons")
err := GenerateAppIcon(inputPath)
if err != nil {
log.Fatal(err)
}
return
}
// ios launch image mode
if action == "launchImage" && platform == "ios" {
fmt.Println("output ios launch images")
err := GenerateLaunchImage()
if err != nil {
log.Fatal(err)
}
return
}
if action == "appIcon" && platform == "android" {
fmt.Println("output android launcher icons")
err := GenerateLauncherIcon(inputPath)
if err != nil {
log.Fatal(err)
}
return
}
if action == "launchImage" && platform == "ios" {
fmt.Println("output android splash screen images")
err := GenerateSplashScreen(inputPath)
if err != nil {
log.Fatal(err)
}
return
}
// convert file format
if action == "convert" && inputPath != "" {
dir := filepath.Dir(outputPath)
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err = os.MkdirAll(dir, 0755); err != nil {
log.Fatal("mkdir failed", err)
}
}
dest, err := os.Create(outputPath)
if err != nil {
log.Fatal("opening destination file failed", err)
}
defer dest.Close()
inFile, err := util.OpenURI(inputPath)
if err != nil {
log.Fatal("opening source image failed", err)
}
defer inFile.Close()
srcImg, _, err := util.ImageDecode(inFile)
if err != nil {
log.Fatal("decoding source image failed", err)
}
outExt := filepath.Ext(outputPath)
if it, ok := imageFormatMap[strings.ToLower(outExt)]; ok {
err = util.SaveImage(srcImg, outputPath, it)
} else {
log.Fatal("unsupported target image format")
}
if err != nil {
log.Fatal("encoding failed", err)
}
return
}
args := flag.Args()
if inputPath != "" {
args = append(args, inputPath)
}
if action == "transparent" && len(args) > 0 {
log.Println("transparent color")
for _, uri := range args {
im, err := util.Transparent(uri, red, green, blue, transparentWhiteDirect)
if err != nil {
log.Println(err)
continue
}
fn := outputPath
if fn == "" {
fn = inputPath[:len(inputPath)-len(filepath.Ext(inputPath))] + ".transparent.png"
}
outExt := filepath.Ext(fn)
if it, ok := imageFormatMap[strings.ToLower(outExt)]; ok {
if err = util.SaveImage(im, outputPath, it); err != nil {
log.Println("encoding failed", err)
continue
}
if it == util.IT_png {
if err = util.DoCrush(compress, fn); err != nil {
log.Println(err)
}
}
} else {
log.Println("unsupported target image format")
}
}
return
}
if action == "invert" && len(args) > 0 {
log.Println("invert color")
for _, uri := range args {
im, err := util.Invert(uri)
if err != nil {
log.Println(err)
continue
}
fn := outputPath
if fn == "" {
fn = inputPath[:len(inputPath)-len(filepath.Ext(inputPath))] + ".invert.png"
}
outExt := filepath.Ext(fn)
if it, ok := imageFormatMap[strings.ToLower(outExt)]; ok {
if err = util.SaveImage(im, outputPath, it); err != nil {
log.Println("encoding failed", err)
continue
}
if it == util.IT_png {
if err = util.DoCrush(compress, fn); err != nil {
log.Println(err)
}
}
} else {
log.Println("unsupported target image format")
}
}
return
}
if action == "resize" && len(args) > 0 {
log.Println("resize images")
for _, uri := range args {
im, err := util.Resize(uri, outputWidth, outputHeight)
if err != nil {
log.Println(err)
continue
}
fn := outputPath
if fn == "" {
fn = inputPath[:len(inputPath)-len(filepath.Ext(inputPath))] + ".resized.png"
}
outExt := filepath.Ext(fn)
if it, ok := imageFormatMap[strings.ToLower(outExt)]; ok {
if err = util.SaveImage(im, outputPath, it); err != nil {
log.Println("encoding failed", err)
continue
}
if it == util.IT_png {
if err = util.DoCrush(compress, fn); err != nil {
log.Println(err)
}
}
} else {
log.Println("unsupported target image format")
}
}
}
if action == "cutedge" && len(args) > 0 {
log.Println("cut edge of images")
for _, uri := range args {
im, err := util.CutEdge(uri, cutEdgePosition, cutEdgeStep)
if err != nil {
log.Println(err)
continue
}
fn := outputPath
if fn == "" {
fn = inputPath[:len(inputPath)-len(filepath.Ext(inputPath))] + ".cutedge.png"
}
outExt := filepath.Ext(fn)
if it, ok := imageFormatMap[strings.ToLower(outExt)]; ok {
if err = util.SaveImage(im, outputPath, it); err != nil {
log.Println("encoding failed", err)
continue
}
if it == util.IT_png {
if err = util.DoCrush(compress, fn); err != nil {
log.Println(err)
}
}
} else {
log.Println("unsupported target image format")
}
}
}
if action == "info" && len(args) > 0 {
log.Println("info color")
for _, uri := range args {
im, err := util.Info(uri)
if err != nil {
log.Println(err)
continue
}
fmt.Println(uri)
for k, c := range im {
r, g, b, a := k.RGBA()
fmt.Printf("r=%3d,g=%3d,b=%3d,a=%3d: count:=%d\n", r&0xff, g&0xff, b&0xff, a&0xff, c)
}
fmt.Println("===============================")
}
return
}
}