-
Notifications
You must be signed in to change notification settings - Fork 3
/
aseprite.go
125 lines (97 loc) · 2.82 KB
/
aseprite.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
// Package aseprite implements a decoder for Aseprite sprite files.
//
// Layers are flattened, blending modes are applied,
// and frames are arranged on a single texture atlas.
// Invisible and reference layers are ignored.
// Tilesets and external files are not supported.
//
// Aseprite file format spec: https://github.com/aseprite/aseprite/blob/main/docs/ase-file-specs.md
package aseprite
import (
"image"
"image/color"
"io"
"time"
)
// LoopDirection enumerates all loop animation directions.
type LoopDirection uint8
const (
Forward LoopDirection = iota
Reverse
PingPong
PingPongReverse
)
// Tag is an animation tag.
type Tag struct {
// Name is the name of the tag. Can be duplicate.
Name string
// Lo is the first frame in the animation.
Lo uint16
// Hi is the last frame in the animation.
Hi uint16
// Repeat specifies how many times to repeat the animation.
Repeat uint16
// LoopDirection is the looping direction of the animation.
LoopDirection LoopDirection
}
// Frame represents a single frame in the sprite.
type Frame struct {
// Bounds is the image bounds of the frame in the sprite's atlas.
Bounds image.Rectangle
// Duration is the time in seconds that the frame should be displayed for
// in a tag animation loop.
Duration time.Duration
// Data lists all optional user data set in the cels that make up the frame.
// The data of invisible and reference layers is not included.
Data [][]byte
}
// Slice represents a single slice.
type Slice struct {
// Bounds is the bounds of the image in the texture atlas.
Bounds image.Rectangle
// Center is the 9-slices center relative to Bounds.
Center image.Rectangle
// Pivot is the pivot point relative to Bounds.
Pivot image.Point
// Name is the name of the slice. Can be duplicate.
Name string
// Data is optional user data.
Data []byte
// Color is the slice color.
Color color.Color
}
// Aseprite holds the results of a parsed Aseprite image file.
type Aseprite struct {
// Image contains all frame images in a single image.
// Frame bounds specify where the frame images are located.
image.Image
// Frames lists all frames that make up the sprite.
Frames []Frame
// Tags lists all animation tags.
Tags []Tag
// Slices lists all slices.
Slices []Slice
// LayerData lists the user data of all visible layers.
LayerData [][]byte
}
func (spr *Aseprite) readFrom(r io.Reader) error {
var f file
if _, err := f.ReadFrom(r); err != nil {
return err
}
f.initPalette()
if err := f.initLayers(); err != nil {
return err
}
if err := f.initCels(); err != nil {
return err
}
var framesr []image.Rectangle
spr.Image, framesr = f.buildAtlas()
userdata := f.buildUserData()
spr.Frames, userdata = f.buildFrames(framesr, userdata)
spr.LayerData = f.buildLayerData(userdata)
spr.Tags = f.buildTags()
spr.Slices = f.buildSlices()
return nil
}