/
fill.go
124 lines (110 loc) · 3.46 KB
/
fill.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
/*
* Copyright (C) 2014 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: jouyouyun <jouyouwen717@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package graphic
import (
"fmt"
"image"
"image/draw"
)
// TODO: is FillXXX() still necessary?
// FillStyle define the type to fill image.
type FillStyle string
const (
FillTile FillStyle = "tile" // 平铺
FillCenter = "center" // 居中
)
// FillImage generate a new image file in target width and height through
// source image file, there are many fill sytles to choice from.
func FillImage(srcfile, dstfile string, width, height int, style FillStyle, f Format) (err error) {
srcimg, err := LoadImage(srcfile)
if err != nil {
return
}
dstimg, err := Fill(srcimg, width, height, style)
if err != nil {
return
}
err = SaveImage(dstfile, dstimg, f)
dstimg.Pix = nil
return
}
// FillImageCache generate a new image in target width and height through
// source image, and save it to cache directory, if already exists,
// just return it.
func FillImageCache(srcfile string, width, height int, style FillStyle, f Format) (dstfile string, useCache bool, err error) {
dstfile = generateCacheFilePath(fmt.Sprintf("FillImageCache%s%d%d%s%s", srcfile, width, height, style, f))
if isFileExists(dstfile) {
// return cache file
useCache = true
return
}
err = FillImage(srcfile, dstfile, width, height, style, f)
return
}
// FillImage generate a new image in target width and height through
// source image, there are many fill sytles to choice from.
func Fill(srcimg image.Image, width, height int, style FillStyle) (dstimg *image.RGBA, err error) {
switch style {
case FillTile:
dstimg = doFillImageInTileStyle(srcimg, width, height)
case FillCenter:
dstimg = doFillImageInCenterStyle(srcimg, width, height)
default:
err = fmt.Errorf("unknown fill style %v", style)
return
}
return
}
func doFillImageInTileStyle(srcimg image.Image, width, height int) (dstimg *image.RGBA) {
dstimg = image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
iw, ih := GetSize(srcimg)
endx := width - 1
endy := height - 1
for x := 0; x <= endx; x += iw {
for y := 0; y <= endy; y += ih {
draw.Draw(dstimg, image.Rect(x, y, x+iw, y+ih), srcimg, image.Point{0, 0}, draw.Src)
}
}
return
}
func doFillImageInCenterStyle(srcimg image.Image, width, height int) (dstimg *image.RGBA) {
dstimg = image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
iw, ih := GetSize(srcimg)
var srcX, srcY, dstX, dstY, clipWidth, clipHeight int
if width > iw {
srcX = 0
clipWidth = iw
dstX = width/2 - iw/2
} else {
dstX = 0
clipWidth = width
srcX = iw/2 - width/2
}
if height > ih {
srcY = 0
clipHeight = ih
dstY = height/2 - ih/2
} else {
dstY = 0
clipHeight = height
srcY = ih/2 - height/2
}
draw.Draw(dstimg, image.Rect(dstX, dstY, dstX+clipWidth, dstY+clipHeight), srcimg, image.Point{srcX, srcY}, draw.Src)
return
}