-
Notifications
You must be signed in to change notification settings - Fork 8
/
overlay.go
69 lines (60 loc) · 1.8 KB
/
overlay.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
package imageutil
import (
"image"
"image/draw"
"strings"
)
func Overlay(src, overlay image.Image, offset image.Point) image.Image {
output := image.NewRGBA(src.Bounds())
draw.Draw(output, src.Bounds(), src, image.Point{}, draw.Src)
draw.Draw(output, overlay.Bounds().Add(offset), overlay, image.Point{}, draw.Src)
return output
}
const (
LocUpper = "upper"
LocMiddle = "middle"
LocLower = "lower"
LocLeft = "left"
LocCenter = "center"
LocRight = "right"
LocUpperLeft = "upperleft"
LocUpperRight = "upperright"
LocLowerLeft = "lowerleft"
LocLowerRight = "lowerright"
)
func OverlayMore(src, overlay image.Image, overlayLocation string, padX, padY int) image.Image {
return Overlay(src, overlay, OverlayOffset(src.Bounds(), overlay.Bounds(), overlayLocation, padX, padY))
}
func OverlayOffset(src, overlay image.Rectangle, overlayLocation string, padX, padY int) image.Point {
pt := image.Point{}
if strings.Contains(overlayLocation, LocUpper) {
pt.Y = src.Min.Y + padY
} else if strings.Contains(overlayLocation, LocLower) {
pt.Y = src.Max.Y - overlay.Dy() - padY
} else {
pt.Y = src.Max.Y - ((src.Dy() - overlay.Dy()) / 2) + padY
}
if strings.Contains(overlayLocation, LocLeft) {
pt.X = src.Min.X + padX
} else if strings.Contains(overlayLocation, LocRight) {
pt.X = src.Max.X - overlay.Dx() - padX
} else {
pt.X = src.Max.X - ((src.Dx() - overlay.Dx()) / 2) + padX
}
return pt
}
/*
func OverlayCenterYLeftAlign(src, overlay image.Image) image.Image {
h1 := src.Bounds().Dy()
h2 := overlay.Bounds().Dy()
offset := image.Pt(0, (h1-h2)/2)
return Overlay(src, overlay, offset)
}
func OverlayLowerLeft(src, overlay image.Image) image.Image {
return Overlay(
src, overlay,
image.Pt(
src.Bounds().Min.X,
src.Bounds().Max.Y-overlay.Bounds().Dy()))
}
*/