forked from discord/lilliput
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpmux.go
167 lines (135 loc) · 3.55 KB
/
webpmux.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
package lilliput
// #cgo darwin CXXFLAGS: -I${SRCDIR}/deps/osx/include
// #cgo linux CXXFLAGS: -I${SRCDIR}/deps/linux/include
// #cgo LDFLAGS: -lwebpmux -lwebpdemux -lwebp
// #cgo darwin LDFLAGS: -L${SRCDIR}/deps/osx/lib
// #cgo linux LDFLAGS: -L${SRCDIR}/deps/linux/lib
// #include "webpmux.hpp"
import "C"
import (
"io"
"time"
"unsafe"
)
type WebPDecoder struct {
info C.WebPAnimInfo
decoder *C.WebPAnimDecoder
lastTime C.int
}
type WebPEncoder struct {
encoder *C.WebPAnimEncoder
buffer []byte
size int
lastTime C.int
}
var webpAnimationDecodingEnabled bool = true
func (d *WebPDecoder) Header() (*ImageHeader, error) {
return &ImageHeader{
width: int(d.info.canvas_width),
height: int(d.info.canvas_height),
pixelType: PixelType(C.CV_8UC4),
orientation: OrientationTopLeft,
numFrames: int(d.info.frame_count),
}, nil
}
func (d *WebPDecoder) Description() string {
return "WEBP"
}
func (d *WebPDecoder) Duration() time.Duration {
return 0
}
func (d *WebPDecoder) HasMore() bool {
return C.WebPAnimDecoderHasMoreFrames(d.decoder) != 0 && (webpAnimationDecodingEnabled || d.lastTime == C.int(0))
}
func (d *WebPDecoder) DecodeTo(f *Framebuffer) error {
if d.decoder == nil {
return ErrDecodingFailed
}
if !d.HasMore() {
return io.EOF
}
err := f.resizeMat(int(d.info.canvas_width), int(d.info.canvas_height), PixelType(C.CV_8UC4))
if err != nil {
return err
}
res := C.webpmux_decoder_read_data(d.decoder, f.mat)
if res < 0 {
return ErrDecodingFailed
}
f.duration = time.Duration(res-d.lastTime) * time.Millisecond
d.lastTime = res
return nil
}
func (d *WebPDecoder) SkipFrame() error {
if d.decoder == nil {
return ErrDecodingFailed
}
if !d.HasMore() {
return io.EOF
}
if C.webpmux_decoder_skip_frame(d.decoder) != 0 {
return ErrDecodingFailed
}
return nil
}
func (d *WebPDecoder) Close() {
if d.decoder != nil {
C.WebPAnimDecoderDelete(d.decoder)
d.decoder = nil
}
}
func (e *WebPEncoder) Encode(frame *Framebuffer, opt map[int]int) ([]byte, error) {
if webpAnimationDecodingEnabled {
if e.encoder == nil {
e.encoder = C.webpmux_create_encoder(C.int(frame.width), C.int(frame.height))
if e.encoder == nil {
return nil, ErrBufTooSmall
}
}
if frame == nil {
e.Close()
return e.buffer[:e.size], nil
}
C.webpmux_encoder_add_frame(e.encoder, frame.mat, e.lastTime, C.int(opt[WebpQuality]))
e.lastTime += C.int(frame.duration / time.Millisecond)
return nil, nil
} else {
if e.lastTime != C.int(0) {
return nil, nil
}
res := C.webpmux_encode_single_frame(e.encoder, frame.mat, C.int(opt[WebpQuality]), unsafe.Pointer(&e.buffer[0]), C.size_t(cap(e.buffer)))
if res == 0 {
return nil, ErrDecodingFailed
}
e.size = int(res)
return e.buffer[:e.size], nil
}
}
func (e *WebPEncoder) Close() {
if e.encoder != nil {
e.size = int(C.webpmux_encoder_write(e.encoder, unsafe.Pointer(&e.buffer[0]), C.size_t(cap(e.buffer)), e.lastTime))
e.encoder = nil
}
}
func newWebPDecoder(buf []byte) (*WebPDecoder, error) {
retval := &WebPDecoder{
info: C.WebPAnimInfo{},
decoder: nil,
lastTime: C.int(0),
}
retval.decoder = C.webpmux_create_decoder(unsafe.Pointer(&buf[0]), C.size_t(len(buf)), unsafe.Pointer(&retval.info))
if retval.decoder == nil {
return nil, ErrInvalidImage
}
return retval, nil
}
func newWebPEncoder(decodedBy Decoder, dstBuf []byte) (*WebPEncoder, error) {
return &WebPEncoder{
encoder: nil,
buffer: dstBuf,
lastTime: 0,
}, nil
}
func SetWebPAnimDecodingEnabled(enabled bool) {
webpAnimationDecodingEnabled = enabled
}