-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmerge.go
133 lines (116 loc) · 2.61 KB
/
merge.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
package mergi
import (
"errors"
"fmt"
"image"
"image/draw"
"strings"
)
const (
top = "T"
bottom = "B"
)
// Merge uses to merge multiple images according to given templates
//
// ex: "TT" - top, top - horizontal merge (2 images)
//
// ex: "TB" - top, bottom - vertical merge (2 images)
//
// for more merge techniques https://github.com/noelyahan/mergi/examples/merge
func Merge(template string, imgs []image.Image) (image.Image, error) {
// return a nil
tokens := strings.Split(template, "")
if len(tokens) != len(imgs) {
msg := fmt.Sprintf("Mergi founds a template=[%d] images=[%d] missmatch\n",
len(tokens), len(imgs))
return nil, errors.New(msg)
}
for i, v := range imgs {
if v == nil {
msg := fmt.Sprintf("Mergi founds a error image=[%v] on [%d]\n",
v, i)
return nil, errors.New(msg)
}
}
first := imgs[0]
yCount := getYAxisCount(template)
xCount := 0
// to get default height calculate first yCount imag Y
height := 0
width := 0
for i, tmp := range imgs {
if tmp == nil {
msg := fmt.Sprintf("Mergi founds a error imageh=[%v]\n",
tmp)
return nil, errors.New(msg)
}
if i <= yCount {
height += tmp.Bounds().Max.Y
}
if tokens[i] == top {
width += tmp.Bounds().Max.X
}
}
b := image.Rect(0, 0, width, height)
resImage := image.NewRGBA(b)
// T drawing
draw.Draw(resImage, b, first, image.ZP, draw.Src)
// t - top
// b - bottom
prev := top
currX := first.Bounds().Max.X
currY := first.Bounds().Max.Y
isNotFirstColumn := false
for i, t := range tokens {
if i == 0 {
// Because initial image will be always in top
continue
}
first = imgs[i]
if t == top {
isNotFirstColumn = true
currY = 0
if xCount >= 1 {
if len(imgs) > (i - 1) {
currX += imgs[i-1].Bounds().Max.X
}
}
xCount++
offset := image.Pt(currX, 0)
draw.Draw(resImage, first.Bounds().Add(offset), first, image.ZP, draw.Over)
} else if t == bottom {
offset := image.Pt(0, currY)
if prev == top && isNotFirstColumn {
offset = image.Pt(currX, currY)
} else if isNotFirstColumn {
offset = image.Pt(currX, currY)
}
if prev == top && !isNotFirstColumn {
offset = image.Pt(0, currY)
}
if i >= yCount {
isNotFirstColumn = true
}
draw.Draw(resImage, first.Bounds().Add(offset), first, image.ZP, draw.Over)
}
prev = t
currY += first.Bounds().Max.Y
}
return resImage, nil
}
func getYAxisCount(template string) int {
tCount := 0
bCount := 0
arr := strings.Split(template, "")
for _, v := range arr {
if tCount == 2 {
break
}
if v == top {
tCount++
} else if v == bottom {
bCount++
}
}
return bCount
}