-
Notifications
You must be signed in to change notification settings - Fork 4
/
img.go
281 lines (263 loc) · 6.33 KB
/
img.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Package imgfactory 图片操作工具库
package imgfactory
import (
"bytes"
"image"
"image/color"
"image/draw"
"image/gif"
"io"
"net/http"
"os"
"strings"
"github.com/FloatTech/gg"
"github.com/disintegration/imaging"
)
// Load 加载图片
func Load(path string) (img image.Image, err error) {
if strings.HasPrefix(path, "http") {
var res *http.Response
res, err = http.Get(path)
if err != nil {
return
}
img, _, err = image.Decode(res.Body)
_ = res.Body.Close()
return
}
return gg.LoadImage(path)
}
// Parse 解析图片数据流
func Parse(r io.Reader) (img image.Image, err error) {
img, _, err = image.Decode(r)
return
}
// NewFactory 设置底图
func NewFactoryBG(w, h int, fillColor color.Color) *Factory {
c := color.NRGBAModel.Convert(fillColor).(color.NRGBA)
if (c == color.NRGBA{0, 0, 0, 0}) {
return NewFactory(image.NewNRGBA(image.Rect(0, 0, w, h)))
}
return NewFactory(&image.NRGBA{
Pix: bytes.Repeat([]byte{c.R, c.G, c.B, c.A}, w*h),
Stride: 4 * w,
Rect: image.Rect(0, 0, w, h),
})
}
// LoadFirstFrame 载入图片第一帧作底图
func LoadFirstFrame(path string, w, h int) (*Factory, error) {
im, err := Load(path)
if err != nil {
return nil, err
}
return Size(im, w, h), nil
}
// ParseFirstFrame 解析图片第一帧作底图
func ParseFirstFrame(r io.Reader, w, h int) (*Factory, error) {
im, err := Parse(r)
if err != nil {
return nil, err
}
return Size(im, w, h), nil
}
// LoadAllFrames 加载每一帧图片
func LoadAllFrames(path string, w, h int) ([]*Factory, error) {
var res *http.Response
var err error
var im *gif.GIF
if strings.HasPrefix(path, "http") {
res, err = http.Get(path)
if err != nil {
return nil, err
}
im, err = gif.DecodeAll(res.Body)
_ = res.Body.Close()
if err != nil {
return nil, err
}
} else {
file, err := os.Open(path)
if err != nil {
return nil, err
}
im, err = gif.DecodeAll(file)
_ = file.Close()
if err != nil {
return nil, err
}
}
img, err := Load(path)
if err != nil {
return nil, err
}
im0 := Size(img, w, h)
ims := make([]*Factory, len(im.Image))
for i, v := range im.Image {
ims[i] = im0.InsertUp(Size(v, w, h).im, 0, 0, 0, 0).Clone()
}
return ims, nil
}
// LoadAllTrueFrames 加载每一帧显示出的图片
func LoadAllTrueFrames(path string, w, h int) ([]*Factory, error) {
var res *http.Response
var err error
var im *gif.GIF
if strings.HasPrefix(path, "http") {
res, err = http.Get(path)
if err != nil {
return nil, err
}
im, err = gif.DecodeAll(res.Body)
_ = res.Body.Close()
if err != nil {
return nil, err
}
} else {
file, err := os.Open(path)
if err != nil {
return nil, err
}
im, err = gif.DecodeAll(file)
_ = file.Close()
if err != nil {
return nil, err
}
}
imgWidth, imgHeight := getGifDimensions(im)
overpaintImage := image.NewRGBA(image.Rect(0, 0, imgWidth, imgHeight))
img, err := Load(path)
if err != nil {
return nil, err
}
im0 := Size(img, w, h)
ims := make([]*Factory, len(im.Image))
for i, v := range im.Image {
draw.Draw(overpaintImage, overpaintImage.Bounds(), v, image.Point{}, draw.Over)
ims[i] = im0.InsertUp(Size(overpaintImage, w, h).im, 0, 0, 0, 0).Clone()
}
return ims, nil
}
// Size 变形
func Size(im image.Image, w, h int) *Factory {
sz := im.Bounds().Size()
// 修改尺寸
switch {
case w > 0 && h > 0:
return NewFactory(imaging.Resize(im, w, h, imaging.Lanczos))
case w == 0 && h > 0:
return NewFactory(imaging.Resize(im, h*sz.X/sz.Y, h, imaging.Lanczos))
case h == 0 && w > 0:
return NewFactory(imaging.Resize(im, w, w*sz.Y/sz.X, imaging.Lanczos))
default:
nim := image.NewNRGBA(image.Rect(0, 0, sz.X, sz.Y))
draw.Over.Draw(nim, nim.Bounds(), im, im.Bounds().Min)
return NewFactory(nim)
}
}
// Rotate 旋转
func Rotate(img image.Image, angle float64, w, h int) *Factory {
return NewFactory(imaging.Rotate(Size(img, w, h).im, angle, color.NRGBA{0, 0, 0, 0}))
}
// MergeW 横向合并图片
func MergeW(im []*image.NRGBA) *Factory {
dc := make([]*Factory, len(im))
h := im[0].Bounds().Size().Y
w := 0
for i, value := range im {
dc[i] = Size(value, 0, h)
w += dc[i].W()
}
ds := NewFactoryBG(w, h, color.NRGBA{0, 0, 0, 0})
x := 0
for _, value := range dc {
ds = ds.InsertUp(value.im, value.W(), h, x, 0)
x += value.W()
}
return ds
}
// MergeH 纵向合并图片
func MergeH(im []*image.NRGBA) *Factory {
dc := make([]*Factory, len(im))
w := im[0].Bounds().Size().X
h := 0
for i, value := range im {
dc[i] = Size(value, 0, w)
h += dc[i].H()
}
ds := NewFactoryBG(w, h, color.NRGBA{0, 0, 0, 0})
y := 0
for _, value := range dc {
ds = ds.InsertUp(value.im, w, value.H(), 0, y)
y += value.H()
}
return ds
}
// Text 文本框 字体, 大小, 颜色 , 背景色, 文本
func Text(font string, size float64, col []int, col1 []int, txt string) *Factory {
var dst Factory
dc := gg.NewContext(10, 10)
dc.SetRGBA255(0, 0, 0, 0)
dc.Clear()
dc.SetRGBA255(col[0], col[1], col[2], col[3])
err := dc.LoadFontFace(font, size+size/2)
if err != nil {
return &dst
}
w, h := dc.MeasureString(txt)
w -= size * 2
dc1 := gg.NewContext(int(w), int(h))
dc1.SetRGBA255(col1[0], col1[1], col1[2], col1[3])
dc1.Clear()
dc1.SetRGBA255(col[0], col[1], col[2], col[3])
err = dc1.LoadFontFace(font, size)
if err != nil {
return &dst
}
dc1.DrawStringAnchored(txt, w/2, h/2, 0.5, 0.5)
dst.im = image.NewNRGBA(image.Rect(0, 0, int(w), int(h)))
draw.Over.Draw(dst.im, dst.im.Bounds(), dc1.Image(), dc1.Image().Bounds().Min)
return &dst
}
// Limit 限制图片在 xmax*ymax 之内
func Limit(img image.Image, xmax, ymax int) image.Image {
// 避免图片过大, 最大 xmax*ymax
x := img.Bounds().Size().X
y := img.Bounds().Size().Y
hasChanged := false
if x > xmax {
y = y * xmax / x
x = xmax
hasChanged = true
}
if y > ymax {
x = x * ymax / y
y = ymax
hasChanged = true
}
if hasChanged {
img = Size(img, x, y).im
}
return img
}
// 获取gif真实大小
func getGifDimensions(gif *gif.GIF) (x, y int) {
var lowestX int
var lowestY int
var highestX int
var highestY int
for _, img := range gif.Image {
if img.Rect.Min.X < lowestX {
lowestX = img.Rect.Min.X
}
if img.Rect.Min.Y < lowestY {
lowestY = img.Rect.Min.Y
}
if img.Rect.Max.X > highestX {
highestX = img.Rect.Max.X
}
if img.Rect.Max.Y > highestY {
highestY = img.Rect.Max.Y
}
}
return highestX - lowestX, highestY - lowestY
}