From ef6ad8d1515e05b4bea3d26576ae747bf1bab6d6 Mon Sep 17 00:00:00 2001 From: Thomas Rudin Date: Wed, 19 Jun 2019 21:12:04 +0200 Subject: [PATCH] param2 fullpalette support --- app/setup.go | 9 +++- colormapping/palette.go | 45 ++++++++++++++++++ colormapping/palette_test.go | 30 ++++++++++++ .../testdata/unifieddyes_palette_extended.png | Bin 0 -> 290 bytes doc/license.md | 5 ++ mapblockparser/mapblock.go | 5 ++ mapblockparser/mapdata.go | 4 +- mapblockrenderer/renderer.go | 10 +++- mapblockrenderer/renderer_test.go | 16 ++++++- .../testdata/unifieddyes_palette_extended.png | Bin 0 -> 290 bytes static/pics/unifieddyes_palette_extended.png | Bin 0 -> 290 bytes 11 files changed, 118 insertions(+), 6 deletions(-) create mode 100644 colormapping/palette.go create mode 100644 colormapping/palette_test.go create mode 100644 colormapping/testdata/unifieddyes_palette_extended.png create mode 100644 mapblockrenderer/testdata/unifieddyes_palette_extended.png create mode 100644 static/pics/unifieddyes_palette_extended.png diff --git a/app/setup.go b/app/setup.go index edf8e9f..f9beb98 100644 --- a/app/setup.go +++ b/app/setup.go @@ -14,6 +14,7 @@ import ( "mapserver/settings" "mapserver/tiledb" "mapserver/tilerenderer" + "mapserver/vfs" "mapserver/worldconfig" "time" @@ -105,8 +106,14 @@ func Setup(p params.ParamsType, cfg *Config) *App { } + fullpalette, err := colormapping.NewPalette(vfs.FSMustByte(false, "/pics/unifieddyes_palette_extended.png")) + + if err != nil { + panic(err) + } + //mapblock renderer - a.Mapblockrenderer = mapblockrenderer.NewMapBlockRenderer(a.BlockAccessor, a.Colormapping) + a.Mapblockrenderer = mapblockrenderer.NewMapBlockRenderer(a.BlockAccessor, a.Colormapping, fullpalette) //mapserver database if a.Worldconfig[worldconfig.CONFIG_PSQL_MAPSERVER] != "" { diff --git a/colormapping/palette.go b/colormapping/palette.go new file mode 100644 index 0000000..736c7d2 --- /dev/null +++ b/colormapping/palette.go @@ -0,0 +1,45 @@ +package colormapping + +import ( + "bytes" + "image/color" + "image/png" +) + +type Palette struct { + colors map[int]*color.RGBA +} + +func NewPalette(imagefile []byte) (*Palette, error) { + palette := &Palette{ + colors: make(map[int]*color.RGBA), + } + + reader := bytes.NewReader(imagefile) + img, err := png.Decode(reader) + + if err != nil { + return nil, err + } + + bounds := img.Bounds() + + index := 0 + for y := bounds.Min.Y; y < bounds.Max.Y; y++ { + for x := bounds.Min.X; x < bounds.Max.X; x++ { + c := img.At(x, y) + r, g, b, a := c.RGBA() + + //fmt.Println("x ", x, " y ", y, " Index: ", index, " Color ", c) + palette.colors[index] = &color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)} + + index++ + } + } + + return palette, nil +} + +func (m *Palette) GetColor(param2 int) *color.RGBA { + return m.colors[param2] +} diff --git a/colormapping/palette_test.go b/colormapping/palette_test.go new file mode 100644 index 0000000..8dc1f17 --- /dev/null +++ b/colormapping/palette_test.go @@ -0,0 +1,30 @@ +package colormapping + +import ( + "fmt" + "io/ioutil" + "testing" +) + +func TestNewPalette(t *testing.T) { + data, err := ioutil.ReadFile("./testdata/unifieddyes_palette_extended.png") + + if err != nil { + t.Fatal(err) + } + + palette, err := NewPalette(data) + + if err != nil { + t.Fatal(err) + } + + color := palette.GetColor(0) + + if color == nil { + t.Fatal("color not found!") + } + + fmt.Println(color) + +} diff --git a/colormapping/testdata/unifieddyes_palette_extended.png b/colormapping/testdata/unifieddyes_palette_extended.png new file mode 100644 index 0000000000000000000000000000000000000000..2ff0323aadaa03a49133702f67c0d4a47d37a158 GIT binary patch literal 290 zcmV+-0p0$IP)Q~yD0002#NklQ~yD0002#NklQ~yD0002#Nkl