Skip to content

Commit dad4cfa

Browse files
authored
refactor: application is now have more coherent commands (#4)
changed the way application handles arguments, added help info
1 parent 7902524 commit dad4cfa

File tree

4 files changed

+129
-78
lines changed

4 files changed

+129
-78
lines changed

.idea/runConfigurations/go_build_github_com_krylphi_autotiler.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,14 @@ To tilesets like this:
4040
* [get yourself Go](https://go.dev/doc/install)
4141
* clone this repository or download sources.
4242
* put simple tileset image (for example 2x3_packed.png) to source folder
43-
* run ```go run . <src image> <dst image> [padding]```
44-
e.g. ```go run . ./examples/2x3_packed.png output.local.png```
45-
* you can optionally set padding for tiles in px. To do so you need to add desired padding as 3rd argument:
46-
e.g. ```go run . ./examples/2x3_packed.png output.local.png 1``` - this will create tilesets with 1 px margin and 2px spacing.
47-
* grab complete tilesets from `out` directory
43+
* run ```go run . -in <file_in> [-o <file_out>] [-p <padding>] [-e <export_type(16,28,48,all)>]```
44+
45+
e.g. ```go run . -in ./examples/2x3_packed.png -o ./out/output.local.png -p 1 -e 16 -e 28 -e 48```
46+
* you can optionally set padding for tiles in px. To do so you need to add desired padding as argument:
47+
48+
e.g. ```go run . -in ./examples/2x3_packed.png -p 1``` - this will create tilesets with 1 px margin and 2px spacing.
49+
* grab complete tilesets from directory specified in `-o`.
50+
* don't worry about filenames, as program will automatically prefix output files with necessary information. E.g. for options `-o ./out/output.local.png -e 16` output files will be `./out/16x1_terrain1_output.local.png` and `./out/16x1_terrain2_output.local.png`
4851
* enjoy
4952
* alternatively you can build an application using `make build` command to use it as a standalone application without Go
5053

internal/unpack/util.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func rotateLeft90(img *image.NRGBA) *image.NRGBA {
5454

5555
// scan copies pixel data from a source image to a destination slice.
5656
// It supports copying a rectangular region of the source image to the destination slice.
57-
// The function uses optimized copying for a single pixel (size == 4) and falls back to a generic copy for larger regions.
57+
// The function uses optimised copying for a single pixel (size == 4) and falls back to a generic copy for larger regions.
5858
//
5959
// Parameters:
6060
// - srcImg: The source image from which to copy pixel data.
@@ -63,7 +63,7 @@ func rotateLeft90(img *image.NRGBA) *image.NRGBA {
6363
// - x2, y2: The bottom-right coordinates of the rectangular region to copy from the source image.
6464
//
6565
// Returns:
66-
// - None
66+
// - None.
6767
func scan(srcImg *image.NRGBA, dstPx []uint8, x1, y1, x2, y2 int) {
6868
size := (x2 - x1) * 4
6969
srcStride := y1*srcImg.Stride + x1*4
@@ -99,7 +99,7 @@ func scan(srcImg *image.NRGBA, dstPx []uint8, x1, y1, x2, y2 int) {
9999
// - fn: The function to be executed in parallel. It should accept a channel of integers as its parameter.
100100
//
101101
// Returns:
102-
// - None
102+
// - None.
103103
func parallel(start, stop int, fn func(<-chan int)) {
104104
count := stop - start
105105
if count < 1 {

main.go

Lines changed: 117 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,37 @@ import (
2828
"fmt"
2929
"image"
3030
"image/png"
31+
"log"
3132
"os"
33+
"path/filepath"
3234
"strconv"
35+
"strings"
3336

3437
"github.com/krylphi/autotiler/internal/unpack"
3538
)
3639

37-
func main() {
40+
const (
41+
inKey = "in"
42+
paddingKey = "p"
43+
exportKey = "e"
44+
outKey = "o"
45+
)
3846

39-
inputFile := os.Args[1]
47+
const (
48+
export16 = "16"
49+
export28 = "28"
50+
export48 = "48"
51+
exportAll = "all"
52+
)
4053

41-
outputFile := os.Args[2]
42-
var err error
43-
var padding = 0
44-
if len(os.Args) > 3 {
45-
padding, err = strconv.Atoi(os.Args[3])
46-
if err != nil {
47-
panic(err)
48-
}
54+
func main() {
55+
args := parseArgs()
56+
inFiles, ok := args[inKey]
57+
if !ok {
58+
log.Print("Missing input file")
59+
os.Exit(1)
4960
}
50-
61+
inputFile := inFiles[0] // todo add handling for multiple inputs
5162
imgFile, err := os.Open(inputFile)
5263
if err != nil {
5364
panic(err)
@@ -59,85 +70,122 @@ func main() {
5970
panic(err)
6071
}
6172

62-
g := unpack.NewUnpacker(img, 2, 3, padding)
63-
if err := g.Init(2); err != nil {
64-
panic(err)
65-
}
66-
67-
// todo cleanup and parallel
68-
69-
canvas, err := g.From6to48Terrain1()
70-
if err != nil {
71-
panic(err)
72-
}
73-
file48t1, err := os.Create(fmt.Sprintf("out/12x4_terrain1_%s", outputFile))
74-
if err != nil {
75-
panic(err)
76-
}
77-
defer file48t1.Close()
78-
79-
err = png.Encode(file48t1, canvas)
73+
padding, err := strconv.Atoi(args[paddingKey][0])
8074
if err != nil {
8175
panic(err)
8276
}
8377

84-
canvas, err = g.From6to48Terrain2()
85-
if err != nil {
78+
unpacker := unpack.NewUnpacker(img, 2, 3, padding)
79+
if err := unpacker.Init(2); err != nil {
8680
panic(err)
8781
}
88-
file48t2, err := os.Create(fmt.Sprintf("out/12x4_terrain2_%s", outputFile))
89-
if err != nil {
90-
panic(err)
91-
}
92-
defer file48t2.Close()
9382

94-
err = png.Encode(file48t2, canvas)
95-
if err != nil {
96-
panic(err)
83+
exports, ok := args[exportKey]
84+
var exportTypes []string
85+
if !ok || len(exports) == 0 || exports[0] == exportAll {
86+
exportTypes = []string{export16, export28, export48}
87+
} else {
88+
exportTypes = exports
9789
}
9890

99-
canvas, err = g.From6to16Terrain1()
100-
if err != nil {
101-
panic(err)
91+
outputFile := "out.local.png"
92+
outFiles, ok := args[outKey]
93+
if ok {
94+
outputFile = outFiles[0]
10295
}
103-
file15t1, err := os.Create(fmt.Sprintf("out/16x1_terrain1_%s", outputFile))
104-
if err != nil {
105-
panic(err)
106-
}
107-
defer file15t1.Close()
10896

109-
err = png.Encode(file15t1, canvas)
110-
if err != nil {
111-
panic(err)
97+
for e := range exportTypes {
98+
exportType := exportTypes[e]
99+
switch exportType {
100+
case export16:
101+
err := produceTileset(unpacker.From6to16Terrain1, outputFile, "16x1_terrain1")
102+
if err != nil {
103+
panic(err)
104+
}
105+
err = produceTileset(unpacker.From6to16Terrain2, outputFile, "16x1_terrain2")
106+
if err != nil {
107+
panic(err)
108+
}
109+
case export28:
110+
err := produceTileset(unpacker.From6to28, outputFile, "14x2")
111+
if err != nil {
112+
panic(err)
113+
}
114+
case export48:
115+
err := produceTileset(unpacker.From6to48Terrain1, outputFile, "12x4_terrain1")
116+
if err != nil {
117+
panic(err)
118+
}
119+
err = produceTileset(unpacker.From6to48Terrain2, outputFile, "12x4_terrain2")
120+
if err != nil {
121+
panic(err)
122+
}
123+
}
112124
}
125+
}
113126

114-
canvas, err = g.From6to16Terrain2()
115-
if err != nil {
116-
panic(err)
117-
}
118-
file15t2, err := os.Create(fmt.Sprintf("out/16x1_terrain2_%s", outputFile))
119-
if err != nil {
120-
panic(err)
127+
func parseArgs() map[string][]string {
128+
if len(os.Args) < 2 {
129+
log.Print(
130+
"Usage: autotiler -in <file_in> [-o <file_out>] [-p <padding>] [-e <export_type(16,28,48,all)>]\n" +
131+
" -e can be repeated\n")
132+
os.Exit(1)
133+
}
134+
res := make(map[string][]string)
135+
allTilesets := false
136+
for i := 1; i < len(os.Args); i += 2 {
137+
key := strings.TrimPrefix(os.Args[i], "-")
138+
v, ok := res[key]
139+
value := os.Args[i+1]
140+
if key == exportKey && allTilesets {
141+
continue
142+
}
143+
if key == exportKey && strings.EqualFold(value, exportAll) {
144+
allTilesets = true
145+
v = []string{"all"}
146+
res[key] = v
147+
continue
148+
}
149+
if ok {
150+
v = append(v, value)
151+
} else {
152+
v = []string{value}
153+
}
154+
res[key] = v
121155
}
122-
defer file15t2.Close()
156+
return res
157+
}
123158

124-
err = png.Encode(file15t2, canvas)
159+
func produceTileset(unpackFunction func() (*image.NRGBA, error), outputPath, exportType string) error {
160+
canvas, err := unpackFunction()
125161
if err != nil {
126-
panic(err)
162+
return err
127163
}
128-
129-
canvas, err = g.From6to28()
164+
cleanPath := filepath.Clean(outputPath)
165+
outputFile := fmt.Sprintf("%s_%s", exportType, filepath.Base(cleanPath))
166+
outputFile = filepath.Join(filepath.Dir(cleanPath), outputFile)
167+
file, err := os.Create(outputFile)
130168
if err != nil {
131-
panic(err)
169+
return err
132170
}
133-
file1, err := os.Create(fmt.Sprintf("out/14x2_%s", outputFile))
171+
defer func(file *os.File) {
172+
err := file.Close()
173+
if err != nil {
174+
log.Println(err)
175+
}
176+
}(file)
177+
err = png.Encode(file, canvas)
134178
if err != nil {
135-
panic(err)
179+
return err
136180
}
137-
defer file1.Close()
181+
return nil
182+
}
138183

139-
err = png.Encode(file1, canvas)
140-
if err != nil {
141-
panic(err)
184+
//nolint:unused //debug function
185+
func printArgs(args map[string][]string) {
186+
for key, params := range args {
187+
for _, param := range params {
188+
log.Printf("%s: %s\n", key, param)
189+
}
142190
}
143191
}

0 commit comments

Comments
 (0)