Debian Codegraph @

ui/bgra.go

raw symbols SCIP
1 package ui
2
3 import (
4 "github.com/golang/freetype/raster"
5 "image"
6 "image/color"
7 "image/draw"
8 )
9
10 // BGRA is like RGBA but in wayland's byte order
11 type BGRA struct {
12 Pix []uint8
13 Stride int
14 Rect image.Rectangle
15 }
16
17 // NewBGRA returns a new BGRA image with the given bounds.
18 func NewBGRA(r image.Rectangle) *BGRA {
19 w, h := r.Dx(), r.Dy()
20 buf := make([]uint8, 4*w*h)
21 return &BGRA{buf, 4 * w, r}
22 }
23
24 func NewBGRAWithData(r image.Rectangle, data []uint8) *BGRA {
25 w, h := r.Dx(), r.Dy()
26 if len(data) < 4*w*h {
27 panic("not enough data supplied")
28 }
29 return &BGRA{data, 4 * w, r}
30 }
31
32 func (p *BGRA) ColorModel() color.Model { return color.RGBAModel }
33
34 func (p *BGRA) Bounds() image.Rectangle { return p.Rect }
35
36 func (p *BGRA) At(x, y int) color.Color {
37 return p.RGBAAt(x, y)
38 }
39
40 func (p *BGRA) RGBAAt(x, y int) color.RGBA {
41 if !(image.Point{x, y}.In(p.Rect)) {
42 return color.RGBA{}
43 }
44 i := p.PixOffset(x, y)
45 return color.RGBA{p.Pix[i+2], p.Pix[i+1], p.Pix[i+0], p.Pix[i+3]}
46 }
47
48 // PixOffset returns the index of the first element of Pix that corresponds to
49 // the pixel at (x, y).
50 func (p *BGRA) PixOffset(x, y int) int {
51 return (y-p.Rect.Min.Y)*p.Stride + (x-p.Rect.Min.X)*4
52 }
53
54 func (p *BGRA) Set(x, y int, c color.Color) {
55 if !(image.Point{x, y}.In(p.Rect)) {
56 return
57 }
58 i := p.PixOffset(x, y)
59 c1 := color.RGBAModel.Convert(c).(color.RGBA)
60 p.Pix[i+0] = c1.B
61 p.Pix[i+1] = c1.G
62 p.Pix[i+2] = c1.R
63 p.Pix[i+3] = c1.A
64 }
65
66 func (p *BGRA) SetRGBA(x, y int, c color.RGBA) {
67 if !(image.Point{x, y}.In(p.Rect)) {
68 return
69 }
70 i := p.PixOffset(x, y)
71 p.Pix[i+0] = c.B
72 p.Pix[i+1] = c.G
73 p.Pix[i+2] = c.R
74 p.Pix[i+3] = c.A
75 }
76
77 // SubImage returns an image representing the portion of the image p visible
78 // through r. The returned value shares pixels with the original image.
79 func (p *BGRA) SubImage(r image.Rectangle) image.Image {
80 r = r.Intersect(p.Rect)
81 // If r1 and r2 are Rectangles, r1.Intersect(r2) is not
82 // guaranteed to be inside either r1 or r2 if the intersection
83 // is empty. Without explicitly checking for this, the Pix[i:]
84 // expression below can panic.
85 if r.Empty() {
86 return &BGRA{}
87 }
88 i := p.PixOffset(r.Min.X, r.Min.Y)
89 return &BGRA{
90 Pix: p.Pix[i:],
91 Stride: p.Stride,
92 Rect: r,
93 }
94 }
95
96 // Opaque scans the entire image and reports whether it is fully opaque.
97 func (p *BGRA) Opaque() bool {
98 if p.Rect.Empty() {
99 return true
100 }
101 i0 := 0
102 i1 := p.Rect.Dx() * 4
103 for y := p.Rect.Min.Y; y < p.Rect.Max.Y; y++ {
104 for i := i0; i < i1; i += 4 {
105 if p.Pix[i+3] != 0xff {
106 return false
107 }
108 }
109 i0 += p.Stride
110 i1 += p.Stride
111 }
112 return true
113 }
114
115 // a BGRAPainter is used to interface with the freetype renderer and
116 // raster library
117 type BGRAPainter struct {
118 image *BGRA
119 r, g, b, a uint32
120 op draw.Op
121 }
122
123 func (p *BGRA) Painter() *BGRAPainter {
124 return &BGRAPainter{
125 image: p,
126 }
127 }
128
129 func (p *BGRAPainter) SetColor(c color.Color) {
130 p.r, p.g, p.b, p.a = c.RGBA()
131 }
132
133 func (p *BGRAPainter) Paint(spans []raster.Span, done bool) {
134 b := p.image.Rect
135 pix := p.image.Pix
136 for _, s := range spans {
137 if s.Y < b.Min.Y {
138 continue
139 }
140 if s.Y >= b.Max.Y {
141 // since spans are ordered, we know we're done
142 // at this point
143 return
144 }
145 if s.X0 < b.Min.X {
146 s.X0 = b.Min.X
147 }
148 if s.X1 > b.Max.X {
149 s.X1 = b.Max.X
150 }
151 if s.X0 >= s.X1 {
152 continue
153 }
154 // This code mimics drawGlyphOver in $GOROOT/src/image/draw/draw.go.
155 ma := s.Alpha
156 const m = 1<<16 - 1
157 i0 := (s.Y-b.Min.Y)*p.image.Stride + (s.X0-b.Min.X)*4
158 i1 := i0 + (s.X1-s.X0)*4
159 if p.op == draw.Over {
160 for i := i0; i < i1; i += 4 {
161 dr := uint32(pix[i+0])
162 dg := uint32(pix[i+1])
163 db := uint32(pix[i+2])
164 da := uint32(pix[i+3])
165 a := (m - (p.a * ma / m)) * 0x101
166 pix[i+0] = uint8((dr*a + p.r*ma) / m >> 8)
167 pix[i+1] = uint8((dg*a + p.g*ma) / m >> 8)
168 pix[i+2] = uint8((db*a + p.b*ma) / m >> 8)
169 pix[i+3] = uint8((da*a + p.a*ma) / m >> 8)
170 }
171 } else {
172 for i := i0; i < i1; i += 4 {
173 pix[i+0] = uint8(p.r * ma / m >> 8)
174 pix[i+1] = uint8(p.g * ma / m >> 8)
175 pix[i+2] = uint8(p.b * ma / m >> 8)
176 pix[i+3] = uint8(p.a * ma / m >> 8)
177 }
178 }
179 }
180 }