1
0
forked from MTSR/mapserver
mapserver/colormapping/colormapping.go

136 lines
2.5 KiB
Go
Raw Normal View History

2019-01-10 17:21:34 +03:00
package colormapping
2019-01-11 12:49:19 +03:00
import (
2019-01-13 18:37:03 +03:00
"bufio"
"bytes"
"errors"
"image/color"
"mapserver/vfs"
"strconv"
"strings"
2019-02-28 22:20:59 +03:00
"github.com/sirupsen/logrus"
2019-01-11 12:49:19 +03:00
)
2019-01-10 17:21:34 +03:00
type ColorMapping struct {
2019-06-20 08:55:18 +03:00
colors map[string]*color.RGBA
extendedpaletteblock map[string]bool
extendedpalette *Palette
2019-01-11 13:23:50 +03:00
}
2019-06-20 08:14:14 +03:00
func (m *ColorMapping) GetColor(name string, param2 int) *color.RGBA {
//TODO: list of node->palette
2019-06-20 08:55:18 +03:00
if m.extendedpaletteblock[name] {
2019-06-20 08:14:14 +03:00
// param2 coloring
return m.extendedpalette.GetColor(param2)
}
2019-01-13 18:37:03 +03:00
return m.colors[name]
2019-01-11 13:23:50 +03:00
}
2019-02-08 09:40:40 +03:00
func (m *ColorMapping) LoadBytes(buffer []byte) (int, error) {
2019-01-13 18:37:03 +03:00
scanner := bufio.NewScanner(bytes.NewReader(buffer))
2019-02-08 09:40:40 +03:00
count := 0
line := 0
2019-01-13 18:37:03 +03:00
for scanner.Scan() {
2019-02-08 09:40:40 +03:00
line++
2019-01-13 18:37:03 +03:00
txt := strings.Trim(scanner.Text(), " ")
2019-01-11 13:23:50 +03:00
2019-01-13 18:37:03 +03:00
if len(txt) == 0 {
//empty
continue
}
2019-01-11 13:23:50 +03:00
2019-01-13 18:37:03 +03:00
if strings.HasPrefix(txt, "#") {
//comment
continue
}
2019-01-11 13:23:50 +03:00
2019-01-13 18:37:03 +03:00
parts := strings.Fields(txt)
2019-01-11 13:23:50 +03:00
2019-01-13 18:37:03 +03:00
if len(parts) < 4 {
2019-02-08 09:40:40 +03:00
return 0, errors.New("invalid line: #" + strconv.Itoa(line))
2019-01-13 18:37:03 +03:00
}
2019-01-10 17:21:34 +03:00
2019-01-13 18:37:03 +03:00
if len(parts) >= 4 {
r, err := strconv.ParseInt(parts[1], 10, 32)
if err != nil {
2019-02-08 09:40:40 +03:00
return 0, err
2019-01-13 18:37:03 +03:00
}
2019-01-11 13:23:50 +03:00
2019-01-13 18:37:03 +03:00
g, err := strconv.ParseInt(parts[2], 10, 32)
if err != nil {
2019-02-08 09:40:40 +03:00
return 0, err
2019-01-13 18:37:03 +03:00
}
2019-01-11 13:23:50 +03:00
2019-01-13 18:37:03 +03:00
b, err := strconv.ParseInt(parts[3], 10, 32)
if err != nil {
2019-02-08 09:40:40 +03:00
return 0, err
2019-01-13 18:37:03 +03:00
}
2019-01-11 13:23:50 +03:00
2019-02-27 18:24:53 +03:00
a := int64(255)
if len(parts) >= 5 {
//with alpha
2019-02-28 22:20:59 +03:00
//a, err = strconv.ParseInt(parts[4], 10, 32)
//if err != nil {
// return 0, err
//}
2019-02-27 18:24:53 +03:00
}
c := color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)}
2019-01-13 18:37:03 +03:00
m.colors[parts[0]] = &c
2019-02-08 09:40:40 +03:00
count++
2019-01-13 18:37:03 +03:00
}
}
2019-01-11 13:23:50 +03:00
2019-02-08 09:40:40 +03:00
return count, nil
2019-01-10 17:21:34 +03:00
}
2019-01-11 12:49:19 +03:00
2019-02-08 09:40:40 +03:00
func (m *ColorMapping) LoadVFSColors(useLocal bool, filename string) (int, error) {
2019-07-30 14:56:42 +03:00
buffer, err := vfs.FSByte(useLocal, filename)
2019-01-13 18:37:03 +03:00
if err != nil {
2019-02-08 09:40:40 +03:00
return 0, err
2019-01-13 18:37:03 +03:00
}
2019-01-11 13:23:50 +03:00
2019-01-13 18:37:03 +03:00
log.WithFields(logrus.Fields{"size": len(buffer),
"filename": filename,
2019-07-30 14:44:12 +03:00
"useLocal": useLocal}).Info("Loading colors")
2019-01-11 13:23:50 +03:00
2019-01-13 18:37:03 +03:00
return m.LoadBytes(buffer)
2019-01-11 12:49:19 +03:00
}
2019-01-11 13:44:17 +03:00
func NewColorMapping() *ColorMapping {
2019-06-20 08:14:14 +03:00
extendedpalette, err := NewPalette(vfs.FSMustByte(false, "/pics/unifieddyes_palette_extended.png"))
if err != nil {
panic(err)
}
2019-06-20 08:55:18 +03:00
scanner := bufio.NewScanner(bytes.NewReader(vfs.FSMustByte(false, "/extended_palette.txt")))
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
}
2019-06-20 08:14:14 +03:00
return &ColorMapping{
2019-06-20 08:55:18 +03:00
colors: make(map[string]*color.RGBA),
extendedpaletteblock: extendedpaletteblock,
extendedpalette: extendedpalette,
2019-06-20 08:14:14 +03:00
}
2019-01-11 12:49:19 +03:00
}