-
Notifications
You must be signed in to change notification settings - Fork 8
/
ycbcr.go
46 lines (37 loc) · 805 Bytes
/
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
package imutil
import (
"image"
"image/color"
)
// YCbCr is a raw YCbCr image.
type YCbCr struct {
Pix []byte
Rect image.Rectangle
}
// Compile time interface check.
var _ image.Image = (*YCbCr)(nil)
// ColorModel implements image.Image.
func (i *YCbCr) ColorModel() color.Model {
return color.YCbCrModel
}
// Bounds implements image.Image.
func (i *YCbCr) Bounds() image.Rectangle {
return i.Rect
}
// At implements image.Image.
func (i *YCbCr) At(x, y int) color.Color {
if !(image.Point{x, y}.In(i.Rect)) {
return color.YCbCr{}
}
pixIx := (y*i.Rect.Max.X + x)
// Two pixels = four bytes.
cellIx := (pixIx / 2) * 4
Y0, Cb, Y1, Cr := i.Pix[cellIx+0], i.Pix[cellIx+1], i.Pix[cellIx+2], i.Pix[cellIx+3]
Y := Y0
if pixIx%2 == 1 {
Y = Y1
}
return color.YCbCr{
Y, Cb, Cr,
}
}