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" "errors"
"io" "io"
"io/ioutil" "io/ioutil"
"log"
) )
var ( var (
@ -26,7 +27,7 @@ type DecodedBlock struct {
MapContent []byte MapContent []byte
AirId int AirId int
IgnoreId int IgnoreId int
NameMap map[int]string IndexMap map[int]int
} }
type PosBuf struct { type PosBuf struct {
@ -34,7 +35,7 @@ type PosBuf struct {
Pos int 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] version := data[0]
contentWidth := int(data[2]) contentWidth := int(data[2])
@ -106,7 +107,7 @@ func NewDecodedBlock(data []byte) (db *DecodedBlock, err error) {
offset += 4 offset += 4
airId, ignoreId := -1, -1 airId, ignoreId := -1, -1
nameMap := make(map[int]string) indexMap := make(map[int]int)
if version >= 22 { if version >= 22 {
offset++ offset++
numMappings := int(binary.BigEndian.Uint16(data[offset:])) numMappings := int(binary.BigEndian.Uint16(data[offset:]))
@ -124,7 +125,11 @@ func NewDecodedBlock(data []byte) (db *DecodedBlock, err error) {
case "ignore": case "ignore":
ignoreId = nodeId ignoreId = nodeId
default: 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, MapContent: mapContent,
AirId: airId, AirId: airId,
IgnoreId: ignoreId, IgnoreId: ignoreId,
NameMap: nameMap} IndexMap: indexMap}
return return
} }

View File

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

File diff suppressed because it is too large Load Diff

View File

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