1
0
forked from MTSR/mapserver

Merge pull request #62 from EvidenceBKidscode/dev-opts

Dev opts
This commit is contained in:
Thomas Rudin 2019-09-26 15:38:09 +02:00 committed by GitHub
commit 3dcf3e2538
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 74 additions and 62 deletions

View File

@ -4,7 +4,6 @@ import (
"errors" "errors"
"image" "image"
"image/color" "image/color"
"image/draw"
"mapserver/colormapping" "mapserver/colormapping"
"mapserver/coords" "mapserver/coords"
"mapserver/mapblockaccessor" "mapserver/mapblockaccessor"
@ -209,10 +208,21 @@ func (r *MapBlockRenderer) Render(pos1, pos2 *coords.MapBlockCoords) (*image.NRG
imgX := x * IMG_SCALE imgX := x * IMG_SCALE
imgY := (15 - z) * IMG_SCALE imgY := (15 - z) * IMG_SCALE
rect := image.Rect( r32, g32, b32, a32 := c.RGBA()
imgX, imgY, r8, g8, b8, a8 := uint8(r32), uint8(g32), uint8(b32), uint8(a32)
imgX+IMG_SCALE, imgY+IMG_SCALE, for Y := imgY; Y < imgY + IMG_SCALE; Y++ {
) ix := (Y * IMG_SIZE + imgX) << 2
for X := 0; X < IMG_SCALE; X++ {
img.Pix[ix] = r8
ix++
img.Pix[ix] = g8
ix++
img.Pix[ix] = b8
ix++
img.Pix[ix] = a8
ix++
}
}
if c.A != 0xFF || !r.enableTransparency { if c.A != 0xFF || !r.enableTransparency {
//not transparent, mark as rendered //not transparent, mark as rendered
@ -220,8 +230,6 @@ func (r *MapBlockRenderer) Render(pos1, pos2 *coords.MapBlockCoords) (*image.NRG
xzOccupationMap[x][z] = true xzOccupationMap[x][z] = true
} }
draw.Draw(img, rect, &image.Uniform{c}, image.ZP, draw.Src)
if foundBlocks == EXPECTED_BLOCKS_PER_FLAT_MAPBLOCK { if foundBlocks == EXPECTED_BLOCKS_PER_FLAT_MAPBLOCK {
return img, nil return img, nil
} }

View File

@ -14,10 +14,7 @@ import (
"mapserver/tiledb" "mapserver/tiledb"
"strconv" "strconv"
"time" "time"
"github.com/disintegration/imaging"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
) )
@ -29,6 +26,33 @@ type TileRenderer struct {
Eventbus *eventbus.Eventbus Eventbus *eventbus.Eventbus
} }
func resizeImage(src *image.NRGBA, tgt *image.NRGBA, xoffset int, yoffset int) {
if src == nil {
return
}
w := src.Bounds().Dy() >> 1
h := src.Bounds().Dx() >> 1
sinc := src.Bounds().Dy() * 4
tinc := tgt.Bounds().Dx() * 4
for y := 0; y < h; y++ {
six := y * sinc * 2
tix := 4 * xoffset + (yoffset + y) * tinc
for x := 0; x < w; x++ {
r := (uint16(src.Pix[six]) + uint16(src.Pix[six+4]) + uint16(src.Pix[six+sinc]) + uint16(src.Pix[six+sinc+4])) >> 2
g := (uint16(src.Pix[six+1]) + uint16(src.Pix[six+5]) + uint16(src.Pix[six+sinc+1]) + uint16(src.Pix[six+sinc+5])) >> 2
b := (uint16(src.Pix[six+2]) + uint16(src.Pix[six+6]) + uint16(src.Pix[six+sinc+2]) + uint16(src.Pix[six+sinc+6])) >> 2
a := (uint16(src.Pix[six+3]) + uint16(src.Pix[six+7]) + uint16(src.Pix[six+sinc+3]) + uint16(src.Pix[six+sinc+7])) >> 2
tgt.Pix[tix] = uint8(r)
tgt.Pix[tix+1] = uint8(g)
tgt.Pix[tix+2] = uint8(b)
tgt.Pix[tix+3] = uint8(a)
tix+=4
six+=8
}
}
}
func NewTileRenderer(mapblockrenderer *mapblockrenderer.MapBlockRenderer, func NewTileRenderer(mapblockrenderer *mapblockrenderer.MapBlockRenderer,
tdb *tiledb.TileDB, tdb *tiledb.TileDB,
dba db.DBAccessor, dba db.DBAccessor,
@ -45,37 +69,33 @@ func NewTileRenderer(mapblockrenderer *mapblockrenderer.MapBlockRenderer,
const ( const (
IMG_SIZE = 256 IMG_SIZE = 256
SUB_IMG_SIZE = IMG_SIZE >> 1
) )
func (tr *TileRenderer) Render(tc *coords.TileCoords) ([]byte, error) { func (tr *TileRenderer) Render(tc *coords.TileCoords) (error) {
//No tile in db //No tile in db
img, data, err := tr.renderImage(tc, 2) _, err := tr.renderImage(tc, 2)
if err != nil { if err != nil {
return nil, err return err
} }
if img == nil { return nil
//empty tile
return nil, nil
}
return data, nil
} }
func (tr *TileRenderer) renderImage(tc *coords.TileCoords, recursionDepth int) (*image.NRGBA, []byte, error) { func (tr *TileRenderer) renderImage(tc *coords.TileCoords, recursionDepth int) (*image.NRGBA, error) {
if recursionDepth < 2 { if recursionDepth < 2 {
cachedtile, err := tr.tdb.GetTile(tc) cachedtile, err := tr.tdb.GetTile(tc)
if err != nil { if err != nil {
return nil, nil, err return nil, err
} }
if cachedtile != nil { if cachedtile != nil {
reader := bytes.NewReader(cachedtile) reader := bytes.NewReader(cachedtile)
cachedimg, err := png.Decode(reader) cachedimg, err := png.Decode(reader)
if err != nil { if err != nil {
return nil, nil, err return nil, err
} }
rect := image.Rectangle{ rect := image.Rectangle{
@ -87,14 +107,14 @@ func (tr *TileRenderer) renderImage(tc *coords.TileCoords, recursionDepth int) (
draw.Draw(img, rect, cachedimg, image.ZP, draw.Src) draw.Draw(img, rect, cachedimg, image.ZP, draw.Src)
log.WithFields(logrus.Fields{"x": tc.X, "y": tc.Y, "zoom": tc.Zoom}).Debug("Cached image") log.WithFields(logrus.Fields{"x": tc.X, "y": tc.Y, "zoom": tc.Zoom}).Debug("Cached image")
return img, cachedtile, nil return img, nil
} }
} }
if recursionDepth <= 1 && tc.Zoom < 13 { if recursionDepth <= 1 && tc.Zoom < 13 {
//non-cached layer and not in "origin" zoom, skip tile //non-cached layer and not in "origin" zoom, skip tile
log.WithFields(logrus.Fields{"x": tc.X, "y": tc.Y, "zoom": tc.Zoom}).Debug("Skip image") log.WithFields(logrus.Fields{"x": tc.X, "y": tc.Y, "zoom": tc.Zoom}).Debug("Skip image")
return nil, nil, nil return nil, nil
} }
log.WithFields(logrus.Fields{"x": tc.X, "y": tc.Y, "zoom": tc.Zoom}).Debug("RenderImage") log.WithFields(logrus.Fields{"x": tc.X, "y": tc.Y, "zoom": tc.Zoom}).Debug("RenderImage")
@ -106,11 +126,11 @@ func (tr *TileRenderer) renderImage(tc *coords.TileCoords, recursionDepth int) (
currentLayer := layer.FindLayerById(tr.layers, tc.LayerId) currentLayer := layer.FindLayerById(tr.layers, tc.LayerId)
if currentLayer == nil { if currentLayer == nil {
return nil, nil, errors.New("No layer found") return nil, errors.New("No layer found")
} }
if tc.Zoom > 13 || tc.Zoom < 1 { if tc.Zoom > 13 || tc.Zoom < 1 {
return nil, nil, errors.New("Invalid zoom") return nil, errors.New("Invalid zoom")
} }
if tc.Zoom == 13 { if tc.Zoom == 13 {
@ -129,17 +149,20 @@ func (tr *TileRenderer) renderImage(tc *coords.TileCoords, recursionDepth int) (
} }
log.WithFields(fields).Debug("mapblock render from tilerender") log.WithFields(fields).Debug("mapblock render from tilerender")
return nil, nil, err return nil, err
} }
if img == nil { if img == nil {
return nil, nil, nil return nil, nil
} }
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
png.Encode(buf, img) png.Encode(buf, img)
return img, buf.Bytes(), nil tile := buf.Bytes()
tr.tdb.SetTile(tc, tile)
return img, nil
} }
//zoom 1-12 //zoom 1-12
@ -155,24 +178,24 @@ func (tr *TileRenderer) renderImage(tc *coords.TileCoords, recursionDepth int) (
start := time.Now() start := time.Now()
upperLeft, _, err := tr.renderImage(quads.UpperLeft, recursionDepth-1) upperLeft, err := tr.renderImage(quads.UpperLeft, recursionDepth-1)
if err != nil { if err != nil {
return nil, nil, err return nil, err
} }
upperRight, _, err := tr.renderImage(quads.UpperRight, recursionDepth-1) upperRight, err := tr.renderImage(quads.UpperRight, recursionDepth-1)
if err != nil { if err != nil {
return nil, nil, err return nil, err
} }
lowerLeft, _, err := tr.renderImage(quads.LowerLeft, recursionDepth-1) lowerLeft, err := tr.renderImage(quads.LowerLeft, recursionDepth-1)
if err != nil { if err != nil {
return nil, nil, err return nil, err
} }
lowerRight, _, err := tr.renderImage(quads.LowerRight, recursionDepth-1) lowerRight, err := tr.renderImage(quads.LowerRight, recursionDepth-1)
if err != nil { if err != nil {
return nil, nil, err return nil, err
} }
t := time.Now() t := time.Now()
@ -186,29 +209,10 @@ func (tr *TileRenderer) renderImage(tc *coords.TileCoords, recursionDepth int) (
}, },
) )
rect := image.Rect(0, 0, 128, 128) resizeImage(upperLeft, img, 0, 0)
if upperLeft != nil { resizeImage(upperRight, img, SUB_IMG_SIZE, 0)
resizedImg := imaging.Resize(upperLeft, 128, 128, imaging.Lanczos) resizeImage(lowerLeft, img, 0, SUB_IMG_SIZE)
draw.Draw(img, rect, resizedImg, image.ZP, draw.Src) resizeImage(lowerRight, img, SUB_IMG_SIZE, SUB_IMG_SIZE)
}
rect = image.Rect(128, 0, 256, 128)
if upperRight != nil {
resizedImg := imaging.Resize(upperRight, 128, 128, imaging.Lanczos)
draw.Draw(img, rect, resizedImg, image.ZP, draw.Src)
}
rect = image.Rect(0, 128, 128, 256)
if lowerLeft != nil {
resizedImg := imaging.Resize(lowerLeft, 128, 128, imaging.Lanczos)
draw.Draw(img, rect, resizedImg, image.ZP, draw.Src)
}
rect = image.Rect(128, 128, 256, 256)
if lowerRight != nil {
resizedImg := imaging.Resize(lowerRight, 128, 128, imaging.Lanczos)
draw.Draw(img, rect, resizedImg, image.ZP, draw.Src)
}
t = time.Now() t = time.Now()
quadresize := t.Sub(start) quadresize := t.Sub(start)
@ -242,5 +246,5 @@ func (tr *TileRenderer) renderImage(tc *coords.TileCoords, recursionDepth int) (
tr.Eventbus.Emit(eventbus.TILE_RENDERED, tc) tr.Eventbus.Emit(eventbus.TILE_RENDERED, tc)
return img, buf.Bytes(), nil return img, nil
} }

View File

@ -19,7 +19,7 @@ func worker(ctx *app.App, coords <-chan *coords.TileCoords, done chan bool) {
} }
logrus.WithFields(fields).Debug("Tile render job tile") logrus.WithFields(fields).Debug("Tile render job tile")
_, err := ctx.Tilerenderer.Render(tc) err := ctx.Tilerenderer.Render(tc)
if err != nil { if err != nil {
fields := logrus.Fields{ fields := logrus.Fields{
"X": tc.X, "X": tc.X,