More colors. Store indices of colors in decoded blocks instead of strings.

This commit is contained in:
Sascha L. Teichmann 2014-09-09 23:33:53 +02:00
parent 73b6fa4fb9
commit baee56b27b
4 changed files with 2137 additions and 2004 deletions

View File

@ -10,6 +10,7 @@ import (
"errors"
"io"
"io/ioutil"
"log"
)
var (
@ -26,7 +27,7 @@ type DecodedBlock struct {
MapContent []byte
AirId int
IgnoreId int
NameMap map[int]string
IndexMap map[int]int
}
type PosBuf struct {
@ -34,7 +35,7 @@ type PosBuf struct {
Pos int
}
func NewDecodedBlock(data []byte) (db *DecodedBlock, err error) {
func NewDecodedBlock(data []byte, nameIndex map[string]int) (db *DecodedBlock, err error) {
version := data[0]
contentWidth := int(data[2])
@ -106,7 +107,7 @@ func NewDecodedBlock(data []byte) (db *DecodedBlock, err error) {
offset += 4
airId, ignoreId := -1, -1
nameMap := make(map[int]string)
indexMap := make(map[int]int)
if version >= 22 {
offset++
numMappings := int(binary.BigEndian.Uint16(data[offset:]))
@ -124,7 +125,11 @@ func NewDecodedBlock(data []byte) (db *DecodedBlock, err error) {
case "ignore":
ignoreId = nodeId
default:
nameMap[nodeId] = name
if index, found := nameIndex[name]; found {
indexMap[nodeId] = index
} else {
log.Printf("Missing color entry for %s.", name)
}
}
}
}
@ -134,7 +139,7 @@ func NewDecodedBlock(data []byte) (db *DecodedBlock, err error) {
MapContent: mapContent,
AirId: airId,
IgnoreId: ignoreId,
NameMap: nameMap}
IndexMap: indexMap}
return
}

View File

@ -12,7 +12,12 @@ import (
"strings"
)
func ParseColors(filename string) (colors map[string]color.RGBA, err error) {
type Colors struct {
Colors []color.RGBA
NameIndex map[string]int
}
func ParseColors(filename string) (colors *Colors, err error) {
var file *os.File
if file, err = os.Open(filename); err != nil {
@ -20,7 +25,8 @@ func ParseColors(filename string) (colors map[string]color.RGBA, err error) {
}
defer file.Close()
colors = make(map[string]color.RGBA)
nameIndex := make(map[string]int)
cols := make([]color.RGBA, 0, 2200)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
@ -32,9 +38,12 @@ func ParseColors(filename string) (colors map[string]color.RGBA, err error) {
var name string
if n, _ := fmt.Sscanf(
line, "%s %d %d %d %d", &name, &c.R, &c.G, &c.B, &c.A); n > 0 {
colors[name] = c
idx := len(cols)
cols = append(cols, c)
nameIndex[name] = idx
}
}
err = scanner.Err()
colors = &Colors{Colors: cols, NameIndex: nameIndex}
return
}

File diff suppressed because it is too large Load Diff

View File

@ -7,7 +7,6 @@ package main
import (
"flag"
"fmt"
"image/color"
"log"
"bitbucket.org/s_l_teichmann/mtredisalize/common"
@ -38,7 +37,7 @@ func main() {
flag.Parse()
var err error
var colors map[string]color.RGBA
var colors *Colors
if colors, err = ParseColors(colorsfile); err != nil {
log.Fatalf("Cannot open color file: %s", err)
@ -70,7 +69,7 @@ func main() {
count := func(block *common.Block) {
numBlocks++
bytesTotal += int64(len(block.Data))
if db, err := NewDecodedBlock(block.Data); err == nil {
if db, err := NewDecodedBlock(block.Data, colors.NameIndex); err == nil {
versions[db.Version]++
} else {
log.Printf("WARN: Decoding block failed: %s", err)