-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclip.go
81 lines (77 loc) · 2.5 KB
/
clip.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
package imagescaling
import (
"image"
)
func (m *Image) clip(x0, y0, x1, y1 int) {
switch m.ext {
case "jpeg":
if m.opImage == nil {
m.opImage = m.image.(*image.YCbCr).SubImage(image.Rect(x0, y0, x1, y1)).(*image.YCbCr)
}else{
m.opImage = m.opImage.(*image.YCbCr).SubImage(image.Rect(x0, y0, x1, y1)).(*image.YCbCr)
}
case "png":
switch m.image.(type) {
case *image.NRGBA:
if m.opImage == nil {
m.opImage = m.image.(*image.NRGBA).SubImage(image.Rect(x0, y0, x1, y1)).(*image.NRGBA)
}else{
m.opImage = m.opImage.(*image.NRGBA).SubImage(image.Rect(x0, y0, x1, y1)).(*image.NRGBA)
}
case *image.RGBA:
if m.opImage == nil {
m.opImage = m.image.(*image.RGBA).SubImage(image.Rect(x0, y0, x1, y1)).(*image.RGBA)
}else{
m.opImage = m.opImage.(*image.RGBA).SubImage(image.Rect(x0, y0, x1, y1)).(*image.RGBA)
}
}
case "gif":
if m.opImage == nil {
m.opImage = m.image.(*image.Paletted).SubImage(image.Rect(x0, y0, x1, y1)).(*image.Paletted)
}else{
m.opImage = m.opImage.(*image.Paletted).SubImage(image.Rect(x0, y0, x1, y1)).(*image.Paletted)
}
case "bmp":
if m.opImage == nil {
m.opImage = m.image.(*image.RGBA).SubImage(image.Rect(x0, y0, x1, y1)).(*image.RGBA)
}else{
m.opImage = m.opImage.(*image.RGBA).SubImage(image.Rect(x0, y0, x1, y1)).(*image.RGBA)
}
}
}
func (m *Image) CenterClip() *Image {
if m.err != nil {
return m
}
if m.opImage == nil {
m.opImage = m.image
}
halfWidth := int(m.opImage.Bounds().Dx()/2)
halfHeight := int(m.opImage.Bounds().Dy()/2)
if m.opImage.Bounds().Dy()> m.opImage.Bounds().Dx(){
m.clip(0,halfHeight-halfWidth, m.opImage.Bounds().Dx(), halfHeight+halfWidth)
return m
}
m.clip(halfWidth-halfHeight,0, halfHeight+halfWidth, m.opImage.Bounds().Dy())
return m
}
func (m *Image) CustomClip(x0,y0,x1,y1 uint) *Image {
if m.err != nil {
return m
}
if (x0 >= x1) ||
(y0 >= y1) {
m.err = ERRCUSTOMCLIP
return m
}
if m.opImage == nil {
m.opImage = m.image
}
if x1 > uint(m.opImage.Bounds().Dx()) ||
y1 > uint(m.opImage.Bounds().Dy()) {
m.err = ERRCUSTOMCLIPPARA
return m
}
m.clip(int(x0), int(y0), int(x1), int(y1))
return m
}