/
export_webp.go
68 lines (56 loc) · 1.44 KB
/
export_webp.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
package simnpcs2
import (
"image"
"os"
"github.com/sizeofint/webpanimation"
)
type webpExport struct {
anim *webpanimation.WebpAnimation
config webpanimation.WebPConfig
timeline int
timestep int
}
func newWebPExport(width, height int) *webpExport {
anim := webpanimation.NewWebpAnimation(width, height, 0)
anim.WebPAnimEncoderOptions.SetKmin(9)
anim.WebPAnimEncoderOptions.SetKmax(17)
config := webpanimation.NewWebpConfig()
config.SetLossless(1)
return &webpExport{
anim: anim,
config: config,
timeline: 0,
timestep: 50,
}
}
func (m *webpExport) ExportWebp(name string) error {
// Write the final frame.
m.timeline += m.timestep
if err := m.anim.AddFrame(nil, m.timeline, m.config); err != nil {
return err
}
f, err := os.Create(name)
if err != nil {
return err
}
// Encode animation and write result bytes in buffer.
if err = m.anim.Encode(f); err != nil {
return err
}
if err = f.Close(); err != nil {
return err
}
m.anim.ReleaseMemory() // TODO: This doesn't really prevent crashes?
return nil
}
func (m *World) storeWebPFrame() error {
// Write the current map to the animation.
// Create a colored image of the given width and height.
img := image.NewNRGBA(image.Rect(0, 0, m.Width, m.Height))
m.renderFrame(img)
if err := m.webpExport.anim.AddFrame(img, m.webpExport.timeline, m.webpExport.config); err != nil {
return err
}
m.webpExport.timeline += m.webpExport.timestep
return nil
}