-
Notifications
You must be signed in to change notification settings - Fork 174
/
twitterify.go
180 lines (155 loc) · 4.05 KB
/
twitterify.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
package main
import (
"flag"
"fmt"
"image"
"image/color"
"image/gif"
"image/png"
"io"
"os"
"path/filepath"
"golang.org/x/image/draw"
)
var (
width = flag.Int("width", 506, "target min width")
height = flag.Int("height", 128, "target min height")
repeat = flag.Int("repeat", 3, "repeat count")
transparent = flag.Bool("transparent", false, "transparent background")
duplicate = flag.Bool("duplicate", false, "use duplication instead of repeating animation")
duration = flag.Int("duration", 0, "override frame duration")
)
func handleGif(infile io.Reader, outfile io.Writer) error {
// TODO: fix handling of different disposals
source, err := gif.DecodeAll(infile)
if err != nil {
return fmt.Errorf("failed to decode gif: %v", err)
}
size := image.Pt(source.Config.Width, source.Config.Height)
if size.X < *width {
size.X = *width
}
if size.Y < *height {
size.Y = *height
}
var target gif.GIF
target.Config = source.Config
target.Config.Width = size.X
target.Config.Height = size.Y
target.LoopCount = source.LoopCount
target.BackgroundIndex = source.BackgroundIndex
offset := image.Pt(
size.X/2-source.Config.Width/2,
size.Y/2-source.Config.Height/2,
)
for i, m := range source.Image {
d := image.NewPaletted(image.Rectangle{image.ZP, size}, m.Palette)
if !*transparent {
for k := range d.Pix {
d.Pix[k] = m.Pix[0]
}
}
draw.Draw(d, m.Bounds().Add(offset), m, image.ZP, draw.Over)
delay := source.Delay[i]
if *duration > 0 {
delay = *duration
}
if *duplicate {
target.Image = append(target.Image, d)
target.Delay = append(target.Delay, delay/2)
if len(source.Disposal) > 0 {
target.Disposal = append(target.Disposal, source.Disposal[i])
}
if i != len(source.Image)-1 {
target.Image = append(target.Image, d)
target.Delay = append(target.Delay, delay/2)
if len(source.Disposal) > 0 {
target.Disposal = append(target.Disposal, source.Disposal[i])
}
}
} else {
target.Image = append(target.Image, d)
target.Delay = append(target.Delay, delay)
if len(source.Disposal) > 0 {
target.Disposal = append(target.Disposal, source.Disposal[i])
}
}
}
if !*duplicate {
n := len(source.Image)
for k := 1; k < *repeat; k++ {
for i := 0; i < n; i++ {
target.Image = append(target.Image, target.Image[i])
target.Delay = append(target.Delay, target.Delay[i])
if len(target.Disposal) > 0 {
target.Disposal = append(target.Disposal, target.Disposal[i])
}
}
}
}
err = gif.EncodeAll(outfile, &target)
if err != nil {
return fmt.Errorf("failed to encode to gif: %v", err)
}
return nil
}
func handlePng(infile io.Reader, outfile io.Writer) error {
source, err := png.Decode(infile)
if err != nil {
return fmt.Errorf("failed to decode png: %v", err)
}
size := source.Bounds().Size()
if size.X < *width {
size.X = *width
}
if size.Y < *height {
size.Y = *height
}
offset := image.Pt(
size.X/2-source.Bounds().Dx()/2,
size.Y/2-source.Bounds().Dy()/2,
)
target := image.NewRGBA(image.Rectangle{image.ZP, size})
if !*transparent {
draw.Draw(target, target.Bounds(), &image.Uniform{color.White}, image.ZP, draw.Over)
}
draw.Draw(target, source.Bounds().Add(offset), source, image.ZP, draw.Over)
err = png.Encode(outfile, target)
if err != nil {
return fmt.Errorf("failed to encode to png: %v", err)
}
return nil
}
func main() {
flag.Parse()
if flag.Arg(0) == "" || flag.Arg(1) == "" {
flag.Usage()
os.Exit(1)
}
infile, err := os.Open(flag.Arg(0))
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
defer infile.Close()
outfile, err := os.Create(flag.Arg(1))
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
defer outfile.Close()
switch filepath.Ext(flag.Arg(0)) {
case ".gif":
err := handleGif(infile, outfile)
if err != nil {
fmt.Fprintf(os.Stderr, "failed twitterifying gif: %v\n", err)
os.Exit(1)
}
case ".png":
err := handlePng(infile, outfile)
if err != nil {
fmt.Fprintf(os.Stderr, "failed twitterifying png: %v\n", err)
os.Exit(1)
}
}
}