-
Notifications
You must be signed in to change notification settings - Fork 174
/
split-sheet.go
92 lines (73 loc) · 1.61 KB
/
split-sheet.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
package main
import (
"encoding/json"
"flag"
"fmt"
"image"
"image/color"
"image/png"
"io"
"os"
"path/filepath"
"golang.org/x/image/draw"
)
type Atlas struct {
Frames []Frame
Meta Meta
}
type Frame struct {
Frame struct {
X, Y, W, H int
}
}
type Meta struct {
FrameTags []FrameTag
}
type FrameTag struct {
Name string
From int
}
var (
folder = flag.String("folder", "", "output folder")
transparent = flag.Bool("transparent", true, "transparent background")
)
func handlePng(infile io.Reader, outfile io.Writer) error {
return nil
}
func main() {
flag.Parse()
if flag.Arg(0) == "" || flag.Arg(1) == "" || flag.Arg(2) == "" {
flag.Usage()
os.Exit(1)
}
infile, err := os.Open(flag.Arg(0))
check(err)
defer infile.Close()
atlasfile, err := os.Open(flag.Arg(1))
check(err)
defer atlasfile.Close()
source, err := png.Decode(infile)
check(err)
var atlas Atlas
check(json.NewDecoder(atlasfile).Decode(&atlas))
for _, frametag := range atlas.Meta.FrameTags {
frame := atlas.Frames[frametag.From].Frame
target := image.NewRGBA(image.Rect(0, 0, frame.W, frame.H))
if !*transparent {
draw.Draw(target, target.Bounds(), &image.Uniform{color.White}, image.ZP, draw.Src)
}
draw.Draw(target, target.Bounds(), source, image.Pt(frame.X, frame.Y), draw.Src)
outname := filepath.Join(flag.Arg(2), "gopher-"+frametag.Name+".png")
os.MkdirAll(filepath.Dir(outname), 0755)
outfile, err := os.Create(outname)
check(err)
check(png.Encode(outfile, target))
outfile.Close()
}
}
func check(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "failed: %v\n", err)
os.Exit(1)
}
}