-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.go
274 lines (244 loc) · 6.15 KB
/
image.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
package fastimagehash
/*
#cgo amd64 CFLAGS: -I${SRCDIR}/include/amd64
#cgo arm64 CFLAGS: -I${SRCDIR}/include/arm64
#cgo amd64 LDFLAGS: -L${SRCDIR}/lib/amd64
#cgo arm64 LDFLAGS: -L${SRCDIR}/lib/arm64
#cgo darwin LDFLAGS: -lyuv444_to_rgba_darwin
#cgo darwin LDFLAGS: -lyuv422_to_rgba_darwin
#cgo darwin LDFLAGS: -lyuv420_to_rgba_darwin
#cgo linux LDFLAGS: -lyuv444_to_rgba_linux
#cgo linux LDFLAGS: -lyuv422_to_rgba_linux
#cgo linux LDFLAGS: -lyuv420_to_rgba_linux
#cgo LDFLAGS: -ldl -lm
#include "yuv444_to_rgba.h"
#include "yuv422_to_rgba.h"
#include "yuv420_to_rgba.h"
*/
import "C"
import (
"image"
"image/color"
"github.com/pkg/errors"
_ "github.com/benesch/cgosymbolizer"
)
var (
ErrYUV444ToRGBA = errors.New("failed to yuv444_to_rgba")
ErrYUV422ToRGBA = errors.New("failed to yuv422_to_rgba")
ErrYUV420ToRGBA = errors.New("failed to yuv420_to_rgba")
ErrYUVNotSupportSubsampleRatio = errors.New("not support subsample ratio")
)
//go:generate go run ./cmd/compile f yuv444_to_rgba image.cpp
func yuv444ToRGBA(y, u, v []byte, strideY, strideU, strideV int, width, height int) (*image.RGBA, error) {
out := make([]byte, width*height*4)
yBuf, err := halideBufferYUV(y, strideY, width, height)
if err != nil {
return nil, errors.WithStack(err)
}
defer halideBufferFree(yBuf)
uBuf, err := halideBufferYUV(u, strideU, width, height)
if err != nil {
return nil, errors.WithStack(err)
}
defer halideBufferFree(uBuf)
vBuf, err := halideBufferYUV(v, strideV, width, height)
if err != nil {
return nil, errors.WithStack(err)
}
defer halideBufferFree(vBuf)
outBuf, err := halideBufferRGBA(out, width, height)
if err != nil {
return nil, errors.WithStack(err)
}
defer halideBufferFree(outBuf)
ret := C.yuv444_to_rgba(
yBuf,
uBuf,
vBuf,
C.int(strideY),
C.int(strideU),
C.int(strideV),
C.int(width),
C.int(height),
outBuf,
)
if ret != C.int(0) {
return nil, errors.WithStack(ErrYUV444ToRGBA)
}
return &image.RGBA{
Pix: out,
Stride: width * 4,
Rect: image.Rect(0, 0, width, height),
}, nil
}
//go:generate go run ./cmd/compile f yuv422_to_rgba image.cpp
func yuv422ToRGBA(y, u, v []byte, strideY, strideU, strideV int, width, height int) (*image.RGBA, error) {
out := make([]byte, width*height*4)
yBuf, err := halideBufferYUV(y, strideY, width, height)
if err != nil {
return nil, errors.WithStack(err)
}
defer halideBufferFree(yBuf)
uBuf, err := halideBufferYUV(u, strideU, width/2, height)
if err != nil {
return nil, errors.WithStack(err)
}
defer halideBufferFree(uBuf)
vBuf, err := halideBufferYUV(v, strideV, width/2, height)
if err != nil {
return nil, errors.WithStack(err)
}
defer halideBufferFree(vBuf)
outBuf, err := halideBufferRGBA(out, width, height)
if err != nil {
return nil, errors.WithStack(err)
}
defer halideBufferFree(outBuf)
ret := C.yuv422_to_rgba(
yBuf,
uBuf,
vBuf,
C.int(strideY),
C.int(strideU),
C.int(strideV),
C.int(width),
C.int(height),
outBuf,
)
if ret != C.int(0) {
return nil, errors.WithStack(ErrYUV422ToRGBA)
}
return &image.RGBA{
Pix: out,
Stride: width * 4,
Rect: image.Rect(0, 0, width, height),
}, nil
}
//go:generate go run ./cmd/compile f yuv420_to_rgba image.cpp
func yuv420ToRGBA(y, u, v []byte, strideY, strideU, strideV int, width, height int) (*image.RGBA, error) {
out := make([]byte, width*height*4)
yBuf, err := halideBufferYUV(y, strideY, width, height)
if err != nil {
return nil, errors.WithStack(err)
}
defer halideBufferFree(yBuf)
uBuf, err := halideBufferYUV(u, strideU, width/2, height/2)
if err != nil {
return nil, errors.WithStack(err)
}
defer halideBufferFree(uBuf)
vBuf, err := halideBufferYUV(v, strideV, width/2, height/2)
if err != nil {
return nil, errors.WithStack(err)
}
defer halideBufferFree(vBuf)
outBuf, err := halideBufferRGBA(out, width, height)
if err != nil {
return nil, errors.WithStack(err)
}
defer halideBufferFree(outBuf)
ret := C.yuv420_to_rgba(
yBuf,
uBuf,
vBuf,
C.int(strideY),
C.int(strideU),
C.int(strideV),
C.int(width),
C.int(height),
outBuf,
)
if ret != C.int(0) {
return nil, errors.WithStack(ErrYUV420ToRGBA)
}
return &image.RGBA{
Pix: out,
Stride: width * 4,
Rect: image.Rect(0, 0, width, height),
}, nil
}
func YCbCrToRGBA(ycbcr *image.YCbCr) (*image.RGBA, error) {
width, height := ycbcr.Rect.Dx(), ycbcr.Rect.Dy()
switch ycbcr.SubsampleRatio {
case image.YCbCrSubsampleRatio444:
return yuv444ToRGBA(
ycbcr.Y,
ycbcr.Cb,
ycbcr.Cr,
ycbcr.YStride,
ycbcr.CStride,
ycbcr.CStride,
width,
height,
)
case image.YCbCrSubsampleRatio422:
return yuv422ToRGBA(
ycbcr.Y,
ycbcr.Cb,
ycbcr.Cr,
ycbcr.YStride,
ycbcr.CStride,
ycbcr.CStride,
width,
height,
)
case image.YCbCrSubsampleRatio420:
return yuv420ToRGBA(
ycbcr.Y,
ycbcr.Cb,
ycbcr.Cr,
ycbcr.YStride,
ycbcr.CStride,
ycbcr.CStride,
width,
height,
)
default:
return nil, errors.Wrapf(ErrYUVNotSupportSubsampleRatio, "ratio=%d", ycbcr.SubsampleRatio)
}
}
func ConvertToRGBA(img image.Image) (*image.RGBA, error) {
switch img.(type) {
case *image.YCbCr:
return YCbCrToRGBA(img.(*image.YCbCr))
case *image.RGBA:
return img.(*image.RGBA), nil
case *image.NRGBA:
// todo: calc non-alpha-premultiplied
v := img.(*image.NRGBA)
return &image.RGBA{
Pix: v.Pix,
Stride: v.Stride,
Rect: v.Rect,
}, nil
default:
return convertRGBAModel(img), nil
}
}
func convertYCbCrModel(img image.Image, subsample image.YCbCrSubsampleRatio) *image.YCbCr {
b := img.Bounds()
yuv := image.NewYCbCr(img.Bounds(), subsample)
for y := 0; y < b.Dy(); y += 1 {
for x := 0; x < b.Dx(); x += 1 {
rgba := color.RGBAModel.Convert(img.At(x, y)).(color.RGBA)
yy, uu, vv := color.RGBToYCbCr(rgba.R, rgba.G, rgba.B)
cy := yuv.YOffset(x, y)
ci := yuv.COffset(x, y)
yuv.Y[cy] = yy
yuv.Cb[ci] = uu
yuv.Cr[ci] = vv
}
}
return yuv
}
func convertRGBAModel(img image.Image) *image.RGBA {
b := img.Bounds()
rgba := image.NewRGBA(b)
for y := b.Min.Y; y < b.Max.Y; y += 1 {
for x := b.Min.X; x < b.Max.X; x += 1 {
c := color.RGBAModel.Convert(img.At(x, y))
rgba.Set(x, y, c)
}
}
return rgba
}