-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.go
60 lines (53 loc) · 1.18 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
package main
import (
"fmt"
)
// MarshalBinary converts palette entries to byte slice.
func (v Palette) MarshalBinary() (data []byte, err error) {
data = make([]byte, 3*len(v))
for i := 0; i < len(v); i++ {
data[i*3] = v[i].r
data[i*3+1] = v[i].g
data[i*3+2] = v[i].b
}
return data, nil
}
// UnmarshalBinary converts byte slice to palette entries.
func (v Palette) UnmarshalBinary(data []byte) error {
if len(v)*3 != len(data) {
return fmt.Errorf("Len is not valid. required: %d, actual: %d", len(v)*3, len(data))
}
for i := 0; i < len(v); i++ {
v[i].r = data[i*3]
v[i].g = data[i*3+1]
v[i].b = data[i*3+2]
}
return nil
}
// Rgb holds pixel data.
type Rgb struct {
r byte
g byte
b byte
}
// Palette holds palette data.
type Palette []Rgb
// ImageFrame holds picture data.
type ImageFrame struct {
width int
height int
xOffset int
yOffset int
delay int
palette Palette
transparencyIndex int
data []byte
}
// ImageData holds picture frames.
type ImageData struct {
width int
height int
palette Palette
transparencyIndex int
frames []ImageFrame
}