-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathimage.go
178 lines (154 loc) · 4.39 KB
/
image.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package duitdraw
import (
"fmt"
"image"
"image/color"
"image/draw"
"sync"
"golang.org/x/exp/shiny/imageutil"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
type Image struct {
sync.Mutex // Locking is used only internally.
R image.Rectangle // The extent of the image.
m image.Image
Display *Display
Pix Pix // The pixel format for the image.
}
// MakeImage returns an Image from an *image.RGBA.
func (d *Display) MakeImage(m *image.RGBA) *Image {
return &Image{
Display: d,
R: m.Bounds(),
m: m,
}
}
// DrawImage draws a normal image.Image over dst.
// Image should implement draw.Image, but it currently doesn't.
// This cannot be changed now, as Draw is already defined.
func (dst *Image) DrawImage(r image.Rectangle, src image.Image, pt image.Point, op draw.Op) {
dst.Lock()
defer dst.Unlock()
if m, ok := dst.m.(draw.Image); ok {
draw.Draw(m, r, src, pt, op)
}
}
// Draw copies the source image with upper left corner p1 to the destination
// rectangle r, through the specified mask using operation SoverD. The
// coordinates are aligned so p1 in src and mask both correspond to r.min in
// the destination.
func (dst *Image) Draw(r image.Rectangle, src, mask *Image, p1 image.Point) {
dst.Lock()
defer dst.Unlock()
var im draw.Image
switch m := dst.m.(type) {
case *image.RGBA:
im = m
default:
fmt.Printf("shiny: image dst not implemented %T\n", m)
return
}
if src == nil {
fmt.Println("shiny: Draw: src is nil")
return
}
if mask == nil {
draw.Draw(im, r, src.m, p1, draw.Src)
return
}
draw.DrawMask(im, r, src.m, p1, mask.m, p1, draw.Over)
}
// Border draws a retangular border of size r and width n, with n positive
// meaning the border is inside r. It uses SoverD.
func (dst *Image) Border(r image.Rectangle, n int, src *Image, sp image.Point) {
dst.Lock()
defer dst.Unlock()
for _, r := range imageutil.Border(r, n) {
draw.Draw(dst.m.(*image.RGBA), r, src.m, sp, draw.Src)
}
}
// Free is currently ignored.
// TODO: do we need anything about this?
func (i *Image) Free() error {
return nil
}
// Load copies the pixel data from the buffer to the specified rectangle of the image.
// The buffer must be big enough to fill the rectangle.
//
// Duit calls Load with Load(rgba.Bounds(), rgba.Pix), so we assume image.RGBA Pix data.
func (dst *Image) Load(r image.Rectangle, data []byte) (int, error) {
w, h := r.Dx(), r.Dy()
if len(data) != 4*w*h {
return 0, fmt.Errorf("image Load: wrong data size")
}
m := &image.RGBA{data, 4 * w, r}
dst.R = r
dst.m = m
// Is len(data) ok? Duit does not read the first argument anyway.
return len(data), nil
}
func (dst *Image) Bytes(pt image.Point, src *Image, sp image.Point, f *Font, b []byte) image.Point {
return dst.String(pt, src, sp, f, string(b))
}
// String draws the string in the specified font using SoverD on the image,
// placing the upper left corner at p.
func (dst *Image) String(pt image.Point, src *Image, sp image.Point, f *Font, s string) image.Point {
dst.Lock()
defer dst.Unlock()
m := dst.m.(*image.RGBA)
ascent := f.face.Metrics().Ascent
dot := fixed.P(pt.X, pt.Y).Add(fixed.Point26_6{Y: ascent})
drawer := font.Drawer{
Dst: m,
Src: src.m,
Face: f.face,
Dot: dot,
}
drawer.DrawString(s)
dx := int(drawer.Dot.Sub(dot).X / 64)
ret := pt.Add(image.Point{dx, 0})
// fmt.Printf("shiny: String(%s) to %p at %d,%d => %d %d\n", s, m, pt.X, pt.Y, ret.X, ret.Y) // TODO remove
return ret
}
// Line draws a line in the source color from p0 to p1, of thickness
// 1+2*radius, with the specified ends, using SoverD. The source is aligned so
// sp corresponds to p0. See the Plan 9 documentation for more information.
func (dst *Image) Line(p0, p1 image.Point, end0, end1, radius int, src *Image, sp image.Point) {
dst.Lock()
defer dst.Unlock()
line(dst.m.(*image.RGBA), p0.X, p0.Y, p1.X, p1.Y, src.m.At(0, 0))
}
// Line draws a line with Besenham's algorithm.
// It only uses integer pixels.
func line(m *image.RGBA, x0, y0, x1, y1 int, c color.Color) {
abs := func(x int) int {
if x < 0 {
return -x
}
return x
}
var dx, dy, sx, sy, e, e2 int
dx = abs(x1 - x0)
dy = -abs(y1 - y0)
if sx = -1; x0 < x1 {
sx = 1
}
if sy = -1; y0 < y1 {
sy = 1
}
e = dx + dy
for {
m.Set(x0, y0, c)
if x0 == x1 && y0 == y1 {
break
}
if e2 = 2 * e; e2 >= dy {
e += dy
x0 += sx
} else if e2 <= dx {
e += dx
y0 += sy
}
}
}