forked from gen2brain/x264-go
/
ycbcr.go
53 lines (42 loc) · 1.19 KB
/
ycbcr.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 x264
// #include <string.h>
import "C"
import "unsafe"
import (
"image"
"image/color"
"image/draw"
)
// YCbCr is an in-memory image of Y'CbCr colors.
type YCbCr struct {
*image.YCbCr
}
// NewYCbCr returns a new YCbCr image with the given bounds and subsample ratio.
func NewYCbCr(r image.Rectangle) *YCbCr {
return &YCbCr{image.NewYCbCr(r, image.YCbCrSubsampleRatio420)}
}
// Set sets pixel color.
func (p *YCbCr) Set(x, y int, c color.Color) {
p.setYCbCr(x, y, p.ColorModel().Convert(c).(color.YCbCr))
}
func (p *YCbCr) setYCbCr(x, y int, c color.YCbCr) {
if !image.Pt(x, y).In(p.Rect) {
return
}
yi := p.YOffset(x, y)
ci := p.COffset(x, y)
p.Y[yi] = c.Y
p.Cb[ci] = c.Cb
p.Cr[ci] = c.Cr
}
// ToYCbCr converts image.Image to YCbCr.
func (p *YCbCr) ToYCbCr(src image.Image) {
bounds := src.Bounds()
draw.Draw(p, bounds, src, bounds.Min, draw.Src)
}
// Copy arbitary YCbCr to buffer that allocated by x264_picture_alloc()
func (p *YCbCr) CopyToCPointer(CY, CCb, CCr unsafe.Pointer) {
C.memcpy(CY, unsafe.Pointer(&p.Y[0]), C.size_t(uint(len(p.Y))))
C.memcpy(CCb, unsafe.Pointer(&p.Cb[0]), C.size_t(uint(len(p.Cb))))
C.memcpy(CCr, unsafe.Pointer(&p.Cr[0]), C.size_t(uint(len(p.Cr))))
}