-
Notifications
You must be signed in to change notification settings - Fork 8
/
images.go
146 lines (132 loc) · 2.83 KB
/
images.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
package imageutil
import (
"image"
"golang.org/x/image/draw"
)
type Images []image.Image
func (imgs Images) Dimensions() []image.Point {
points := []image.Point{}
for _, img := range imgs {
points = append(points, image.Point{
X: img.Bounds().Canon().Max.X,
Y: img.Bounds().Canon().Max.Y})
}
return points
}
func (imgs Images) Dxs() []int {
dxs := []int{}
for _, img := range imgs {
dxs = append(dxs, img.Bounds().Dx())
}
return dxs
}
func (imgs Images) DxMax() int {
dxMax := 0
for _, img := range imgs {
if dx := img.Bounds().Dx(); dx > dxMax {
dxMax = dx
}
}
return dxMax
}
func (imgs Images) DxMin() int {
dxMin := 0
for i, img := range imgs {
if i == 0 {
dxMin = img.Bounds().Dx()
} else if dx := img.Bounds().Dx(); dx < dxMin {
dxMin = dx
}
}
return dxMin
}
// DxSum returns the sum of widths up to and including
// `maxIndexInclusive`. Use a negative value for `maxIndexInclusive`
// to include all elements.
func (imgs Images) DxSum(maxIndexInclusive int) int {
dxSum := 0
for i, img := range imgs {
if maxIndexInclusive >= 0 && i > maxIndexInclusive {
break
}
dxSum += img.Bounds().Dx()
}
return dxSum
}
func (imgs Images) Dys() []int {
dys := []int{}
for _, img := range imgs {
dys = append(dys, img.Bounds().Dy())
}
return dys
}
func (imgs Images) DyMax() int {
dyMax := 0
for _, img := range imgs {
if dy := img.Bounds().Dy(); dy > dyMax {
dyMax = dy
}
}
return dyMax
}
func (imgs Images) DyMin() int {
dyMin := 0
for i, img := range imgs {
if i == 0 {
dyMin = img.Bounds().Dy()
} else if dy := img.Bounds().Dy(); dy < dyMin {
dyMin = dy
}
}
return dyMin
}
// DySum returns the sum of heights up to and including
// `maxIndexInclusive`. Use a negative value for `maxIndexInclusive`
// to include all elements.
func (imgs Images) DySum(maxIndexInclusive int) int {
dySum := 0
for i, img := range imgs {
if maxIndexInclusive >= 0 && i > maxIndexInclusive {
break
}
dySum += img.Bounds().Dy()
}
return dySum
}
// ConsistentSize resizes and crops the images so that they have all
// the same size. It prioritize resizing images to max Dx and then
// cropping Dy so they are consistent.
func (imgs Images) ConsistentSize(scale draw.Scaler, yAlign string) {
if len(imgs) == 0 {
return
}
dxMax := imgs.DxMax()
if dxMax <= 0 {
return
}
for i, img := range imgs {
if img.Bounds().Dx() != dxMax {
imgs[i] = Resize(uint(dxMax), 0, img, scale)
}
}
dyMin := imgs.DyMin()
if dyMin <= 0 {
return
}
for i, img := range imgs {
if img.Bounds().Dy() != dyMin {
imgs[i] = CropY(img, uint(dyMin), yAlign)
}
}
}
func (imgs Images) Stats() ImagesStats {
return ImagesStats{
Dxs: imgs.Dxs(),
DxMax: imgs.DxMax(),
DxMin: imgs.DxMin(),
DxSum: imgs.DxSum(-1),
Dys: imgs.Dys(),
DyMax: imgs.DyMax(),
DyMin: imgs.DyMin(),
DySum: imgs.DySum(-1)}
}