-
Notifications
You must be signed in to change notification settings - Fork 6
/
Aseprite.js
725 lines (687 loc) · 19.2 KB
/
Aseprite.js
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
const zlib = require('zlib');
/**
* Aseprite Class to consume an Aseprite file and get information from it
*/
class Aseprite {
constructor(buffer, name) {
this._offset = 0;
this._buffer = buffer;
this.frames = [];
this.layers = [];
this.slices = [];
this.fileSize;
this.numFrames;
this.width;
this.height;
this.colorDepth;
this.paletteIndex;
this.numColors;
this.pixelRatio;
this.name = name;
this.tags = [];
this.tilesets = [];
}
//#region Static Constants
/**
* Map of the possible flag values for a Layer
*/
static LAYER_FLAG_MAP = {
visible: 0b1,
editable: 0b10,
lockMovement: 0b100,
background: 0b1000,
preferLinkedCels: 0b10000,
collapsedGroup: 0b100000,
reference: 0b1000000
};
//#endregion Static Constants
/**
* Reads the next byte (8-bit unsigned) value in the buffer
*
* @returns {number}
*/
readNextByte() {
const nextByte = this._buffer.readUInt8(this._offset);
this._offset += 1;
return nextByte;
}
/**
* Reads a byte (8-bit unsigned) value in the buffer at a specific location
*
* @param {number} offset - Offset location in the buffer
* @returns {number}
*/
readByte(offset) {
return this._buffer.readUInt8(offset);
}
/**
* Reads the next word (16-bit unsigned) value in the buffer
*
* @returns {number}
*/
readNextWord() {
const word = this._buffer.readUInt16LE(this._offset);
this._offset += 2;
return word;
}
/**
* Reads a word (16-bit unsigned) value at a specific location
*
* @param {number} offset - Offset location in the buffer
* @returns {number}
*/
readWord(offset) {
return this._buffer.readUInt16LE(offset);
}
/**
* Reads the next short (16-bit signed) value in the buffer
*
* @returns {number}
*/
readNextShort() {
const short = this._buffer.readInt16LE(this._offset);
this._offset += 2;
return short;
}
/**
* Reads a short (16-bit signed) value at a specific location
*
* @param {number} offset - Offset location in the buffer
* @returns {number}
*/
readShort(offset) {
return this._buffer.readInt16LE(offset);
}
/**
* Reads the next DWord (32-bit unsigned) value from the buffer
*
* @returns {number}
*/
readNextDWord() {
const dWord = this._buffer.readUInt32LE(this._offset);
this._offset += 4;
return dWord;
}
/**
* Reads a DWord (32-bit unsigned) value at a specific location
*
* @param {number} offset - Offset location in the buffer
* @returns {number}
*/
readDWord(offset) {
return this._buffer.readUInt32LE(offset);
}
/**
* Reads the next long (32-bit signed) value from the buffer
*
* @returns {number}
*/
readNextLong() {
const long = this._buffer.readInt32LE(this._offset);
this._offset += 4;
return long;
}
/**
* Reads a long (32-bit signed) value at a specific location
*
* @param {number} offset - Offset location in the buffer
* @returns {number}
*/
readLong(offset) {
return this._buffer.readInt32LE(offset);
}
/**
* Reads the next fixed (32-bit fixed point 16.16) value from the buffer
*
* @returns {number}
*/
readNextFixed() {
const fixed = this._buffer.readFloatLE(this._offset);
this._offset += 4;
return fixed;
}
/**
* Reads a fixed (32-bit fixed point 16.16) value at a specific location
* @param {number} offset - Offset location in the buffer
* @returns {number}
*/
readFixed(offset) {
return this._buffer.readFloatLE(offset);
}
/**
* Reads the next numBytes bytes and creates a string from the buffer
*
* @param {number} numBytes - Number of bytes to read
* @returns {string}
*/
readNextBytes(numBytes) {
let strBuff = Buffer.alloc(numBytes);
for (let i = 0; i < numBytes; i++) {
strBuff.writeUInt8(this.readNextByte(), i);
}
return strBuff.toString();
}
/**
* Copy the next numBytes bytes of the buffer into a new buffer
*
* @param {number} numBytes - Number of bytes to read
* @returns {Buffer}
*/
readNextRawBytes(numBytes) {
let buff = Buffer.alloc(numBytes);
for (let i = 0; i < numBytes; i++) {
buff.writeUInt8(this.readNextByte(), i);
}
return buff;
}
/**
* Create a new buffer with numBytes size, offset by a value, from a buffer
*
* @param {number} numBytes - Number of bytes to read
* @param {Buffer} b - Buffer to read from
* @param {number} offset - Offset value to start reading from
* @returns {Buffer}
*/
readRawBytes(numBytes, b, offset) {
let buff = Buffer.alloc(numBytes - offset);
for (let i = 0; i < numBytes - offset; i++) {
buff.writeUInt8(b.readUInt8(offset + i), i);
}
return buff;
}
/**
* Reads the next word to get the length of the string, then reads the string
* and returns it
*
* @returns {string}
*/
readNextString() {
const numBytes = this.readNextWord();
return this.readNextBytes(numBytes);
}
/**
* Skips a number of bytes in the buffer
*
* @param {number} numBytes - Number of bytes to skip
*/
skipBytes(numBytes) {
this._offset += numBytes;
}
/**
* Translates the listed flags for a layer into true/false values
* dictating whether or not that flag is "on"(true) or "off"(false)
*
* @private
* @param {object} flagMap Object of flags and their binary value
* @param {number} flagValue Value of the layer flags
* @returns {object} Map of the flags and if they are true or false
*/
#translateFlags(flagValue) {
// Create an object to put the flags and their "toggle" (true/false)
const translatedFlagMap = {};
// Iterate through the flags and their binary value, use bitwise op
// to see if the flag is present in the "layer flags" and add the
// flag to the map with the accompanying "toggle"
for( const flag in Aseprite.LAYER_FLAG_MAP) {
translatedFlagMap[flag] = (flagValue & Aseprite.LAYER_FLAG_MAP[flag]) == Aseprite.LAYER_FLAG_MAP[flag];
}
return translatedFlagMap;
}
/**
* Reads the 128-byte header of an Aseprite file and stores the information
*
* @returns {number} Number of frames in the file
*/
readHeader() {
this.fileSize = this.readNextDWord();
// Consume the next word (16-bit unsigned) value in the buffer
// to skip the "Magic number" (0xA5E0)
this.readNextWord();
this.numFrames = this.readNextWord();
this.width = this.readNextWord();
this.height = this.readNextWord();
this.colorDepth = this.readNextWord();
/**
* Skip 14 bytes to account for:
* Dword - Layer opacity flag
* Word - deprecated speed (ms) between frame
* Dword - 0 value
* Dword - 0 value
*/
this.skipBytes(14);
this.paletteIndex = this.readNextByte();
// Skip 3 bytes for empty data
this.skipBytes(3);
this.numColors = this.readNextWord();
const pixW = this.readNextByte();
const pixH = this.readNextByte();
this.pixelRatio = `${pixW}:${pixH}`;
/**
* Skip 92 bytes to account for:
* Short - X position of the grid
* Short - Y position of the grid
* Word - Grid width
* Word - Grid height, defaults to 0 if there is no grid
* (Defaults to 16x16 if there is no grid size)
* Last 84 bytes is set to 0 for future use
*/
this.skipBytes(92);
return this.numFrames;
}
/**
* Reads a frame and stores the information
*/
readFrame() {
const bytesInFrame = this.readNextDWord();
// skip bytes for the magic number (0xF1FA)
// TODO: Add a check in to make sure the magic number is correct
// (this should help to make sure we're doing what we're supposed to)
this.skipBytes(2);
// TODO Use the old chunk of data if `newChunk` is 0
const oldChunk = this.readNextWord();
const frameDuration = this.readNextWord();
// Skip 2 bytes that are reserved for future use
this.skipBytes(2);
const newChunk = this.readNextDWord();
let cels = [];
for(let i = 0; i < newChunk; i ++) {
let chunkData = this.readChunk();
switch(chunkData.type) {
case 0x0004:
case 0x0011:
case 0x2016:
case 0x2017:
case 0x2020:
this.skipBytes(chunkData.chunkSize - 6);
break;
case 0x2022:
this.readSliceChunk();
break;
case 0x2004:
this.readLayerChunk();
break;
case 0x2005:
let celData = this.readCelChunk(chunkData.chunkSize);
cels.push(celData);
break;
case 0x2007:
this.readColorProfileChunk();
break;
case 0x2018:
this.readFrameTagsChunk();
break;
case 0x2019:
this.palette = this.readPaletteChunk();
break;
case 0x2023:
this.tilesets.push(this.readTilesetChunk());
break;
default: // ignore unknown chunk types
this.skipBytes(chunkData.chunkSize - 6);
}
}
this.frames.push({ bytesInFrame,
frameDuration,
numChunks: newChunk,
cels});
}
/**
* Reads the Color Profile Chunk and stores the information
* Color Profile Chunk is type 0x2007
*/
readColorProfileChunk() {
const types = [
'None',
'sRGB',
'ICC'
]
const typeInd = this.readNextWord();
const type = types[typeInd];
const flag = this.readNextWord();
const fGamma = this.readNextFixed();
this.skipBytes(8);
if (typeInd === 2) {
//TODO: Handle ICC profile data properly instead of skipping
const skip = this.readNextDWord();
this.skipBytes(skip);
}
this.colorProfile = {
type,
flag,
fGamma};
}
/**
* Reads the Tags Chunk and stores the information
* Tags Cunk is type 0x2018
*/
readFrameTagsChunk() {
const loops = [
'Forward',
'Reverse',
'Ping-pong',
'Ping-pong Reverse'
]
const numTags = this.readNextWord();
this.skipBytes(8);
for(let i = 0; i < numTags; i ++) {
let tag = {};
tag.from = this.readNextWord();
tag.to = this.readNextWord();
const loopsInd = this.readNextByte();
tag.animDirection = loops[loopsInd];
tag.repeat = this.readNextWord();
this.skipBytes(6);
tag.color = this.readNextRawBytes(3).toString('hex');
this.skipBytes(1);
tag.name = this.readNextString();
this.tags.push(tag);
}
}
/**
* Reads the Palette Chunk and stores the information
* Palette Chunk is type 0x2019
*
* @returns {Palette}
*/
readPaletteChunk() {
const paletteSize = this.readNextDWord();
const firstColor = this.readNextDWord();
const secondColor = this.readNextDWord();
this.skipBytes(8);
let colors = [];
for (let i = 0; i < paletteSize; i++) {
let flag = this.readNextWord();
let red = this.readNextByte();
let green = this.readNextByte();
let blue = this.readNextByte();
let alpha = this.readNextByte();
let name;
if (flag === 1) {
name = this.readNextString();
}
colors.push({
red,
green,
blue,
alpha,
name: name !== undefined ? name : "none"
});
}
let palette = {
paletteSize,
firstColor,
lastColor: secondColor,
colors
}
this.colorDepth === 8 ? palette.index = this.paletteIndex : '';
return palette;
}
/**
* Reads the Tileset Chunk and stores the information
* Tileset Chunk is type 0x2023
*/
readTilesetChunk() {
const id = this.readNextDWord();
const flags = this.readNextDWord();
const tileCount = this.readNextDWord();
const tileWidth = this.readNextWord();
const tileHeight = this.readNextWord();
this.skipBytes(16);
const name = this.readNextString();
const tileset = {
id,
tileCount,
tileWidth,
tileHeight,
name };
if ((flags & 1) !== 0) {
tileset.externalFile = {}
tileset.externalFile.id = this.readNextDWord();
tileset.externalFile.tilesetId = this.readNextDWord();
}
if ((flags & 2) !== 0) {
const dataLength = this.readNextDWord();
const buff = this.readNextRawBytes(dataLength);
tileset.rawTilesetData = zlib.inflateSync(buff);
}
return tileset;
}
/**
* Reads the Slice Chunk and stores the information
* Slice Chunk is type 0x2022
*/
readSliceChunk() {
const numSliceKeys = this.readNextDWord();
const flags = this.readNextDWord();
this.skipBytes(4);
const name = this.readNextString();
const keys = [];
for(let i = 0; i < numSliceKeys; i ++) {
const frameNumber = this.readNextDWord();
const x = this.readNextLong();
const y = this.readNextLong();
const width = this.readNextDWord();
const height = this.readNextDWord();
const key = { frameNumber, x, y, width, height };
if((flags & 1) !== 0) {
key.patch = this.readSlicePatchChunk();
}
if((flags & 2) !== 0) {
key.pivot = this.readSlicePivotChunk();
}
keys.push(key);
}
this.slices.push({ flags, name, keys });
}
/**
* Reads the Patch portion of a Slice Chunk
*
* @returns {Object} patch - Patch information that was in the chunk
* @returns {number} patch.x - Patch X location
* @returns {number} patch.y - Patch Y location
* @returns {number} patch.width - Patch width
* @returns {number} patch.height - Patch height
*/
readSlicePatchChunk() {
const x = this.readNextLong();
const y = this.readNextLong();
const width = this.readNextDWord();
const height = this.readNextDWord();
return { x, y, width, height };
}
/**
* Reads the Pivot portion of a Slice Chunk
*
* @returns {Object} pivot - Pivot information that was in the chunk
* @returns {number} pivot.x - Pivot X location
* @returns {number} pivot.y - Pivot Y location
*/
readSlicePivotChunk() {
const x = this.readNextLong();
const y = this.readNextLong();
return { x, y };
}
/**
* Reads the Layer Chunk and stores the information
* Layer Chunk is type 0x2004
*/
readLayerChunk() {
const layer = {}
layer.flags = this.#translateFlags(this.readNextWord());
layer.type = this.readNextWord();
layer.layerChildLevel = this.readNextWord();
this.skipBytes(4);
layer.blendMode = this.readNextWord();
layer.opacity = this.readNextByte();
this.skipBytes(3);
layer.name = this.readNextString();
if (layer.type == 2) {
layer.tilesetIndex =this.readNextDWord()
}
this.layers.push(layer);
}
/**
* Reads a Cel Chunk in its entirety and returns the information
* Cel Chunk is type 0x2005
*
* @param {number} chunkSize - Size of the Cel Chunk to read
* @returns {Object} Cel information
*/
readCelChunk(chunkSize) {
const layerIndex = this.readNextWord();
const x = this.readNextShort();
const y = this.readNextShort();
const opacity = this.readNextByte();
const celType = this.readNextWord();
const zIndex = this.readNextShort();
this.skipBytes(5);
if (celType === 1) {
return {
layerIndex,
xpos: x,
ypos: y,
opacity,
celType,
zIndex,
w: 0,
h: 0,
rawCelData: undefined,
link: this.readNextWord()
};
}
const w = this.readNextWord();
const h = this.readNextWord();
const chunkBase = {
layerIndex,
xpos: x,
ypos: y,
opacity,
celType,
zIndex,
w,
h
};
if (celType === 0 || celType === 2) {
const buff = this.readNextRawBytes(chunkSize - 26); // take the first 20 bytes off for the data above and chunk info
return {
...chunkBase,
rawCelData: celType === 2 ? zlib.inflateSync(buff) : buff
}
}
if (celType === 3) {
return { ...chunkBase, ...this.readTilemapCelChunk(chunkSize) }
}
}
readTilemapCelChunk(chunkSize) {
const bitsPerTile = this.readNextWord();
const bitmaskForTileId = this.readNextDWord();
const bitmaskForXFlip = this.readNextDWord();
const bitmaskForYFlip = this.readNextDWord();
const bitmaskFor90CWRotation = this.readNextDWord();
this.skipBytes(10);
const buff = this.readNextRawBytes(chunkSize - 54);
const rawCelData = zlib.inflateSync(buff);
const tilemapMetadata = {
bitsPerTile,
bitmaskForTileId,
bitmaskForXFlip,
bitmaskForYFlip,
bitmaskFor90CWRotation };
return { tilemapMetadata, rawCelData };
}
/**
* Reads the next Chunk Info block to get how large and what type the next Chunk is
*
* @returns {Object} chunkInfo
* @returns {number} chunkInfo.chunkSize - The size of the Chunk read
* @returns {number} chunkInfo.type - The type of the Chunk
*/
readChunk() {
const cSize = this.readNextDWord();
const type = this.readNextWord();
return {chunkSize: cSize, type: type};
}
/**
* Processes the Aseprite file and stores the information
*/
parse() {
const numFrames = this.readHeader();
for(let i = 0; i < numFrames; i ++) {
this.readFrame();
}
for(let i = 0; i < numFrames; i ++) {
for (let j = 0; j < this.frames[i].cels.length; j++) {
const cel = this.frames[i].cels[j];
if (cel.celType === 1) {
for (let k = 0; k < this.frames[cel.link].cels.length; k++) {
const srcCel = this.frames[cel.link].cels[k];
if (srcCel.layerIndex === cel.layerIndex) {
cel.w = srcCel.w;
cel.h = srcCel.h;
cel.rawCelData = srcCel.rawCelData;
}
if (cel.rawCelData) {
break;
}
}
}
}
}
}
/**
* Converts an amount of Bytes to a human readable format
*
* @param {number} bytes - Bytes to format
* @param {number} decimals - Number of decimals to format the number to
* @returns {string} - Amount of Bytes formatted in a more human readable format
*/
formatBytes(bytes,decimals) {
if (bytes === 0) {
return '0 Byte';
}
const k = 1024;
const dm = decimals + 1 || 3;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
};
/**
* Attempts to return the data in a string format
*
* @returns {string}
*/
toJSON() {
return {
fileSize: this.fileSize,
numFrames: this.numFrames,
frames: this.frames.map(frame => {
return {
size: frame.bytesInFrame,
duration: frame.frameDuration,
chunks: frame.numChunks,
cels: frame.cels.map(cel => {
return {
layerIndex: cel.layerIndex,
xpos: cel.xpos,
ypos: cel.ypos,
opacity: cel.opacity,
celType: cel.celType,
w: cel.w,
h: cel.h,
rawCelData: 'buffer'
}
}) }
}),
palette: this.palette,
tilesets: this.tilesets,
width: this.width,
height: this.height,
colorDepth: this.colorDepth,
numColors: this.numColors,
pixelRatio: this.pixelRatio,
layers: this.layers,
slices: this.slices
};
}
}
module.exports = Aseprite;