-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstencil.go
48 lines (40 loc) · 804 Bytes
/
stencil.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
package edgingo
import (
"image"
"image/draw"
)
type Stencil struct {
Top int
Bottom int
Left int
Right int
}
func NewStencil() *Stencil {
return &Stencil{
Top: 0,
Bottom: 0,
Left: 0,
Right: 0,
}
}
func (s *Stencil) Set(side ImageSide, value int) {
switch side {
case SideTop:
s.Top = value
case SideBottom:
s.Bottom = value
case SideLeft:
s.Left = value
case SideRight:
s.Right = value
}
}
func (s *Stencil) Cut(rgba *image.RGBA, width, height int) *image.RGBA {
if s.Top == 0 && s.Bottom == 0 && s.Left == 0 && s.Right == 0 {
return rgba
}
cropped := image.NewRGBA(image.Rect(0, 0, width-(s.Left+s.Right), height-(s.Top+s.Bottom)))
offset := image.Pt(s.Left, s.Top)
draw.Draw(cropped, cropped.Bounds(), rgba, offset, draw.Src)
return cropped
}