-
-
Notifications
You must be signed in to change notification settings - Fork 382
/
image.go
114 lines (100 loc) · 3.06 KB
/
image.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
package caire
import (
"image"
"image/color"
)
// Grayscale converts the source image to grayscale mode.
func (c *Carver) Grayscale(src *image.NRGBA) *image.NRGBA {
dx, dy := src.Bounds().Max.X, src.Bounds().Max.Y
dst := image.NewNRGBA(src.Bounds())
for x := 0; x < dx; x++ {
for y := 0; y < dy; y++ {
r, g, b, _ := src.At(x, y).RGBA()
lum := float32(r)*0.299 + float32(g)*0.587 + float32(b)*0.114
pixel := color.Gray{Y: uint8(lum / 256)}
dst.Set(x, y, pixel)
}
}
return dst
}
// RotateImage90 rotate the image by 90 degree counter clockwise.
func (c *Carver) RotateImage90(src *image.NRGBA) *image.NRGBA {
b := src.Bounds()
dst := image.NewNRGBA(image.Rect(0, 0, b.Max.Y, b.Max.X))
for dstY := 0; dstY < b.Max.X; dstY++ {
for dstX := 0; dstX < b.Max.Y; dstX++ {
srcX := b.Max.X - dstY - 1
srcY := dstX
srcOff := srcY*src.Stride + srcX*4
dstOff := dstY*dst.Stride + dstX*4
copy(dst.Pix[dstOff:dstOff+4], src.Pix[srcOff:srcOff+4])
}
}
return dst
}
// RotateImage270 rotate the image by 270 degree counter clockwise.
func (c *Carver) RotateImage270(src *image.NRGBA) *image.NRGBA {
b := src.Bounds()
dst := image.NewNRGBA(image.Rect(0, 0, b.Max.Y, b.Max.X))
for dstY := 0; dstY < b.Max.X; dstY++ {
for dstX := 0; dstX < b.Max.Y; dstX++ {
srcX := dstY
srcY := b.Max.Y - dstX - 1
srcOff := srcY*src.Stride + srcX*4
dstOff := dstY*dst.Stride + dstX*4
copy(dst.Pix[dstOff:dstOff+4], src.Pix[srcOff:srcOff+4])
}
}
return dst
}
// imgToPix converts an image to a pixel array.
func (c *Carver) imgToPix(src *image.NRGBA) []uint8 {
bounds := src.Bounds()
pixels := make([]uint8, 0, bounds.Max.X*bounds.Max.Y*4)
for x := bounds.Min.X; x < bounds.Max.X; x++ {
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
r, g, b, _ := src.At(y, x).RGBA()
pixels = append(pixels, uint8(r>>8), uint8(g>>8), uint8(b>>8), 255)
}
}
return pixels
}
// pixToImage converts an array buffer to an image.
func (c *Carver) pixToImage(pixels []uint8) image.Image {
dst := image.NewNRGBA(image.Rect(0, 0, c.Width, c.Height))
bounds := dst.Bounds()
dx, dy := bounds.Max.X, bounds.Max.Y
col := color.NRGBA{
R: uint8(0),
G: uint8(0),
B: uint8(0),
A: uint8(255),
}
for x := bounds.Min.X; x < dx; x++ {
for y := bounds.Min.Y; y < dy*4; y += 4 {
col.R = uint8(pixels[y+x*dy*4])
col.G = uint8(pixels[y+x*dy*4+1])
col.B = uint8(pixels[y+x*dy*4+2])
col.A = uint8(pixels[y+x*dy*4+3])
dst.SetNRGBA(x, int(y/4), col)
}
}
return dst
}
// rgbToGrayscale converts an image to grayscale mode and
// returns the pixel values as an one dimensional array.
func (c *Carver) rgbToGrayscale(src *image.NRGBA) []uint8 {
width, height := src.Bounds().Dx(), src.Bounds().Dy()
gray := make([]uint8, width*height)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
r, g, b, _ := src.At(x, y).RGBA()
gray[y*width+x] = uint8(
(0.299*float64(r) +
0.587*float64(g) +
0.114*float64(b)) / 256,
)
}
}
return gray
}