Moved bloch decoding and rendering from tilemapper to common.

This commit is contained in:
Sascha L. Teichmann 2014-09-12 20:22:34 +02:00
parent a73e9b35c5
commit b445cfb33a
4 changed files with 208 additions and 220 deletions

View File

@ -1,11 +1,24 @@
package common
import (
"compress/zlib"
"encoding/binary"
"errors"
"io"
"io/ioutil"
"log"
)
// Error returned if a Producer has run to its end.
var ErrNoMoreBlocks = errors.New("No more blocks.")
var (
ErrNoMoreBlocks = errors.New("No more blocks.")
ErrMapContentSizeMismatch = errors.New("Content size does not match.")
)
const (
mapBlockSize = 16
nodeCount = mapBlockSize * mapBlockSize * mapBlockSize
)
type (
// Block data from Minetest database.
@ -28,4 +41,185 @@ type (
// Closes the open database connections.
Close() error
}
DecodedBlock struct {
Version byte
MapContent []byte
AirId int32
IgnoreId int32
IndexMap map[int32]int32
}
)
// The content of the map and the meta data are compressed with zlib.
// Unfortunately the byte length of this two structures are not stored
// explicitly in the block data. To access the informations behind
// them (e.g. the node id mappings) we have to count the bytes consumed
// by the zlib reader and continue our extraction process behind this
// offset. posBuf implements such a counting reader source.
type posBuf struct {
Data []byte
Pos int
}
func NewDecodedBlock(data []byte, nameIndex map[string]int32) (db *DecodedBlock, err error) {
version := data[0]
contentWidth := int(data[2])
paramsWidth := int(data[3])
uncompressedLen := nodeCount * (contentWidth + paramsWidth)
offset := 2
if version >= 22 {
offset = 4
}
buf := NewposBuf(data[offset:])
var zr io.ReadCloser
if zr, err = zlib.NewReader(buf); err != nil {
return
}
var mapContent []byte
mapContent, err = ioutil.ReadAll(zr)
zr.Close()
if err != nil {
return
}
if uncompressedLen != len(mapContent) {
err = ErrMapContentSizeMismatch
return
}
offset += buf.Pos
buf.Pos = 0
buf.Data = data[offset:]
if zr, err = zlib.NewReader(buf); err != nil {
return
}
// Discard the meta data.
_, err = io.Copy(ioutil.Discard, zr)
zr.Close()
if err != nil {
return
}
offset += buf.Pos
switch {
case version <= 21:
offset += 2
case version == 23:
offset++
case version == 24:
ver := data[offset]
offset++
if ver == 1 {
num := int(binary.BigEndian.Uint16(data[offset:]))
offset += 2 + 10*num
}
}
offset++
numStaticObjects := int(binary.BigEndian.Uint16(data[offset:]))
offset += 2
for i := 0; i < numStaticObjects; i++ {
offset += 13
dataSize := int(binary.BigEndian.Uint16(data[offset:]))
offset += dataSize + 2
}
offset += 4
airId, ignoreId := int32(-1), int32(-1)
indexMap := make(map[int32]int32)
if version >= 22 {
offset++
numMappings := int(binary.BigEndian.Uint16(data[offset:]))
offset += 2
for i := 0; i < numMappings; i++ {
nodeId := int32(binary.BigEndian.Uint16(data[offset:]))
offset += 2
nameLen := int(binary.BigEndian.Uint16(data[offset:]))
offset += 2
name := string(data[offset : offset+nameLen])
offset += nameLen
switch name {
case "air":
airId = nodeId
case "ignore":
ignoreId = nodeId
default:
if index, found := nameIndex[name]; found {
indexMap[nodeId] = index
} else {
log.Printf("Missing color entry for %s.", name)
}
}
}
}
db = &DecodedBlock{
Version: version,
MapContent: mapContent,
AirId: airId,
IgnoreId: ignoreId,
IndexMap: indexMap}
return
}
func (db *DecodedBlock) Content(x, y, z int) (content int32, found bool) {
pos := z<<8 + y<<4 + x
switch {
case db.Version >= 24:
pos <<= 1
content = int32(db.MapContent[pos])<<8 | int32(db.MapContent[pos+1])
case db.Version >= 20:
if db.MapContent[pos] <= 0x80 {
content = int32(db.MapContent[pos])
} else {
content = int32(db.MapContent[pos])<<4 | int32(db.MapContent[pos+0x2000])>>4
}
default:
return
}
if content != db.AirId && content != db.IgnoreId {
content, found = db.IndexMap[content]
}
return
}
func NewposBuf(data []byte) *posBuf {
return &posBuf{Data: data}
}
func (pb *posBuf) Read(p []byte) (int, error) {
pl := len(p)
ml := len(pb.Data)
if pb.Pos >= ml {
return 0, io.EOF
}
rest := ml - pb.Pos
if pl > rest {
copy(p, pb.Data[pb.Pos:])
pb.Pos = ml
return rest, io.EOF
}
copy(p, pb.Data[pb.Pos:pb.Pos+pl])
pb.Pos += pl
return pl, nil
}
func (pb *posBuf) ReadByte() (byte, error) {
if pb.Pos >= len(pb.Data) {
return 0, io.EOF
}
c := pb.Data[pb.Pos]
pb.Pos++
return c, nil
}

