-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscale.go
50 lines (46 loc) · 1.14 KB
/
scale.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
package imagescaling
import "github.com/nfnt/resize"
func (m *Image) scale(h, w uint) {
if m.opImage == nil {
m.opImage = resize.Resize(w, h, m.image, resize.Lanczos3)
}else{
m.opImage = resize.Resize(w, h, m.opImage, resize.Lanczos3)
}
}
func (m *Image) ProportionScale(p float64) *Image {
if m.err != nil {
return m
}
if p < 0 {
m.err = ERRPROPORTION
return m
}
if m.opImage == nil {
m.opImage = m.image
}
m.scale(uint(float64(m.opImage.Bounds().Max.Y) * p), uint(float64(m.opImage.Bounds().Max.X)*p))
return m
}
func (m *Image) FixScale(h, w uint) *Image {
if m.err != nil {
return m
}
if h == 0 && w == 0 {
m.err = ERRFIXLENGTHSCALE
return m
}
if m.opImage == nil {
m.opImage = m.image
}
if h == 0 && w != 0 {
ratio := w/uint(m.opImage.Bounds().Max.X)
m.scale(uint(m.opImage.Bounds().Max.Y)*ratio, w)
return m
}
if h != 0 && w == 0 {
ratio := h/uint(m.opImage.Bounds().Max.Y)
m.scale(h, uint(m.opImage.Bounds().Max.X)*ratio)
return m
}
return m
}