/
main.go
69 lines (60 loc) · 1.27 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
// chris 091315
// webp2png converts WebP images to PNG.
//
// Usage of ./webp2png:
// -input string
// can be - for standard in (default "-")
// -output string
// can be - for standard out (default "-")
//
// If the output is not standard out and already exists, it will be
// truncated when opened for writing.
//
// Both the input and output files will be opened before doing any WebP
// decoding or PNG encoding.
package main
import (
"flag"
"io"
"log"
"os"
"image/png"
"golang.org/x/image/webp"
)
func main() {
inname := flag.String("input", "-", "can be - for standard in")
outname := flag.String("output", "-", "can be - for standard out")
flag.Parse()
var (
infile io.Reader
outfile io.Writer
)
if *inname == "-" {
infile = os.Stdin
} else {
file, err := os.Open(*inname)
if err != nil {
log.Fatal(err)
}
defer file.Close()
infile = file
}
if *outname == "-" {
outfile = os.Stdout
} else {
flag := os.O_WRONLY | os.O_CREATE | os.O_TRUNC
file, err := os.OpenFile(*outname, flag, 0666)
if err != nil {
log.Fatal(err)
}
defer file.Close()
outfile = file
}
if m, err := webp.Decode(infile); err != nil {
log.Fatal(err)
} else {
if err := png.Encode(outfile, m); err != nil {
log.Fatal(err)
}
}
}