1
0
forked from MTSR/mapserver
This commit is contained in:
NatureFreshMilk 2019-01-11 11:44:17 +01:00
parent a57724d5f0
commit f6547e1d69
4 changed files with 64 additions and 22 deletions

View File

@ -8,17 +8,14 @@ import (
"strings"
"strconv"
"github.com/sirupsen/logrus"
"image/color"
)
type Color struct {
R,G,B,A int
}
type ColorMapping struct {
colors map[string]*Color
colors map[string]*color.RGBA
}
func (m *ColorMapping) GetColor(name string) *Color {
func (m *ColorMapping) GetColor(name string) *color.RGBA {
return m.colors[name]
}
@ -37,7 +34,6 @@ func (m *ColorMapping) LoadBytes(buffer []byte) error {
continue
}
c := Color{}
parts := strings.Fields(txt)
if len(parts) < 4 {
@ -60,22 +56,14 @@ func (m *ColorMapping) LoadBytes(buffer []byte) error {
return err
}
c.R = int(r)
c.G = int(g)
c.B = int(b)
c := color.RGBA{uint8(r),uint8(g),uint8(b), 0xFF}
m.colors[parts[0]] = &c
}
if len(parts) >= 5 {
//with alpha
a, err := strconv.ParseInt(parts[4], 10, 32)
if err != nil {
return err
}
c.A = int(a)
}
m.colors[parts[0]] = &c
}
return nil
@ -96,6 +84,6 @@ func (m *ColorMapping) LoadVFSColors(useLocal bool, filename string) error {
return m.LoadBytes(buffer)
}
func CreateColorMapping() *ColorMapping {
return &ColorMapping{colors: make(map[string]*Color)}
func NewColorMapping() *ColorMapping {
return &ColorMapping{colors: make(map[string]*color.RGBA)}
}

View File

@ -5,7 +5,7 @@ import (
)
func TestNewMapping(t *testing.T) {
m := CreateColorMapping()
m := NewColorMapping()
err := m.LoadVFSColors(false, "/colors.txt")
if err != nil {
t.Fatal(err)

View File

@ -4,6 +4,8 @@ import (
"mapserver/colormapping"
"mapserver/coords"
"mapserver/mapblockaccessor"
"image"
"image/color"
)
type MapBlockRenderer struct {
@ -15,6 +17,17 @@ func NewMapBlockRenderer(accessor *mapblockaccessor.MapBlockAccessor, colors *co
return MapBlockRenderer{accessor: accessor, colors: colors}
}
func (r *MapBlockRenderer) Render(pos1, pos2 coords.MapBlockCoords) {
//TODO
const (
IMG_SCALE = 16
IMG_SIZE = IMG_SCALE * 16
)
func (r *MapBlockRenderer) Render(pos1, pos2 coords.MapBlockCoords) *image.RGBA {
upLeft := image.Point{0, 0}
lowRight := image.Point{IMG_SIZE, IMG_SIZE}
img := image.NewRGBA(image.Rectangle{upLeft, lowRight})
cyan := color.RGBA{100, 200, 200, 0xff}
img.Set(10, 10, cyan)
return img
}

View File

@ -0,0 +1,41 @@
package mapblockrenderer
import (
"os"
"io/ioutil"
"mapserver/coords"
"testing"
"mapserver/testutils"
"mapserver/db"
"mapserver/colormapping"
"mapserver/mapblockaccessor"
"image/png"
)
func TestSimpleRender(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "TestMigrate.*.sqlite")
if err != nil {
panic(err)
}
defer os.Remove(tmpfile.Name())
testutils.CreateTestDatabase(tmpfile.Name())
a, err := db.NewSqliteAccessor(tmpfile.Name())
if err != nil {
panic(err)
}
err = a.Migrate()
if err != nil {
panic(err)
}
cache := mapblockaccessor.NewMapBlockAccessor(a)
c := colormapping.NewColorMapping()
r := NewMapBlockRenderer(cache, c)
img := r.Render(coords.NewMapBlockCoords(0, 10, 0), coords.NewMapBlockCoords(0, -1, 0))
f, _ := os.Create("image.png")
png.Encode(f, img)
}