-
Notifications
You must be signed in to change notification settings - Fork 7
/
primitives.go
83 lines (71 loc) · 1.75 KB
/
primitives.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
package spice
import (
"bytes"
"encoding/binary"
"errors"
"image"
)
type Rect struct {
Top uint32
Left uint32
Bottom uint32
Right uint32
}
func (rect *Rect) Decode(r *bytes.Reader) error {
binary.Read(r, binary.LittleEndian, &rect.Top)
binary.Read(r, binary.LittleEndian, &rect.Left)
binary.Read(r, binary.LittleEndian, &rect.Bottom)
return binary.Read(r, binary.LittleEndian, &rect.Right)
}
func (rect *Rect) Rectangle() image.Rectangle {
return image.Rectangle{
Min: image.Point{X: int(rect.Left), Y: int(rect.Top)},
Max: image.Point{X: int(rect.Right), Y: int(rect.Bottom)},
}
}
type Point struct {
X uint32
Y uint32
}
func (point *Point) Decode(r *bytes.Reader) error {
if err := binary.Read(r, binary.LittleEndian, &point.X); err != nil {
return err
}
return binary.Read(r, binary.LittleEndian, &point.Y)
}
type QMask struct {
MaskFlags uint8 // 1=INVERS
Pos Point
ImagePtr uint32
*Image
}
func (qmask *QMask) Decode(r *bytes.Reader) error {
binary.Read(r, binary.LittleEndian, &qmask.MaskFlags)
qmask.Pos.Decode(r)
return binary.Read(r, binary.LittleEndian, &qmask.ImagePtr)
}
type DisplayBase struct {
Surface uint32
Box Rect // Rect box
ClipType uint8 // 0=NONE, 1=RECTS
NumRects uint32 // if clip_type=1
Rects []Rect // if clip_type=1
}
func (res *DisplayBase) Decode(r *bytes.Reader) error {
binary.Read(r, binary.LittleEndian, &res.Surface)
res.Box.Decode(r)
binary.Read(r, binary.LittleEndian, &res.ClipType)
switch res.ClipType {
case 0:
return nil
case 1:
binary.Read(r, binary.LittleEndian, &res.NumRects)
res.Rects = make([]Rect, res.NumRects)
for i := uint32(0); i < res.NumRects; i++ {
res.Rects[i].Decode(r)
}
return nil
default:
return errors.New("invalid display base")
}
}