Write image tile to PNG file.

This commit is contained in:
Sascha L. Teichmann 2014-09-10 18:58:12 +02:00
parent 26aabeb6d5
commit 4fd0722ade
3 changed files with 67 additions and 27 deletions

24
tilemapper/image.go Normal file
View File

@ -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
}

View File

@ -7,6 +7,7 @@ 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"
@ -19,9 +20,11 @@ func main() {
x, y, z int x, y, z int
width, height, depth int width, height, depth int
colorsfile string colorsfile string
outfile string
) )
flag.IntVar(&port, "port", 6379, "port to of mtredisalize server") 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.StringVar(&host, "host", "localhost", "host to mtredisalize server")
flag.IntVar(&x, "x", -9, "x of query cuboid") flag.IntVar(&x, "x", -9, "x of query cuboid")
flag.IntVar(&y, "y", -75, "y 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(&height, "h", 18, "height of query cuboid (shorthand)")
flag.IntVar(&depth, "d", 150, "depth 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(&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() flag.Parse()
@ -43,8 +48,6 @@ func main() {
log.Fatalf("Cannot open color file: %s", err) log.Fatalf("Cannot open color file: %s", err)
} }
_ = colors
address := fmt.Sprintf("%s:%d", host, port) address := fmt.Sprintf("%s:%d", host, port)
var client *Client var client *Client
@ -59,32 +62,23 @@ func main() {
c1 := common.Coord{X: q1x, Y: q1y, Z: q1z} c1 := common.Coord{X: q1x, Y: q1y, Z: q1z}
c2 := common.Coord{X: q2x, Y: q2y, Z: q2z} c2 := common.Coord{X: q2x, Y: q2y, Z: q2z}
cuboid := common.Cuboid{P1: common.MinCoord(c1, c2), P2: common.MaxCoord(c1, c2)} cuboid := common.Cuboid{P1: common.MinCoord(c1, c2), P2: common.MaxCoord(c1, c2)}
log.Printf("query cuboid: %s", cuboid)
numBlocks := 0 renderer := NewRenderer(q1x, q1z, width, height)
bytesTotal := int64(0)
versions := make(map[byte]int) drawBlock := func(block *common.Block) {
if err := renderer.RenderBlock(block, colors.NameIndex); err != nil {
count := func(block *common.Block) { log.Printf("WARN: rendering block failed: %s", err)
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)
} }
} }
if err = client.QueryCuboid(cuboid, count); err != nil { if err = client.QueryCuboid(cuboid, drawBlock); err != nil {
log.Fatalf("query failed: %s", err) log.Fatalf("query failed: %s", err)
} }
fmt.Printf("num of blocks: %d\n", numBlocks) image := renderer.CreateImage(
fmt.Printf("num of bytes: %d\n", bytesTotal) colors.Colors, color.RGBA{R: 0, G: 0, B: 0, A: 0xff})
fmt.Println("uses block versions:") if err = SaveAsPNG(outfile, image); err != nil {
for version, count := range versions { log.Fatalf("writing image failed: %s", err)
fmt.Printf("%d: %d\n", version, count)
} }
} }

View File

@ -5,6 +5,8 @@
package main package main
import ( import (
"image"
"image/color"
"math" "math"
"bitbucket.org/s_l_teichmann/mtredisalize/common" "bitbucket.org/s_l_teichmann/mtredisalize/common"
@ -50,24 +52,26 @@ func NewRenderer(xOfs, zOfs int16, width, height int) (renderer *Renderer) {
return return
} }
func (r *Renderer) RenderBlock(coord common.Coord, data []byte, nameIndex map[string]int) (err error) { func (r *Renderer) RenderBlock(block *common.Block, nameIndex map[string]int) (err error) {
x := coord.X + r.xOfs // fmt.Printf("%d %d\n", r.xOfs, r.zOfs)
z := coord.Z + r.zOfs // fmt.Printf("%d %d\n", block.Coord.X, block.Coord.Z)
dimIndex := (x << 4) + 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 // We do not need to render the block if the whole 16x16 area
// is already filled and the block is strictly below. // 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 return
} }
var db *DecodedBlock var db *DecodedBlock
if db, err = NewDecodedBlock(data, nameIndex); err != nil { if db, err = NewDecodedBlock(block.Data, nameIndex); err != nil {
return return
} }
_ = db _ = db
ay := int32(coord.Y) << 4 ay := int32(block.Coord.Y) << 4
_ = ay _ = ay
@ -81,3 +85,21 @@ func (r *Renderer) RenderBlock(coord common.Coord, data []byte, nameIndex map[st
return 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
}