optimization

This commit is contained in:
Dawid Wysokiński 2020-08-01 12:12:21 +02:00
parent 79fc85bed9
commit 2f88a9f177
3 changed files with 19 additions and 16 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 621 KiB

After

Width:  |  Height:  |  Size: 77 KiB

View File

@ -24,12 +24,12 @@ func main() {
ContinentNumbers: true,
Markers: []*generator.Marker{
&generator.Marker{
Color: "#fff",
Color: "#f0f",
Villages: villages,
Larger: false,
},
&generator.Marker{
Color: "#ff0",
Color: "#fff",
Villages: []*models.Village{
&models.Village{
X: 500,

View File

@ -19,6 +19,7 @@ const (
defaultGridLineColor = "#fff"
defaultContinentNumberColor = "#fff"
defaultMapSize = 1000
defaultQuality = 80
)
type Marker struct {
@ -39,6 +40,7 @@ type Config struct {
Scale float32
CenterX int
CenterY int
Quality int
}
func (cfg *Config) init() {
@ -54,6 +56,9 @@ func (cfg *Config) init() {
if cfg.MapSize <= 0 {
cfg.MapSize = defaultMapSize
}
if cfg.Quality <= 0 {
cfg.Quality = defaultQuality
}
if cfg.Scale < 1 {
cfg.Scale = 1
}
@ -78,15 +83,17 @@ func Generate(cfg Config) error {
imgHalfHeight := imgHalfWidth
g := new(errgroup.Group)
backgroundColor, err := parseHexColorFast(cfg.BackgroundColor)
if err != nil {
return errors.Wrap(err, "map-generator")
}
if cfg.BackgroundColor != defaultBackgroundColor {
backgroundColor, err := parseHexColorFast(cfg.BackgroundColor)
if err != nil {
return errors.Wrap(err, "map-generator")
}
// Background.
for y := 0; y < cfg.MapSize; y++ {
for x := 0; x < cfg.MapSize; x++ {
img.Set(x, y, backgroundColor)
// Background.
for y := 0; y < cfg.MapSize; y++ {
for x := 0; x < cfg.MapSize; x++ {
img.Set(x, y, backgroundColor)
}
}
}
@ -129,11 +136,7 @@ func Generate(cfg Config) error {
for y := mapSizeDividedBy10; y < cfg.MapSize; y += mapSizeDividedBy10 {
for x := 0; x < cfg.MapSize; x++ {
img.Set(x, y, gridLineColor)
}
}
for x := mapSizeDividedBy10; x < cfg.MapSize; x += mapSizeDividedBy10 {
for y := 0; y < cfg.MapSize; y++ {
img.Set(x, y, gridLineColor)
img.Set(y, x, gridLineColor)
}
}
}
@ -170,7 +173,7 @@ func Generate(cfg Config) error {
}, draw.Src)
if err := jpeg.Encode(cfg.Destination, centered, &jpeg.Options{
Quality: 80,
Quality: cfg.Quality,
}); err != nil {
return errors.Wrap(err, "map-generator")
}