-
Notifications
You must be signed in to change notification settings - Fork 2
/
copy.go
85 lines (79 loc) · 1.9 KB
/
copy.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
package imagex
import (
"github.com/xuzhuoxi/infra-go/mathx"
"image"
"image/draw"
)
// CopyImage
// 复制图像
func CopyImage(srcImg image.Image) draw.Image {
copyImg := CopyImageStruct(srcImg)
if nil == copyImg {
return nil
}
CopyImageTo(srcImg, copyImg)
return copyImg
}
// XCopyImage
// 复制图像
func XCopyImage(srcImg image.Image) draw.Image {
copyImg := CopyImageStruct(srcImg)
if nil == copyImg {
return nil
}
XCopyImageTo(srcImg, copyImg)
return copyImg
}
// CopyImageTo
// 把源图像复制到目标图像
func CopyImageTo(srcImg image.Image, dstImg draw.Image) {
srcRect := srcImg.Bounds()
dstRect := dstImg.Bounds()
minX := mathx.MaxInt(dstRect.Min.X, srcRect.Min.X)
minY := mathx.MaxInt(dstRect.Min.Y, srcRect.Min.Y)
maxX := mathx.MinInt(dstRect.Max.X, srcRect.Max.X)
maxY := mathx.MinInt(dstRect.Max.Y, srcRect.Max.Y)
for y := minY; y < maxY; y++ {
for x := minX; x < maxX; x++ {
dstImg.Set(x, y, srcImg.At(x, y))
}
}
}
// XCopyImageTo
// 把源图像复制到目标图像
func XCopyImageTo(srcImg image.Image, dstImg draw.Image) {
draw.Draw(dstImg, srcImg.Bounds(), srcImg, image.Pt(0, 0), draw.Over)
}
// CopyImageStruct
// 复制图像
func CopyImageStruct(srcImg image.Image) draw.Image {
rect := srcImg.Bounds()
var clone draw.Image
switch t := srcImg.(type) {
case *image.Alpha:
clone = image.NewAlpha(rect)
case *image.CMYK:
clone = image.NewCMYK(rect)
case *image.Gray:
clone = image.NewGray(rect)
case *image.Gray16:
clone = image.NewGray16(rect)
case *image.NRGBA:
clone = image.NewNRGBA(rect)
case *image.NRGBA64:
clone = image.NewNRGBA64(rect)
case *image.Paletted:
clone = image.NewPaletted(rect, t.Palette)
case *image.RGBA:
clone = image.NewRGBA(rect)
case *image.RGBA64:
clone = image.NewRGBA64(rect)
case *image.YCbCr:
clone = image.NewRGBA(rect)
case *image.NYCbCrA:
clone = image.NewNRGBA(rect)
default:
return nil
}
return clone
}