-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimagepool.go
275 lines (236 loc) · 5.29 KB
/
imagepool.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
269
270
271
272
273
274
275
package bp
import (
"image"
)
const (
notyetSupportedSampleRate string = "not yet supported sample rate"
)
type ImageRGBAPool struct {
pool chan []byte
rect image.Rectangle
width int
height int
stride int
length int
}
func (b *ImageRGBAPool) init(rect image.Rectangle) {
b.rect = rect
b.width = rect.Dx()
b.height = rect.Dy()
b.stride = imageRGBAStride(rect)
b.length = rect.Dx() * rect.Dy() * 4
}
func (b *ImageRGBAPool) createImageRGBARef(pix []byte, pool *ImageRGBAPool) *ImageRGBARef {
ref := newImageRGBARef(pix, &image.RGBA{
Pix: pix,
Stride: b.stride,
Rect: b.rect,
}, pool)
ref.setFinalizer()
return ref
}
func (b *ImageRGBAPool) GetRef() *ImageRGBARef {
var pix []byte
select {
case pix = <-b.pool:
// reuse exists pool
default:
// create []byte
pix = make([]byte, b.length)
}
return b.createImageRGBARef(pix, b)
}
func (b *ImageRGBAPool) preload(rate float64) {
if 0 < cap(b.pool) {
preloadSize := int(float64(cap(b.pool)) * rate)
for i := 0; i < preloadSize; i += 1 {
b.Put(make([]byte, b.length))
}
}
}
func (b *ImageRGBAPool) Put(pix []byte) bool {
if cap(pix) < b.length {
// discard small buffer
return false
}
select {
case b.pool <- pix[:b.length]:
// free capacity
return true
default:
// full capacity, discard it
return false
}
}
func (b *ImageRGBAPool) Len() int {
return len(b.pool)
}
func (b *ImageRGBAPool) Cap() int {
return cap(b.pool)
}
func NewImageRGBAPool(poolSize int, rect image.Rectangle, funcs ...optionFunc) *ImageRGBAPool {
opt := newOption()
for _, fn := range funcs {
fn(opt)
}
b := &ImageRGBAPool{
pool: make(chan []byte, poolSize),
// other field initialize to b.init(rect, sample)
}
b.init(rect)
if opt.preload {
b.preload(opt.preloadRate)
}
return b
}
type ImageNRGBAPool struct {
ImageRGBAPool
}
func (b *ImageNRGBAPool) createImageNRGBARef(pix []byte, pool *ImageNRGBAPool) *ImageNRGBARef {
ref := newImageNRGBARef(pix, &image.NRGBA{
Pix: pix,
Stride: b.stride,
Rect: b.rect,
}, pool)
ref.setFinalizer()
return ref
}
func (b *ImageNRGBAPool) GetRef() *ImageNRGBARef {
var pix []byte
select {
case pix = <-b.pool:
// reuse exists pool
default:
// create []byte
pix = make([]byte, b.length)
}
return b.createImageNRGBARef(pix, b)
}
func NewImageNRGBAPool(poolSize int, rect image.Rectangle, funcs ...optionFunc) *ImageNRGBAPool {
opt := newOption()
for _, fn := range funcs {
fn(opt)
}
b := new(ImageNRGBAPool)
b.pool = make(chan []byte, poolSize)
b.init(rect)
if opt.preload {
b.preload(opt.preloadRate)
}
return b
}
type ImageYCbCrPool struct {
pool chan []byte
rect image.Rectangle
sample image.YCbCrSubsampleRatio
yIdx int
uIdx int
vIdx int
strideY int
strideUV int
length int
}
func (b *ImageYCbCrPool) init(rect image.Rectangle, sample image.YCbCrSubsampleRatio) {
w, h := rect.Dx(), rect.Dy()
cw, ch := yuvSize(rect, sample)
i0 := (w * h) + (0 * cw * ch)
i1 := (w * h) + (1 * cw * ch)
i2 := (w * h) + (2 * cw * ch)
b.rect = rect
b.sample = sample
b.yIdx = i0
b.uIdx = i1
b.vIdx = i2
b.strideY = w
b.strideUV = cw
b.length = i2
}
func (b *ImageYCbCrPool) YStride(stride int) {
b.strideY = stride
}
func (b *ImageYCbCrPool) UVStride(stride int) {
b.strideUV = stride
}
func (b *ImageYCbCrPool) createImageYCbCrRef(pix []byte, pool *ImageYCbCrPool) *ImageYCbCrRef {
ref := newImageYCbCrRef(pix, &image.YCbCr{
Y: pix[0:b.yIdx:b.yIdx],
Cb: pix[b.yIdx:b.uIdx:b.uIdx],
Cr: pix[b.uIdx:b.vIdx:b.vIdx],
YStride: b.strideY,
CStride: b.strideUV,
Rect: b.rect,
SubsampleRatio: b.sample,
}, pool)
ref.setFinalizer()
return ref
}
func (b *ImageYCbCrPool) GetRef() *ImageYCbCrRef {
var pix []byte
select {
case pix = <-b.pool:
// reuse exists pool
default:
// create []byte
pix = make([]byte, b.length)
}
return b.createImageYCbCrRef(pix, b)
}
func (b *ImageYCbCrPool) preload(rate float64) {
if 0 < cap(b.pool) {
preloadSize := int(float64(cap(b.pool)) * rate)
for i := 0; i < preloadSize; i += 1 {
b.Put(make([]byte, b.length))
}
}
}
func (b *ImageYCbCrPool) Put(pix []byte) bool {
if cap(pix) < b.length {
// discard small buffer
return false
}
select {
case b.pool <- pix[:b.length]:
// free capacity
return true
default:
// full capacity, discard it
return false
}
}
func (b *ImageYCbCrPool) Len() int {
return len(b.pool)
}
func (b *ImageYCbCrPool) Cap() int {
return cap(b.pool)
}
func NewImageYCbCrPool(poolSize int, rect image.Rectangle, sample image.YCbCrSubsampleRatio, funcs ...optionFunc) *ImageYCbCrPool {
opt := newOption()
for _, fn := range funcs {
fn(opt)
}
if sample != image.YCbCrSubsampleRatio420 {
panic(notyetSupportedSampleRate)
}
b := &ImageYCbCrPool{
pool: make(chan []byte, poolSize),
// other field initialize to b.init(rect, sample)
}
b.init(rect, sample)
if opt.preload {
b.preload(opt.preloadRate)
}
return b
}
func imageRGBAStride(rect image.Rectangle) int {
return rect.Dx() * 4
}
func yuvSize(rect image.Rectangle, sample image.YCbCrSubsampleRatio) (int, int) {
w, h := rect.Dx(), rect.Dy()
if sample == image.YCbCrSubsampleRatio420 {
cw := ((rect.Max.X + 1) / 2) - (rect.Min.X / 2)
ch := ((rect.Max.Y + 1) / 2) - (rect.Min.Y / 2)
return cw, ch
}
// 4:4:4
return w, h
}