/
bgra.go
40 lines (33 loc) · 1.08 KB
/
bgra.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
package cmd
import (
"image"
"image/color"
)
// BGRA is an in-memory image whose At method returns color.BGRA values.
type BGRA struct {
// Pix holds the image's pixels, in B, G, R, A order. The pixel at
// (x, y) starts at Pix[(y-Rect.Min.Y)*Stride + (x-Rect.Min.X)*4].
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 (p *BGRA) ColorModel() color.Model { return color.RGBAModel }
func (p *BGRA) Bounds() image.Rectangle { return p.Rect }
func (p *BGRA) At(x, y int) color.Color {
return p.RGBAAt(x, y)
}
func (p *BGRA) RGBAAt(x, y int) color.RGBA {
if !(image.Point{x, y}.In(p.Rect)) {
return color.RGBA{}
}
i := p.PixOffset(x, y)
s := p.Pix[i : i+4 : i+4] // Small cap improves performance, see https://golang.org/issue/27857
return color.RGBA{s[2], s[1], s[0], s[3]}
}
// PixOffset returns the index of the first element of Pix that corresponds to
// the pixel at (x, y).
func (p *BGRA) PixOffset(x, y int) int {
return (y-p.Rect.Min.Y)*p.Stride + (x-p.Rect.Min.X)*4
}