forked from anthonynsimon/bild
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
106 lines (84 loc) · 2.03 KB
/
helpers.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
package cmd
import (
"errors"
"fmt"
"image"
"os"
"strconv"
"strings"
"github.com/anthonynsimon/bild/imgio"
)
var jpgExtensions = []string{".jpg", ".jpeg"}
var pngExtensions = []string{".png"}
var bmpExtensions = []string{".bmp"}
var (
// ErrWrongSize is thrown when the provided size string does not match the expected form.
errWrongSize = errors.New("size must be of form [width]x[height], i.e. 400x200")
)
type size struct {
Width int
Height int
}
func resolveEncoder(outputfile string, defaultEncoding imgio.Encoder) imgio.Encoder {
lower := strings.ToLower(outputfile)
for _, ext := range jpgExtensions {
if strings.HasSuffix(lower, ext) {
return imgio.JPEGEncoder(100)
}
}
for _, ext := range pngExtensions {
if strings.HasSuffix(lower, ext) {
return imgio.PNGEncoder()
}
}
for _, ext := range bmpExtensions {
if strings.HasSuffix(lower, ext) {
return imgio.BMPEncoder()
}
}
return defaultEncoding
}
func apply(fin, fout string, process func(image.Image) (image.Image, error)) {
in, err := imgio.Open(fin)
exitIfNotNil(err)
result, err := process(in)
exitIfNotNil(err)
encoder := resolveEncoder(fout, imgio.PNGEncoder())
err = imgio.Save(fout, result, encoder)
exitIfNotNil(err)
}
func apply2(fin1, fin2, fout string, process func(image.Image, image.Image) (image.Image, error)) {
in1, err := imgio.Open(fin1)
exitIfNotNil(err)
in2, err := imgio.Open(fin2)
exitIfNotNil(err)
result, err := process(in1, in2)
exitIfNotNil(err)
encoder := resolveEncoder(fout, imgio.PNGEncoder())
err = imgio.Save(fout, result, encoder)
exitIfNotNil(err)
}
func exitIfNotNil(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func parseSizeStr(sizestr string) (*size, error) {
parts := strings.Split(sizestr, "x")
if len(parts) != 2 {
return nil, errWrongSize
}
w, err := strconv.Atoi(parts[0])
if err != nil || w < 0 {
return nil, errWrongSize
}
h, err := strconv.Atoi(parts[1])
if err != nil || h < 0 {
return nil, errWrongSize
}
return &size{
Width: w,
Height: h,
}, nil
}