/
renderer.go
383 lines (316 loc) · 9.25 KB
/
renderer.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package image
import (
"image"
"image/color/palette"
"image/draw"
"image/gif"
"math"
"math/rand"
"time"
"github.com/nfnt/resize"
"github.com/nomad-software/meme/cli"
"github.com/nomad-software/meme/data"
gfx "github.com/nomad-software/meme/image/draw"
"github.com/nomad-software/meme/image/stream"
)
const (
maxImageSize = 650 // px
shakeDelay = 2 // 100ths of a second
shakeFrames = 10 // Number of frames to create when shaking static images
shakeIntensity = 8 // px
)
// RenderImage performs the graphical manipulation of the image.
func RenderImage(opt cli.Options, st stream.Stream) stream.Stream {
if opt.Trigger {
st = shake(st)
st = trigger(st)
} else if opt.Shake {
st = shake(st)
}
if opt.Trigger || opt.Shake || (opt.Gif && st.IsGif()) {
st = renderGif(opt, st)
} else {
st = renderImage(opt, st)
}
return st
}
// Trigger adds the triggered banner.
func trigger(st stream.Stream) stream.Stream {
src := st.DecodeGif()
width := uint(src.Config.Width + (shakeIntensity * 2))
ds := Decal(data.TriggeredDecal)
decal := resize.Resize(width, 0, ds.DecodeImage(), resize.NearestNeighbor)
queue := make(chan triggerInfo)
for x, frame := range src.Image {
ti := triggerInfo{
frame: frame,
decal: decal,
index: x,
}
go processTrigger(ti, queue)
}
for range src.Image {
ti := <-queue
src.Image[ti.index] = ti.frame
}
close(queue)
return stream.EncodeGif(src)
}
// A unit of work containing frame information for triggering gifs.
type triggerInfo struct {
frame *image.Paletted
decal image.Image
index int
}
// Draw the triggered banner on the frame.
func processTrigger(ti triggerInfo, output chan triggerInfo) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
rx := r.Intn(shakeIntensity*2) - shakeIntensity
ry := r.Intn(shakeIntensity*2) - shakeIntensity
fHeight := ti.frame.Bounds().Dy()
dHeight := ti.decal.Bounds().Dy()
x := shakeIntensity + rx
y := (fHeight - (dHeight - shakeIntensity)) + ry
point := image.Point{x, -y}
draw.FloydSteinberg.Draw(ti.frame, ti.frame.Bounds(), ti.decal, point)
output <- ti
}
// Shake randomly shakes an image.
func shake(st stream.Stream) stream.Stream {
if st.IsGif() {
return shakeGif(st)
}
return shakeImage(st)
}
// Create a random point for shaking the image.
func pointShaker() func() image.Point {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return func() image.Point {
x := r.Intn(shakeIntensity + 1)
y := r.Intn(shakeIntensity + 1)
return image.Point{x, y}
}
}
// Create a rectangle to crop the shaking image to not expose the borders.
func shakeBounds(b image.Rectangle) image.Rectangle {
x := b.Bounds().Max.X - shakeIntensity*2
y := b.Bounds().Max.Y - shakeIntensity*2
return image.Rect(0, 0, x, y)
}
// shakeGif randomly shakes a gif.
// This function can't use concurrency because normalising the gif needs a
// shared base image for all frames.
func shakeGif(st stream.Stream) stream.Stream {
src := st.DecodeGif()
base := src.Image[0]
images := make([]*image.Paletted, len(src.Image))
delays := make([]int, len(src.Image))
crop := shakeBounds(base.Bounds())
shakePoint := pointShaker()
for x, frame := range src.Image {
// Draw on the base.
draw.Draw(base, frame.Bounds(), frame, frame.Bounds().Min, draw.Over)
// Create a new frame.
img := image.NewPaletted(base.Bounds(), frame.Palette)
draw.Draw(img, img.Bounds(), base, shakePoint(), draw.Src)
images[x] = img.SubImage(crop).(*image.Paletted)
delays[x] = shakeDelay
}
dst := &gif.GIF{
Image: images,
Delay: delays,
}
return stream.EncodeGif(dst)
}
// A unit of work containing frame information for shaking an image.
type shakeImgInfo struct {
img image.Image
frame *image.Paletted
point image.Point
crop image.Rectangle
index int
}
// shakeImage randomly shakes an image creating a gif animation.
// This function can use concurrency because nothing is shared between frames.
func shakeImage(st stream.Stream) stream.Stream {
src := st.DecodeImage()
images := make([]*image.Paletted, shakeFrames)
delays := make([]int, shakeFrames)
crop := shakeBounds(src.Bounds())
queue := make(chan shakeImgInfo)
shakePoint := pointShaker()
for x := range images {
si := shakeImgInfo{
img: src,
point: shakePoint(),
crop: crop,
index: x,
}
go processImageShake(si, queue)
}
for range images {
si := <-queue
images[si.index] = si.frame
delays[si.index] = shakeDelay
}
close(queue)
dst := &gif.GIF{
Image: images,
Delay: delays,
}
return stream.EncodeGif(dst)
}
// Process shaking an image.
func processImageShake(si shakeImgInfo, output chan shakeImgInfo) {
img := image.NewPaletted(si.img.Bounds(), palette.Plan9)
draw.FloydSteinberg.Draw(img, img.Bounds(), si.img, si.point)
si.frame = img.SubImage(si.crop).(*image.Paletted)
output <- si
}
// RenderImage performs the graphical manipulation of the image.
func renderImage(opt cli.Options, st stream.Stream) stream.Stream {
img := st.DecodeImage()
img = reduceImage(img, maxImageSize)
// Draw on the text.
ctx := gfx.NewContext(img)
if opt.Top != "" {
gfx.TopBanner(ctx, opt.Top)
}
if opt.Bottom != "" {
gfx.BottomBanner(ctx, opt.Bottom)
}
return stream.EncodeImage(ctx.Image())
}
// reduceImage will resize an image if any of its dimensions are above the passed max
// size.
func reduceImage(img image.Image, maxSize uint) image.Image {
w := img.Bounds().Dx()
h := img.Bounds().Dy()
if w > h && w > int(maxSize) {
img = resize.Resize(maxSize, 0, img, resize.NearestNeighbor)
} else if h > w && h > int(maxSize) {
img = resize.Resize(0, maxSize, img, resize.NearestNeighbor)
}
return img
}
// A unit of work containing frame information for drawing text.
type drawInfo struct {
bounds image.Rectangle
frame *image.Paletted
index int
top string
bottom string
}
// RenderGif performs the graphical manipulation of the gif.
func renderGif(opt cli.Options, st stream.Stream) stream.Stream {
src := st.DecodeGif()
src = reduceGif(opt, src, maxImageSize)
queue := make(chan drawInfo)
for x, frame := range src.Image {
fi := drawInfo{
bounds: src.Image[0].Bounds(),
frame: frame,
index: x,
top: opt.Top,
bottom: opt.Bottom,
}
go processFrameDraw(fi, queue)
}
for range src.Image {
fi := <-queue
src.Image[fi.index] = fi.frame
}
close(queue)
return stream.EncodeGif(src)
}
// Process drawing on each gif frame.
func processFrameDraw(fi drawInfo, output chan drawInfo) {
// Expand each frame, if needed, so it's the same size as the base.
// This is to make it easier to draw and position the text.
img := image.NewPaletted(fi.bounds, fi.frame.Palette)
draw.Draw(img, fi.frame.Bounds(), fi.frame, fi.frame.Bounds().Min, draw.Src)
// Draw on the text.
ctx := gfx.NewContext(img)
if fi.top != "" {
gfx.TopBanner(ctx, fi.top)
}
if fi.bottom != "" {
gfx.BottomBanner(ctx, fi.bottom)
}
// Convert the graphic context to a paletted image.
img = image.NewPaletted(img.Bounds(), img.Palette)
draw.Draw(img, img.Bounds(), ctx.Image(), image.ZP, draw.Src)
fi.frame = img.SubImage(fi.frame.Bounds()).(*image.Paletted)
output <- fi
}
// A unit of work containing frame information for reducing a gif frame.
type reduceInfo struct {
config *image.Config
base *image.RGBA
frame *image.Paletted
fctr float64
index int
}
// reduceGif will resize a gif if any of its dimensions are above the passed max
// size.
func reduceGif(opt cli.Options, src *gif.GIF, maxSize int) *gif.GIF {
first := src.Image[0]
fctr := calcFactor(first, maxSize)
// No reduction necessary.
if fctr == 1.0 {
return src
}
base := image.NewRGBA(first.Bounds())
for x, frame := range src.Image {
rs := reduceInfo{
config: &src.Config,
base: base,
frame: frame,
fctr: fctr,
index: x,
}
src.Image[x] = processFrameReduction(rs)
}
return src
}
// Process resizing each gif frame at max quality.
func processFrameReduction(rs reduceInfo) *image.Paletted {
if rs.index == 0 {
resBounds := reduceBounds(rs.frame.Bounds(), rs.fctr)
rs.config.Width = resBounds.Dx()
rs.config.Height = resBounds.Dy()
}
if rs.frame.Bounds().Dx() == 0 || rs.frame.Bounds().Dy() == 0 {
return rs.frame // Empty frame, don't change or can cause corruption.
}
// Draw over the base.
draw.Draw(rs.base, rs.frame.Bounds(), rs.frame, rs.frame.Bounds().Min, draw.Over)
// Resize the base to the required size.
w := uint(rs.config.Width)
h := uint(rs.config.Height)
res := resize.Resize(w, h, rs.base, resize.NearestNeighbor)
// Create a new frame.
img := image.NewPaletted(res.Bounds(), rs.frame.Palette)
draw.Draw(img, res.Bounds(), res, image.ZP, draw.Src)
return img
}
// Calculate the reduction factor from a desired maximum size.
func calcFactor(img image.Image, maxSize int) float64 {
w := img.Bounds().Dx()
h := img.Bounds().Dy()
fctr := 1.0
if w > h && w > maxSize {
fctr = float64(maxSize) / float64(w)
} else if h > w && h > maxSize {
fctr = float64(maxSize) / float64(w)
}
return fctr
}
// Recalculate bounds sizes using a passed factor.
func reduceBounds(src image.Rectangle, factor float64) image.Rectangle {
x0 := int(math.Floor(float64(src.Min.X) * factor))
y0 := int(math.Floor(float64(src.Min.Y) * factor))
x1 := int(math.Floor(float64(src.Max.X) * factor))
y1 := int(math.Floor(float64(src.Max.Y) * factor))
return image.Rect(x0, y0, x1, y1)
}