-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (56 loc) · 1.7 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
package main
import (
"bufio"
"flag"
"fmt"
"image"
"os"
"github.com/ericlevine/meme/writer"
)
var (
inFilename, outFilename, top, bottom string
)
func init() {
flag.StringVar(&inFilename, "in", "", "Input image file.")
flag.StringVar(&outFilename, "out", "", "Output image file.")
flag.StringVar(&top, "top", "", "Top meme text.")
flag.StringVar(&bottom, "bottom", "", "Bottom meme text.")
}
func main() {
flag.Parse()
if inFilename == "" || outFilename == "" {
fmt.Println("-in and -out flags are required")
os.Exit(1)
}
in, err := os.Open(inFilename)
if err != nil { errorExit("Error opening input file.", err) }
defer in.Close()
outFile, err := os.Create(outFilename)
if err != nil { errorExit("Error creating output file.", err) }
defer outFile.Close()
out := bufio.NewWriter(outFile)
_, format, err := image.DecodeConfig(in)
if err != nil { errorExit("Error decoding format.", err) }
_, err = in.Seek(0, 0)
if err != nil { errorExit("Error seeking back to beginning of file.", err) }
if format == "gif" {
err = writer.WriteMemeGIF(in, out, top, bottom)
if err != nil { errorExit("Could not create meme.", err) }
} else if format == "jpeg" {
err = writer.WriteMemeJPEG(in, out, top, bottom)
if err != nil { errorExit("Could not create meme.", err) }
} else if format == "png" {
err = writer.WriteMemePNG(in, out, top, bottom)
if err != nil { errorExit("Could not create meme.", err) }
} else {
fmt.Println("Unsupported image format.")
os.Exit(1)
}
err = out.Flush()
if err != nil { errorExit("Error writing file.", err) }
}
func errorExit(message string, err error) {
fmt.Println(message)
fmt.Println(err)
os.Exit(1)
}