-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
85 lines (71 loc) · 1.58 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
package main
import (
"bytes"
"flag"
"fmt"
"image/png"
"io/ioutil"
"log"
"os"
"github.com/poolqa/CgbiPngFix/ipaPng"
)
type CommandOptions struct {
Output string
Input string
}
var ShowHelper bool
var Options CommandOptions
func init() {
flag.BoolVar(&ShowHelper, "h", false, "show this help")
// 注意 `signal`。默认是 -s string,有了 `signal` 之后,变为 -s signal
flag.StringVar(&Options.Output, "o", "", "set fixed png `output` file")
flag.StringVar(&Options.Input, "i", "", "set source ios png `input` file")
// 改变默认的 Usage,flag包中的Usage 其实是一个函数类型。这里是覆盖默认函数实现,具体见后面Usage部分的分析
flag.Usage = usage
}
func usage() {
fmt.Fprintf(os.Stderr, `ios png fix version: v0.0.1
Usage: nginx [-h] [-o filename] [-i filename]
Options:
`)
flag.PrintDefaults()
}
func main() {
flag.Parse()
if ShowHelper {
flag.Usage()
os.Exit(0)
}
if Options.Input == "" {
flag.Usage()
os.Exit(0)
}
doCgbiToPng(Options.Input, Options.Output)
}
func doCgbiToPng(input string, output string) {
f, err := os.Open(input)
if err != nil {
log.Fatal(err)
}
b, err := ioutil.ReadAll(f)
f.Close()
if err != nil {
log.Fatal(err)
}
cgbi, err := ipaPng.Decode(bytes.NewReader(b))
if err != nil {
fmt.Printf("err:%v\n", err)
log.Fatal(err)
}
fo, err := os.OpenFile(output, os.O_WRONLY|os.O_CREATE, os.FileMode(0666))
if err != nil {
fmt.Printf("err:%v\n", err)
log.Fatal(err)
}
defer fo.Close()
err = png.Encode(fo, cgbi.Img)
if err != nil {
fmt.Printf("err:%v\n", err)
log.Fatal(err)
}
}