Pulled over empty tile caching from coverage-exp branch.

This commit is contained in:
Sascha L. Teichmann 2015-07-22 12:06:21 +02:00
parent 39df42b675
commit 0f10fce1f0
4 changed files with 50 additions and 11 deletions

View File

@ -9,7 +9,7 @@ on YouTube.
A live map of an online world can be viewed [here](http://maps.mt.sha-bang.de/). A live map of an online world can be viewed [here](http://maps.mt.sha-bang.de/).
See [COMPILE](https://bitbucket.org/s_l_teichmann/mtsatellite/src/default/COMPILE.md) how to compile See [COMPILE](https://bitbucket.org/s_l_teichmann/mtsatellite/src/default/COMPILE.md) how to compile
MTSatellite. Essentially you need Go 1.3 (or higher) and a GNU/Linux system. MTSatellite. Essentially you need Go 1.4 (or higher) and a GNU/Linux system.
See [SETUP](https://bitbucket.org/s_l_teichmann/mtsatellite/src/default/SETUP.md) how to bring See [SETUP](https://bitbucket.org/s_l_teichmann/mtsatellite/src/default/SETUP.md) how to bring
MTSatellite to life. MTSatellite to life.

View File

@ -6,6 +6,7 @@ package common
import ( import (
"image/color" "image/color"
"io/ioutil"
"log" "log"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -41,6 +42,8 @@ var tileDepths = [...][2]int16{
{-1024, -257}, {-1024, -257},
{-1934, -1025}} {-1934, -1025}}
var BackgroundColor = color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}
type BaseTileCreator struct { type BaseTileCreator struct {
client *RedisClient client *RedisClient
colors *Colors colors *Colors
@ -48,6 +51,7 @@ type BaseTileCreator struct {
yOrder *YOrder yOrder *YOrder
baseDir string baseDir string
update bool update bool
emptyImage []byte
} }
func NewBaseTileCreator( func NewBaseTileCreator(
@ -116,11 +120,25 @@ func (btc *BaseTileCreator) CreateTile(x, z int16, i, j int) error {
oareas, nareas = nareas, oareas[0:0] oareas, nareas = nareas, oareas[0:0]
} }
path := filepath.Join(btc.baseDir, strconv.Itoa(i), strconv.Itoa(j)+".png")
// Empty images are likely to be produced during seeding.
if !btc.update && btc.renderer.IsEmpty() {
// To avoid redundant encoding cache the resulting empty image.
if btc.emptyImage == nil {
var err error
m := BackgroundImage((tileWidth-2)*16, (tileHeight-2)*16, BackgroundColor)
if btc.emptyImage, err = EncodeToMem(m); err != nil {
return err
}
}
log.Printf("Writing empty (%d, %d) to file %s\n", x, z, path)
return ioutil.WriteFile(path, btc.emptyImage, 0666)
}
image := btc.renderer.CreateShadedImage( image := btc.renderer.CreateShadedImage(
16, 16, (tileWidth-2)*16, (tileHeight-2)*16, 16, 16, (tileWidth-2)*16, (tileHeight-2)*16,
btc.colors, color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}) btc.colors, BackgroundColor)
path := filepath.Join(btc.baseDir, strconv.Itoa(i), strconv.Itoa(j)+".png")
log.Printf("Writing (%d, %d) to file %s\n", x, z, path) log.Printf("Writing (%d, %d) to file %s\n", x, z, path)

View File

@ -6,6 +6,7 @@ package common
import ( import (
"bufio" "bufio"
"bytes"
"errors" "errors"
"image" "image"
"image/png" "image/png"
@ -35,6 +36,15 @@ func nextSuffix() string {
return strconv.Itoa(int(1e9 + r%1e9))[1:] return strconv.Itoa(int(1e9 + r%1e9))[1:]
} }
func EncodeToMem(img image.Image) ([]byte, error) {
var buf bytes.Buffer
enc := png.Encoder{png.BestCompression}
if err := enc.Encode(&buf, img); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func SaveAsPNG(path string, img image.Image) (err error) { func SaveAsPNG(path string, img image.Image) (err error) {
var file *os.File var file *os.File
if file, err = os.Create(path); err != nil { if file, err = os.Create(path); err != nil {

View File

@ -8,6 +8,7 @@ import (
"container/heap" "container/heap"
"image" "image"
"image/color" "image/color"
"image/draw"
"math" "math"
) )
@ -195,6 +196,10 @@ func (r *Renderer) IsFilled() bool {
return true return true
} }
func (r *Renderer) IsEmpty() bool {
return r.SolidBlocks == 0 && r.TransparentBlocks == 0
}
func (r *Renderer) RenderBlock(block *Block, colors *Colors) (err error) { func (r *Renderer) RenderBlock(block *Block, colors *Colors) (err error) {
bx := block.Coord.X - r.xOfs bx := block.Coord.X - r.xOfs
@ -406,6 +411,12 @@ func safeColor(x int32) uint8 {
} }
} }
func BackgroundImage(width, height int, bg color.RGBA) *image.RGBA {
m := image.NewRGBA(image.Rect(0, 0, width, height))
draw.Draw(m, m.Bounds(), &image.Uniform{bg}, image.ZP, draw.Src)
return m
}
func (r *Renderer) CreateShadedImage( func (r *Renderer) CreateShadedImage(
xOfs, zOfs, width, height int, xOfs, zOfs, width, height int,
cols *Colors, background color.RGBA) *image.RGBA { cols *Colors, background color.RGBA) *image.RGBA {