-
Notifications
You must be signed in to change notification settings - Fork 4
/
pixmap.go
66 lines (60 loc) · 1.64 KB
/
pixmap.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
package resvg
import "github.com/kanrichan/resvg-go/internal"
// Pixmap tinyskia pixmap
type Pixmap struct {
wk *Worker
ptr int32
}
// NewPixmap allocates a new `Pixmap`.
// Pixmap's width is limited by int32::MAX/4.
// `Pixmap` are not goroutine-safe, don't forget to close!
func (wk *Worker) NewPixmap(width uint32, height uint32) (*Pixmap, error) {
if !wk.used.CompareAndSwap(false, true) {
return nil, ErrWorkerIsBeingUsed
}
defer wk.used.Store(false)
pm, err := internal.TinySkiaPixmapNew(wk.ctx, wk.mod, width, height)
if err != nil {
return nil, err
}
return &Pixmap{wk, pm}, nil
}
// NewPixmapDecodePNG decodes a PNG data into a `Pixmap`.
func (wk *Worker) NewPixmapDecodePNG(data []byte) (*Pixmap, error) {
if !wk.used.CompareAndSwap(false, true) {
return nil, ErrWorkerIsBeingUsed
}
defer wk.used.Store(false)
pm, err := internal.TinySkiaPixmapDecodePNG(wk.ctx, wk.mod, data)
if err != nil {
return nil, err
}
return &Pixmap{wk, pm}, nil
}
// Close cloes the `Pixmap` and recovers memory.
func (pm *Pixmap) Close() error {
if !pm.wk.used.CompareAndSwap(false, true) {
return ErrWorkerIsBeingUsed
}
defer pm.wk.used.Store(false)
if pm.ptr == 0 {
return ErrPointerIsNil
}
err := internal.TinySkiaPixmapDelete(pm.wk.ctx, pm.wk.mod, pm.ptr)
if err != nil {
return err
}
pm.ptr = 0
return nil
}
// EncodePNG encodes pixmap into a PNG data.
func (pm *Pixmap) EncodePNG() ([]byte, error) {
if !pm.wk.used.CompareAndSwap(false, true) {
return nil, ErrWorkerIsBeingUsed
}
defer pm.wk.used.Store(false)
if pm.ptr == 0 {
return nil, ErrPointerIsNil
}
return internal.TinySkiaPixmapEncodePng(pm.wk.ctx, pm.wk.mod, pm.ptr)
}