mirror of
https://bitbucket.org/s_l_teichmann/mtsatellite
synced 2024-11-09 11:40:23 +01:00
173 lines
3.5 KiB
Go
173 lines
3.5 KiB
Go
// Copyright 2014 by Sascha L. Teichmann
|
|
// Use of this source code is governed by the MIT license
|
|
// that can be found in the LICENSE file.
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"image"
|
|
"image/color"
|
|
"image/png"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"time"
|
|
|
|
"bitbucket.org/s_l_teichmann/mtredisalize/common"
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
type subBaseLine struct {
|
|
mapDir string
|
|
}
|
|
|
|
func newSubBaseLine(mapDir string) *subBaseLine {
|
|
return &subBaseLine{mapDir: mapDir}
|
|
}
|
|
|
|
func (sb *subBaseLine) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
xs := vars["x"]
|
|
ys := vars["y"]
|
|
zs := vars["z"]
|
|
|
|
x, y, z := toUint(xs), toUint(ys), toUint(zs)
|
|
if z < 9 {
|
|
http.ServeFile(rw, r, filepath.Join(sb.mapDir,
|
|
strconv.Itoa(int(z)),
|
|
strconv.Itoa(int(x)),
|
|
fmt.Sprintf("%d.png", y)))
|
|
return
|
|
}
|
|
|
|
if z > 16 {
|
|
z = 16
|
|
}
|
|
tx := x >> (z - 8)
|
|
ty := y >> (z - 8)
|
|
|
|
baseTile := filepath.Join(
|
|
sb.mapDir, "8", strconv.Itoa(int(tx)), fmt.Sprintf("%d.png", ty))
|
|
|
|
rw.Header().Set("Cache-Control", "private, max-age=0, no-cache")
|
|
|
|
var err error
|
|
var fi os.FileInfo
|
|
if fi, err = os.Stat(baseTile); err != nil {
|
|
http.NotFound(rw, r)
|
|
return
|
|
}
|
|
|
|
if checkLastModified(rw, r, fi.ModTime()) {
|
|
return
|
|
}
|
|
|
|
if checkETag(rw, r, fi) {
|
|
return
|
|
}
|
|
|
|
rx := x & ^(^uint(0) << (z - 8))
|
|
ry := y & ^(^uint(0) << (z - 8))
|
|
|
|
parts := uint(1) << (z - 8)
|
|
|
|
w := uint(256) / parts
|
|
xo := w * rx
|
|
yo := w * (parts - 1 - ry)
|
|
|
|
img := common.LoadPNG(baseTile)
|
|
|
|
type subImage interface {
|
|
SubImage(image.Rectangle) image.Image
|
|
}
|
|
|
|
if si, ok := img.(subImage); ok {
|
|
img = si.SubImage(image.Rect(int(xo), int(yo), int(xo+w), int(yo+w)))
|
|
} else {
|
|
// Should not happen.
|
|
http.Error(rw,
|
|
http.StatusText(http.StatusInternalServerError),
|
|
http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
img = blowUp(img)
|
|
|
|
rw.Header().Set("Content-Type", "image/png")
|
|
if err = png.Encode(rw, img); err != nil {
|
|
log.Printf("WARN: encoding image failed: %s", err)
|
|
return
|
|
}
|
|
}
|
|
|
|
func blowUp(src image.Image) *image.RGBA {
|
|
|
|
// TODO: Fast path for image.RGBA
|
|
|
|
dst := image.NewRGBA(image.Rect(0, 0, 256, 256))
|
|
|
|
// fix point numbers x:8
|
|
dx, dy := src.Bounds().Dx(), src.Bounds().Dy()
|
|
|
|
bx, by := src.Bounds().Min.X<<8, src.Bounds().Min.Y<<8
|
|
|
|
py := by
|
|
for y := 0; y < 256; y++ {
|
|
sy := (py >> 8) & 0xff
|
|
ox := -1
|
|
px := bx
|
|
var col color.Color
|
|
for x := 0; x < 256; x++ {
|
|
sx := (px >> 8) & 0xff
|
|
if sx != ox { // Minimize interface indirection access.
|
|
ox = sx
|
|
col = src.At(sx, sy)
|
|
}
|
|
dst.Set(x, y, col)
|
|
px += dx
|
|
}
|
|
py += dy
|
|
}
|
|
|
|
return dst
|
|
}
|
|
|
|
func checkETag(w http.ResponseWriter, r *http.Request, fi os.FileInfo) bool {
|
|
etag := fmt.Sprintf("%x-%x", fi.ModTime().Unix(), fi.Size())
|
|
if ifNoneMatch := r.Header.Get("If-None-Match"); ifNoneMatch == etag {
|
|
w.WriteHeader(http.StatusNotModified)
|
|
return true
|
|
}
|
|
w.Header().Set("ETag", etag)
|
|
return false
|
|
}
|
|
|
|
func checkLastModified(w http.ResponseWriter, r *http.Request, modtime time.Time) bool {
|
|
|
|
if modtime.IsZero() {
|
|
return false
|
|
}
|
|
|
|
// The Date-Modified header truncates sub-second precision, so
|
|
// use mtime < t+1s instead of mtime <= t to check for unmodified.
|
|
if t, err := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); err == nil && modtime.Before(t.Add(1*time.Second)) {
|
|
w.WriteHeader(http.StatusNotModified)
|
|
return true
|
|
}
|
|
w.Header().Set("Last-Modified", modtime.UTC().Format(http.TimeFormat))
|
|
return false
|
|
}
|
|
|
|
func toUint(s string) uint {
|
|
var err error
|
|
var x int
|
|
if x, err = strconv.Atoi(s); err != nil {
|
|
log.Printf("WARN: Cannot convert to int: %s", err)
|
|
return 0
|
|
}
|
|
return uint(x)
|
|
}
|