forked from gomatcha/matcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
47 lines (42 loc) · 834 Bytes
/
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
package proto
import (
"image"
"image/color"
)
func ColorEncode(c color.Color) *Color {
if c == nil {
return nil
}
r, g, b, a := c.RGBA()
return &Color{
Red: r,
Green: g,
Blue: b,
Alpha: a,
}
}
func ImageEncode(img image.Image) *Image {
if img == nil {
return nil
}
bounds := img.Bounds()
newImg := image.NewRGBA(bounds)
for x := bounds.Min.X; x < bounds.Max.X; x++ {
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
newImg.Set(x, y, img.At(x, y))
}
}
return &Image{
Width: int64(bounds.Max.X - bounds.Min.X),
Height: int64(bounds.Max.Y - bounds.Min.Y),
Stride: int64(newImg.Stride),
Data: newImg.Pix,
}
}
func ImageDecode(img *Image) image.Image {
return &image.RGBA{
Pix: img.Data,
Stride: int(img.Stride),
Rect: image.Rect(0, 0, int(img.Width), int(img.Height)),
}
}