diff --git a/tilemapper/image.go b/tilemapper/image.go new file mode 100644 index 0000000..db03040 --- /dev/null +++ b/tilemapper/image.go @@ -0,0 +1,24 @@ +// 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 ( + "bufio" + "image" + "image/png" + "os" +) + +func SaveAsPNG(path string, img image.Image) (err error) { + var file *os.File + if file, err = os.Create(path); err != nil { + return + } + writer := bufio.NewWriter(file) + err = png.Encode(writer, img) + writer.Flush() + file.Close() + return +} diff --git a/tilemapper/main.go b/tilemapper/main.go index 938867d..237082b 100644 --- a/tilemapper/main.go +++ b/tilemapper/main.go @@ -7,6 +7,7 @@ package main import ( "flag" "fmt" + "image/color" "log" "bitbucket.org/s_l_teichmann/mtredisalize/common" @@ -19,9 +20,11 @@ func main() { x, y, z int width, height, depth int colorsfile string + outfile string ) flag.IntVar(&port, "port", 6379, "port to of mtredisalize server") + flag.IntVar(&port, "p", 6379, "port to of mtredisalize server (shorthand)") flag.StringVar(&host, "host", "localhost", "host to mtredisalize server") flag.IntVar(&x, "x", -9, "x of query cuboid") flag.IntVar(&y, "y", -75, "y of query cuboid") @@ -33,6 +36,8 @@ func main() { flag.IntVar(&height, "h", 18, "height of query cuboid (shorthand)") flag.IntVar(&depth, "d", 150, "depth of query cuboid (shorthand)") flag.StringVar(&colorsfile, "colors", "colors.txt", "definition of colors") + flag.StringVar(&outfile, "output", "out.png", "image file of result") + flag.StringVar(&outfile, "o", "out.png", "image file of result (shorthand)") flag.Parse() @@ -43,8 +48,6 @@ func main() { log.Fatalf("Cannot open color file: %s", err) } - _ = colors - address := fmt.Sprintf("%s:%d", host, port) var client *Client @@ -59,32 +62,23 @@ func main() { c1 := common.Coord{X: q1x, Y: q1y, Z: q1z} c2 := common.Coord{X: q2x, Y: q2y, Z: q2z} cuboid := common.Cuboid{P1: common.MinCoord(c1, c2), P2: common.MaxCoord(c1, c2)} - log.Printf("query cuboid: %s", cuboid) - numBlocks := 0 - bytesTotal := int64(0) + renderer := NewRenderer(q1x, q1z, width, height) - versions := make(map[byte]int) - - count := func(block *common.Block) { - numBlocks++ - bytesTotal += int64(len(block.Data)) - if db, err := NewDecodedBlock(block.Data, colors.NameIndex); err == nil { - versions[db.Version]++ - } else { - log.Printf("WARN: Decoding block failed: %s", err) + drawBlock := func(block *common.Block) { + if err := renderer.RenderBlock(block, colors.NameIndex); err != nil { + log.Printf("WARN: rendering block failed: %s", err) } } - if err = client.QueryCuboid(cuboid, count); err != nil { + if err = client.QueryCuboid(cuboid, drawBlock); err != nil { log.Fatalf("query failed: %s", err) } - fmt.Printf("num of blocks: %d\n", numBlocks) - fmt.Printf("num of bytes: %d\n", bytesTotal) + image := renderer.CreateImage( + colors.Colors, color.RGBA{R: 0, G: 0, B: 0, A: 0xff}) - fmt.Println("uses block versions:") - for version, count := range versions { - fmt.Printf("%d: %d\n", version, count) + if err = SaveAsPNG(outfile, image); err != nil { + log.Fatalf("writing image failed: %s", err) } } diff --git a/tilemapper/renderer.go b/tilemapper/renderer.go index 7b05dca..296e5cc 100644 --- a/tilemapper/renderer.go +++ b/tilemapper/renderer.go @@ -5,6 +5,8 @@ package main import ( + "image" + "image/color" "math" "bitbucket.org/s_l_teichmann/mtredisalize/common" @@ -50,24 +52,26 @@ func NewRenderer(xOfs, zOfs int16, width, height int) (renderer *Renderer) { return } -func (r *Renderer) RenderBlock(coord common.Coord, data []byte, nameIndex map[string]int) (err error) { - x := coord.X + r.xOfs - z := coord.Z + r.zOfs - dimIndex := (x << 4) + z +func (r *Renderer) RenderBlock(block *common.Block, nameIndex map[string]int) (err error) { + // fmt.Printf("%d %d\n", r.xOfs, r.zOfs) + // fmt.Printf("%d %d\n", block.Coord.X, block.Coord.Z) + bx := block.Coord.X - r.xOfs + bz := block.Coord.Z - r.zOfs + dimIndex := (bx << 4) + bz // We do not need to render the block if the whole 16x16 area // is already filled and the block is strictly below. - if r.filled[dimIndex] == 0xff && r.minYs[dimIndex] > coord.Y { + if r.filled[dimIndex] == 0xff && r.minYs[dimIndex] > block.Coord.Y { return } var db *DecodedBlock - if db, err = NewDecodedBlock(data, nameIndex); err != nil { + if db, err = NewDecodedBlock(block.Data, nameIndex); err != nil { return } _ = db - ay := int32(coord.Y) << 4 + ay := int32(block.Coord.Y) << 4 _ = ay @@ -81,3 +85,21 @@ func (r *Renderer) RenderBlock(coord common.Coord, data []byte, nameIndex map[st return } + +func (r *Renderer) CreateImage(colors []color.RGBA, background color.RGBA) *image.RGBA { + pw, ph := r.width<<4, r.height<<4 + image := image.NewRGBA(image.Rect(0, 0, pw, ph)) + ofs, numCols := 0, int32(len(colors)) + for z := 0; z < ph; z++ { + for x := 0; x < pw; x++ { + colIdx := r.cBuffer[ofs] + if colIdx >= 0 && colIdx < numCols { + image.Set(x, z, colors[colIdx]) + } else { + image.Set(x, z, background) + } + ofs++ + } + } + return image +}