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

View File

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