This repository has been archived by the owner on Feb 25, 2021. It is now read-only.
forked from chai2010/webp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrgb.go
135 lines (120 loc) · 3.34 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
// 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"
)
// _RGB is an in-memory image whose At method returns color.RGBA values.
//
// Notes: _RGB use the same struct with image.RGBA!!!
type _RGB struct {
_Pix []uint8
_Stride int
_Rect image.Rectangle
}
func (p *_RGB) BaseType() image.Image { return p }
func (p *_RGB) Pix() []byte { return p._Pix }
func (p *_RGB) Stride() int { return p._Stride }
func (p *_RGB) Rect() image.Rectangle { return p._Rect }
func (p *_RGB) Channels() int { return 3 }
func (p *_RGB) Depth() reflect.Kind { return reflect.Uint8 }
func (p *_RGB) ColorModel() color.Model { return color.RGBAModel }
func (p *_RGB) Bounds() image.Rectangle { return p._Rect }
func (p *_RGB) At(x, y int) color.Color {
return p.RGBAAt(x, y)
}
func (p *_RGB) RGBAAt(x, y int) color.RGBA {
if !(image.Point{x, y}.In(p._Rect)) {
return color.RGBA{}
}
i := p.PixOffset(x, y)
return color.RGBA{p._Pix[i+0], p._Pix[i+1], p._Pix[i+2], 0xFF}
}
// PixOffset returns the index of the first element of _Pix that corresponds to
// the pixel at (x, y).
func (p *_RGB) PixOffset(x, y int) int {
return (y-p._Rect.Min.Y)*p._Stride + (x-p._Rect.Min.X)*3
}
func (p *_RGB) Set(x, y int, c color.Color) {
if !(image.Point{x, y}.In(p._Rect)) {
return
}
i := p.PixOffset(x, y)
c1 := color.RGBAModel.Convert(c).(color.RGBA)
p._Pix[i+0] = c1.R
p._Pix[i+1] = c1.G
p._Pix[i+2] = c1.B
}
func (p *_RGB) SetRGBA(x, y int, c color.RGBA) {
if !(image.Point{x, y}.In(p._Rect)) {
return
}
i := p.PixOffset(x, y)
p._Pix[i+0] = c.R
p._Pix[i+1] = c.G
p._Pix[i+2] = c.B
}
// 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 *_RGB) 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 &_RGB{}
}
i := p.PixOffset(r.Min.X, r.Min.Y)
return &_RGB{
_Pix: p._Pix[i:],
_Stride: p._Stride,
_Rect: r,
}
}
// Opaque scans the entire image and reports whether it is fully opaque.
func (p *_RGB) Opaque() bool {
if p._Rect.Empty() {
return true
}
i0, i1 := 3, p._Rect.Dx()*3
for y := p._Rect.Min.Y; y < p._Rect.Max.Y; y++ {
for i := i0; i < i1; i += 3 {
if p._Pix[i] != 0xff {
return false
}
}
i0 += p._Stride
i1 += p._Stride
}
return true
}
// newRGB returns a new _RGB with the given bounds.
func newRGB(r image.Rectangle) *_RGB {
w, h := r.Dx(), r.Dy()
buf := make([]uint8, 3*w*h)
return &_RGB{buf, 3 * w, r}
}
func newRGBFromImage(m image.Image) *_RGB {
switch m := m.(type) {
case *_RGB:
return m
default:
b := m.Bounds()
rgb := newRGB(b)
dstColorRGBA64 := &color.RGBA64{}
dstColor := color.Color(dstColorRGBA64)
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()
dstColorRGBA64.R = uint16(pr)
dstColorRGBA64.G = uint16(pg)
dstColorRGBA64.B = uint16(pb)
rgb.Set(x, y, dstColor)
}
}
return rgb
}
}