-
Notifications
You must be signed in to change notification settings - Fork 8
/
rgb.go
56 lines (47 loc) · 1.1 KB
/
rgb.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
package camera
import (
"image"
"image/color"
)
type RGB struct {
Pix []uint8
Stride int
Rect image.Rectangle
}
func NewRGB(r image.Rectangle) *RGB {
w, h := r.Dx(), r.Dy()
buf := make([]uint8, 3*w*h)
return &RGB{Pix: buf, Stride: 3 * w, Rect: r}
}
func NewRGBFromBytes(data []byte, r image.Rectangle) *RGB {
if len(data) != 3*r.Dx()*r.Dy() {
panic("unexpected image data")
}
return &RGB{Pix: data, Stride: 3 * r.Dx(), Rect: r}
}
func (im *RGB) ColorModel() color.Model {
return color.RGBAModel
}
func (im *RGB) Bounds() image.Rectangle {
return im.Rect
}
func (im *RGB) At(x, y int) color.Color {
if !(image.Point{x, y}.In(im.Rect)) {
return color.RGBA{}
}
i := im.PixOffset(x, y)
return color.RGBA{R: im.Pix[i+0], G: im.Pix[i+1], B: im.Pix[i+2], A: 255}
}
func (im *RGB) Set(x, y int, c color.Color) {
if !(image.Point{x, y}.In(im.Rect)) {
return
}
i := im.PixOffset(x, y)
c1 := color.RGBAModel.Convert(c).(color.RGBA)
im.Pix[i+0] = c1.R
im.Pix[i+1] = c1.G
im.Pix[i+2] = c1.B
}
func (im *RGB) PixOffset(x, y int) int {
return (y-im.Rect.Min.Y)*im.Stride + (x-im.Rect.Min.X)*3
}