forked from boppreh/steamgrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverlays.go
373 lines (332 loc) · 11.4 KB
/
overlays.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
package main
import (
"bytes"
"errors"
"fmt"
"image"
"runtime/debug"
// "image/draw"
"image/jpeg"
"image/png"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/kmicki/apng"
"github.com/kmicki/webpanimation"
"golang.org/x/image/draw"
)
// LoadOverlays from the given dir, returning a map of name -> image.
func LoadOverlays(dir string, artStyles map[string][]string) (overlays map[string]image.Image, err error) {
overlays = make(map[string]image.Image, 0)
if _, err = os.Stat(dir); err != nil {
return overlays, nil
}
files, err := ioutil.ReadDir(dir)
if err != nil {
return
}
imageExtensions := []string{"png", "jpg", "jpeg", "gif"}
for _, file := range files {
isImage := false
for _, extension := range imageExtensions {
isImage = isImage || strings.HasSuffix(file.Name(), extension)
}
if !isImage {
continue
}
reader, err := os.Open(filepath.Join(dir, file.Name()))
if err != nil {
return nil, err
}
defer reader.Close()
img, _, err := image.Decode(reader)
if err != nil {
return overlays, err
}
name := strings.TrimSuffix(file.Name(), filepath.Ext(file.Name()))
// Normalize overlay name.
for _, artStyleExtensions := range artStyles {
if strings.HasSuffix(name, artStyleExtensions[1]) {
name = strings.TrimSuffix(name, artStyleExtensions[1])
name = strings.TrimRight(strings.ToLower(name), "s")
name = name + artStyleExtensions[1]
}
}
overlays[name] = img
}
return
}
// ApplyOverlay to the game image, depending on the category. The
// resulting image is saved over the original.
func ApplyOverlay(game *Game, overlays map[string]image.Image, artStyleExtensions []string, convertWebpToApng bool, convertWebpToApngCoversBanners bool, maxMem uint64) error {
if game.CleanImageBytes == nil || len(game.Tags) == 0 {
return nil
}
buf := new(bytes.Buffer)
bufReady := false
var errBuff error
errBuff = nil
convertWebpToApng = convertWebpToApng || (convertWebpToApngCoversBanners &&
(strings.Contains(artStyleExtensions[1], "cover")) || (strings.Contains(artStyleExtensions[1], "banner")))
isApng := false
isWebp := false
formatFound := false
var err error
var webpImage *webpanimation.WebpAnimationDecoded
defer func() {
if webpImage != nil {
webpanimation.ReleaseDecoder(webpImage)
}
}()
// Try WEBP
var gameImage image.Image
webpImage, err = webpanimation.GetInfo(bytes.NewBuffer(game.CleanImageBytes))
if err == nil {
formatFound = true
if err != nil {
formatFound = false
} else if webpImage.FrameCnt <= 1 {
webpFrame, ok := webpanimation.GetNextFrame(webpImage)
if ok {
gameImage = webpFrame.Image
} else {
err = errors.New("can't get the first frame of single-frame WEBP image")
}
} else {
isWebp = true
memNeeded := uint64(webpImage.Width) * uint64(webpImage.Height) * 4 * uint64(webpImage.FrameCnt)
if convertWebpToApng && maxMem > 0 {
if memNeeded > maxMem {
fmt.Println("WEBP animation too big to convert to APNG. Leaving WEBP.")
convertWebpToApng = false
} else if memNeeded > maxMem/2 {
// free up memory for big conversion
debug.FreeOSMemory()
}
}
}
}
// Try APNG
var apngImage apng.APNG
if !formatFound {
apngImage, err = apng.DecodeAll(bytes.NewBuffer(game.CleanImageBytes))
if err == nil {
if len(apngImage.Frames) > 1 {
isApng = true
} else {
gameImage = apngImage.Frames[0].Image
}
} else {
gameImage, _, err = image.Decode(bytes.NewBuffer(game.CleanImageBytes))
if err != nil {
return err
}
}
}
applied := false
var webpanim *webpanimation.WebpAnimation
defer func() {
if webpanim != nil {
webpanim.ReleaseMemory()
//fmt.Println("WEBPAnim Memory Released")
}
}()
for _, tag := range game.Tags {
// Normalize tag name by lower-casing it and remove trailing "s" from
// plurals. Also, <, > and / are replaced with - because you can't have
// them in Windows paths.
tagName := strings.TrimRight(strings.ToLower(tag), "s")
tagName = strings.Replace(tagName, "<", "-", -1)
tagName = strings.Replace(tagName, ">", "-", -1)
tagName = strings.Replace(tagName, "/", "-", -1)
overlayImage, ok := overlays[tagName+artStyleExtensions[1]]
if !ok {
continue
}
overlaySize := overlayImage.Bounds().Max
if isApng {
fmt.Printf("Apply Overlay to APNG.")
originalSize := apngImage.Frames[0].Image.Bounds().Max
// Scale overlay to imageSize so the images won't get that huge…
overlayScaled := image.NewRGBA(image.Rect(0, 0, originalSize.X, originalSize.Y))
if originalSize.X != overlaySize.X && originalSize.Y != overlaySize.Y {
// https://godoc.org/golang.org/x/image/draw#Kernel.Scale
draw.ApproxBiLinear.Scale(overlayScaled, overlayScaled.Bounds(), overlayImage, overlayImage.Bounds(), draw.Over, nil)
} else {
draw.Draw(overlayScaled, overlayScaled.Bounds(), overlayImage, image.Point{}, draw.Src)
}
for i, frame := range apngImage.Frames {
result := image.NewRGBA(image.Rect(0, 0, originalSize.X, originalSize.Y))
// No idea why these offsets are negative:
draw.Draw(result, result.Bounds(), frame.Image, image.Point{0 - frame.XOffset, 0 - frame.YOffset}, draw.Over)
draw.Draw(result, result.Bounds(), overlayScaled, image.Point{0, 0}, draw.Over)
apngImage.Frames[i].Image = result
apngImage.Frames[i].XOffset = 0
apngImage.Frames[i].YOffset = 0
apngImage.Frames[i].BlendOp = apng.BLEND_OP_OVER
fmt.Printf("\rApply Overlay to APNG. Overlayed frame %8d/%d", i, len(apngImage.Frames))
}
applied = true
fmt.Printf("\rOverlay applied to %v frames of APNG \n", len(apngImage.Frames))
} else if isWebp {
fmt.Printf("Apply Overlay to WEBP.")
if webpImage == nil {
fmt.Printf("\rWebPImage not initialized.\n")
continue
}
originalSize := image.Point{webpImage.Width, webpImage.Height}
var webpConfig webpanimation.WebPConfig
var encoder *apng.FrameByFrameEncoder
if convertWebpToApng {
bufReady = true
encoder = apng.InitializeEncoding(buf, uint32(webpImage.FrameCnt), uint(webpImage.LoopCount))
} else {
webpanim = webpanimation.NewWebpAnimation(webpImage.Width, webpImage.Height, webpImage.LoopCount)
webpanim.WebPAnimEncoderOptions.SetKmin(9)
webpanim.WebPAnimEncoderOptions.SetKmax(17)
webpConfig = webpanimation.NewWebpConfig()
webpConfig.SetLossless(1)
}
// Scale overlay to imageSize so the images won't get that huge…
overlayScaled := image.NewRGBA(image.Rect(0, 0, originalSize.X, originalSize.Y))
var result *image.RGBA
if originalSize.X != overlaySize.X && originalSize.Y != overlaySize.Y {
// https://godoc.org/golang.org/x/image/draw#Kernel.Scale
draw.ApproxBiLinear.Scale(overlayScaled, overlayScaled.Bounds(), overlayImage, overlayImage.Bounds(), draw.Over, nil)
} else {
draw.Draw(overlayScaled, overlayScaled.Bounds(), overlayImage, image.Point{}, draw.Src)
}
i := 0
var lastTimestamp int
frame, ok := webpanimation.GetNextFrame(webpImage)
for ok {
if v, o := frame.Image.(*image.RGBA); o {
result = v
} else {
result = image.NewRGBA(image.Rect(0, 0, originalSize.X, originalSize.Y))
draw.Draw(result, result.Bounds(), frame.Image, image.Point{0, 0}, draw.Over)
}
draw.Draw(result, result.Bounds(), overlayScaled, image.Point{0, 0}, draw.Over)
var delay uint16
if i == 0 {
delay = 0
} else {
delay = uint16(frame.Timestamp - lastTimestamp)
}
lastTimestamp = frame.Timestamp
if convertWebpToApng {
apngFrame := apng.Frame{
Image: result,
IsDefault: false,
XOffset: 0,
YOffset: 0,
DisposeOp: apng.DISPOSE_OP_NONE,
BlendOp: apng.BLEND_OP_OVER,
DelayNumerator: delay,
DelayDenominator: 1000,
}
encoder.EncodeFrame(apngFrame)
fmt.Printf("\rApply Overlay to WEBP as APNG. Overlayed frame %8d/%d", i, webpImage.FrameCnt)
} else {
err = webpanim.AddFrame(result, frame.Timestamp, webpConfig)
fmt.Printf("\rApply Overlay to WEBP. Overlayed frame %8d/%d", i, webpImage.FrameCnt)
}
i++
frame, ok = webpanimation.GetNextFrame(webpImage)
}
applied = true
if convertWebpToApng {
errBuff = encoder.Finish()
fmt.Printf("\rOverlay applied to %v frames of WEBP as APNG \n", webpImage.FrameCnt)
} else {
fmt.Printf("\rOverlay applied to %v frames of WEBP \n", webpImage.FrameCnt)
}
} else {
fmt.Printf("Apply Overlay to Single Image.")
originalSize := gameImage.Bounds().Max
// We expect overlays in the correct format so we have to scale the image if it doesn't fit
result := image.NewRGBA(image.Rect(0, 0, overlaySize.X, overlaySize.Y))
if originalSize.X != overlaySize.X && originalSize.Y != overlaySize.Y {
// scale to fit overlay
// https://godoc.org/golang.org/x/image/draw#Kernel.Scale
draw.ApproxBiLinear.Scale(result, result.Bounds(), gameImage, gameImage.Bounds(), draw.Over, nil)
} else {
draw.Draw(result, result.Bounds(), gameImage, image.Point{}, draw.Src)
}
draw.Draw(result, result.Bounds(), overlayImage, image.Point{0, 0}, draw.Over)
gameImage = result
applied = true
fmt.Printf("\rApplied Overlay to Single Image.\n")
}
}
if !applied {
if isWebp && convertWebpToApng {
bufReady = true
// Convert to APNG without overlay
fmt.Printf("Convert WEBP to APNG.")
if webpImage == nil {
fmt.Printf("\rWebPImage not initialized.\n")
return nil
}
originalSize := image.Point{webpImage.Width, webpImage.Height}
encoder := apng.InitializeEncoding(buf, uint32(webpImage.FrameCnt), uint(webpImage.LoopCount))
i := 0
var lastTimestamp int
frame, ok := webpanimation.GetNextFrame(webpImage)
var result *image.RGBA
for ok {
if v, o := frame.Image.(*image.RGBA); o {
result = v
} else {
result = image.NewRGBA(image.Rect(0, 0, originalSize.X, originalSize.Y))
draw.Draw(result, result.Bounds(), frame.Image, image.Point{0, 0}, draw.Over)
}
var delay uint16
if i == 0 {
delay = 0
} else {
delay = uint16(frame.Timestamp - lastTimestamp)
}
lastTimestamp = frame.Timestamp
apngFrame := apng.Frame{
Image: result,
IsDefault: false,
XOffset: 0,
YOffset: 0,
DisposeOp: apng.DISPOSE_OP_NONE,
BlendOp: apng.BLEND_OP_OVER,
DelayNumerator: delay,
DelayDenominator: 1000,
}
encoder.EncodeFrame(apngFrame)
fmt.Printf("\rConvert to WEBP as APNG. Frame %8d/%d", i, webpImage.FrameCnt)
i++
frame, ok = webpanimation.GetNextFrame(webpImage)
}
errBuff = encoder.Finish()
applied = true
fmt.Printf("\rConverted %v frames from WEBP to APNG \n", webpImage.FrameCnt)
} else {
return nil
}
}
if bufReady {
err = errBuff
} else {
if game.ImageExt == ".jpg" || game.ImageExt == ".jpeg" {
err = jpeg.Encode(buf, gameImage, &jpeg.Options{Quality: 95})
} else if (game.ImageExt == ".png" && isApng) || (isWebp && convertWebpToApng) {
err = apng.Encode(buf, apngImage)
} else if (game.ImageExt == ".png" && !isWebp) || (formatFound && !isWebp) {
err = png.Encode(buf, gameImage)
} else if isWebp {
err = webpanim.Encode(buf)
}
}
if err != nil {
return err
}
game.OverlayImageBytes = buf.Bytes()
return nil
}