1
0
forked from MTSR/mapserver

Use a very simplified image downsampling for optimization purpose

This commit is contained in:
Pierre-Yves Rollo 2019-09-24 17:06:52 +02:00
parent 17b06037e4
commit f8bf1e9e53

View File

@ -14,8 +14,6 @@ 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 +27,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,6 +70,7 @@ 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) ([]byte, error) {
@ -186,29 +212,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)