-
Notifications
You must be signed in to change notification settings - Fork 5
/
pc_unpack.go
executable file
·166 lines (146 loc) · 3.62 KB
/
pc_unpack.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
Pc_unpack converts Stunt GP textures to one of the more popular formats.
This program accepts PC, DC and PS2 textures.
It can output jpg or png files.
*/
package main
import (
"errors"
"fmt"
"image"
"image/jpeg"
"image/png"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/spf13/pflag"
"github.com/stuntkit/stunt_gp_tools/pkg/texture"
)
// flags
var (
outputName string
)
func parseFlags() {
pflag.StringVarP(&outputName, "output", "o", "", "name of the output file")
pflag.Parse()
}
func usage() {
fmt.Println("Unpacks image from texture format used by Stunt GP")
fmt.Println("Flags:")
pflag.PrintDefaults()
}
func main() {
failed := false
parseFlags()
args := pflag.Args()
if len(args) < 1 {
usage()
os.Exit(1)
}
if outputName != "" && len(args) > 1 {
fmt.Println("Output name can only be used with one input file")
usage()
os.Exit(1)
}
for _, inputName := range args {
f, err := os.Stat(inputName)
if err != nil {
fmt.Printf("Failed to get info about %s: %s", inputName, err)
failed = true
}
if f.IsDir() {
walkFunc := getWalkFunc(&failed)
err := filepath.Walk(inputName, walkFunc)
if err != nil {
fmt.Printf("Failed to unpack dir %s: %s", inputName, err)
failed = true
}
} else {
fmt.Printf("%s, %d", outputName, len(args))
if outputName == "" || len(args) > 1 {
outputName = strings.TrimSuffix(inputName, filepath.Ext(inputName)) + ".png"
}
err := unpackTexture(inputName, outputName)
if err != nil {
fmt.Printf("Failed to unpack %s: %s", inputName, err)
failed = true
}
}
}
if failed {
os.Exit(1)
}
}
func getWalkFunc(failed *bool) filepath.WalkFunc {
return func(path string, info fs.FileInfo, err error) error {
if !info.IsDir() {
outputName = strings.TrimSuffix(path, filepath.Ext(path)) + ".png"
err := unpackTexture(path, outputName)
if err != nil {
fail := true
failed = &fail
}
}
return nil
}
}
func unpackTexture(inputName, outputName string) error {
// file deepcode ignore PT: This is CLI tool, this is intended to be traversable
file, err := os.Open(inputName)
if err != nil {
return fmt.Errorf("couldn't open file %s: %s", inputName, err)
}
config, format, err := image.DecodeConfig(file)
if err != nil {
return fmt.Errorf("couldn't read image file config %s: %s", inputName, err)
}
if format != texture.FormatName {
return nil
}
fmt.Printf("Unpacking %s: %dx%d\n", inputName, config.Width, config.Height)
_, err = file.Seek(0, 0)
if err != nil {
return fmt.Errorf("couldn't seek in image file %s: %s", inputName, err)
}
img, _, err := image.Decode(file)
if err != nil {
return fmt.Errorf("couldn't read image file %s: %s", inputName, err)
}
err = file.Close()
if err != nil {
return fmt.Errorf("couldn't close image file %s: %s", inputName, err)
}
outputFile, err := os.Create(outputName)
if err != nil {
return fmt.Errorf("couldn't create output image file %s: %s", outputName, err)
}
ext := filepath.Ext(strings.ToLower(outputName))
switch ext {
case ".jpg":
fallthrough
case ".jpeg":
o := jpeg.Options{Quality: 90}
err = jpeg.Encode(outputFile, img, &o)
case ".png":
err = png.Encode(outputFile, img)
case ".pc":
err = texture.Encode(outputFile, img)
case ".dc":
encoder := texture.Encoder{
Compress: true,
Format: texture.FDreamcast,
}
err = encoder.Encode(outputFile, img)
default:
err = errors.New("unknown output format")
}
if err != nil {
return fmt.Errorf("couldn't pack output image %s: %s", outputName, err)
}
err = outputFile.Close()
if err != nil {
return fmt.Errorf("couldn't close image file %s: %s", outputName, err)
}
return nil
}