-
Notifications
You must be signed in to change notification settings - Fork 0
/
gifsicle.go
231 lines (187 loc) · 4.89 KB
/
gifsicle.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package gifsicle
import (
"errors"
"fmt"
"image/gif"
"io"
"github.com/munchpass/gifsicle-go/embedbinwrapper"
)
// For the -O[level] or --optimize[=level] arguments.
//
// See https://www.lcdf.org/gifsicle/man.html for more information.
type OptimizeLevel string
var (
// Store only the changed portion of each image. This is the default.
OPTIMIZE_LEVEL_ONE OptimizeLevel = "1"
// Store only the changed portion of each image, and use transparency.
OPTIMIZE_LEVEL_TWO OptimizeLevel = "2"
// Tries several optimization methods (usually slower, sometimes better results).
OPTIMIZE_LEVEL_THREE OptimizeLevel = "3"
// Preserve empty transparent frames (they are dropped by default).
OPTIMIZE_LEVEL_KEEP_EMPTY OptimizeLevel = "keep-empty"
)
// Gifsicle wraps the gifsicle CLI tool.
type Gifsicle struct {
binWrapper *embedbinwrapper.EmbedBinWrapper
inputFile string
inputGif *gif.GIF
input io.Reader
outputFile string
output io.Writer
// Integer from 0-200 for GIF compression
lossy uint
optimizeLevel OptimizeLevel
// A number from 2-256 specifying the number of colors (makes a meaningful difference
// in compressed size)
numColors *uint
}
func NewGifsicle() (*Gifsicle, error) {
binWrapper, err := createBinWrapper("gifsicle")
if err != nil {
return nil, fmt.Errorf("failed to create binary wrapper for gifsicle: %v", err)
}
bin := &Gifsicle{
binWrapper: binWrapper,
optimizeLevel: OPTIMIZE_LEVEL_ONE,
lossy: 20,
}
return bin, nil
}
// Turns on debug mode.
func (g *Gifsicle) Debug() *Gifsicle {
g.binWrapper = g.binWrapper.Debug()
return g
}
// InputFile sets image file to convert.
// Input or InputGif called before will be ignored.
func (g *Gifsicle) InputFile(file string) *Gifsicle {
g.input = nil
g.inputGif = nil
g.inputFile = file
return g
}
// Input sets reader to convert.
// InputFile or InputImage called before will be ignored.
func (g *Gifsicle) Input(reader io.Reader) *Gifsicle {
g.inputFile = ""
g.inputGif = nil
g.input = reader
return g
}
// InputGif sets gif to convert.
// InputFile or Input called before will be ignored.
func (g *Gifsicle) InputGif(inputGif *gif.GIF) *Gifsicle {
g.inputFile = ""
g.input = nil
g.inputGif = inputGif
return g
}
// OutputFile specify the name of the output jpeg file.
// Output called before will be ignored.
func (g *Gifsicle) OutputFile(file string) *Gifsicle {
g.output = nil
g.outputFile = file
return g
}
// Output specify writer to write jpeg file content.
// OutputFile called before will be ignored.
func (g *Gifsicle) Output(writer io.Writer) *Gifsicle {
g.outputFile = ""
g.output = writer
return g
}
// For the -O[level] or --optimize[=level] arguments.
//
// See https://www.lcdf.org/gifsicle/man.html for more information.
func (g *Gifsicle) OptimizeLevel(l OptimizeLevel) *Gifsicle {
g.optimizeLevel = l
return g
}
// Sets the --lossy parameter.
//
// This parameter ranges from 0-200 (higher value -> more compression).
//
// This is the main compression parameter used in websites like ezgif!
func (g *Gifsicle) Lossy(lossy uint) *Gifsicle {
if lossy > 200 {
lossy = 200
}
g.lossy = lossy
return g
}
// Sets the --colors parameter.
//
// This parameter ranges from 2-256 (lower value -> more compression).
func (g *Gifsicle) NumColors(numColors uint) *Gifsicle {
if numColors < 2 {
numColors = 2
}
if numColors > 256 {
numColors = 256
}
g.numColors = &numColors
return g
}
// Version returns gifsicle --version
func (g *Gifsicle) Version() (string, error) {
return version(g.binWrapper)
}
// Reset resets all parameters to default values
func (g *Gifsicle) Reset() *Gifsicle {
g.lossy = 20
g.optimizeLevel = OPTIMIZE_LEVEL_ONE
g.numColors = nil
return g
}
func (g *Gifsicle) setInput() error {
if g.input != nil {
g.binWrapper.StdIn(g.input)
} else if g.inputGif != nil {
r, err := createReaderFromGif(g.inputGif)
if err != nil {
return fmt.Errorf("createReaderFromGif: %v", err)
}
g.binWrapper.StdIn(r)
} else if g.inputFile != "" {
g.binWrapper.Arg(g.inputFile)
} else {
return errors.New("undefined input")
}
return nil
}
func (g *Gifsicle) getOutput() (string, error) {
if g.output != nil {
return "", nil
} else if g.outputFile != "" {
return g.outputFile, nil
} else {
return "", errors.New("undefined output")
}
}
func (g *Gifsicle) Run() error {
defer g.binWrapper.Reset()
g.binWrapper.Arg(fmt.Sprintf("--lossy=%d", g.lossy))
g.binWrapper.Arg(fmt.Sprintf("--optimize=%s", g.optimizeLevel))
if g.numColors != nil {
g.binWrapper.Arg("--colors", fmt.Sprintf("%d", *g.numColors))
}
output, err := g.getOutput()
if err != nil {
return err
}
if output != "" {
g.binWrapper.Arg("--output", output)
}
err = g.setInput()
if err != nil {
return err
}
if g.output != nil {
g.binWrapper.SetStdOut(g.output)
}
err = g.binWrapper.Run()
if err != nil {
return errors.New(err.Error() + ". " + string(g.binWrapper.StdErr()))
}
return nil
}