From 3aabb452b43b0c875500dc7d626fc84f76249d3a Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Fri, 20 Jan 2023 17:44:59 +0100 Subject: [PATCH] use colormapping repo --- app/app.go | 3 +- app/setup.go | 3 +- colormapping/colormapping.go | 148 ------------------ colormapping/colormapping_test.go | 38 ----- colormapping/logger.go | 11 -- colormapping/palette.go | 45 ------ colormapping/palette_test.go | 30 ---- .../testdata/unifieddyes_palette_extended.png | Bin 290 -> 0 bytes go.mod | 1 + go.sum | 2 + mapblockrenderer/renderer.go | 3 +- mapblockrenderer/renderer_benchmark_test.go | 3 +- mapblockrenderer/renderer_test.go | 3 +- tilerenderer/renderer_benchmark_test.go | 3 +- tilerenderer/renderer_test.go | 3 +- 15 files changed, 17 insertions(+), 279 deletions(-) delete mode 100644 colormapping/colormapping.go delete mode 100644 colormapping/colormapping_test.go delete mode 100644 colormapping/logger.go delete mode 100644 colormapping/palette.go delete mode 100644 colormapping/palette_test.go delete mode 100644 colormapping/testdata/unifieddyes_palette_extended.png 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 2ff0323aadaa03a49133702f67c0d4a47d37a158..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 290 zcmV+-0p0$IP)Q~yD0002#Nkl