This commit is contained in:
Sascha L. Teichmann 2016-04-24 11:21:07 +02:00
commit cf14aed031
10 changed files with 124 additions and 46 deletions

View File

@ -5,6 +5,7 @@
package main
import (
"image/color"
"log"
"os"
"path/filepath"
@ -39,7 +40,7 @@ func createBaseLevel(
address string,
xMin, yMin, zMin, xMax, yMax, zMax int,
transparent bool, transparentDim float32,
colorsFile, outDir string,
colorsFile string, bg color.RGBA, outDir string,
numWorkers int) (err error) {
var colors *common.Colors
@ -65,7 +66,7 @@ func createBaseLevel(
}
done.Add(1)
btc := common.NewBaseTileCreator(
client, colors,
client, colors, bg,
int16(yMin), int16(yMax),
transparent, baseDir, false)
go createTiles(btc, jobs, &done)

View File

@ -19,6 +19,7 @@ func main() {
xMin, yMin, zMin int
xMax, yMax, zMax int
colorsFile string
bgColor string
outDir string
numWorkers int
skipBaseLevel bool
@ -28,6 +29,8 @@ func main() {
version bool
)
defaultBgColor := common.ColorToHex(common.BackgroundColor)
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")
@ -38,6 +41,8 @@ func main() {
flag.IntVar(&zMin, "zmin", -1933, "z min of the area to tile")
flag.IntVar(&zMax, "zmax", 1932, "z max of the area to tile")
flag.StringVar(&colorsFile, "colors", "colors.txt", "definition of colors")
flag.StringVar(&bgColor, "background", defaultBgColor, "background color")
flag.StringVar(&bgColor, "bg", defaultBgColor, "background color (shorthand)")
flag.StringVar(&outDir, "output-dir", "map", "directory with the resulting image tree")
flag.StringVar(&outDir, "o", "map", "directory with the resulting image tree")
flag.IntVar(&numWorkers, "workers", 1, "number of workers")
@ -62,6 +67,8 @@ func main() {
common.PrintVersionAndExit()
}
bg := common.ParseColorDefault(bgColor, common.BackgroundColor)
if !skipBaseLevel {
td := common.Clamp32f(float32(transparentDim/100.0), 0.0, 1.0)
address := fmt.Sprintf("%s:%d", host, port)
@ -69,14 +76,15 @@ func main() {
address,
xMin, yMin, zMin, xMax, yMax, zMax,
transparent, td,
colorsFile,
colorsFile, bg,
outDir,
numWorkers); err != nil {
log.Fatalf("Creating base level tiles failed: %s", err)
}
}
if !skipPyramid {
if err := createPyramid(outDir, numWorkers); err != nil {
pc := pyramidCreator{numWorkers: numWorkers, outDir: outDir, bg: bg}
if err := pc.create(); err != nil {
log.Fatalf("Creating pyramid tiles failed: %s", err)
}
}

View File

@ -6,6 +6,7 @@ package main
import (
"image"
"image/color"
"image/draw"
"io/ioutil"
"log"
@ -21,6 +22,12 @@ import (
"github.com/bamiaux/rez"
)
type pyramidCreator struct {
numWorkers int
outDir string
bg color.RGBA
}
func findMaxDir(files []os.FileInfo) (min, max int) {
min, max = math.MaxInt32, math.MinInt32
for _, file := range files {
@ -64,7 +71,10 @@ type pyramidJob struct {
dst string
}
func createParentLevel(oldDir string, jobs chan pyramidJob) (newDir string, err error) {
func (pc *pyramidCreator) createParentLevel(
oldDir string,
jobs chan pyramidJob) (newDir string, err error) {
oldName := filepath.Base(oldDir)
var oldLevel int
@ -155,11 +165,14 @@ var dps = [4]image.Point{
image.Pt(256, 256),
image.Pt(256, 0)}
func fuseTile(scratch, resized *image.RGBA, conv rez.Converter, job *pyramidJob) error {
func (pc *pyramidCreator) fuseTile(
scratch, resized *image.RGBA,
conv rez.Converter,
job *pyramidJob) error {
for i, path := range job.src {
img := common.LoadPNG(path)
img := common.LoadPNG(path, pc.bg)
sr := clipRect(img.Bounds())
r := sr.Sub(sr.Min).Add(dps[i])
@ -176,7 +189,7 @@ func fuseTile(scratch, resized *image.RGBA, conv rez.Converter, job *pyramidJob)
return common.SaveAsPNG(job.dst, resized)
}
func fuseTiles(jobs chan pyramidJob, done *sync.WaitGroup) {
func (pc *pyramidCreator) fuseTiles(jobs chan pyramidJob, done *sync.WaitGroup) {
defer done.Done()
scratch := image.NewRGBA(image.Rect(0, 0, 512, 512))
resized := image.NewRGBA(image.Rect(0, 0, 256, 256))
@ -194,41 +207,41 @@ func fuseTiles(jobs chan pyramidJob, done *sync.WaitGroup) {
}
for job := range jobs {
if err := fuseTile(scratch, resized, conv, &job); err != nil {
if err := pc.fuseTile(scratch, resized, conv, &job); err != nil {
log.Printf("WARN: Writing image failed: %s\n", err)
}
}
}
func createPyramid(outDir string, numWorker int) (err error) {
func (pc *pyramidCreator) create() (err error) {
for oldDir := filepath.Join(outDir, baseLevelDir); oldDir != ""; {
if oldDir, err = createLevel(oldDir, numWorker); err != nil {
for oldDir := filepath.Join(pc.outDir, baseLevelDir); oldDir != ""; {
if oldDir, err = pc.createLevel(oldDir); err != nil {
return
}
}
return
}
func createLevel(oldDir string, numWorker int) (newDir string, err error) {
func (pc *pyramidCreator) createLevel(oldDir string) (string, error) {
jobs := make(chan pyramidJob)
var done sync.WaitGroup
for i := 0; i < numWorker; i++ {
for i := 0; i < pc.numWorkers; i++ {
done.Add(1)
go fuseTiles(jobs, &done)
go pc.fuseTiles(jobs, &done)
}
newDir, err = createParentLevel(oldDir, jobs)
newDir, err := pc.createParentLevel(oldDir, jobs)
close(jobs)
if err != nil {
return
return newDir, err
}
done.Wait()
return
return newDir, err
}

View File

@ -8,7 +8,6 @@ import (
"flag"
"fmt"
"image"
"image/color"
"log"
"os"
"runtime/pprof"
@ -23,6 +22,7 @@ func main() {
x, y, z int
width, height, depth int
colorsfile string
bgColor string
outfile string
shaded bool
transparent bool
@ -31,6 +31,8 @@ func main() {
version bool
)
defaultBgColor := common.ColorToHex(common.BackgroundColor)
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")
@ -44,6 +46,8 @@ func main() {
flag.IntVar(&height, "h", 16, "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(&bgColor, "background", defaultBgColor, "background color")
flag.StringVar(&bgColor, "bg", defaultBgColor, "background color (shorthand)")
flag.StringVar(&outfile, "output", "out.png", "image file of result")
flag.StringVar(&outfile, "o", "out.png", "image file of result (shorthand)")
flag.BoolVar(&shaded, "shaded", true, "draw relief")
@ -60,6 +64,8 @@ func main() {
common.PrintVersionAndExit()
}
bg := common.ParseColorDefault(bgColor, common.BackgroundColor)
if cpuProfile != "" {
f, err := os.Create(cpuProfile)
if err != nil {
@ -69,9 +75,8 @@ func main() {
defer pprof.StopCPUProfile()
}
var err error
var colors *common.Colors
var err error
if colors, err = common.ParseColors(colorsfile); err != nil {
log.Fatalf("Cannot open color file: %s", err)
}
@ -134,10 +139,9 @@ func main() {
if shaded {
image = renderer.CreateShadedImage(
16, 16, (width-2)*16, (height-2)*16,
colors, color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff})
colors, bg)
} else {
image = renderer.CreateImage(
colors.Colors, color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff})
image = renderer.CreateImage(colors.Colors, bg)
}
if err = common.SaveAsPNG(outfile, image); err != nil {

View File

@ -25,6 +25,7 @@ func main() {
redisPort int
redisHost string
colorsFile string
bgColor string
workers int
transparent bool
transparentDim float64
@ -35,6 +36,9 @@ func main() {
yMin int
yMax int
)
defaultBgColor := common.ColorToHex(common.BackgroundColor)
flag.IntVar(&webPort, "web-port", 8808, "port of the web server")
flag.IntVar(&webPort, "p", 8808, "port of the web server (shorthand)")
flag.StringVar(&webHost, "web-host", "localhost", "address to bind web server")
@ -54,6 +58,8 @@ func main() {
flag.IntVar(&workers, "workers", 1, "number of workers to render tiles")
flag.StringVar(&colorsFile, "colors", "colors.txt", "colors used to render map tiles.")
flag.StringVar(&colorsFile, "c", "colors.txt", "colors used to render map tiles (shorthand).")
flag.StringVar(&bgColor, "background", defaultBgColor, "background color")
flag.StringVar(&bgColor, "bg", defaultBgColor, "background color (shorthand)")
flag.BoolVar(&transparent, "transparent", false, "Render transparent blocks.")
flag.BoolVar(&transparent, "t", false, "Render transparent blocks (shorthand).")
flag.Float64Var(&transparentDim,
@ -76,9 +82,11 @@ func main() {
common.PrintVersionAndExit()
}
bg := common.ParseColorDefault(bgColor, common.BackgroundColor)
router := mux.NewRouter()
subBaseLine := newSubBaseLine(mapDir)
subBaseLine := newSubBaseLine(mapDir, bg)
router.Path("/map/{z:[0-9]+}/{x:[0-9]+}/{y:[0-9]+}.png").Handler(subBaseLine)
var btu baseTilesUpdates
@ -116,7 +124,7 @@ func main() {
mapDir,
redisAddress,
allowedUpdateIps,
colors,
colors, bg,
yMin, yMax,
transparent,
workers,

View File

@ -7,6 +7,7 @@ package main
import (
"fmt"
"image"
"image/color"
"image/png"
"log"
"net/http"
@ -21,10 +22,11 @@ import (
type subBaseLine struct {
mapDir string
bg color.RGBA
}
func newSubBaseLine(mapDir string) *subBaseLine {
return &subBaseLine{mapDir: mapDir}
func newSubBaseLine(mapDir string, bg color.RGBA) *subBaseLine {
return &subBaseLine{mapDir: mapDir, bg: bg}
}
func (sb *subBaseLine) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
@ -77,7 +79,7 @@ func (sb *subBaseLine) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
xo := w * rx
yo := w * (parts - 1 - ry)
img := common.LoadPNG(baseTile)
img := common.LoadPNG(baseTile, sb.bg)
type subImage interface {
SubImage(image.Rectangle) image.Image

View File

@ -7,6 +7,7 @@ package main
import (
"encoding/json"
"image"
"image/color"
"image/draw"
"log"
"net"
@ -34,6 +35,7 @@ type tileUpdater struct {
redisAddress string
ips []net.IP
colors *common.Colors
bg color.RGBA
yMin, yMax int16
workers int
transparent bool
@ -71,6 +73,7 @@ func newTileUpdater(
mapDir, redisAddress string,
ips []net.IP,
colors *common.Colors,
bg color.RGBA,
yMin, yMax int,
transparent bool,
workers int,
@ -83,6 +86,7 @@ func newTileUpdater(
ips: ips,
changes: map[xz]bool{},
colors: colors,
bg: bg,
yMin: int16(yMin),
yMax: int16(yMax),
transparent: transparent,
@ -171,11 +175,11 @@ func (tu *tileUpdater) doUpdates() {
continue
}
btc := common.NewBaseTileCreator(
client, tu.colors,
client, tu.colors, tu.bg,
tu.yMin, tu.yMax,
tu.transparent, baseDir, true)
done.Add(1)
go updateBaseTiles(jobs, btc, &done)
go tu.updateBaseTiles(jobs, btc, &done)
}
parentJobs := make(map[xz]uint16)
@ -193,7 +197,7 @@ func (tu *tileUpdater) doUpdates() {
pJobs := make(chan xzm)
for i, n := 0, common.Min(len(parentJobs), tu.workers); i < n; i++ {
done.Add(1)
go updatePyramidTiles(level, tu.mapDir, pJobs, &done)
go tu.updatePyramidTiles(level, pJobs, &done)
}
ppJobs := make(map[xz]uint16)
for c, mask := range parentJobs {
@ -212,13 +216,15 @@ func (tu *tileUpdater) doUpdates() {
}
}
func updatePyramidTiles(level int, baseDir string, jobs chan xzm, done *sync.WaitGroup) {
func (tu *tileUpdater) updatePyramidTiles(
level int, jobs chan xzm, done *sync.WaitGroup) {
defer done.Done()
scratch := image.NewRGBA(image.Rect(0, 0, 256, 256))
resized := image.NewRGBA(image.Rect(0, 0, 128, 128))
for job := range jobs {
if err := updatePyramidTile(scratch, resized, level, baseDir, job); err != nil {
if err := tu.updatePyramidTile(scratch, resized, level, job); err != nil {
log.Printf("Updating pyramid tile failed: %s\n", err)
}
}
@ -244,28 +250,29 @@ var ofs = [4][2]int{
var windowSize = image.Pt(128, 128)
func updatePyramidTile(scratch, resized *image.RGBA, level int, baseDir string, j xzm) error {
func (tu *tileUpdater) updatePyramidTile(scratch, resized *image.RGBA, level int, j xzm) error {
var orig image.Image
origPath := filepath.Join(
baseDir,
tu.mapDir,
strconv.Itoa(level),
strconv.Itoa(int(j.P.X)),
strconv.Itoa(int(j.P.Z))+".png")
sr := resized.Bounds()
levelDir := strconv.Itoa(level + 1)
for i := uint16(0); i < 4; i++ {
if j.Mask&(1<<i) != 0 {
//log.Printf("level %d: modified %d\n", level, i)
o := ofs[i]
bx, bz := int(2*j.P.X), int(2*j.P.Z)
path := filepath.Join(
baseDir,
strconv.Itoa(level+1),
tu.mapDir,
levelDir,
strconv.Itoa(bx+o[0]),
strconv.Itoa(bz+o[1])+".png")
img := common.LoadPNG(path)
img := common.LoadPNG(path, tu.bg)
if err := rez.Convert(resized, img, common.ResizeFilter); err != nil {
return err
}
@ -274,7 +281,7 @@ func updatePyramidTile(scratch, resized *image.RGBA, level int, baseDir string,
} else {
// Load lazy
if orig == nil {
orig = common.LoadPNG(origPath)
orig = common.LoadPNG(origPath, tu.bg)
}
//log.Printf("level %d: copied %d\n", level, i)
min := orig.Bounds().Min.Add(dps[i])
@ -286,7 +293,10 @@ func updatePyramidTile(scratch, resized *image.RGBA, level int, baseDir string,
return common.SaveAsPNGAtomic(origPath, scratch)
}
func updateBaseTiles(jobs chan xz, btc *common.BaseTileCreator, done *sync.WaitGroup) {
func (tu *tileUpdater) updateBaseTiles(
jobs chan xz,
btc *common.BaseTileCreator, done *sync.WaitGroup) {
defer btc.Close()
defer done.Done()
for job := range jobs {

View File

@ -59,11 +59,13 @@ type BaseTileCreator struct {
baseDir string
update bool
emptyImage []byte
bg color.RGBA
}
func NewBaseTileCreator(
client *RedisClient,
colors *Colors,
bg color.RGBA,
yMin, yMax int16,
transparent bool,
baseDir string,
@ -73,6 +75,7 @@ func NewBaseTileCreator(
return &BaseTileCreator{
client: client,
colors: colors,
bg: bg,
renderer: renderer,
yOrder: NewYOrder(renderer, yOrderCapacity),
yMin: yMin,
@ -141,7 +144,7 @@ func (btc *BaseTileCreator) CreateTile(x, z int16, i, j int) error {
// 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)
m := BackgroundImage((tileWidth-2)*16, (tileHeight-2)*16, btc.bg)
if btc.emptyImage, err = EncodeToMem(m); err != nil {
return err
}
@ -152,7 +155,7 @@ func (btc *BaseTileCreator) CreateTile(x, z int16, i, j int) error {
image := btc.renderer.CreateShadedImage(
16, 16, (tileWidth-2)*16, (tileHeight-2)*16,
btc.colors, BackgroundColor)
btc.colors, btc.bg)
log.Printf("Writing (%d, %d) to file %s\n", x, z, path)

View File

@ -8,8 +8,10 @@ import (
"bufio"
"fmt"
"image/color"
"log"
"os"
"sort"
"strconv"
"strings"
)
@ -121,3 +123,29 @@ func (colors *Colors) BlendColors(span *Span, col color.RGBA, pos int32) color.R
}
return col
}
func ParseColor(col string) (color.RGBA, error) {
col = strings.TrimLeft(col, "#")
rgb, err := strconv.ParseUint(col, 16, 32)
if err != nil {
return color.RGBA{}, err
}
return color.RGBA{
R: uint8(rgb >> 16),
G: uint8(rgb >> 8),
B: uint8(rgb),
A: 0xff}, nil
}
func ParseColorDefault(col string, def color.RGBA) color.RGBA {
c, err := ParseColor(col)
if err != nil {
log.Printf("WARN: cannot parse color '%s': %s\n", col, err)
return def
}
return c
}
func ColorToHex(col color.RGBA) string {
return fmt.Sprintf("#%02x%02x%02x", col.R, col.G, col.B)
}

View File

@ -9,6 +9,7 @@ import (
"bytes"
"errors"
"image"
"image/color"
"image/png"
"log"
"os"
@ -94,18 +95,18 @@ func SaveAsPNGAtomic(path string, img image.Image) (err error) {
return os.Rename(tmpPath, path)
}
func LoadPNG(path string) image.Image {
func LoadPNG(path string, bg color.RGBA) image.Image {
var err error
var file *os.File
if file, err = os.Open(path); err != nil {
return image.White
return image.NewUniform(bg)
}
defer file.Close()
reader := bufio.NewReader(file)
var img image.Image
if img, err = png.Decode(reader); err != nil {
log.Printf("WARN: decoding '%s' failed: %s\n", path, err)
return image.White
return image.NewUniform(bg)
}
return img
}