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 0000000..2ff0323 Binary files /dev/null and b/colormapping/testdata/unifieddyes_palette_extended.png differ diff --git a/doc/license.md b/doc/license.md index ea1fb14..83cc8ff 100644 --- a/doc/license.md +++ b/doc/license.md @@ -65,3 +65,8 @@ * atm3_front.png * WTFPL * Source: http://git.gpcf.eu/?p=atm.git;a=summary + + +* unifieddyes_palette_extended.png + * GPL 2.0 + * Source: https://gitlab.com/VanessaE/unifieddyes diff --git a/mapblockparser/mapblock.go b/mapblockparser/mapblock.go index db071cd..33f6bf9 100644 --- a/mapblockparser/mapblock.go +++ b/mapblockparser/mapblock.go @@ -84,6 +84,11 @@ func (mb *MapBlock) GetNodeId(x, y, z int) int { return mb.Mapdata.ContentId[pos] } +func (mb *MapBlock) GetParam2(x, y, z int) int { + pos := getNodePos(x, y, z) + return mb.Mapdata.Param2[pos] +} + func (mb *MapBlock) GetNodeName(x, y, z int) string { id := mb.GetNodeId(x, y, z) return mb.BlockMapping[id] diff --git a/mapblockparser/mapdata.go b/mapblockparser/mapdata.go index 031f622..6d7b1ba 100644 --- a/mapblockparser/mapdata.go +++ b/mapblockparser/mapdata.go @@ -39,8 +39,8 @@ func parseMapdata(mapblock *MapBlock, data []byte) (int, error) { for i := 0; i < 4096; i++ { mapd.ContentId[i] = readU16(rawdata, i*2) - mapd.Param1[i] = readU8(rawdata, (i*2)+2) - mapd.Param2[i] = readU8(rawdata, (i*2)+3) //TODO: last item has wrong value + mapd.Param1[i] = readU8(rawdata, (4096*2)+i) + mapd.Param2[i] = readU8(rawdata, (4096*3)+i) } return cr.Count, nil diff --git a/mapblockrenderer/renderer.go b/mapblockrenderer/renderer.go index b121628..c3c0e13 100644 --- a/mapblockrenderer/renderer.go +++ b/mapblockrenderer/renderer.go @@ -17,14 +17,16 @@ import ( type MapBlockRenderer struct { accessor *mapblockaccessor.MapBlockAccessor colors *colormapping.ColorMapping + fullpalette *colormapping.Palette enableShadow bool enableTransparency bool } -func NewMapBlockRenderer(accessor *mapblockaccessor.MapBlockAccessor, colors *colormapping.ColorMapping) *MapBlockRenderer { +func NewMapBlockRenderer(accessor *mapblockaccessor.MapBlockAccessor, colors *colormapping.ColorMapping, fullpalette *colormapping.Palette) *MapBlockRenderer { return &MapBlockRenderer{ accessor: accessor, colors: colors, + fullpalette: fullpalette, enableShadow: true, enableTransparency: false, } @@ -136,6 +138,12 @@ func (r *MapBlockRenderer) Render(pos1, pos2 *coords.MapBlockCoords) (*image.NRG } c := r.colors.GetColor(nodeName) + hasfullpalette := nodeName == "unifiedbricks:brickblock" + + if hasfullpalette { + param2 := mb.GetParam2(x, y, z) + c = r.fullpalette.GetColor(param2) + } if c == nil { continue diff --git a/mapblockrenderer/renderer_test.go b/mapblockrenderer/renderer_test.go index 2cdce63..6a42143 100644 --- a/mapblockrenderer/renderer_test.go +++ b/mapblockrenderer/renderer_test.go @@ -2,7 +2,6 @@ package mapblockrenderer import ( "fmt" - "github.com/sirupsen/logrus" "io/ioutil" "mapserver/colormapping" "mapserver/coords" @@ -13,6 +12,8 @@ import ( "os" "testing" "time" + + "github.com/sirupsen/logrus" ) func TestSimpleRender(t *testing.T) { @@ -51,7 +52,18 @@ func TestSimpleRender(t *testing.T) { t.Fatal(err) } - r := NewMapBlockRenderer(cache, c) + palettedata, err := ioutil.ReadFile("./testdata/unifieddyes_palette_extended.png") + if err != nil { + t.Fatal(err) + } + + palette, err := colormapping.NewPalette(palettedata) + + if err != nil { + t.Fatal(err) + } + + r := NewMapBlockRenderer(cache, c, palette) os.Mkdir("../test-output", 0755) results := make(chan JobResult, 100) diff --git a/mapblockrenderer/testdata/unifieddyes_palette_extended.png b/mapblockrenderer/testdata/unifieddyes_palette_extended.png new file mode 100644 index 0000000..2ff0323 Binary files /dev/null and b/mapblockrenderer/testdata/unifieddyes_palette_extended.png differ diff --git a/static/pics/unifieddyes_palette_extended.png b/static/pics/unifieddyes_palette_extended.png new file mode 100644 index 0000000..2ff0323 Binary files /dev/null and b/static/pics/unifieddyes_palette_extended.png differ