use colormapping repo

This commit is contained in:
BuckarooBanzay 2023-01-20 17:44:59 +01:00
parent a9aab89f29
commit 3aabb452b4
15 changed files with 17 additions and 279 deletions

View File

@ -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 {

View File

@ -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"

View File

@ -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,
}
}

View File

@ -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")
//}
}

View File

@ -1,11 +0,0 @@
package colormapping
import (
"github.com/sirupsen/logrus"
)
var log *logrus.Entry
func init() {
log = logrus.WithFields(logrus.Fields{"prefix": "colormapping"})
}

View File

@ -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]
}

View File

@ -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)
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 290 B

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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"
)

View File

@ -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"
)

View File

@ -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"
)

View File

@ -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"
)

View File

@ -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"
)