-
Notifications
You must be signed in to change notification settings - Fork 123
/
thumbhash.go
53 lines (45 loc) · 1.48 KB
/
thumbhash.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
package lilliput
// #include "thumbhash.hpp"
import "C"
import (
"io"
"unsafe"
)
// thumbhashEncoder handles the encoding of images into ThumbHash format.
// ThumbHash is a very compact representation of a placeholder for an image.
type thumbhashEncoder struct {
encoder C.thumbhash_encoder
buf []byte
}
// newThumbhashEncoder creates a new ThumbHash encoder instance.
// It takes a decoder and a buffer as input, initializing the C-based encoder.
// Returns an error if the provided buffer is too small.
func newThumbhashEncoder(decodedBy Decoder, buf []byte) (*thumbhashEncoder, error) {
buf = buf[:1]
enc := C.thumbhash_encoder_create(unsafe.Pointer(&buf[0]), C.size_t(cap(buf)))
if enc == nil {
return nil, ErrBufTooSmall
}
return &thumbhashEncoder{
encoder: enc,
buf: buf,
}, nil
}
// Encode converts the given Framebuffer into a ThumbHash byte representation.
// The opt parameter allows passing encoding options as key-value pairs.
// Returns the encoded bytes or an error if the input is invalid.
func (e *thumbhashEncoder) Encode(f *Framebuffer, opt map[int]int) ([]byte, error) {
if f == nil {
return nil, io.EOF
}
length := C.thumbhash_encoder_encode(e.encoder, f.mat)
if length <= 0 {
return nil, ErrInvalidImage
}
return e.buf[:length], nil
}
// Close releases the resources associated with the ThumbHash encoder.
// This should be called when the encoder is no longer needed.
func (e *thumbhashEncoder) Close() {
C.thumbhash_encoder_release(e.encoder)
}