-
Notifications
You must be signed in to change notification settings - Fork 0
/
rgb24.go
160 lines (145 loc) · 3.34 KB
/
rgb24.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"image"
"image/color"
)
/*
RGB24 is a structure holding a 24-bit raw RGB image
*/
type RGB24 struct {
Pix []uint8
Stride int
Rect image.Rectangle
}
/*
RGB is a single pixel in the RGB24 image type
*/
type RGB struct {
R, G, B uint8
}
/*
RGBModel is the color model for a 24-bit image
*/
var RGBModel = color.ModelFunc(func(c color.Color) color.Color {
r, g, b, _ := c.RGBA()
return RGB{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8)}
})
/*
RGBA implements the Color interface for the RGB pixel type
*/
func (c RGB) RGBA() (r, g, b, a uint32) {
r = uint32(c.R) << 8
g = uint32(c.G) << 8
b = uint32(c.B) << 8
return
}
/*
NewRGB creates a black RGB24 image
*/
func NewRGB(r image.Rectangle) *RGB24 {
return &RGB24{
Rect: r.Canon(),
Stride: 3 * r.Dx(),
Pix: make([]uint8, 3*r.Dx()*r.Dy()),
}
}
/*
FromImage constructs an RGB24 from a given image
*/
func FromImage(img image.Image) *RGB24 {
if r, ok := img.(*RGB24); ok {
return r
}
// this is really slow for now...
r := NewRGB(img.Bounds())
for x := r.Rect.Min.X; x < r.Rect.Max.X; x++ {
for y := r.Rect.Min.Y; y < r.Rect.Max.Y; y++ {
r.Set(x, y, img.At(x, y))
}
}
return r
}
/*
FromRaw constructs an RGB24 image from raw bytes in RGB-row major order
*/
func FromRaw(b []byte, stride int, cols int, rows int) *RGB24 {
ret := &RGB24{
Pix: b,
Stride: stride,
Rect: image.Rect(0, 0, cols, rows),
}
return ret
}
/*
At implements the image.Image interface for RGB24
*/
func (p *RGB24) At(x, y int) color.Color {
if !(image.Point{x, y}.In(p.Rect)) {
return RGB{}
}
i := p.PixOffset(x, y)
return RGB{
p.Pix[i], p.Pix[i+1], p.Pix[i+2],
}
}
/*
Set implements the image.Image interface for RGB24
*/
func (p *RGB24) Set(x, y int, c color.Color) {
if !(image.Point{x, y}.In(p.Rect)) {
return
}
i := p.PixOffset(x, y)
c1 := RGBModel.Convert(c).(RGB)
p.Pix[i+0] = uint8(c1.R)
p.Pix[i+1] = uint8(c1.G)
p.Pix[i+2] = uint8(c1.B)
}
/*
ColorModel implements the image.Image interface for RGB24
*/
func (p *RGB24) ColorModel() color.Model {
return RGBModel
}
/*
SubImage returns an *RGB24 which is the crop of the given image using the rectangle r
*/
func (p *RGB24) SubImage(r image.Rectangle) image.Image {
r = r.Intersect(p.Rect)
// If r1 and r2 are Rectangles, r1.Intersect(r2) is not guaranteed to be inside
// either r1 or r2 if the intersection is empty. Without explicitly checking for
// this, the Pix[i:] expression below can panic.
if r.Empty() {
return &RGB24{}
}
// TODO: implement this much faster sub image routine, but this requires image stride
// in the C code. right now just copy the image bytes to a new slice
i := p.PixOffset(r.Min.X, r.Min.Y)
return &RGB24{
Pix: p.Pix[i:],
Stride: p.Stride,
Rect: r,
}
// ret := &RGB24{
// Stride: r.Dx() * 3,
// Rect: image.Rect(0, 0, r.Dx(), r.Dy()),
// }
// for y := r.Min.Y; y < r.Max.Y; y++ {
// for x := r.Min.X; x < r.Max.X; x++ {
// i := p.PixOffset(x, y)
// ret.Pix = append(ret.Pix, p.Pix[i], p.Pix[i+1], p.Pix[i+2])
// }
// }
// return ret
}
// PixOffset returns the index of the first element of Pix that corresponds to
// the pixel at (x, y).
func (p *RGB24) PixOffset(x, y int) int {
return (y-p.Rect.Min.Y)*p.Stride + (x-p.Rect.Min.X)*3
}
/*
Bounds returns the bounding rectangle of the image
*/
func (p *RGB24) Bounds() image.Rectangle {
return p.Rect
}