diff --git a/app/app.go b/app/app.go index ace9738..027b2b6 100644 --- a/app/app.go +++ b/app/app.go @@ -2,7 +2,6 @@ package app import ( "mapserver/blockaccessor" - "mapserver/colormapping" "mapserver/db" "mapserver/eventbus" "mapserver/mapblockaccessor" @@ -12,6 +11,8 @@ import ( "mapserver/settings" "mapserver/tiledb" "mapserver/tilerenderer" + + "github.com/minetest-go/colormapping" ) type App struct { diff --git a/app/setup.go b/app/setup.go index 520c4c7..a7bb8f2 100644 --- a/app/setup.go +++ b/app/setup.go @@ -2,7 +2,6 @@ package app import ( "mapserver/blockaccessor" - "mapserver/colormapping" "mapserver/db/postgres" "mapserver/db/sqlite" "mapserver/eventbus" @@ -18,6 +17,8 @@ import ( "mapserver/worldconfig" "time" + "github.com/minetest-go/colormapping" + "io/ioutil" "os" diff --git a/colormapping/colormapping.go b/colormapping/colormapping.go deleted file mode 100644 index 95b1c1f..0000000 --- a/colormapping/colormapping.go +++ /dev/null @@ -1,148 +0,0 @@ -package colormapping - -import ( - "bufio" - "bytes" - "errors" - "image/color" - "mapserver/public" - "strconv" - "strings" - - "github.com/sirupsen/logrus" -) - -type ColorMapping struct { - colors map[string]*color.RGBA - extendedpaletteblock map[string]bool - extendedpalette *Palette -} - -func (m *ColorMapping) GetColor(name string, param2 int) *color.RGBA { - //TODO: list of node->palette - if m.extendedpaletteblock[name] { - // param2 coloring - return m.extendedpalette.GetColor(param2) - } - - return m.colors[name] -} - -func (m *ColorMapping) GetColors() map[string]*color.RGBA { - return m.colors -} - -func (m *ColorMapping) LoadBytes(buffer []byte) (int, error) { - scanner := bufio.NewScanner(bytes.NewReader(buffer)) - count := 0 - line := 0 - - for scanner.Scan() { - line++ - - txt := strings.Trim(scanner.Text(), " ") - - if len(txt) == 0 { - //empty - continue - } - - if strings.HasPrefix(txt, "#") { - //comment - continue - } - - parts := strings.Fields(txt) - - if len(parts) < 4 { - return 0, errors.New("invalid line: #" + strconv.Itoa(line)) - } - - if len(parts) >= 4 { - r, err := strconv.ParseInt(parts[1], 10, 32) - if err != nil { - return 0, err - } - - g, err := strconv.ParseInt(parts[2], 10, 32) - if err != nil { - return 0, err - } - - b, err := strconv.ParseInt(parts[3], 10, 32) - if err != nil { - return 0, err - } - - a := int64(255) - - if len(parts) >= 5 { - //with alpha - //a, err = strconv.ParseInt(parts[4], 10, 32) - //if err != nil { - // return 0, err - //} - } - - c := color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)} - m.colors[parts[0]] = &c - count++ - } - } - - return count, nil -} - -func (m *ColorMapping) LoadVFSColors(filename string) (int, error) { - buffer, err := public.Files.ReadFile(filename) - if err != nil { - return 0, err - } - - log.WithFields(logrus.Fields{"size": len(buffer), - "filename": filename, - }).Info("Loading colors") - - return m.LoadBytes(buffer) -} - -func NewColorMapping() *ColorMapping { - data, err := public.Files.ReadFile("pics/unifieddyes_palette_extended.png") - if err != nil { - panic(err) - } - - extendedpalette, err := NewPalette(data) - if err != nil { - panic(err) - } - - data, err = public.Files.ReadFile("extended_palette.txt") - if err != nil { - panic(err) - } - - scanner := bufio.NewScanner(bytes.NewReader(data)) - extendedpaletteblock := make(map[string]bool) - - if err != nil { - panic(err) - } - - for scanner.Scan() { - txt := strings.Trim(scanner.Text(), " ") - - if len(txt) == 0 { - //empty - continue - } - - extendedpaletteblock[txt] = true - } - - return &ColorMapping{ - colors: make(map[string]*color.RGBA), - extendedpaletteblock: extendedpaletteblock, - extendedpalette: extendedpalette, - } -} diff --git a/colormapping/colormapping_test.go b/colormapping/colormapping_test.go deleted file mode 100644 index 7191ada..0000000 --- a/colormapping/colormapping_test.go +++ /dev/null @@ -1,38 +0,0 @@ -package colormapping - -import ( - "testing" -) - -func TestNewMapping(t *testing.T) { - m := NewColorMapping() - _, err := m.LoadVFSColors("colors/vanessa.txt") - if err != nil { - t.Fatal(err) - } - - _, err = m.LoadVFSColors("colors/scifi_nodes.txt") - if err != nil { - t.Fatal(err) - } - - c := m.GetColor("scifi_nodes:blacktile2", 0) - if c == nil { - panic("no color") - } - - c = m.GetColor("default:river_water_flowing", 0) - if c == nil { - panic("no color") - } - - c = m.GetColor("unifiedbricks:brickblock_multicolor_dark", 100) - if c == nil { - panic("no color") - } - - //if c.A != 128 { - // panic("wrong alpha") - //} - -} diff --git a/colormapping/logger.go b/colormapping/logger.go deleted file mode 100644 index e1e8e1b..0000000 --- a/colormapping/logger.go +++ /dev/null @@ -1,11 +0,0 @@ -package colormapping - -import ( - "github.com/sirupsen/logrus" -) - -var log *logrus.Entry - -func init() { - log = logrus.WithFields(logrus.Fields{"prefix": "colormapping"}) -} diff --git a/colormapping/palette.go b/colormapping/palette.go deleted file mode 100644 index 736c7d2..0000000 --- a/colormapping/palette.go +++ /dev/null @@ -1,45 +0,0 @@ -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 deleted file mode 100644 index 8dc1f17..0000000 --- a/colormapping/palette_test.go +++ /dev/null @@ -1,30 +0,0 @@ -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 deleted file mode 100644 index 2ff0323..0000000 Binary files a/colormapping/testdata/unifieddyes_palette_extended.png and /dev/null differ diff --git a/go.mod b/go.mod index 6dde806..c0f96db 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module mapserver require ( github.com/gorilla/websocket v1.5.0 github.com/lib/pq v1.10.7 + github.com/minetest-go/colormapping v1.0.0 github.com/minetest-go/mapparser v0.1.8 github.com/patrickmn/go-cache v2.1.0+incompatible github.com/prometheus/client_golang v1.14.0 diff --git a/go.sum b/go.sum index ed13e09..f04605b 100644 --- a/go.sum +++ b/go.sum @@ -182,6 +182,8 @@ github.com/mattn/go-sqlite3 v1.14.10/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4 github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/minetest-go/colormapping v1.0.0 h1:kT2v8NG7kPf63p8cl/GPM6YSrxaT5O3zOW8HIs4xHjo= +github.com/minetest-go/colormapping v1.0.0/go.mod h1:arMBjO6+z8+yhLvrsX4tCnYq5fzGcPFUOj/4Xb78Ktc= github.com/minetest-go/mapparser v0.1.1 h1:Ir7ChKX0lwnyr6Zg/Jd3ipQT1vPCAQYN6GWmQXK020Y= github.com/minetest-go/mapparser v0.1.1/go.mod h1:Rjv12WQ3Tn9BsOr0ohFguGWFol3vIfgQcTUy5wCACgo= github.com/minetest-go/mapparser v0.1.8 h1:tmkno1Qi8nIdjQXJLlnq38uVGygZWA8i3ilre/bB9W8= diff --git a/mapblockrenderer/renderer.go b/mapblockrenderer/renderer.go index a859783..d302209 100644 --- a/mapblockrenderer/renderer.go +++ b/mapblockrenderer/renderer.go @@ -4,11 +4,12 @@ import ( "errors" "image" "image/color" - "mapserver/colormapping" "mapserver/coords" "mapserver/mapblockaccessor" "time" + "github.com/minetest-go/colormapping" + "github.com/prometheus/client_golang/prometheus" "github.com/sirupsen/logrus" ) diff --git a/mapblockrenderer/renderer_benchmark_test.go b/mapblockrenderer/renderer_benchmark_test.go index b967d88..b001eb5 100644 --- a/mapblockrenderer/renderer_benchmark_test.go +++ b/mapblockrenderer/renderer_benchmark_test.go @@ -2,7 +2,6 @@ package mapblockrenderer import ( "io/ioutil" - "mapserver/colormapping" "mapserver/coords" "mapserver/db/sqlite" "mapserver/mapblockaccessor" @@ -11,6 +10,8 @@ import ( "testing" "time" + "github.com/minetest-go/colormapping" + "github.com/sirupsen/logrus" ) diff --git a/mapblockrenderer/renderer_test.go b/mapblockrenderer/renderer_test.go index b48d89a..8afbe91 100644 --- a/mapblockrenderer/renderer_test.go +++ b/mapblockrenderer/renderer_test.go @@ -3,7 +3,6 @@ package mapblockrenderer import ( "fmt" "io/ioutil" - "mapserver/colormapping" "mapserver/coords" "mapserver/db/sqlite" "mapserver/layer" @@ -13,6 +12,8 @@ import ( "testing" "time" + "github.com/minetest-go/colormapping" + "github.com/sirupsen/logrus" ) diff --git a/tilerenderer/renderer_benchmark_test.go b/tilerenderer/renderer_benchmark_test.go index b22ca97..7e03035 100644 --- a/tilerenderer/renderer_benchmark_test.go +++ b/tilerenderer/renderer_benchmark_test.go @@ -2,7 +2,6 @@ package tilerenderer import ( "io/ioutil" - "mapserver/colormapping" "mapserver/coords" "mapserver/db/sqlite" "mapserver/layer" @@ -14,6 +13,8 @@ import ( "testing" "time" + "github.com/minetest-go/colormapping" + "github.com/sirupsen/logrus" ) diff --git a/tilerenderer/renderer_test.go b/tilerenderer/renderer_test.go index e7401e6..71459f4 100644 --- a/tilerenderer/renderer_test.go +++ b/tilerenderer/renderer_test.go @@ -2,7 +2,6 @@ package tilerenderer import ( "io/ioutil" - "mapserver/colormapping" "mapserver/coords" "mapserver/db/sqlite" "mapserver/layer" @@ -14,6 +13,8 @@ import ( "testing" "time" + "github.com/minetest-go/colormapping" + "github.com/sirupsen/logrus" )