-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
200 lines (163 loc) · 4.42 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
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
package main
import (
"errors"
"flag"
"fmt"
"image"
"image/color"
"image/gif"
"io"
"io/ioutil"
"os"
colorful "github.com/lucasb-eyer/go-colorful"
)
var config struct {
inFile string
outputFile string
cycles int
repeats int
frameRate int
black bool
}
func init() {
flag.StringVar(&config.inFile, "in", "", "input GIF file")
flag.StringVar(&config.outputFile, "out", "", "output GIF file")
flag.IntVar(&config.cycles, "cycles", 1, "number of color cycles during the GIF")
flag.IntVar(&config.repeats, "repeats", 0, "number of times to repeat GIF before color shifting")
flag.IntVar(&config.frameRate, "framerate", 10, "frame rate in 100ths of seconds for static GIFs")
flag.BoolVar(&config.black, "black", false, "add color to a black and white image before color shifting")
}
func main() {
if err := mainErr(); err != nil {
fmt.Fprintf(os.Stderr, "partygif: error: %s\n", err)
os.Exit(1)
}
}
func mainErr() error {
flag.Parse()
if len(flag.Args()) != 0 {
return errors.New("does not take any non-flag arguments")
}
inputFile, err := openInFile()
if err != nil {
return fmt.Errorf("opening input file: %s", err)
}
defer func() { _ = inputFile.Close() }()
outputFile, err := openOutputFile()
if err != nil {
return fmt.Errorf("opening output file: %s", err)
}
defer func() { _ = outputFile.Close() }()
return partyGIF(inputFile, outputFile)
}
func partyGIF(inputFile io.Reader, outputFile io.Writer) error {
img, err := gif.DecodeAll(inputFile)
if err != nil {
return fmt.Errorf("decoding input file: %s", err)
}
if config.black {
colorize(img)
}
addRepeats(img)
colorShift(img)
if err := gif.EncodeAll(outputFile, img); err != nil {
return fmt.Errorf("encoding gif to output file: %s", err)
}
return nil
}
func colorize(img *gif.GIF) {
for _, frame := range img.Image {
for i := range frame.Palette {
rawColor := newRawColor(frame.Palette[i])
rawColor.r = rawColor.a
frame.Palette[i] = rawColor
}
}
}
func addRepeats(img *gif.GIF) {
if len(img.Image) == 1 {
img.Delay = []int{config.frameRate}
}
originalLength := len(img.Image)
for i := 0; i < config.repeats-1; i++ {
for j := 0; j < originalLength; j++ {
img.Image = append(img.Image, copyFrame(img.Image[j]))
img.Delay = append(img.Delay, img.Delay[j])
img.Disposal = append(img.Disposal, img.Disposal[j])
}
}
// clean restart of original GIF
for repeatIndex := 0; repeatIndex < config.repeats; repeatIndex++ {
repeatBegin := repeatIndex * originalLength
lastFrameInRepeat := repeatBegin + originalLength - 1
img.Disposal[lastFrameInRepeat] = gif.DisposalBackground
}
}
func copyFrame(frame *image.Paletted) *image.Paletted {
copyPix := make([]uint8, len(frame.Pix))
copy(copyPix, frame.Pix)
copyPalette := make(color.Palette, len(frame.Palette))
copy(copyPalette, frame.Palette)
return &image.Paletted{
Pix: copyPix,
Stride: frame.Stride,
Rect: frame.Rect,
Palette: copyPalette,
}
}
func colorShift(img *gif.GIF) {
hueStep := 360 / float64(len(img.Image)) * float64(config.cycles)
for frameIndex, frame := range img.Image {
hueShift := hueStep * float64(frameIndex)
for i := range frame.Palette {
frame.Palette[i] = shiftHue(hueShift, frame.Palette[i])
}
}
}
func shiftHue(shift float64, col color.Color) color.Color {
rgb, ok := colorful.MakeColor(col)
if !ok {
return col
}
hue, chroma, lum := rgb.Hcl()
hue += shift
color := newRawColor(colorful.Hcl(hue, chroma, lum).Clamped())
_, _, _, alpha := col.RGBA()
color.a = alpha
return color
}
func newRawColor(color color.Color) rawColor {
r, g, b, a := color.RGBA()
return rawColor{r: r, g: g, b: b, a: a}
}
type rawColor struct{ r, g, b, a uint32 }
func (c rawColor) RGBA() (uint32, uint32, uint32, uint32) {
return c.r, c.g, c.b, c.a
}
func openInFile() (io.ReadCloser, error) {
if config.inFile == "" {
return ioutil.NopCloser(os.Stdin), nil
}
file, err := os.Open(config.inFile)
if err != nil {
return nil, fmt.Errorf("opening file: %s: %s", config.inFile, err)
}
return file, nil
}
func openOutputFile() (io.WriteCloser, error) {
if config.outputFile == "" {
return struct {
io.Writer
io.Closer
}{
Writer: os.Stdout,
Closer: ioutil.NopCloser(nil),
}, nil
}
flags := os.O_WRONLY | os.O_CREATE | os.O_TRUNC
file, err := os.OpenFile(config.outputFile, flags, 0644)
if err != nil {
return nil, fmt.Errorf("opening file: %s: %s", config.outputFile, err)
}
return file, nil
}