-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpicture.go
180 lines (143 loc) · 3.04 KB
/
picture.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
package webp
/*
#cgo pkg-config: libwebp
#include <stdlib.h>
#include "webp_util.h"
*/
import "C"
import (
"fmt"
"image"
"io"
"strings"
"unsafe"
)
type Hint int
const (
DefaultHint Hint = iota
PictureHint
PhotoHint
GraphHint
LastHint
)
var hints = map[string]Hint{
"default": DefaultHint,
"picture": PictureHint,
"photo": PhotoHint,
"graph": GraphHint,
}
func GetHint(s string) Hint {
v, ok := hints[strings.ToLower(s)]
if !ok {
return DefaultHint
}
return v
}
type Picture interface {
Bounds() image.Rectangle
RGBA() bool
}
type AllocatedPicture struct {
Picture
cpicture *C.WebPPicture
}
func NewPicture(img image.Image) *AllocatedPicture {
switch p := img.(type) {
case *image.RGBA:
return &AllocatedPicture{Picture: NewRGBAImage(p)}
case *image.NRGBA:
return &AllocatedPicture{Picture: NewNRGBAImage(p)}
case *image.YCbCr:
return &AllocatedPicture{Picture: NewYUVAPicture(p)}
}
return nil
}
func (p *AllocatedPicture) Init() error {
var pic C.struct_WebPPicture
i, err := C.WebPPictureInit(&pic)
if err != nil {
return err
}
if i != 1 {
return fmt.Errorf("error occurred initializing the picutre: %d", i)
}
pic.width = C.int(p.Bounds().Dx())
pic.height = C.int(p.Bounds().Dy())
err = p.alloc()
if err != nil {
return err
}
p.cpicture = &pic
return nil
}
func (p *AllocatedPicture) alloc() error {
i, err := C.WebPPictureAlloc(p.cpicture)
if err != nil {
return err
}
if i != 1 {
return fmt.Errorf("error occurred allocating picture: %d", i)
}
return nil
}
func (p *AllocatedPicture) Free() error {
_, err := C.WebPPictureFree(p.cpicture)
if err != nil {
return err
}
return nil
}
func (p *AllocatedPicture) importRGBA(pic *RGBAPicture) error {
cb := C.CBytes([]byte(pic.Pixels())) // translate the pixels to
defer C.free(cb)
// import our pixels into the image
status, err := C.WebPPictureImportRGBA(p.cpicture, (*C.uint8_t)(cb), C.int(pic.Stride()))
if err != nil {
return err
}
if status != 1 {
return fmt.Errorf("error importing RGBA: %d", status)
}
return nil
}
func (p *AllocatedPicture) importYUVA(pic *YUVAPicture) {
// YUVA
y := C.CBytes([]byte(pic.y))
u := C.CBytes([]byte(pic.u))
v := C.CBytes([]byte(pic.v))
defer func() {
C.free(y)
C.free(u)
C.free(v)
}()
p.cpicture.y = (*C.uint8_t)(y)
p.cpicture.u = (*C.uint8_t)(u)
p.cpicture.v = (*C.uint8_t)(v)
p.cpicture.y_stride = C.int(pic.yStride)
p.cpicture.uv_stride = C.int(pic.uvStride)
}
func (p *AllocatedPicture) Encode(w io.Writer, config *Config) error {
conf, err := config.toCStruct()
if err != nil {
return err
}
p.cpicture.writer = C.WebPWriterFunction(C.writeTo)
wh.add(unsafe.Pointer(p.cpicture), w)
defer wh.del(unsafe.Pointer(p.cpicture))
if p.Picture.RGBA() {
// encode RGBA
if err := p.importRGBA(p.Picture.(*RGBAPicture)); err != nil {
return err
}
} else {
p.importYUVA(p.Picture.(*YUVAPicture))
}
status, err := C.WebPEncode(conf, p.cpicture)
if err != nil {
return err
}
if status != 1 {
return fmt.Errorf("error encoding the image: %d", status)
}
return nil
}