-
Notifications
You must be signed in to change notification settings - Fork 8
/
combine.go
188 lines (172 loc) · 4.41 KB
/
combine.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package imageutil
import (
"image"
"image/color"
"image/draw"
"github.com/grokify/mogo/image/colors"
)
func MergeXSameY(images []image.Image, larger bool) image.Image {
if len(images) == 0 {
return nil
} else if len(images) == 1 {
return images[0]
}
images = ResizeSameY(images, larger)
// _, _, minY, _, sumX, _ := SliceXY(images, -1)
imgs := Images(images)
minY := imgs.DyMin()
sumX := imgs.DxSum(-1)
output := image.NewRGBA(image.Rect(0, 0, sumX, minY))
sumXPrev := 0
for _, img := range images {
if IsNilOrEmpty(img) {
continue
}
draw.Draw(output, img.Bounds().Add(image.Pt(sumXPrev, 0)),
img, image.Point{}, draw.Src)
sumXPrev += img.Bounds().Dx()
}
return output
}
func MergeXSameYRead(locations []string, larger bool) (image.Image, error) {
images, err := ReadImages(locations)
if err != nil {
return nil, err
}
return MergeXSameY(images, true), nil
}
func MergeYSameX(images []image.Image, larger bool) image.Image {
if len(images) == 0 {
return nil
} else if len(images) == 1 {
return images[0]
}
images = ResizeSameX(images, larger)
// minX, _, _, _, _, sumY := SliceXY(images, -1)
imgs := Images(images)
minX := imgs.DxMin()
sumY := imgs.DySum(-1)
output := image.NewRGBA(image.Rect(0, 0, minX, sumY))
sumYPrev := 0
for _, img := range images {
if IsNilOrEmpty(img) {
continue
}
draw.Draw(output, img.Bounds().Add(image.Pt(0, sumYPrev)),
img, image.Point{}, draw.Src)
sumYPrev += img.Bounds().Dy()
}
return output
}
func MergeYSameXRead(locations []string, larger bool) (image.Image, error) {
images, err := ReadImages(locations)
if err != nil {
return nil, err
}
return MergeYSameX(images, true), nil
}
type Matrix [][]image.Image
// AddBackgroundColor adds a background of `color.Color` to the images.
// It is is useful when the image has a transparent background. Use
// `colornames` for more colors, e.g. `colornames.Blue`.
func (matrix Matrix) AddBackgroundColor(clr color.Color) {
for i, row := range matrix {
for j, img := range row {
matrix[i][j] = AddBackgroundColor(img, clr)
}
}
}
func (matrix Matrix) AddBackgroundColorHex(hexcolor string) error {
clr, err := colors.ParseHex(hexcolor)
if err != nil {
return err
}
matrix.AddBackgroundColor(clr)
return nil
}
// Merge combines a set of images resizing each row element's
// height for consistent rows, an each row's width for consistent
// widths.
func (matrix Matrix) Merge(largerX, largerY bool) image.Image {
if len(matrix) == 0 {
return nil
}
rowImages := []image.Image{}
for _, rowParts := range matrix {
if len(rowParts) > 0 {
rowImages = append(rowImages, MergeXSameY(rowParts, largerY))
}
}
if len(rowImages) == 0 {
return nil
}
return MergeYSameX(rowImages, largerX)
}
func (matrix Matrix) DxSumMax() int {
dxMax := 0
for _, row := range matrix {
rowDxSum := Images(row).DxSum(-1)
if rowDxSum > dxMax {
dxMax = rowDxSum
}
}
return dxMax
}
func (matrix Matrix) DxSumMin() int {
dxMin := 0
for i, row := range matrix {
if i == 0 {
dxMin = Images(row).DxSum(-1)
}
rowDxSum := Images(row).DxSum(-1)
if rowDxSum < dxMin {
dxMin = rowDxSum
}
}
return dxMin
}
func (matrix Matrix) Dimensions() [][]image.Point {
points := [][]image.Point{}
for r, row := range matrix {
points = append(points, []image.Point{})
for _, img := range row {
points[r] = append(points[r], img.Bounds().Canon().Max)
}
}
return points
}
func MatrixRead(imglocations [][]string) (Matrix, error) {
matrixImages := Matrix{}
for _, row := range imglocations {
if len(row) == 0 {
continue
}
images, err := ReadImages(row)
if err != nil {
return nil, err
}
matrixImages = append(matrixImages, images)
}
return matrixImages, nil
}
// MergeXRGBA returns an image that is composed
// of non-overlapping imagers in ImageMetaSet. The code here
// is adapted from https://stackoverflow.com/a/35965499/1908967
func MergeXRGBA(imgs []image.Image) image.Image {
imgs2 := Images(imgs)
mergedRGBA := image.NewRGBA(image.Rectangle{
image.Point{0, 0},
image.Point{imgs2.DxSum(-1), imgs2.DyMax()}})
for i, img := range imgs2 {
if i == 0 {
draw.Draw(mergedRGBA, img.Bounds(), img, image.Point{0, 0}, draw.Src)
} else {
startingPostionI := image.Point{imgs2.DxSum(i - 1), 0}
rectangleI := image.Rectangle{
startingPostionI,
startingPostionI.Add(img.Bounds().Size())}
draw.Draw(mergedRGBA, rectangleI, img, image.Point{0, 0}, draw.Src)
}
}
return mergedRGBA
}