/
utils.go
103 lines (97 loc) · 2.2 KB
/
utils.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
package native
import (
"github.com/anthonynsimon/bild/parallel"
"github.com/gojek/darkroom/pkg/config"
"github.com/gojek/darkroom/pkg/processor"
"image"
)
func isOpaque(im image.Image) bool {
// Check if image has Opaque() method:
if oim, ok := im.(interface {
Opaque() bool
}); ok {
return oim.Opaque() // It does, call it and return its result!
}
// No Opaque() method, we need to loop through all pixels and check manually:
rect := im.Bounds()
isOpaque := true
f := func(start, end int) {
for y := rect.Min.Y + start; isOpaque && y < rect.Min.Y+end; y++ {
for x := rect.Min.X; isOpaque && x < rect.Max.X; x++ {
if _, _, _, a := im.At(x, y).RGBA(); a != 0xffff {
isOpaque = false // Found a non-opaque pixel: image is non-opaque
}
}
}
}
if config.ConcurrentImageProcessingEnabled() {
parallel.Line(rect.Dy(), f)
} else {
f(rect.Min.Y, rect.Max.Y)
}
return isOpaque // All pixels are opaque, so is the image
}
// rw: required width, rh: required height, aw: actual width, ah: actual height
func getResizeWidthAndHeight(rw, rh, aw, ah int) (int, int) {
if rh == 0 {
h := (rw * ah) / aw
return rw, h
} else if rw == 0 {
w := (rh * aw) / ah
return w, rh
} else {
h := (rw * ah) / aw
if h <= rh {
return rw, h
}
w := (rh * aw) / ah
return w, rh
}
}
// rw: required width, rh: required height, aw: actual width, ah: actual height
func getResizeWidthAndHeightForCrop(rw, rh, aw, ah int) (int, int) {
h := (rw * ah) / aw
if h >= rh {
return rw, h
}
w := (rh * aw) / ah
return w, rh
}
// w: scaled width, h: scaled height, rw: required width, rh: required height
func getStartingPointForCrop(w, h, rw, rh int, cropPoint processor.CropPoint) (int, int) {
x := (w - rw) / 2
y := (h - rh) / 2
switch cropPoint {
case processor.CropTop:
y = 0
break
case processor.CropTopLeft:
x = 0
y = 0
break
case processor.CropTopRight:
x = w - rw
y = 0
break
case processor.CropLeft:
x = 0
break
case processor.CropRight:
x = w - rw
break
case processor.CropBottom:
y = h - rh
break
case processor.CropBottomLeft:
x = 0
y = h - rh
break
case processor.CropBottomRight:
x = w - rw
y = h - rh
break
default:
break
}
return x, y
}