/
imgsplit.go
132 lines (113 loc) · 2.74 KB
/
imgsplit.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
package main
import (
"bytes"
"encoding/xml"
"fmt"
"image"
"image/color"
_ "image/jpeg"
"image/png"
"io/ioutil"
"log"
"os"
"path/filepath"
)
// Can be used to split a image sliced by SpriteDecomposer
type Sprite struct {
Image string `xml:"image,attr"`
TransparentColor string `xml:"transparentColor,attr"`
Animation Animation `xml:"animation"`
}
func loopOverBounds(r image.Rectangle, operator func(x, y int)) {
for x := r.Min.X; x < r.Max.X; x++ {
for y := r.Min.Y; y < r.Max.Y; y++ {
operator(x, y)
}
}
}
func strToColor(str string) color.Color {
ret := color.RGBA{R: 0, G: 0, B: 0, A: 0}
fmt.Sscanf(str, "%x", &ret.R)
fmt.Sscanf(str, "%x", &ret.G)
fmt.Sscanf(str, "%x", &ret.B)
return ret
}
func (s *Sprite) Split(img image.Image, outputDir string, prefix string) error {
return s.Animation.SplitTo(img, outputDir, prefix)
}
type Animation struct {
Cuts []Cut `xml:"cut"`
}
func (a *Animation) SplitTo(img image.Image, out string, prefix string) error {
parts := len(a.Cuts)
padCount := len(fmt.Sprintf("%d", parts))
nameFmt := fmt.Sprintf("-%%0%dd.png", padCount)
var err error
for i, _ := range a.Cuts {
err = a.Cuts[i].SplitTo(img, filepath.Join(out, prefix+fmt.Sprintf(nameFmt, i)))
if err != nil {
return err
}
}
return nil
}
type Cut struct {
W int `xml:"w,attr"`
H int `xml:"h,attr"`
X int `xml:"x,attr"`
Y int `xml:"y,attr"`
Row int `xml:"row,attr"`
Col int `xml:"col,attr"`
}
type subImager interface {
SubImage(r image.Rectangle) image.Image
}
func (c *Cut) SplitTo(img image.Image, outfile string) error {
if img, ok := img.(subImager); ok {
sub := img.SubImage(c.AsRect())
file, err := os.Create(outfile)
if err != nil {
return err
}
defer file.Close()
return png.Encode(file, sub)
} else {
return fmt.Errorf("Cannot take a SubImage")
}
}
func (c *Cut) AsRect() image.Rectangle {
return image.Rectangle{
Min: image.Point{X: c.X, Y: c.Y},
Max: image.Point{X: c.X + c.W, Y: c.Y + c.H},
}
}
func splitIt(xmlFile string) {
buf, err := ioutil.ReadFile(xmlFile)
if err != nil {
log.Printf("Error reading file. %v", err)
return
}
sprite := &Sprite{}
err = xml.Unmarshal(buf, sprite)
log.Printf("Sprite file: %v", sprite)
if err != nil {
log.Printf("Unmarshal error: %v", err)
return
}
dir, _ := filepath.Split(xmlFile)
srcImage := filepath.Join(dir, sprite.Image)
buf, err = ioutil.ReadFile(srcImage)
if err != nil {
log.Printf("Error reading image file. Cause: %v", err)
return
}
fullImage, _, err := image.Decode(bytes.NewBuffer(buf))
if err != nil {
log.Printf("Error decoding image. Cause: %v", err)
return
}
err = sprite.Split(fullImage, dir, "part")
if err != nil {
log.Printf("Error spliting the image. Cause: %v", err)
}
}