-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
138 lines (128 loc) · 2.85 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
package main
import (
"image"
"image/jpeg"
"image/png"
"io"
"log"
"os"
"path/filepath"
"strings"
"github.com/jackmordaunt/icns/v2"
"github.com/spf13/afero"
"github.com/spf13/pflag"
)
var (
fs = afero.NewOsFs()
piping bool
input io.Reader
output io.Writer
)
func main() {
var (
inputPath = pflag.StringP(
"input",
"i",
"",
"Input image for conversion to icns from jpg|png or visa versa.",
)
outputPath = pflag.StringP(
"output",
"o",
"",
"Output path, defaults to <path/to/image>.(icns|png) depending on input.",
)
resize = pflag.IntP(
"resize",
"r",
5,
"Quality of resize algorithm. Values range from 0 to 5, fastest to slowest execution time. Defaults to slowest for best quality.",
)
)
pflag.Parse()
in, out, algorithm := sanitiseInputs(*inputPath, *outputPath, *resize)
if !piping {
if in == "" {
usage()
os.Exit(0)
}
sourcef, err := fs.Open(in)
if err != nil {
log.Fatalf("opening source image: %v", err)
}
defer sourcef.Close()
input = sourcef
if err := fs.MkdirAll(filepath.Dir(out), 0755); err != nil {
log.Fatalf("preparing output directory: %v", err)
}
outputf, err := fs.Create(out)
if err != nil {
log.Fatalf("creating icns file: %v", err)
}
defer outputf.Close()
output = outputf
}
img, format, err := image.Decode(input)
if err != nil {
log.Fatalf("decoding input: %v", err)
}
if format == "icns" {
imageType := strings.ToLower(filepath.Ext(out))
if _, ok := encoders[imageType]; !ok {
imageType = ".png"
}
if err := encoders[imageType](output, img); err != nil {
log.Fatalf("encoding %s: %v", imageType, err)
}
} else {
enc := icns.NewEncoder(output).
WithAlgorithm(algorithm)
if err := enc.Encode(img); err != nil {
log.Fatalf("encoding icns: %v", err)
}
}
}
func sanitiseInputs(
inputPath string,
outputPath string,
resize int,
) (string, string, icns.InterpolationFunction) {
if filepath.Ext(inputPath) == ".icns" {
if outputPath == "" {
outputPath = changeExtensionTo(inputPath, "png")
}
if filepath.Ext(outputPath) == "" {
outputPath += ".png"
}
}
if filepath.Ext(inputPath) != ".icns" {
if outputPath == "" {
outputPath = changeExtensionTo(inputPath, "icns")
}
if filepath.Ext(outputPath) == "" {
outputPath += ".icns"
}
}
if resize < 0 {
resize = 0
}
if resize > 5 {
resize = 5
}
return inputPath, outputPath, icns.InterpolationFunction(resize)
}
func changeExtensionTo(path, ext string) string {
if !strings.HasPrefix(ext, ".") {
ext = "." + ext
}
return filepath.Base(path[:len(path)-len(filepath.Ext(path))] + ext)
}
type encoderFunc func(io.Writer, image.Image) error
func encodeJPEG(w io.Writer, m image.Image) error {
return jpeg.Encode(w, m, &jpeg.Options{Quality: 100})
}
var encoders = map[string]encoderFunc{
".png": png.Encode,
".jpg": encodeJPEG,
".jpeg": encodeJPEG,
}