-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
115 lines (102 loc) · 2.45 KB
/
writer.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
115
package pixi
import (
"encoding/binary"
"fmt"
"io"
)
func StartPixi(w io.Writer) (*Pixi, error) {
// write file type
_, err := w.Write([]byte(PixiFileType))
if err != nil {
return nil, err
}
// write file version
_, err = w.Write([]byte(fmt.Sprintf("%04d", PixiVersion)))
if err != nil {
return nil, err
}
return &Pixi{}, nil
}
func WriteLayer(w io.Writer, d DiskLayer) error {
tiles := d.DiskTiles()
if tiles != len(d.TileBytes) {
return FormatError("invalid TileBytes: must have same number of elements as tiles in data set for valid pixi files")
}
if tiles != len(d.TileOffsets) {
return FormatError("invalid TileOffsets: must have same number of elements as tiles in data set for valid pixi files")
}
// first four: dimension count, field count, configuration, compression
err := binary.Write(w, binary.BigEndian, uint32(len(d.Dimensions)))
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, uint32(len(d.Fields)))
if err != nil {
return err
}
configuration := uint32(0)
if d.Separated {
configuration = 1
}
err = binary.Write(w, binary.BigEndian, configuration)
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, d.Compression)
if err != nil {
return err
}
// write layer name
err = binary.Write(w, binary.BigEndian, uint32(len([]byte(d.Name))))
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, []byte(d.Name))
if err != nil {
return err
}
// write dimension sizes and tile sizes
for _, dim := range d.Dimensions {
err = binary.Write(w, binary.BigEndian, int64(dim.Size))
if err != nil {
return err
}
}
for _, dim := range d.Dimensions {
err = binary.Write(w, binary.BigEndian, int64(dim.TileSize))
if err != nil {
return err
}
}
// write field types and names
for _, f := range d.Fields {
err = binary.Write(w, binary.BigEndian, f.Type)
if err != nil {
return err
}
}
for _, f := range d.Fields {
err = binary.Write(w, binary.BigEndian, uint16(len([]byte(f.Name))))
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, []byte(f.Name))
if err != nil {
return err
}
}
// write tile bytes, offsets, and start of next layer
err = binary.Write(w, binary.BigEndian, d.TileBytes)
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, d.TileOffsets)
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, d.NextLayerStart)
if err != nil {
return err
}
return nil
}