-
Notifications
You must be signed in to change notification settings - Fork 10
/
layer.go
188 lines (160 loc) · 3.99 KB
/
layer.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
179
180
181
182
183
184
185
186
187
188
package bring
import (
"image"
"image/draw"
"github.com/tfriedel6/canvas"
"github.com/tfriedel6/canvas/backend/softwarebackend"
)
type layer struct {
width int
height int
image *image.RGBA
gc *canvas.Canvas
visible bool
modified bool
modifiedRect image.Rectangle
pathOpen bool
pathRect image.Rectangle
autosize bool
}
func (l *layer) updateModifiedRect(modArea image.Rectangle) {
before := l.modifiedRect
l.modifiedRect = l.modifiedRect.Union(modArea)
l.modified = l.modified || !before.Eq(l.modifiedRect)
}
func (l *layer) resetModified() {
l.modifiedRect = image.Rectangle{}
l.modified = false
}
func (l *layer) setupCanvas() {
be := softwarebackend.New(l.width, l.height)
be.Image = l.image
l.gc = canvas.New(be)
}
func (l *layer) fitRect(x int, y int, w int, h int) {
rect := image.Rect(x, y, x+w, y+h)
final := l.image.Bounds().Union(rect)
l.Resize(final.Max.X, final.Max.Y)
}
func copyImage(dest draw.Image, x, y int, src image.Image, sr image.Rectangle, op draw.Op) {
dp := image.Pt(x, y)
dr := image.Rectangle{Min: dp, Max: dp.Add(sr.Size())}
draw.Draw(dest, dr, src, sr.Min, op)
}
func (l *layer) Copy(srcLayer *layer, srcx, srcy, srcw, srch, x, y int, op draw.Op) {
srcImg := srcLayer.image
srcDim := srcImg.Bounds()
// If entire rectangle outside source canvas, stop
if srcx >= srcDim.Max.X || srcy >= srcDim.Max.Y {
return
}
// Otherwise, clip rectangle to area
if srcx+srcw > srcDim.Max.X {
srcw = srcDim.Max.X - srcx
}
if srcy+srch > srcDim.Max.Y {
srch = srcDim.Max.Y - srcy
}
// Stop if nothing to draw.
if srcw == 0 || srch == 0 {
return
}
if l.autosize {
l.fitRect(x, y, srcw, srch)
}
srcCopyDim := image.Rect(srcx, srcy, srcx+srcw, srcy+srch)
copyImage(l.image, x, y, srcImg, srcCopyDim, op)
l.updateModifiedRect(image.Rect(x, y, x+srcw, y+srch))
}
func (l *layer) Draw(x, y int, src image.Image, op draw.Op) {
srcDim := src.Bounds()
if l.autosize {
l.fitRect(x, y, srcDim.Max.X, srcDim.Max.Y)
}
copyImage(l.image, x, y, src, srcDim, op)
l.updateModifiedRect(image.Rect(x, y, x+srcDim.Max.X, y+srcDim.Max.Y))
}
func (l *layer) Resize(w int, h int) {
original := l.image.Bounds()
if w == l.width && h == l.height {
return
}
newImage := image.NewRGBA(image.Rect(0, 0, w, h))
draw.Draw(newImage, l.image.Bounds(), l.image, image.Pt(0, 0), draw.Src)
l.image = newImage
l.width = w
l.height = h
l.setupCanvas()
l.updateModifiedRect(original.Union(l.image.Bounds()))
}
func (l *layer) appendToPath(rect image.Rectangle) {
if !l.pathOpen {
l.gc.BeginPath()
l.pathOpen = true
l.pathRect = image.Rectangle{}
}
l.pathRect = l.pathRect.Union(rect)
}
func (l *layer) endPath() {
l.updateModifiedRect(l.pathRect)
l.pathOpen = false
l.pathRect = image.Rectangle{}
}
func (l *layer) Rect(x int, y int, width int, height int) {
l.appendToPath(image.Rect(x, y, x+width, y+height))
l.gc.Rect(float64(x), float64(y), float64(width), float64(height))
}
func (l *layer) Fill(r byte, g byte, b byte, a byte, op draw.Op) {
// Ignores op, as the canvas library does not support it :/
l.gc.SetFillStyle(r, g, b, a)
l.gc.Fill()
l.endPath()
}
type layers map[int]*layer
func newLayers() layers {
ls := make(layers)
ls[0] = newBuffer()
ls[0].visible = true
return ls
}
func newBuffer() *layer {
l := &layer{
image: image.NewRGBA(image.Rect(0, 0, 0, 0)),
autosize: true,
}
l.setupCanvas()
return l
}
func newVisibleLayer(l0 *layer) *layer {
l := &layer{
width: l0.width,
height: l0.height,
image: image.NewRGBA(image.Rect(0, 0, l0.width, l0.height)),
visible: true,
}
l.setupCanvas()
return l
}
func (ls layers) getDefault() *layer {
return ls[0]
}
func (ls layers) get(id int) *layer {
if l, ok := ls[id]; ok {
return l
}
if id > 0 {
ls[id] = newVisibleLayer(ls[0])
} else {
ls[id] = newBuffer()
}
return ls[id]
}
func (ls layers) delete(id int) {
if id == 0 {
return
}
ls[0].updateModifiedRect(ls[id].image.Bounds())
ls[id].image = nil
ls[id] = nil
delete(ls, id)
}