data.go 1.52 KB
Newer Older
Jonny Schäfer's avatar
Jonny Schäfer committed
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
package radolan

import (
	"bufio"
)

// encoding types of the composite
type encoding int

const (
	runlength encoding = iota
	littleEndian
	singleByte
	unknown
)

// parsing methods
var parse = [4]func(c *Composite, rd *bufio.Reader) error{}

// init maps the parsing methods to the encoding type
func init() {
	parse[runlength] = (*Composite).parseRunlength
	parse[littleEndian] = (*Composite).parseLittleEndian
	parse[singleByte] = (*Composite).parseSingleByte
	parse[unknown] = (*Composite).parseUnknown
}

// identifyEncoing identifies the encoding type of the data section by
// only comparing header characteristics.
// This method requires header data to be already written.
func (c *Composite) identifyEncoding() encoding {
	values := c.Dx * c.Dy

	if c.level != nil {
		return runlength
	}
	if c.dataLength == values*2 {
		return littleEndian
	}
	if c.dataLength == values {
		return singleByte
	}

	return unknown
}

// parseData parses the composite data and writes the related fields.
// This method requires header data to be already written.
func (c *Composite) parseData(reader *bufio.Reader) error {
	if c.Dx == 0 || c.Dy == 0 {
		return newError("parseData", "parsed header data required")
	}

	// create Data fields
	c.Data = make([][]RVP6, c.Dy)
	for i := range c.Data {
		c.Data[i] = make([]RVP6, c.Dx)
	}

	return parse[c.identifyEncoding()](c, reader)
}

// parseUnknown performs no action and always returns an error.
func (c *Composite) parseUnknown(rd *bufio.Reader) error {
	return newError("parseUnknown", "unknown encoding")
}