-
Notifications
You must be signed in to change notification settings - Fork 124
/
giflib.go
268 lines (222 loc) · 5.99 KB
/
giflib.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
package lilliput
// #include "giflib.hpp"
import "C"
import (
"errors"
"io"
"sync/atomic"
"time"
"unsafe"
)
type gifDecoder struct {
decoder C.giflib_decoder
mat C.opencv_mat
buf []byte
frameIndex int
loopCount int
frameCount int
animationInfoRead bool
bgRed uint8
bgGreen uint8
bgBlue uint8
bgAlpha uint8
}
type gifEncoder struct {
encoder C.giflib_encoder
decoder C.giflib_decoder
buf []byte
frameIndex int
hasFlushed bool
}
const defaultMaxFrameDimension = 10000
var (
gifMaxFrameDimension uint64
ErrGifEncoderNeedsDecoder = errors.New("GIF encoder needs decoder used to create image")
)
// SetGIFMaxFrameDimension sets the largest GIF width/height that can be
// decoded
func SetGIFMaxFrameDimension(dim uint64) {
// TODO we should investigate if this can be removed/become a mat check in decoder
atomic.StoreUint64(&gifMaxFrameDimension, dim)
}
func newGifDecoder(buf []byte) (*gifDecoder, 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.giflib_decoder_create(mat)
if decoder == nil {
return nil, ErrInvalidImage
}
return &gifDecoder{
decoder: decoder,
mat: mat,
buf: buf,
frameIndex: 0,
}, nil
}
func (d *gifDecoder) Header() (*ImageHeader, error) {
return &ImageHeader{
width: int(C.giflib_decoder_get_width(d.decoder)),
height: int(C.giflib_decoder_get_height(d.decoder)),
pixelType: PixelType(C.CV_8UC4),
orientation: OrientationTopLeft,
numFrames: d.FrameCount(),
contentLength: len(d.buf),
}, nil
}
func (d *gifDecoder) FrameHeader() (*ImageHeader, error) {
return &ImageHeader{
width: int(C.giflib_decoder_get_frame_width(d.decoder)),
height: int(C.giflib_decoder_get_frame_height(d.decoder)),
pixelType: PixelType(C.CV_8UC4),
orientation: OrientationTopLeft,
numFrames: 1,
contentLength: len(d.buf),
}, nil
}
func (d *gifDecoder) Close() {
C.giflib_decoder_release(d.decoder)
C.opencv_mat_release(d.mat)
d.buf = nil
}
func (d *gifDecoder) Description() string {
return "GIF"
}
func (d *gifDecoder) IsStreamable() bool {
return true
}
func (d *gifDecoder) HasSubtitles() bool {
return false
}
func (d *gifDecoder) ICC() []byte {
return []byte{}
}
func (d *gifDecoder) Duration() time.Duration {
return time.Duration(0)
}
func (d *gifDecoder) BackgroundColor() uint32 {
d.readAnimationInfo()
return uint32(d.bgRed)<<16 | uint32(d.bgGreen)<<8 | uint32(d.bgBlue) | uint32(d.bgAlpha)<<24
}
// readAnimationInfo reads the GIF info from the decoder and caches it
// this involves reading extension blocks and is relatively expensive
// so we only do it once when we need it
func (d *gifDecoder) readAnimationInfo() {
if !d.animationInfoRead {
info := C.giflib_decoder_get_animation_info(d.decoder)
d.loopCount = int(info.loop_count)
d.frameCount = int(info.frame_count)
d.animationInfoRead = true
d.bgRed = uint8(info.bg_red)
d.bgGreen = uint8(info.bg_green)
d.bgBlue = uint8(info.bg_blue)
d.bgAlpha = uint8(info.bg_alpha)
}
}
func (d *gifDecoder) LoopCount() int {
d.readAnimationInfo()
return d.loopCount
}
func (d *gifDecoder) FrameCount() int {
d.readAnimationInfo()
return d.frameCount
}
func (d *gifDecoder) DecodeTo(f *Framebuffer) error {
h, err := d.Header()
if err != nil {
return err
}
err = f.resizeMat(h.Width(), h.Height(), h.PixelType())
if err != nil {
return err
}
nextFrameResult := int(C.giflib_decoder_decode_frame_header(d.decoder))
if nextFrameResult == C.giflib_decoder_eof {
return io.EOF
}
if nextFrameResult == C.giflib_decoder_error {
return ErrInvalidImage
}
frameHeader, err := d.FrameHeader()
if err != nil {
return ErrInvalidImage
}
maxDim := int(atomic.LoadUint64(&gifMaxFrameDimension))
if frameHeader.Width() > maxDim || frameHeader.Height() > maxDim {
return ErrInvalidImage
}
ret := C.giflib_decoder_decode_frame(d.decoder, f.mat)
if !ret {
return ErrDecodingFailed
}
f.duration = time.Duration(C.giflib_decoder_get_prev_frame_delay(d.decoder)) * 10 * time.Millisecond
f.blend = NoBlend
f.dispose = DisposeMethod(C.giflib_decoder_get_prev_frame_disposal(d.decoder))
f.xOffset = 0
f.yOffset = 0
d.frameIndex++
return nil
}
func (d *gifDecoder) SkipFrame() error {
nextFrameResult := int(C.giflib_decoder_skip_frame(d.decoder))
if nextFrameResult == C.giflib_decoder_eof {
return io.EOF
}
if nextFrameResult == C.giflib_decoder_error {
return ErrInvalidImage
}
return nil
}
func newGifEncoder(decodedBy Decoder, buf []byte) (*gifEncoder, error) {
// we must have a decoder since we can't build our own palettes
// so if we don't get a gif decoder, bail out
if decodedBy == nil {
return nil, ErrGifEncoderNeedsDecoder
}
gifDecoder, ok := decodedBy.(*gifDecoder)
if !ok {
return nil, ErrGifEncoderNeedsDecoder
}
buf = buf[:1]
enc := C.giflib_encoder_create(unsafe.Pointer(&buf[0]), C.size_t(cap(buf)))
if enc == nil {
return nil, ErrBufTooSmall
}
return &gifEncoder{
encoder: enc,
decoder: gifDecoder.decoder,
buf: buf,
frameIndex: 0,
}, nil
}
func (e *gifEncoder) Encode(f *Framebuffer, opt map[int]int) ([]byte, error) {
if e.hasFlushed {
return nil, io.EOF
}
if f == nil {
ret := C.giflib_encoder_flush(e.encoder, e.decoder)
if !ret {
return nil, ErrInvalidImage
}
e.hasFlushed = true
len := C.int(C.giflib_encoder_get_output_length(e.encoder))
return e.buf[:len], nil
}
if e.frameIndex == 0 {
// first run setup
// TODO figure out actual gif width/height?
C.giflib_encoder_init(e.encoder, e.decoder, C.int(f.Width()), C.int(f.Height()))
}
if !C.giflib_encoder_encode_frame(e.encoder, e.decoder, f.mat) {
return nil, ErrInvalidImage
}
e.frameIndex++
return nil, nil
}
func (e *gifEncoder) Close() {
C.giflib_encoder_release(e.encoder)
}
func init() {
SetGIFMaxFrameDimension(defaultMaxFrameDimension)
}