-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpng.go
50 lines (42 loc) · 876 Bytes
/
png.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"fmt"
"github.com/fogleman/gg"
"image/png"
"log"
"os"
"path/filepath"
"strings"
)
func pngCmd(inputs []string) {
for _, path := range inputs {
err := toPng(path)
if err != nil {
log.Println(err.Error())
}
}
}
func toPng(path string) error {
m, err := LoadMap(path)
if err != nil {
return err
}
dc := gg.NewContext(m.Data.Width, m.Data.Height)
for y := 0; y < m.Data.Height; y++ {
for x := 0; x < m.Data.Width; x++ {
colorID := m.Data.Colors[y*m.Data.Width+x]
dc.SetColor(colors[colorID])
dc.SetPixel(x, y)
}
}
f, err := os.Create(strings.TrimSuffix(filepath.Base(path), ".dat") + ".png")
if err != nil {
return fmt.Errorf("failed to create output file: %w", err)
}
defer f.Close()
err = png.Encode(f, dc.Image())
if err != nil {
return fmt.Errorf("failed to save image: %w", err)
}
return nil
}