forked from Encritary/BedrockSkinConverter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bs2i.go
74 lines (61 loc) · 1.41 KB
/
bs2i.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"fmt"
"image"
_ "image/gif"
_ "image/jpeg"
"image/png"
_ "image/png"
"io/ioutil"
"os"
"path"
"strconv"
)
func main() {
if len(os.Args) < 4 {
fmt.Println("Usage: bs2i <x size> <y size> <raw skin path>")
os.Exit(0)
}
xSize, err := strconv.Atoi(os.Args[1])
if err != nil {
fmt.Println("Usage: bs2i <x size> <y size> <raw skin path>")
os.Exit(0)
}
ySize, err := strconv.Atoi(os.Args[2])
if err != nil {
fmt.Println("Usage: bs2i <x size> <y size> <raw skin path>")
os.Exit(0)
}
skinPath := os.Args[3]
stat, err := os.Stat(skinPath)
if err != nil {
fmt.Fprintln(os.Stderr, "Can't", err.Error())
os.Exit(1)
}
if stat.Size() != int64(xSize * ySize * 4) {
fmt.Fprintln(os.Stderr, "Invalid XY size given, size mismatch")
os.Exit(1)
}
bytes, err := ioutil.ReadFile(skinPath)
if err != nil {
fmt.Fprintln(os.Stderr, "Can't", err.Error())
os.Exit(1)
}
img := image.NewRGBA(image.Rect(0, 0, xSize, ySize))
img.Pix = bytes
basePath := path.Base(skinPath)
outputPath := basePath[:len(basePath)-len(path.Ext(skinPath))] + ".png"
output, err := os.Create(outputPath)
if err != nil {
fmt.Fprintln(os.Stderr, "Can't", err.Error())
os.Exit(1)
}
if err := png.Encode(output, img); err != nil {
fmt.Fprintln(os.Stderr, "Can't", err.Error())
os.Exit(1)
}
if err := output.Close(); err != nil {
fmt.Fprintln(os.Stderr, "Can't", err.Error())
os.Exit(1)
}
}