View File

@ -2,15 +2,13 @@
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.
package main
package common
import (
"container/heap"
"image"
"image/color"
"math"
"bitbucket.org/s_l_teichmann/mtredisalize/common"
)
type Renderer struct {
@ -27,14 +25,14 @@ type Renderer struct {
type YOrder struct {
Renderer *Renderer
blocks []*common.Block
blocks []*Block
capacity int
}
func NewYOrder(renderer *Renderer, capacity int) *YOrder {
return &YOrder{
Renderer: renderer,
blocks: make([]*common.Block, 0, capacity),
blocks: make([]*Block, 0, capacity),
capacity: capacity}
}
@ -52,8 +50,8 @@ func copyData(data []byte) []byte {
return ndata
}
func (yo *YOrder) RenderBlock(block *common.Block, nameIndex map[string]int32) (err error) {
var nblock *common.Block
func (yo *YOrder) RenderBlock(block *Block, nameIndex map[string]int32) (err error) {
var nblock *Block
if len(yo.blocks) == yo.capacity {
oblock := yo.blocks[0]
if oblock.Coord.Y < block.Coord.Y {
@ -74,7 +72,7 @@ func (yo *YOrder) RenderBlock(block *common.Block, nameIndex map[string]int32) (
oblock.Coord = block.Coord
nblock = oblock
} else {
nblock = &common.Block{Coord: block.Coord, Data: copyData(block.Data)}
nblock = &Block{Coord: block.Coord, Data: copyData(block.Data)}
}
heap.Push(yo, nblock)
@ -83,7 +81,7 @@ func (yo *YOrder) RenderBlock(block *common.Block, nameIndex map[string]int32) (
func (yo *YOrder) Drain(nameIndex map[string]int32) (err error) {
for len(yo.blocks) > 0 {
if err = yo.Renderer.RenderBlock(heap.Pop(yo).(*common.Block), nameIndex); err != nil {
if err = yo.Renderer.RenderBlock(heap.Pop(yo).(*Block), nameIndex); err != nil {
return
}
}
@ -104,7 +102,7 @@ func (yo *YOrder) Less(i, j int) bool {
}
func (yo *YOrder) Push(x interface{}) {
yo.blocks = append(yo.blocks, x.(*common.Block))
yo.blocks = append(yo.blocks, x.(*Block))
}
func (yo *YOrder) Pop() (x interface{}) {
@ -145,7 +143,7 @@ func (r *Renderer) IsFilled() bool {
return r.filled == r.width<<4*r.height<<4
}
func (r *Renderer) RenderBlock(block *common.Block, nameIndex map[string]int32) (err error) {
func (r *Renderer) RenderBlock(block *Block, nameIndex map[string]int32) (err error) {
bx := block.Coord.X - r.xOfs
bz := block.Coord.Z - r.zOfs

View File

@ -1,204 +0,0 @@
// Copyright 2014 by Sascha L. Teichmann
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.
package main
import (
"compress/zlib"
"encoding/binary"
"errors"
"io"
"io/ioutil"
"log"
)
var (
ErrMapContentSizeMismatch = errors.New("Content size does not match.")
)
const (
mapBlockSize = 16
nodeCount = mapBlockSize * mapBlockSize * mapBlockSize
)
type DecodedBlock struct {
Version byte
MapContent []byte
AirId int32
IgnoreId int32
IndexMap map[int32]int32
}
// The content of the map and the meta data are compressed with zlib.
// Unfortunately the byte length of this two structures are not stored
// explicitly in the block data. To access the informations behind
// them (e.g. the node id mappings) we have to count the bytes consumed
// by the zlib reader and continue our extraction process behind this
// offset. PosBuf implements such a counting reader source.
type PosBuf struct {
Data []byte
Pos int
}
func NewDecodedBlock(data []byte, nameIndex map[string]int32) (db *DecodedBlock, err error) {
version := data[0]
contentWidth := int(data[2])
paramsWidth := int(data[3])
uncompressedLen := nodeCount * (contentWidth + paramsWidth)
offset := 2
if version >= 22 {
offset = 4
}
buf := NewPosBuf(data[offset:])
var zr io.ReadCloser
if zr, err = zlib.NewReader(buf); err != nil {
return
}
var mapContent []byte
mapContent, err = ioutil.ReadAll(zr)
zr.Close()
if err != nil {
return
}
if uncompressedLen != len(mapContent) {
err = ErrMapContentSizeMismatch
return
}
offset += buf.Pos
buf.Pos = 0
buf.Data = data[offset:]
if zr, err = zlib.NewReader(buf); err != nil {
return
}
// Discard the meta data.
_, err = io.Copy(ioutil.Discard, zr)
zr.Close()
if err != nil {
return
}
offset += buf.Pos
switch {
case version <= 21:
offset += 2
case version == 23:
offset++
case version == 24:
ver := data[offset]
offset++
if ver == 1 {
num := int(binary.BigEndian.Uint16(data[offset:]))
offset += 2 + 10*num
}
}
offset++
numStaticObjects := int(binary.BigEndian.Uint16(data[offset:]))
offset += 2
for i := 0; i < numStaticObjects; i++ {
offset += 13
dataSize := int(binary.BigEndian.Uint16(data[offset:]))
offset += dataSize + 2
}
offset += 4
airId, ignoreId := int32(-1), int32(-1)
indexMap := make(map[int32]int32)
if version >= 22 {
offset++
numMappings := int(binary.BigEndian.Uint16(data[offset:]))
offset += 2
for i := 0; i < numMappings; i++ {
nodeId := int32(binary.BigEndian.Uint16(data[offset:]))
offset += 2
nameLen := int(binary.BigEndian.Uint16(data[offset:]))
offset += 2
name := string(data[offset : offset+nameLen])
offset += nameLen
switch name {
case "air":
airId = nodeId
case "ignore":
ignoreId = nodeId
default:
if index, found := nameIndex[name]; found {
indexMap[nodeId] = index
} else {
log.Printf("Missing color entry for %s.", name)
}
}
}
}
db = &DecodedBlock{
Version: version,
MapContent: mapContent,
AirId: airId,
IgnoreId: ignoreId,
IndexMap: indexMap}
return
}
func (db *DecodedBlock) Content(x, y, z int) (content int32, found bool) {
pos := z<<8 + y<<4 + x
switch {
case db.Version >= 24:
pos <<= 1
content = int32(db.MapContent[pos])<<8 | int32(db.MapContent[pos+1])
case db.Version >= 20:
if db.MapContent[pos] <= 0x80 {
content = int32(db.MapContent[pos])
} else {
content = int32(db.MapContent[pos])<<4 | int32(db.MapContent[pos+0x2000])>>4
}
default:
return
}
if content != db.AirId && content != db.IgnoreId {
content, found = db.IndexMap[content]
}
return
}
func NewPosBuf(data []byte) *PosBuf {
return &PosBuf{Data: data}
}
func (pb *PosBuf) Read(p []byte) (int, error) {
pl := len(p)
ml := len(pb.Data)
if pb.Pos >= ml {
return 0, io.EOF
}
rest := ml - pb.Pos
if pl > rest {
copy(p, pb.Data[pb.Pos:])
pb.Pos = ml
return rest, io.EOF
}
copy(p, pb.Data[pb.Pos:pb.Pos+pl])
pb.Pos += pl
return pl, nil
}
func (pb *PosBuf) ReadByte() (byte, error) {
if pb.Pos >= len(pb.Data) {
return 0, io.EOF
}
c := pb.Data[pb.Pos]
pb.Pos++
return c, nil
}

View File

@ -70,8 +70,8 @@ func main() {
q1x, q1y, q1z := int16(x), int16(y), int16(z)
q2x, q2y, q2z := q1x+int16(width)-1, q1y+int16(depth)-1, q1z+int16(height)-1
renderer := NewRenderer(q1x, q1z, width, height)
yOrder := NewYOrder(renderer, 512)
renderer := common.NewRenderer(q1x, q1z, width, height)
yOrder := common.NewYOrder(renderer, 512)
numBlocks := 0
drawBlock := func(block *common.Block) {
@ -115,6 +115,6 @@ func main() {
log.Fatalf("writing image failed: %s", err)
}
fmt.Printf("num blocks: %d\n", numBlocks)
fmt.Printf("rejected blocks: %d\n", renderer.Rejected)
log.Printf("num blocks: %d\n", numBlocks)
log.Printf("rejected blocks: %d\n", renderer.Rejected)
}