-
Notifications
You must be signed in to change notification settings - Fork 8
/
combine.go
135 lines (123 loc) · 3.19 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
package imageutil
import (
"fmt"
"image"
"image/color"
"strings"
"github.com/grokify/mogo/image/colors"
"golang.org/x/image/draw"
)
func debugImagesDimensions(note string, images []image.Image) {
dimensions := []string{note}
for i, img := range images {
dimensions = append(dimensions,
fmt.Sprintf("X%d[%v]y%d[%v]",
i+1, img.Bounds().Dx(), i+1, img.Bounds().Dy()))
}
fmt.Printf(strings.Join(dimensions, " ") + "\n")
}
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)
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)
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 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
}