-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbayer.go
63 lines (57 loc) · 1.69 KB
/
bayer.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
package aravis
import (
"image"
"image/color"
)
type BayerRG struct {
// Pix holds the image's pixels, in bayer order. The pixel at
// (x, y) starts at Pix[(y-Rect.Min.Y)*Stride + (x-Rect.Min.X)].
Pix []uint8
// Stride is the Pix stride (in bytes) between vertically adjacent pixels.
Stride int
// Rect is the image's bounds.
Rect image.Rectangle
}
func NewBayerRG(r image.Rectangle) *BayerRG {
w, h := r.Dx(), r.Dy()
pix := make([]uint8, w*h)
return &BayerRG{pix, w, r}
}
func (p *BayerRG) ColorModel() color.Model { return color.RGBAModel }
func (p *BayerRG) Bounds() image.Rectangle { return p.Rect }
// At returns an RGBA pixel with simple nearest-neighbor debayering
func (p *BayerRG) At(x, y int) color.Color {
if x&1 == 0 && y&1 == 0 {
// top-left: red
return color.RGBA{
p.Pix[(y-p.Rect.Min.Y)*p.Stride+(x-p.Rect.Min.X)],
p.Pix[(y-p.Rect.Min.Y)*p.Stride+(x+1-p.Rect.Min.X)],
p.Pix[(y+1-p.Rect.Min.Y)*p.Stride+(x+1-p.Rect.Min.X)],
0,
}
} else if x&1 == 1 && y&1 == 0 {
// top-right: green
return color.RGBA{
p.Pix[(y-p.Rect.Min.Y)*p.Stride+(x-1-p.Rect.Min.X)],
p.Pix[(y-p.Rect.Min.Y)*p.Stride+(x-p.Rect.Min.X)],
p.Pix[(y+1-p.Rect.Min.Y)*p.Stride+(x-p.Rect.Min.X)],
0,
}
} else if x&1 == 0 && y&1 == 1 {
// bottom-left: green
return color.RGBA{
p.Pix[(y-1-p.Rect.Min.Y)*p.Stride+(x-p.Rect.Min.X)],
p.Pix[(y-p.Rect.Min.Y)*p.Stride+(x-p.Rect.Min.X)],
p.Pix[(y-p.Rect.Min.Y)*p.Stride+(x+1-p.Rect.Min.X)],
0,
}
} else {
// bottom-right: blue
return color.RGBA{
p.Pix[(y-1-p.Rect.Min.Y)*p.Stride+(x-1-p.Rect.Min.X)],
p.Pix[(y-1-p.Rect.Min.Y)*p.Stride+(x-p.Rect.Min.X)],
p.Pix[(y-p.Rect.Min.Y)*p.Stride+(x-p.Rect.Min.X)],
0,
}
}
}