-
Notifications
You must be signed in to change notification settings - Fork 3
/
file.go
325 lines (260 loc) · 6.87 KB
/
file.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package aseprite
import (
"bytes"
"encoding/binary"
"errors"
"image"
"image/color"
"image/draw"
"io"
"math"
"time"
"github.com/askeladdk/aseprite/internal/blend"
)
var errInvalidMagic = errors.New("invalid magic number")
type cel struct {
image image.Image
mask image.Uniform
data []byte
}
func makeCelImage8(f *file, bounds image.Rectangle, opacity byte, pix []byte) cel {
img := image.Paletted{
Pix: pix,
Stride: bounds.Dx(),
Rect: bounds,
Palette: f.palette,
}
mask := image.Uniform{color.Alpha{opacity}}
return cel{&img, mask, nil}
}
func makeCelImage16(f *file, bounds image.Rectangle, opacity byte, pix []byte) cel {
img := image.Gray16{
Pix: pix,
Stride: bounds.Dx() * 2,
Rect: bounds,
}
mask := image.Uniform{color.Alpha{opacity}}
return cel{&img, mask, nil}
}
func makeCelImage32(f *file, bounds image.Rectangle, opacity byte, pix []byte) cel {
img := image.NRGBA{
Pix: pix,
Stride: bounds.Dx() * 4,
Rect: bounds,
}
mask := image.Uniform{color.Alpha{opacity}}
return cel{&img, mask, nil}
}
type layer struct {
flags uint16
blendMode uint16
opacity byte
data []byte
}
func (l *layer) Parse(raw []byte) error {
if typ := binary.LittleEndian.Uint16(raw[2:]); typ == 2 {
return errors.New("tilemap layers not supported")
}
l.flags = binary.LittleEndian.Uint16(raw)
l.blendMode = binary.LittleEndian.Uint16(raw[10:])
l.opacity = raw[12]
return nil
}
type chunk struct {
typ int
raw []byte
}
func (c chunk) Reader() io.Reader {
return bytes.NewReader(c.raw)
}
func (c *chunk) Read(raw []byte) ([]byte, error) {
chunkLen := binary.LittleEndian.Uint32(raw)
c.typ = int(binary.LittleEndian.Uint16(raw[4:]))
c.raw = raw[6:chunkLen]
return raw[chunkLen:], nil
}
type frame struct {
dur time.Duration
chunks []chunk
cels []cel
}
func (f *frame) Read(raw []byte) ([]byte, error) {
if magic := binary.LittleEndian.Uint16(raw[4:]); magic != 0xF1FA {
return nil, errInvalidMagic
}
// frameLen := binary.LittleEndian.Uint32(raw[0:])
oldChunks := binary.LittleEndian.Uint16(raw[6:])
durationMS := binary.LittleEndian.Uint16(raw[8:])
newChunks := binary.LittleEndian.Uint32(raw[12:])
f.dur = time.Millisecond * time.Duration(durationMS)
nchunks := int(newChunks)
if nchunks == 0 {
nchunks = int(oldChunks)
}
f.chunks = make([]chunk, nchunks)
raw = raw[16:]
for i := 0; i < nchunks; i++ {
var c chunk
raw, _ = c.Read(raw)
f.chunks[i] = c
}
return raw, nil
}
type file struct {
framew int
frameh int
flags uint16
bpp uint16
transparent uint8
palette color.Palette
frames []frame
layers []layer
makeCel func(f *file, bounds image.Rectangle, opacity byte, pix []byte) cel
}
func (f *file) ReadFrom(r io.Reader) (int64, error) {
var hdr [128]byte
raw := hdr[:]
if n, err := io.ReadFull(r, raw); err != nil {
return int64(n), err
}
if magic := binary.LittleEndian.Uint16(raw[4:]); magic != 0xA5E0 {
return 128, errInvalidMagic
}
if pixw, pixh := raw[34], raw[35]; pixw != pixh {
return 128, errors.New("unsupported pixel ratio")
}
f.bpp = binary.LittleEndian.Uint16(raw[12:])
f.flags = binary.LittleEndian.Uint16(raw[14:])
f.frames = make([]frame, 0, binary.LittleEndian.Uint16(raw[6:]))
f.framew = int(binary.LittleEndian.Uint16(raw[8:]))
f.frameh = int(binary.LittleEndian.Uint16(raw[10:]))
f.palette = make(color.Palette, binary.LittleEndian.Uint16(raw[32:]))
f.transparent = raw[28]
switch f.bpp {
case 8:
f.makeCel = makeCelImage8
case 16:
f.makeCel = makeCelImage16
case 32:
f.makeCel = makeCelImage32
default:
return 0, errors.New("invalid color depth")
}
for i := range f.palette {
f.palette[i] = color.Black
}
f.palette[f.transparent] = color.Transparent
fileSize := int64(binary.LittleEndian.Uint32(raw))
raw = make([]byte, fileSize-128)
if n, err := io.ReadFull(r, raw); err != nil {
return int64(128 + n), err
}
for len(raw) > 0 {
var fr frame
var err error
if raw, err = fr.Read(raw); err != nil {
return fileSize, err
}
f.frames = append(f.frames, fr)
}
return fileSize, nil
}
func (f *file) buildAtlas() (atlas draw.Image, framesr []image.Rectangle) {
var atlasr image.Rectangle
atlasr, framesr = makeAtlasFrames(len(f.frames), f.framew, f.frameh)
switch f.bpp {
case 8:
atlas = image.NewPaletted(atlasr, f.palette)
case 16:
atlas = image.NewGray16(atlasr)
default:
atlas = image.NewRGBA(atlasr)
}
framebounds := image.Rect(0, 0, f.framew, f.frameh)
dstblend := image.NewRGBA(framebounds)
dst := image.NewRGBA(framebounds)
transparent := &image.Uniform{color.Transparent}
for i, fr := range f.frames {
draw.Draw(dst, framebounds, transparent, image.Point{}, draw.Src)
for layer, c := range fr.cels {
if c.image == nil {
continue
}
src := c.image
sr := src.Bounds()
sp := sr.Min
if mode := f.layers[layer].blendMode; mode > 0 && int(mode) < len(blend.Modes) {
draw.Draw(dstblend, framebounds, transparent, image.Point{}, draw.Src)
blend.Blend(dstblend, sr.Sub(sp), src, sp, dst, sp, blend.Modes[mode])
src = dstblend
sp = image.Point{}
}
draw.DrawMask(dst, sr, src, sp, &c.mask, image.Point{}, draw.Over)
}
draw.Draw(atlas, framesr[i], dst, image.Point{}, draw.Src)
}
return
}
func (f *file) buildUserData() []byte {
n := 0
for _, l := range f.layers {
if l.flags&1 != 0 {
n += len(l.data)
}
}
for _, fr := range f.frames {
for _, c := range fr.cels {
n += len(c.data)
}
}
return make([]byte, 0, n)
}
func (f *file) buildLayerData(userdata []byte) [][]byte {
ld := make([][]byte, 0, len(f.layers))
for _, l := range f.layers {
if l.flags&1 != 0 && len(l.data) > 0 {
ofs := len(userdata)
userdata = append(userdata, l.data...)
ld = append(ld, userdata[ofs:])
}
}
return ld
}
func (f *file) buildFrames(framesr []image.Rectangle, userdata []byte) ([]Frame, []byte) {
frames := make([]Frame, len(f.frames))
for i, fr := range f.frames {
frames[i].Duration = fr.dur
frames[i].Bounds = framesr[i]
frames[i].Data = make([][]byte, 0, len(fr.cels))
for _, c := range fr.cels {
if nd := len(c.data); nd > 0 {
ofs := len(userdata)
userdata = append(userdata, c.data...)
frames[i].Data = append(frames[i].Data, userdata[ofs:])
}
}
}
return frames, userdata
}
func makeAtlasFrames(nframes, framew, frameh int) (atlasr image.Rectangle, framesr []image.Rectangle) {
fw, fh := factorPowerOfTwo(nframes)
if framew > frameh {
fw, fh = fh, fw
}
atlasr = image.Rect(0, 0, fw*framew, fh*frameh)
for i := 0; i < nframes; i++ {
x, y := i%fw, i/fw
framesr = append(framesr, image.Rectangle{
Min: image.Pt(x*framew, y*frameh),
Max: image.Pt((x+1)*framew, (y+1)*frameh),
})
}
return
}
// factorPowerOfTwo computes n=a*b, where a, b are powers of two and a >= b.
func factorPowerOfTwo(n int) (a, b int) {
x := int(math.Ceil(math.Log2(float64(n))))
a = 1 << (x - x/2)
b = 1 << (x / 2)
return
}