forked from discord/lilliput
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebp.go
159 lines (135 loc) · 3.6 KB
/
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
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
package lilliput
// #cgo CFLAGS: -msse -msse2 -msse3 -msse4.1 -msse4.2 -mavx
// #cgo darwin CFLAGS: -I${SRCDIR}/deps/osx/include
// #cgo linux CFLAGS: -I${SRCDIR}/deps/linux/include
// #cgo CXXFLAGS: -std=c++11
// #cgo darwin CXXFLAGS: -I${SRCDIR}/deps/osx/include
// #cgo linux CXXFLAGS: -I${SRCDIR}/deps/linux/include
// #cgo LDFLAGS: -lopencv_core -lopencv_imgproc -lwebp -lwebpmux
// #cgo darwin LDFLAGS: -L${SRCDIR}/deps/osx/lib -L${SRCDIR}/deps/osx/share/OpenCV/3rdparty/lib
// #cgo linux LDFLAGS: -L${SRCDIR}/deps/linux/lib -L${SRCDIR}/deps/linux/share/OpenCV/3rdparty/lib
// #include "webp.hpp"
import "C"
import (
"io"
"time"
"unsafe"
)
type webpDecoder struct {
decoder C.webp_decoder
mat C.opencv_mat
buf []byte
}
type webpEncoder struct {
encoder C.webp_encoder
dstBuf []byte
icc []byte
}
func newWebpDecoder(buf []byte) (*webpDecoder, error) {
mat := C.opencv_mat_create_from_data(C.int(len(buf)), 1, C.CV_8U, unsafe.Pointer(&buf[0]), C.size_t(len(buf)))
if mat == nil {
return nil, ErrBufTooSmall
}
decoder := C.webp_decoder_create(mat)
if decoder == nil {
return nil, ErrInvalidImage
}
return &webpDecoder{
decoder: decoder,
mat: mat,
buf: buf,
}, nil
}
func (d *webpDecoder) Header() (*ImageHeader, error) {
return &ImageHeader{
width: int(C.webp_decoder_get_width(d.decoder)),
height: int(C.webp_decoder_get_height(d.decoder)),
pixelType: PixelType(C.webp_decoder_get_pixel_type(d.decoder)),
orientation: OrientationTopLeft,
numFrames: 1,
contentLength: len(d.buf),
}, nil
}
func (d *webpDecoder) Close() {
C.webp_decoder_release(d.decoder)
C.opencv_mat_release(d.mat)
d.buf = nil
}
func (d *webpDecoder) Description() string {
return "WEBP"
}
func (d *webpDecoder) Duration() time.Duration {
return time.Duration(0)
}
func (d *webpDecoder) HasSubtitles() bool {
return false
}
func (d *webpDecoder) IsStreamable() bool {
return false
}
func (d *webpDecoder) ICC() []byte {
iccDst := make([]byte, 8192)
iccLength := C.webp_decoder_get_icc(d.decoder, unsafe.Pointer(&iccDst[0]), C.size_t(cap(iccDst)))
return iccDst[:iccLength]
}
func (d *webpDecoder) DecodeTo(f *Framebuffer) error {
if f == nil {
return io.EOF
}
h, err := d.Header()
if err != nil {
return err
}
err = f.resizeMat(h.Width(), h.Height(), h.PixelType())
if err != nil {
return err
}
ret := C.webp_decoder_decode(d.decoder, f.mat)
if !ret {
return ErrDecodingFailed
}
return nil
}
func (d *webpDecoder) SkipFrame() error {
return ErrSkipNotSupported
}
func newWebpEncoder(decodedBy Decoder, dstBuf []byte) (*webpEncoder, error) {
dstBuf = dstBuf[:1]
icc := decodedBy.ICC()
var enc C.webp_encoder
if len(icc) > 0 {
enc = C.webp_encoder_create(unsafe.Pointer(&dstBuf[0]), C.size_t(cap(dstBuf)), unsafe.Pointer(&icc[0]), C.size_t(len(icc)))
} else {
enc = C.webp_encoder_create(unsafe.Pointer(&dstBuf[0]), C.size_t(cap(dstBuf)), nil, 0)
}
if enc == nil {
return nil, ErrBufTooSmall
}
return &webpEncoder{
encoder: enc,
dstBuf: dstBuf,
icc: icc,
}, nil
}
func (e *webpEncoder) Encode(f *Framebuffer, opt map[int]int) ([]byte, error) {
if f == nil {
return nil, io.EOF
}
var optList []C.int
var firstOpt *C.int
for k, v := range opt {
optList = append(optList, C.int(k))
optList = append(optList, C.int(v))
}
if len(optList) > 0 {
firstOpt = (*C.int)(unsafe.Pointer(&optList[0]))
}
length := C.webp_encoder_write(e.encoder, f.mat, firstOpt, C.size_t(len(optList)))
if length == 0 {
return nil, ErrInvalidImage
}
return e.dstBuf[:length], nil
}
func (e *webpEncoder) Close() {
C.webp_encoder_release(e.encoder)
}