-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunction.go
221 lines (189 loc) · 4.8 KB
/
function.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package utils
import (
"archive/zip"
"bytes"
"encoding/base64"
"fmt"
"image"
"io"
"os"
"strconv"
"strings"
"github.com/chai2010/webp"
"github.com/corona10/goimagehash"
"github.com/disintegration/imaging"
gowebp "golang.org/x/image/webp"
)
func Exist(file string) bool {
if _, err := os.Stat(file); os.IsNotExist(err) {
return false
}
return true
}
func IndexFunc(length int, f func(i int) bool) int {
for i := 0; i < length; i++ {
if f(i) {
return i
}
}
return -1
}
func WriteWebp(path string, img image.Image, opt *webp.Options) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
err = webp.Encode(f, img, opt)
if err != nil {
return err
}
return nil
}
func DecodeWebp(path string) (*image.Image, error) {
if Exist(path) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
r, err := gowebp.Decode(f)
if err != nil {
return nil, err
}
return &r, nil
}
return nil, fmt.Errorf("解析webp错误: %s不存在", path)
}
var SAME_PIC_THRESHOLD int = 5
var NULL_PIC_THRESHOLD int = 5
var NULL_HASH *goimagehash.ImageHash
func init() {
n, err := base64.StdEncoding.DecodeString("If+BAwEBAUQB/4IAAQIBBEhhc2gBBgABBEtpbmQBBAAAAA//ggH4AQAAAAD///8BAgA=")
if err != nil {
panic(err)
}
null, err := goimagehash.LoadImageHash(bytes.NewBuffer(n))
if err != nil {
panic(err)
}
NULL_HASH = null
}
func IsSamePic(img1 image.Image, img2 image.Image, compare image.Rectangle) (bool, error) {
croped1 := imaging.Crop(img1, compare)
croped2 := imaging.Crop(img2, compare)
h1, err := goimagehash.AverageHash(croped1)
if err != nil {
return false, err
}
h2, err := goimagehash.AverageHash(croped2)
if err != nil {
return false, err
}
dist, err := h1.Distance(h2)
if err != nil {
return false, err
}
if dist <= SAME_PIC_THRESHOLD {
return true, nil
}
return false, nil
}
func IsNullPic(img image.Image, compare image.Rectangle) (bool, error) {
croped1 := imaging.Crop(img, compare)
h, err := goimagehash.AverageHash(croped1)
if err != nil {
return false, err
}
dist, err := h.Distance(NULL_HASH)
if err != nil {
return false, err
}
if dist >= SAME_PIC_THRESHOLD {
return false, nil
}
return true, nil
}
func CropAsset(frame map[string]any, src image.Image) (*image.NRGBA, error) {
rotated, ok := frame["textureRotated"].(bool)
if !ok {
return nil, fmt.Errorf("plist 数据解析错误")
}
spriteSourceSizeStr, ok := frame["spriteSourceSize"].(string)
if !ok {
return nil, fmt.Errorf("plist 数据解析错误")
}
spriteSourceSize, err := str2ints(spriteSourceSizeStr)
if err != nil {
return nil, fmt.Errorf("plist 数据解析错误")
}
textureRectStr, ok := frame["textureRect"].(string)
if !ok {
return nil, fmt.Errorf("plist 数据解析错误")
}
textureRect, err := str2ints(textureRectStr)
if err != nil {
return nil, fmt.Errorf("plist 数据解析错误")
}
spriteOffsetStr, ok := frame["spriteOffset"].(string)
if !ok {
return nil, fmt.Errorf("plist 数据解析错误")
}
spriteOffset, err := str2ints(spriteOffsetStr)
if err != nil {
return nil, fmt.Errorf("plist 数据解析错误")
}
// spriteSourceSize和spriteSize的关系似乎是裁剪而不是拉伸,
// 所以这里要计算差值,注意计算的时候插值要除2,保持图像居中
// ! 这里直接取索引最好用get来判断越界问题
// ! 注意这里的delta需要用未涉及rotated的原值,要在rotated转换之前计算
delta_w := spriteSourceSize[0] - textureRect[2]
delta_h := spriteSourceSize[1] - textureRect[3]
if rotated {
textureRect = []int{
textureRect[0],
textureRect[1],
textureRect[3],
textureRect[2],
}
}
// (texture_rect[0] - sprite_offset[0]) as u32,
// (texture_rect[1] - sprite_offset[1]) as u32,
// texture_rect[2] as u32,
// texture_rect[3] as u32,
cropped := imaging.Crop(src, image.Rect(textureRect[0]-spriteOffset[0], textureRect[1]-spriteOffset[1],
textureRect[0]-spriteOffset[0]+textureRect[2], textureRect[1]-spriteOffset[1]+textureRect[3]))
if rotated {
cropped = imaging.Rotate90(cropped)
}
out := image.NewNRGBA(image.Rect(0, 0, spriteSourceSize[0], spriteSourceSize[1]))
out = imaging.Paste(out, cropped, image.Point{delta_w / 2, delta_h / 2})
return out, nil
}
func str2ints(src string) ([]int, error) {
r := []int{}
l := strings.Split(strings.ReplaceAll(strings.ReplaceAll(src, "{", ""), "}", ""), ",")
for _, v := range l {
p, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return []int{}, err
}
r = append(r, int(p))
}
if len(r) == 0 {
return []int{}, fmt.Errorf("plist 数据解析错误")
}
return r, nil
}
func read(f *zip.File) ([]byte, error) {
buf, err := f.Open()
if err != nil {
return nil, err
}
defer buf.Close()
data, err := io.ReadAll(buf)
if err != nil {
return nil, err
}
return data, nil
}