-
Notifications
You must be signed in to change notification settings - Fork 91
/
rgb.go
157 lines (136 loc) · 3.17 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
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
// Copyright 2014 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package webp
import (
"image"
"image/color"
"reflect"
)
var (
_ image.Image = (*RGBImage)(nil)
_ MemP = (*RGBImage)(nil)
)
type RGBImage struct {
XPix []uint8
XStride int
XRect image.Rectangle
}
func (p *RGBImage) MemPMagic() string {
return MemPMagic
}
func (p *RGBImage) Bounds() image.Rectangle {
return p.XRect
}
func (p *RGBImage) Channels() int {
return 3
}
func (p *RGBImage) DataType() reflect.Kind {
return reflect.Uint8
}
func (p *RGBImage) Pix() []byte {
return p.XPix
}
func (p *RGBImage) Stride() int {
return p.XStride
}
func (p *RGBImage) ColorModel() color.Model { return color.RGBAModel }
func (p *RGBImage) At(x, y int) color.Color {
if !(image.Point{x, y}.In(p.XRect)) {
return color.RGBA{}
}
i := p.PixOffset(x, y)
return color.RGBA{
R: p.XPix[i+0],
G: p.XPix[i+1],
B: p.XPix[i+2],
A: 0xff,
}
}
func (p *RGBImage) RGBAt(x, y int) [3]uint8 {
if !(image.Point{x, y}.In(p.XRect)) {
return [3]uint8{}
}
i := p.PixOffset(x, y)
return [3]uint8{
p.XPix[i+0],
p.XPix[i+1],
p.XPix[i+2],
}
}
// PixOffset returns the index of the first element of Pix that corresponds to
// the pixel at (x, y).
func (p *RGBImage) PixOffset(x, y int) int {
return (y-p.XRect.Min.Y)*p.XStride + (x-p.XRect.Min.X)*3
}
func (p *RGBImage) Set(x, y int, c color.Color) {
if !(image.Point{x, y}.In(p.XRect)) {
return
}
i := p.PixOffset(x, y)
c1 := color.RGBAModel.Convert(c).(color.RGBA)
p.XPix[i+0] = c1.R
p.XPix[i+1] = c1.G
p.XPix[i+2] = c1.B
return
}
func (p *RGBImage) SetRGB(x, y int, c [3]uint8) {
if !(image.Point{x, y}.In(p.XRect)) {
return
}
i := p.PixOffset(x, y)
p.XPix[i+0] = c[0]
p.XPix[i+1] = c[1]
p.XPix[i+2] = c[2]
return
}
// SubImage returns an image representing the portion of the image p visible
// through r. The returned value shares pixels with the original image.
func (p *RGBImage) SubImage(r image.Rectangle) image.Image {
r = r.Intersect(p.XRect)
// 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 &RGBImage{}
}
i := p.PixOffset(r.Min.X, r.Min.Y)
return &RGBImage{
XPix: p.XPix[i:],
XStride: p.XStride,
XRect: r,
}
}
// Opaque scans the entire image and reports whether it is fully opaque.
func (p *RGBImage) Opaque() bool {
return true
}
// NewRGBImage returns a new RGBImage with the given bounds.
func NewRGBImage(r image.Rectangle) *RGBImage {
w, h := r.Dx(), r.Dy()
pix := make([]uint8, 3*w*h)
return &RGBImage{
XPix: pix,
XStride: 3 * w,
XRect: r,
}
}
func NewRGBImageFrom(m image.Image) *RGBImage {
if m, ok := m.(*RGBImage); ok {
return m
}
// convert to RGBImage
b := m.Bounds()
rgb := NewRGBImage(b)
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
pr, pg, pb, _ := m.At(x, y).RGBA()
rgb.SetRGB(x, y, [3]uint8{
uint8(pr >> 8),
uint8(pg >> 8),
uint8(pb >> 8),
})
}
}
return rgb
}